Skip to content

Commit

Permalink
Merge pull request #10 from M0rgan01/apiPlatform3
Browse files Browse the repository at this point in the history
Update to api-platform v3
  • Loading branch information
M0rgan01 committed Feb 9, 2024
2 parents d184bc2 + ba67d99 commit 94092f0
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/ApiPlatform/Resources/ApiAccess.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

namespace PrestaShop\Module\APIResources\ApiPlatform\Resources;

use ApiPlatform\Core\Annotation\ApiProperty;
use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Delete;
use ApiPlatform\Metadata\Get;
Expand Down
2 changes: 1 addition & 1 deletion src/ApiPlatform/Resources/CustomerGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

namespace PrestaShop\Module\APIResources\ApiPlatform\Resources;

use ApiPlatform\Core\Annotation\ApiProperty;
use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\ApiResource;
use PrestaShop\PrestaShop\Core\Domain\Customer\Group\Command\AddCustomerGroupCommand;
use PrestaShop\PrestaShop\Core\Domain\Customer\Group\Command\DeleteCustomerGroupCommand;
Expand Down
2 changes: 1 addition & 1 deletion src/ApiPlatform/Resources/FoundProduct.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

namespace PrestaShop\Module\APIResources\ApiPlatform\Resources;

use ApiPlatform\Core\Annotation\ApiProperty;
use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\GetCollection;
use PrestaShop\PrestaShop\Core\Domain\Product\Query\SearchProducts;
Expand Down
2 changes: 1 addition & 1 deletion src/ApiPlatform/Resources/Hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

namespace PrestaShop\Module\APIResources\ApiPlatform\Resources;

use ApiPlatform\Core\Annotation\ApiProperty;
use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\Put;
Expand Down
2 changes: 1 addition & 1 deletion src/ApiPlatform/Resources/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

namespace PrestaShop\Module\APIResources\ApiPlatform\Resources;

use ApiPlatform\Core\Annotation\ApiProperty;
use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\ApiResource;
use PrestaShop\PrestaShop\Core\Domain\Product\Command\AddProductCommand;
use PrestaShop\PrestaShop\Core\Domain\Product\Command\DeleteProductCommand;
Expand Down
11 changes: 10 additions & 1 deletion tests/Integration/ApiPlatform/ApiTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ protected static function createClient(array $kernelOptions = [], array $default
$defaultOptions['headers']['accept'] = ['application/json'];
}

if (!isset($defaultOptions['headers']['content-type'])) {
$defaultOptions['headers']['content-type'] = ['application/json'];
}

return parent::createClient($kernelOptions, $defaultOptions);
}

Expand All @@ -77,7 +81,12 @@ protected function getBearerToken(array $scopes = []): string
'grant_type' => 'client_credentials',
'scope' => $scopes,
]];
$options = ['extra' => $parameters];
$options = [
'extra' => $parameters,
'headers' => [
'content-type' => 'application/x-www-form-urlencoded',
],
];
$response = $client->request('POST', '/api/oauth2/token', $options);

return json_decode($response->getContent())->access_token;
Expand Down
12 changes: 10 additions & 2 deletions tests/Integration/ApiPlatform/ProductEndpointTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,11 @@ public static function tearDownAfterClass(): void
* @param string $method
* @param string $uri
*/
public function testProtectedEndpoints(string $method, string $uri): void
public function testProtectedEndpoints(string $method, string $uri, string $contentType = 'application/json'): void
{
$options['headers']['content-type'] = $contentType;
// Check that endpoints are not accessible without a proper Bearer token
$client = static::createClient();
$client = static::createClient([], $options);
$response = $client->request($method, $uri);
self::assertResponseStatusCodeSame(401);

Expand Down Expand Up @@ -98,6 +99,7 @@ public function getProtectedEndpoints(): iterable
yield 'update endpoint' => [
'PATCH',
'/api/product/1',
'application/merge-patch+json',
];
}

Expand Down Expand Up @@ -160,6 +162,9 @@ public function testPartialUpdateProduct(int $productId): int
// Update product with partial data, even multilang fields can be updated language by language
$response = $client->request('PATCH', '/api/product/' . $productId, [
'auth_bearer' => $bearerToken,
'headers' => [
'content-type' => 'application/merge-patch+json',
],
'json' => [
'names' => [
self::$frenchLangId => 'nouveau nom',
Expand Down Expand Up @@ -197,6 +202,9 @@ public function testPartialUpdateProduct(int $productId): int
// Update product with partial data, only name default language the other names are not impacted
$response = $client->request('PATCH', '/api/product/' . $productId, [
'auth_bearer' => $bearerToken,
'headers' => [
'content-type' => 'application/merge-patch+json',
],
'json' => [
'names' => [
self::EN_LANG_ID => 'new product name',
Expand Down

0 comments on commit 94092f0

Please sign in to comment.