Skip to content

Commit

Permalink
chore(api): product listing endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
tleon committed Mar 15, 2024
1 parent d63437e commit 65b90da
Show file tree
Hide file tree
Showing 3 changed files with 177 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/ApiPlatform/Resources/Hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
use PrestaShop\PrestaShop\Core\Domain\Hook\Exception\HookNotFoundException;
use PrestaShop\PrestaShop\Core\Domain\Hook\Query\GetHook;
use PrestaShop\PrestaShop\Core\Domain\Hook\Query\GetHookStatus;
use PrestaShop\PrestaShop\Core\Search\Filters;
use PrestaShopBundle\ApiPlatform\Metadata\CQRSGet;
use PrestaShopBundle\ApiPlatform\Metadata\CQRSUpdate;
use PrestaShopBundle\ApiPlatform\Metadata\DQBPaginatedList;
Expand Down Expand Up @@ -86,7 +87,8 @@
provider: QueryListProvider::class,
scopes: ['hook_read'],
ApiResourceMapping: ['[id_hook]' => '[id]'],
queryBuilder: 'prestashop.core.api.query_builder.hook'
queryBuilder: 'prestashop.core.api.query_builder.hook',
filterClass: Filters::class,
),
],
)]
Expand Down
76 changes: 76 additions & 0 deletions src/ApiPlatform/Resources/ProductList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <[email protected]>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/

declare(strict_types=1);

namespace PrestaShop\Module\APIResources\ApiPlatform\Resources;

use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\ApiResource;
use PrestaShop\PrestaShop\Core\Domain\Product\Exception\ProductNotFoundException;
use PrestaShop\PrestaShop\Core\Domain\Shop\Exception\ShopAssociationNotFound;
use PrestaShop\PrestaShop\Core\Search\Filters\ProductFilters;
use PrestaShopBundle\ApiPlatform\Metadata\DQBPaginatedList;
use PrestaShopBundle\ApiPlatform\Provider\QueryListProvider;
use Symfony\Component\HttpFoundation\Response;

#[ApiResource(
operations: [
new DQBPaginatedList(
uriTemplate: '/products',
provider: QueryListProvider::class,
scopes: ['product_read'],
ApiResourceMapping: [
'[id_product]' => '[productId]',
'[final_price_tax_excluded]' => '[price]',
],
queryBuilder: 'prestashop.core.grid.query_builder.product',
filterClass: ProductFilters::class,
),
],
exceptionToStatus: [
ProductNotFoundException::class => Response::HTTP_NOT_FOUND,
ShopAssociationNotFound::class => Response::HTTP_NOT_FOUND,
],
)]
class ProductList
{
#[ApiProperty(identifier: true)]
public int $productId;

public string $type;

public bool $active;

public string $name;

public int $quantity;

public string $price;

public string $category;
}
98 changes: 98 additions & 0 deletions tests/Integration/ApiPlatform/ProductListingEndpointTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <[email protected]>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/

declare(strict_types=1);

namespace PsApiResourcesTest\Integration\ApiPlatform;

use Tests\Resources\Resetter\LanguageResetter;
use Tests\Resources\Resetter\ProductResetter;
use Tests\Resources\ResourceResetter;

class ProductListingEndpointTest extends ApiTestCase
{
protected static int $frenchLangId;

public static function setUpBeforeClass(): void
{
parent::setUpBeforeClass();
(new ResourceResetter())->backupTestModules();
ProductResetter::resetProducts();
LanguageResetter::resetLanguages();
self::$frenchLangId = self::addLanguageByLocale('fr-FR');
self::createApiClient(['product_read']);
}

public static function tearDownAfterClass(): void
{
parent::tearDownAfterClass();
ProductResetter::resetProducts();
LanguageResetter::resetLanguages();
// Reset modules folder that are removed with the FR language
(new ResourceResetter())->resetTestModules();
}

public function getProtectedEndpoints(): iterable
{
yield 'list endpoint' => [
'GET',
'/api/products',
];
}

public function testListProducts(): void
{
$bearerToken = $this->getBearerToken([
'product_read',
]);

$response = static::createClient()->request('GET', '/api/products', ['auth_bearer' => $bearerToken]);
self::assertResponseStatusCodeSame(200);
self::assertCount(19, json_decode($response->getContent())->items);

$response = static::createClient()->request('GET', '/api/products?limit=10', ['auth_bearer' => $bearerToken]);
self::assertResponseStatusCodeSame(200);
self::assertCount(10, json_decode($response->getContent())->items);

$response = static::createClient()->request('GET', '/api/products?limit=1&orderBy=id_product&sortOrder=desc', ['auth_bearer' => $bearerToken]);
self::assertResponseStatusCodeSame(200);
self::assertCount(1, json_decode($response->getContent())->items);
$returnedProduct = json_decode($response->getContent());
self::assertEquals('id_product', $returnedProduct->orderBy);
self::assertEquals('desc', $returnedProduct->sortOrder);
self::assertEquals(1, $returnedProduct->limit);
self::assertEquals([], $returnedProduct->filters);
self::assertEquals('Customizable mug', $returnedProduct->items[0]->name);
self::assertEquals(300, $returnedProduct->items[0]->quantity);
self::assertEquals('16,680000', $returnedProduct->items[0]->price);
self::assertEquals('House Accessory', $returnedProduct->items[0]->category);
self::assertTrue($returnedProduct->items[0]->active);

static::createClient()->request('GET', '/api/products');
self::assertResponseStatusCodeSame(401);
}
}

0 comments on commit 65b90da

Please sign in to comment.