Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentchalamon committed Sep 20, 2023
1 parent 918e574 commit 4e21281
Show file tree
Hide file tree
Showing 16 changed files with 149 additions and 56 deletions.
1 change: 0 additions & 1 deletion api/.env.test
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# define your env variables for the test env here
KERNEL_CLASS='App\Kernel'
APP_SECRET='$ecretf0rt3st'
SYMFONY_DEPRECATIONS_HELPER=999999
PANTHER_APP_ENV=panther
PANTHER_ERROR_SCREENSHOT_DIR=./var/error-screenshots

Expand Down
2 changes: 1 addition & 1 deletion api/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"ext-ctype": "*",
"ext-iconv": "*",
"ext-xml": "*",
"api-platform/core": "dev-main",
"api-platform/core": "^3.2@beta",
"doctrine/doctrine-bundle": "^2.7",
"doctrine/doctrine-migrations-bundle": "^3.2",
"doctrine/orm": "^2.12",
Expand Down
32 changes: 15 additions & 17 deletions api/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion api/phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<!-- https://phpunit.readthedocs.io/en/latest/configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
xsi:noNamespaceSchemaLocation="vendor/bin/.phpunit/phpunit/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="tests/bootstrap.php"
Expand Down
8 changes: 7 additions & 1 deletion api/src/Entity/Book.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#[ApiResource(
uriTemplate: '/admin/books{._format}',
types: ['https://schema.org/Book', 'https://schema.org/Offer'],
extraProperties: ['rfc_7807_compliant_errors' => true],
operations: [
new GetCollection(
itemUriTemplate: '/admin/books/{id}{._format}',
Expand Down Expand Up @@ -64,11 +65,16 @@
'groups' => ['Book:read:admin', 'Enum:read'],
'skip_null_values' => true,
],
denormalizationContext: ['groups' => ['Book:write']],
denormalizationContext: [
'groups' => ['Book:write'],
],
// todo waiting for https://github.com/api-platform/core/pull/5844
// collectDenormalizationErrors: true,
security: 'is_granted("ROLE_ADMIN")'
)]
#[ApiResource(
types: ['https://schema.org/Book', 'https://schema.org/Offer'],
extraProperties: ['rfc_7807_compliant_errors' => true],
operations: [
new GetCollection(
itemUriTemplate: '/books/{id}{._format}'
Expand Down
7 changes: 6 additions & 1 deletion api/src/Entity/Bookmark.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#[ApiResource(
types: ['https://schema.org/BookmarkAction'],
order: ['bookmarkedAt' => 'DESC'],
extraProperties: ['rfc_7807_compliant_errors' => true],
operations: [
new GetCollection(),
new Delete(
Expand All @@ -46,7 +47,11 @@
'book' => '/books/{id}{._format}',
],
],
denormalizationContext: ['groups' => ['Bookmark:write']],
denormalizationContext: [
'groups' => ['Bookmark:write'],
],
// todo waiting for https://github.com/api-platform/core/pull/5844
// collectDenormalizationErrors: true,
mercure: true,
security: 'is_granted("ROLE_USER")'
)]
Expand Down
14 changes: 12 additions & 2 deletions api/src/Entity/Review.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#[ApiResource(
types: ['https://schema.org/Review'],
order: ['publishedAt' => 'DESC'],
extraProperties: ['rfc_7807_compliant_errors' => true],
operations: [
new GetCollection(
uriTemplate: '/admin/reviews{._format}',
Expand Down Expand Up @@ -69,12 +70,17 @@
'skip_null_values' => true,
'groups' => ['Review:read', 'Review:read:admin'],
],
denormalizationContext: ['groups' => ['Review:write', 'Review:write:admin']],
denormalizationContext: [
'groups' => ['Review:write', 'Review:write:admin'],
],
// todo waiting for https://github.com/api-platform/core/pull/5844
// collectDenormalizationErrors: true,
security: 'is_granted("ROLE_ADMIN")'
)]
#[ApiResource(
types: ['https://schema.org/Review'],
order: ['publishedAt' => 'DESC'],
extraProperties: ['rfc_7807_compliant_errors' => true],
uriTemplate: '/books/{bookId}/reviews{._format}',
uriVariables: [
'bookId' => new Link(toProperty: 'book', fromClass: Book::class),
Expand Down Expand Up @@ -128,7 +134,11 @@
'skip_null_values' => true,
'groups' => ['Review:read'],
],
denormalizationContext: ['groups' => ['Review:write']]
denormalizationContext: [
'groups' => ['Review:write'],
],
// todo waiting for https://github.com/api-platform/core/pull/5844
// collectDenormalizationErrors: true
)]
#[ORM\Entity(repositoryClass: ReviewRepository::class)]
#[ORM\UniqueConstraint(fields: ['user', 'book'])]
Expand Down
1 change: 1 addition & 0 deletions api/src/Entity/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
#[ApiResource(
types: ['https://schema.org/Person'],
extraProperties: ['rfc_7807_compliant_errors' => true],
operations: [
new GetCollection(
uriTemplate: '/admin/users{._format}',
Expand Down
2 changes: 1 addition & 1 deletion api/src/State/Processor/MercureProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function __construct(
* @throws ResourceClassNotFoundException
* @throws RuntimeException
*/
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = [])
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): mixed
{
if (isset($context['item_uri_template'])) {
$operation = $this->resourceMetadataCollectionFactory->create($data::class)->getOperation($context['item_uri_template']);
Expand Down
89 changes: 63 additions & 26 deletions api/tests/Api/Admin/BookTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public function testAsNonAdminUserICannotGetACollectionOfBooks(int $expectedCode

self::assertResponseStatusCodeSame($expectedCode);
self::assertResponseHeaderSame('content-type', 'application/problem+json; charset=utf-8');
self::assertResponseHeaderSame('link', '<http://www.w3.org/ns/hydra/error>; rel="http://www.w3.org/ns/json-ld#error",<http://localhost/docs.jsonld>; rel="http://www.w3.org/ns/hydra/core#apiDocumentation"');
self::assertJsonContains([
'@context' => '/contexts/Error',
'@type' => 'hydra:Error',
Expand Down Expand Up @@ -194,6 +195,7 @@ public function testAsNonAdminUserICannotGetABook(int $expectedCode, string $hyd

self::assertResponseStatusCodeSame($expectedCode);
self::assertResponseHeaderSame('content-type', 'application/problem+json; charset=utf-8');
self::assertResponseHeaderSame('link', '<http://www.w3.org/ns/hydra/error>; rel="http://www.w3.org/ns/json-ld#error",<http://localhost/docs.jsonld>; rel="http://www.w3.org/ns/hydra/core#apiDocumentation"');
self::assertJsonContains([
'@context' => '/contexts/Error',
'@type' => 'hydra:Error',
Expand Down Expand Up @@ -249,6 +251,7 @@ public function testAsNonAdminUserICannotCreateABook(int $expectedCode, string $

self::assertResponseStatusCodeSame($expectedCode);
self::assertResponseHeaderSame('content-type', 'application/problem+json; charset=utf-8');
self::assertResponseHeaderSame('link', '<http://www.w3.org/ns/hydra/error>; rel="http://www.w3.org/ns/json-ld#error",<http://localhost/docs.jsonld>; rel="http://www.w3.org/ns/hydra/core#apiDocumentation"');
self::assertJsonContains([
'@context' => '/contexts/Error',
'@type' => 'hydra:Error',
Expand All @@ -272,7 +275,8 @@ public function testAsAdminUserICannotCreateABookWithInvalidData(array $data, in
]);

self::assertResponseStatusCodeSame($statusCode);
self::assertResponseHeaderSame('content-type', 'application/ld+json; charset=utf-8');
self::assertResponseHeaderSame('content-type', 'application/problem+json; charset=utf-8');
self::assertResponseHeaderSame('link', '<http://www.w3.org/ns/hydra/error>; rel="http://www.w3.org/ns/json-ld#error",<http://localhost/docs.jsonld>; rel="http://www.w3.org/ns/hydra/core#apiDocumentation"');
self::assertJsonContains($expected);
}

Expand Down Expand Up @@ -302,32 +306,62 @@ public function getInvalidDataOnCreate(): iterable

public function getInvalidData(): iterable
{
// yield 'empty data' => [
// [
// 'book' => '',
// 'condition' => '',
// ],
// Response::HTTP_BAD_REQUEST,
// [
// '@context' => '/contexts/Error',
// '@type' => 'hydra:Error',
yield 'empty data' => [
[
'book' => '',
'condition' => '',
],
Response::HTTP_BAD_REQUEST,
// todo waiting for https://github.com/api-platform/core/pull/5844
// Response::HTTP_UNPROCESSABLE_ENTITY,
[
'@context' => '/contexts/Error',
'@type' => 'hydra:Error',
'hydra:title' => 'An error occurred',
'hydra:description' => 'The data must belong to a backed enumeration of type '.BookCondition::class,
// todo waiting for https://github.com/api-platform/core/pull/5844[
// '@context' => '/contexts/ConstraintViolationList',
// '@type' => 'ConstraintViolationList',
// 'hydra:title' => 'An error occurred',
// 'hydra:description' => 'The data must belong to a backed enumeration of type '.BookCondition::class,
// ],
// ];
// yield 'invalid condition' => [
// [
// 'book' => 'https://openlibrary.org/books/OL28346544M.json',
// 'condition' => 'invalid condition',
// ],
// Response::HTTP_BAD_REQUEST,
// [
// '@context' => '/contexts/Error',
// '@type' => 'hydra:Error',
// 'hydra:description' => 'book: This value should not be blank.\ncondition: This value should be of type '.BookCondition::class.'.',
// 'violations' => [
// [
// 'propertyPath' => 'book',
// 'hint' => 'This value should not be blank.',
// ],
// [
// 'propertyPath' => 'condition',
// 'hint' => 'The data must belong to a backed enumeration of type '.BookCondition::class,
// ],
// ],
],
];
yield 'invalid condition' => [
[
'book' => 'https://openlibrary.org/books/OL28346544M.json',
'condition' => 'invalid condition',
],
Response::HTTP_BAD_REQUEST,
// todo waiting for https://github.com/api-platform/core/pull/5844
// Response::HTTP_UNPROCESSABLE_ENTITY,
[
'@context' => '/contexts/Error',
'@type' => 'hydra:Error',
'hydra:title' => 'An error occurred',
'hydra:description' => 'The data must belong to a backed enumeration of type '.BookCondition::class,
// todo waiting for https://github.com/api-platform/core/pull/5844
// '@context' => '/contexts/ConstraintViolationList',
// '@type' => 'ConstraintViolationList',
// 'hydra:title' => 'An error occurred',
// 'hydra:description' => 'The data must belong to a backed enumeration of type '.BookCondition::class,
// ],
// ];
// 'hydra:description' => 'condition: This value should be of type '.BookCondition::class.'.',
// 'violations' => [
// [
// 'propertyPath' => 'condition',
// 'hint' => 'The data must belong to a backed enumeration of type '.BookCondition::class,
// ],
// ],
],
];
yield 'invalid book' => [
[
'book' => 'invalid book',
Expand Down Expand Up @@ -427,6 +461,7 @@ public function testAsNonAdminUserICannotUpdateBook(int $expectedCode, string $h

self::assertResponseStatusCodeSame($expectedCode);
self::assertResponseHeaderSame('content-type', 'application/problem+json; charset=utf-8');
self::assertResponseHeaderSame('link', '<http://www.w3.org/ns/hydra/error>; rel="http://www.w3.org/ns/json-ld#error",<http://localhost/docs.jsonld>; rel="http://www.w3.org/ns/hydra/core#apiDocumentation"');
self::assertJsonContains([
'@context' => '/contexts/Error',
'@type' => 'hydra:Error',
Expand Down Expand Up @@ -470,7 +505,8 @@ public function testAsAdminUserICannotUpdateABookWithInvalidData(array $data, in
]);

self::assertResponseStatusCodeSame($statusCode);
self::assertResponseHeaderSame('content-type', 'application/ld+json; charset=utf-8');
self::assertResponseHeaderSame('content-type', 'application/problem+json; charset=utf-8');
self::assertResponseHeaderSame('link', '<http://www.w3.org/ns/hydra/error>; rel="http://www.w3.org/ns/json-ld#error",<http://localhost/docs.jsonld>; rel="http://www.w3.org/ns/hydra/core#apiDocumentation"');
self::assertJsonContains($expected);
}

Expand Down Expand Up @@ -553,6 +589,7 @@ public function testAsNonAdminUserICannotDeleteABook(int $expectedCode, string $

self::assertResponseStatusCodeSame($expectedCode);
self::assertResponseHeaderSame('content-type', 'application/problem+json; charset=utf-8');
self::assertResponseHeaderSame('link', '<http://www.w3.org/ns/hydra/error>; rel="http://www.w3.org/ns/json-ld#error",<http://localhost/docs.jsonld>; rel="http://www.w3.org/ns/hydra/core#apiDocumentation"');
self::assertJsonContains([
'@context' => '/contexts/Error',
'@type' => 'hydra:Error',
Expand Down
Loading

0 comments on commit 4e21281

Please sign in to comment.