forked from PrestaShop/ps_apiresources
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(api): product listing endpoint
- Loading branch information
Showing
3 changed files
with
177 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
98
tests/Integration/ApiPlatform/ProductListingEndpointTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |