Skip to content

Commit 26c840d

Browse files
fix: fix mercure configuration on Review and Book
1 parent 928c2bc commit 26c840d

File tree

8 files changed

+70
-13
lines changed

8 files changed

+70
-13
lines changed

api/src/Entity/Book.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,12 @@
9090
normalizationContext: [
9191
AbstractNormalizer::GROUPS => ['Book:read', 'Enum:read'],
9292
AbstractObjectNormalizer::SKIP_NULL_VALUES => true,
93+
],
94+
mercure: [
95+
'topics' => [
96+
'@=iri(object, ' . UrlGeneratorInterface::ABS_URL . ', get_operation(object, "/admin/books/{id}{._format}"))',
97+
'@=iri(object, ' . UrlGeneratorInterface::ABS_URL . ', get_operation(object, "/books/{id}{._format}"))',
98+
],
9399
]
94100
)]
95101
#[ORM\Entity(repositoryClass: BookRepository::class)]

api/src/Entity/Review.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,9 @@
8080
mercure: [
8181
'topics' => [
8282
'@=iri(object, ' . UrlGeneratorInterface::ABS_URL . ', get_operation(object, "/admin/reviews/{id}{._format}"))',
83+
'@=iri(object, ' . UrlGeneratorInterface::ABS_URL . ', get_operation(object, "/admin/reviews{._format}"))',
8384
'@=iri(object, ' . UrlGeneratorInterface::ABS_URL . ', get_operation(object, "/books/{bookId}/reviews/{id}{._format}"))',
85+
'@=iri(object, ' . UrlGeneratorInterface::ABS_URL . ', get_operation(object, "/books/{bookId}/reviews{._format}"))',
8486
],
8587
]
8688
)]
@@ -142,7 +144,15 @@
142144
denormalizationContext: [
143145
AbstractNormalizer::GROUPS => ['Review:write'],
144146
],
145-
collectDenormalizationErrors: true
147+
collectDenormalizationErrors: true,
148+
mercure: [
149+
'topics' => [
150+
'@=iri(object, ' . UrlGeneratorInterface::ABS_URL . ', get_operation(object, "/admin/reviews/{id}{._format}"))',
151+
'@=iri(object, ' . UrlGeneratorInterface::ABS_URL . ', get_operation(object, "/admin/reviews{._format}"))',
152+
'@=iri(object, ' . UrlGeneratorInterface::ABS_URL . ', get_operation(object, "/books/{bookId}/reviews/{id}{._format}"))',
153+
'@=iri(object, ' . UrlGeneratorInterface::ABS_URL . ', get_operation(object, "/books/{bookId}/reviews{._format}"))',
154+
],
155+
]
146156
)]
147157
#[ORM\Entity(repositoryClass: ReviewRepository::class)]
148158
#[ORM\UniqueConstraint(fields: ['user', 'book'])]

api/tests/Api/Admin/BookTest.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ public function asAdminUserICanGetACollectionOfBooks(FactoryCollection $factory,
7575

7676
self::assertResponseIsSuccessful();
7777
self::assertResponseHeaderSame('content-type', 'application/ld+json; charset=utf-8');
78+
self::assertEquals('<https://localhost/.well-known/mercure>; rel="mercure"', $response->getHeaders()['link'][1]);
7879
self::assertJsonContains([
7980
'hydra:totalItems' => $hydraTotalItems,
8081
]);
@@ -142,6 +143,7 @@ public function asAdminUserICanGetACollectionOfBooksOrderedByTitle(): void
142143

143144
self::assertResponseIsSuccessful();
144145
self::assertResponseHeaderSame('content-type', 'application/ld+json; charset=utf-8');
146+
self::assertEquals('<https://localhost/.well-known/mercure>; rel="mercure"', $response->getHeaders()['link'][1]);
145147
self::assertEquals('Ball Lightning', $response->toArray()['hydra:member'][0]['title']);
146148
self::assertEquals('Hyperion', $response->toArray()['hydra:member'][1]['title']);
147149
self::assertEquals('The Wandering Earth', $response->toArray()['hydra:member'][2]['title']);
@@ -210,10 +212,11 @@ public function asAdminUserICanGetABook(): void
210212
'email' => UserFactory::createOneAdmin()->email,
211213
]);
212214

213-
$this->client->request('GET', '/admin/books/' . $book->getId(), ['auth_bearer' => $token]);
215+
$response = $this->client->request('GET', '/admin/books/' . $book->getId(), ['auth_bearer' => $token]);
214216

215217
self::assertResponseIsSuccessful();
216218
self::assertResponseHeaderSame('content-type', 'application/ld+json; charset=utf-8');
219+
self::assertEquals('<https://localhost/.well-known/mercure>; rel="mercure"', $response->getHeaders(false)['link'][1]);
217220
self::assertJsonContains([
218221
'@id' => '/admin/books/' . $book->getId(),
219222
'book' => $book->book,
@@ -385,6 +388,7 @@ public function asAdminUserICanCreateABook(): void
385388

386389
self::assertResponseStatusCodeSame(Response::HTTP_CREATED);
387390
self::assertResponseHeaderSame('content-type', 'application/ld+json; charset=utf-8');
391+
self::assertEquals('<https://localhost/.well-known/mercure>; rel="mercure"', $response->getHeaders(false)['link'][1]);
388392
self::assertJsonContains([
389393
'book' => 'https://openlibrary.org/books/OL28346544M.json',
390394
'condition' => BookCondition::NewCondition->value,
@@ -508,7 +512,7 @@ public function asAdminUserICanUpdateABook(): void
508512
'email' => UserFactory::createOneAdmin()->email,
509513
]);
510514

511-
$this->client->request('PUT', '/admin/books/' . $book->getId(), [
515+
$response = $this->client->request('PUT', '/admin/books/' . $book->getId(), [
512516
'auth_bearer' => $token,
513517
'json' => [
514518
'@id' => '/books/' . $book->getId(),
@@ -524,6 +528,7 @@ public function asAdminUserICanUpdateABook(): void
524528

525529
self::assertResponseStatusCodeSame(Response::HTTP_OK);
526530
self::assertResponseHeaderSame('content-type', 'application/ld+json; charset=utf-8');
531+
self::assertEquals('<https://localhost/.well-known/mercure>; rel="mercure"', $response->getHeaders(false)['link'][1]);
527532
self::assertJsonContains([
528533
'book' => 'https://openlibrary.org/books/OL28346544M.json',
529534
'condition' => BookCondition::DamagedCondition->value,

api/tests/Api/Admin/ReviewTest.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ public function asAdminUserICanGetACollectionOfReviews(FactoryCollection $factor
7979

8080
self::assertResponseIsSuccessful();
8181
self::assertResponseHeaderSame('content-type', 'application/ld+json; charset=utf-8');
82+
self::assertEquals('<https://localhost/.well-known/mercure>; rel="mercure"', $response->getHeaders()['link'][1]);
8283
self::assertJsonContains([
8384
'hydra:totalItems' => $hydraTotalItems,
8485
]);
@@ -189,10 +190,11 @@ public function asAdminUserICanGetAReview(): void
189190
'email' => UserFactory::createOneAdmin()->email,
190191
]);
191192

192-
$this->client->request('GET', '/admin/reviews/' . $review->getId(), ['auth_bearer' => $token]);
193+
$response = $this->client->request('GET', '/admin/reviews/' . $review->getId(), ['auth_bearer' => $token]);
193194

194195
self::assertResponseIsSuccessful();
195196
self::assertResponseHeaderSame('content-type', 'application/ld+json; charset=utf-8');
197+
self::assertEquals('<https://localhost/.well-known/mercure>; rel="mercure"', $response->getHeaders(false)['link'][1]);
196198
self::assertMatchesJsonSchema(file_get_contents(__DIR__ . '/schemas/Review/item.json'));
197199
}
198200

@@ -268,7 +270,7 @@ public function asAdminUserICanUpdateAReview(): void
268270
'email' => $user->email,
269271
]);
270272

271-
$this->client->request('PUT', '/admin/reviews/' . $review->getId(), [
273+
$response = $this->client->request('PUT', '/admin/reviews/' . $review->getId(), [
272274
'auth_bearer' => $token,
273275
'json' => [
274276
// Must set all data because of standard PUT
@@ -285,6 +287,7 @@ public function asAdminUserICanUpdateAReview(): void
285287

286288
self::assertResponseStatusCodeSame(Response::HTTP_OK);
287289
self::assertResponseHeaderSame('content-type', 'application/ld+json; charset=utf-8');
290+
self::assertEquals('<https://localhost/.well-known/mercure>; rel="mercure"', $response->getHeaders(false)['link'][1]);
288291
self::assertJsonContains([
289292
'body' => 'Very good book!',
290293
'rating' => 5,
@@ -295,7 +298,12 @@ public function asAdminUserICanUpdateAReview(): void
295298
self::assertCount(1, self::getMercureMessages());
296299
self::assertEquals(
297300
new Update(
298-
topics: ['http://localhost/admin/reviews/' . $review->getId(), 'http://localhost/books/' . $review->book->getId() . '/reviews/' . $review->getId()],
301+
topics: [
302+
'http://localhost/admin/reviews/' . $review->getId(),
303+
'http://localhost/admin/reviews',
304+
'http://localhost/books/' . $review->book->getId() . '/reviews/' . $review->getId(),
305+
'http://localhost/books/' . $review->book->getId() . '/reviews',
306+
],
299307
data: self::serialize(
300308
$review->object(),
301309
'jsonld',
@@ -369,7 +377,12 @@ public function asAdminUserICanDeleteAReview(): void
369377
self::assertCount(1, self::getMercureMessages());
370378
self::assertEquals(
371379
new Update(
372-
topics: ['http://localhost/admin/reviews/' . $id, 'http://localhost/books/' . $bookId . '/reviews/' . $id],
380+
topics: [
381+
'http://localhost/admin/reviews/' . $id,
382+
'http://localhost/admin/reviews',
383+
'http://localhost/books/' . $bookId . '/reviews/' . $id,
384+
'http://localhost/books/' . $bookId . '/reviews',
385+
],
373386
data: json_encode(['@id' => '/admin/reviews/' . $id, '@type' => 'https://schema.org/Review']),
374387
),
375388
self::getMercureMessage()

api/tests/Api/Admin/UserTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public function asAdminUserICanGetAUser(): void
138138
'email' => UserFactory::createOneAdmin()->email,
139139
]);
140140

141-
$this->client->request('GET', '/admin/users/' . $user->getId(), ['auth_bearer' => $token]);
141+
$response = $this->client->request('GET', '/admin/users/' . $user->getId(), ['auth_bearer' => $token]);
142142

143143
self::assertResponseIsSuccessful();
144144
self::assertResponseHeaderSame('content-type', 'application/ld+json; charset=utf-8');

api/tests/Api/BookTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public function asAnonymousICanGetACollectionOfBooks(FactoryCollection $factory,
3939

4040
self::assertResponseIsSuccessful();
4141
self::assertResponseHeaderSame('content-type', 'application/ld+json; charset=utf-8');
42+
self::assertEquals('<https://localhost/.well-known/mercure>; rel="mercure"', $response->getHeaders()['link'][1]);
4243
self::assertJsonContains([
4344
'hydra:totalItems' => $hydraTotalItems,
4445
]);
@@ -96,6 +97,7 @@ public function asAdminUserICanGetACollectionOfBooksOrderedByTitle(): void
9697

9798
self::assertResponseIsSuccessful();
9899
self::assertResponseHeaderSame('content-type', 'application/ld+json; charset=utf-8');
100+
self::assertEquals('<https://localhost/.well-known/mercure>; rel="mercure"', $response->getHeaders()['link'][1]);
99101
self::assertEquals('Ball Lightning', $response->toArray()['hydra:member'][0]['title']);
100102
self::assertEquals('Hyperion', $response->toArray()['hydra:member'][1]['title']);
101103
self::assertEquals('The Wandering Earth', $response->toArray()['hydra:member'][2]['title']);
@@ -122,10 +124,11 @@ public function asAnonymousICanGetABook(): void
122124
ReviewFactory::createOne(['rating' => 4, 'book' => $book]);
123125
ReviewFactory::createOne(['rating' => 5, 'book' => $book]);
124126

125-
$this->client->request('GET', '/books/' . $book->getId());
127+
$response = $this->client->request('GET', '/books/' . $book->getId());
126128

127129
self::assertResponseIsSuccessful();
128130
self::assertResponseHeaderSame('content-type', 'application/ld+json; charset=utf-8');
131+
self::assertEquals('<https://localhost/.well-known/mercure>; rel="mercure"', $response->getHeaders(false)['link'][1]);
129132
self::assertJsonContains([
130133
'@id' => '/books/' . $book->getId(),
131134
'book' => $book->book,

api/tests/Api/BookmarkTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ public function asAUserICanGetACollectionOfMyBookmarksWithoutFilters(): void
7070

7171
self::assertResponseIsSuccessful();
7272
self::assertResponseHeaderSame('content-type', 'application/ld+json; charset=utf-8');
73+
self::assertEquals('<https://localhost/.well-known/mercure>; rel="mercure"', $response->getHeaders(false)['link'][1]);
7374
self::assertJsonContains([
7475
'hydra:totalItems' => 35,
7576
]);
@@ -167,6 +168,7 @@ public function asAUserICanCreateABookmark(): void
167168

168169
self::assertResponseIsSuccessful();
169170
self::assertResponseHeaderSame('content-type', 'application/ld+json; charset=utf-8');
171+
self::assertEquals('<https://localhost/.well-known/mercure>; rel="mercure"', $response->getHeaders(false)['link'][1]);
170172
self::assertJsonContains([
171173
'book' => [
172174
'@id' => '/books/' . $book->getId(),

api/tests/Api/ReviewTest.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public function asAnonymousICanGetACollectionOfBookReviewsWithoutFilters(Factory
5353

5454
self::assertResponseIsSuccessful();
5555
self::assertResponseHeaderSame('content-type', 'application/ld+json; charset=utf-8');
56+
self::assertEquals('<https://localhost/.well-known/mercure>; rel="mercure"', $response->getHeaders()['link'][1]);
5657
self::assertJsonContains([
5758
'hydra:totalItems' => $hydraTotalItems,
5859
]);
@@ -266,6 +267,7 @@ public function asAUserICanAddAReviewOnABook(): void
266267

267268
self::assertResponseStatusCodeSame(Response::HTTP_CREATED);
268269
self::assertResponseHeaderSame('content-type', 'application/ld+json; charset=utf-8');
270+
self::assertEquals('<https://localhost/.well-known/mercure>; rel="mercure"', $response->getHeaders(false)['link'][1]);
269271
self::assertJsonContains([
270272
'book' => '/books/' . $book->getId(),
271273
'user' => [
@@ -284,7 +286,12 @@ public function asAUserICanAddAReviewOnABook(): void
284286
self::assertCount(1, self::getMercureMessages());
285287
self::assertMercureUpdateMatchesJsonSchema(
286288
update: self::getMercureMessage(),
287-
topics: ['http://localhost/admin/reviews/' . $review->getId(), 'http://localhost/books/' . $book->getId() . '/reviews/' . $review->getId()],
289+
topics: [
290+
'http://localhost/admin/reviews/' . $review->getId(),
291+
'http://localhost/admin/reviews',
292+
'http://localhost/books/' . $book->getId() . '/reviews/' . $review->getId(),
293+
'http://localhost/books/' . $book->getId() . '/reviews',
294+
],
288295
jsonSchema: file_get_contents(__DIR__ . '/Admin/schemas/Review/item.json')
289296
);
290297
}
@@ -451,7 +458,7 @@ public function asAUserICanUpdateMyBookReview(): void
451458
'authorize' => true,
452459
]);
453460

454-
$this->client->request('PATCH', '/books/' . $review->book->getId() . '/reviews/' . $review->getId(), [
461+
$response = $this->client->request('PATCH', '/books/' . $review->book->getId() . '/reviews/' . $review->getId(), [
455462
'auth_bearer' => $token,
456463
'json' => [
457464
'body' => 'Very good book!',
@@ -464,6 +471,7 @@ public function asAUserICanUpdateMyBookReview(): void
464471

465472
self::assertResponseStatusCodeSame(Response::HTTP_OK);
466473
self::assertResponseHeaderSame('content-type', 'application/ld+json; charset=utf-8');
474+
self::assertEquals('<https://localhost/.well-known/mercure>; rel="mercure"', $response->getHeaders(false)['link'][1]);
467475
self::assertJsonContains([
468476
'body' => 'Very good book!',
469477
'rating' => 5,
@@ -472,7 +480,12 @@ public function asAUserICanUpdateMyBookReview(): void
472480
self::assertCount(1, self::getMercureMessages());
473481
self::assertMercureUpdateMatchesJsonSchema(
474482
update: self::getMercureMessage(),
475-
topics: ['http://localhost/admin/reviews/' . $review->getId(), 'http://localhost/books/' . $review->book->getId() . '/reviews/' . $review->getId()],
483+
topics: [
484+
'http://localhost/admin/reviews/' . $review->getId(),
485+
'http://localhost/admin/reviews',
486+
'http://localhost/books/' . $review->book->getId() . '/reviews/' . $review->getId(),
487+
'http://localhost/books/' . $review->book->getId() . '/reviews',
488+
],
476489
jsonSchema: file_get_contents(__DIR__ . '/Admin/schemas/Review/item.json')
477490
);
478491
}
@@ -560,7 +573,12 @@ public function asAUserICanDeleteMyBookReview(): void
560573
self::assertCount(1, self::getMercureMessages());
561574
self::assertEquals(
562575
new Update(
563-
topics: ['http://localhost/admin/reviews/' . $id, 'http://localhost/books/' . $bookId . '/reviews/' . $id],
576+
topics: [
577+
'http://localhost/admin/reviews/' . $id,
578+
'http://localhost/admin/reviews',
579+
'http://localhost/books/' . $bookId . '/reviews/' . $id,
580+
'http://localhost/books/' . $bookId . '/reviews',
581+
],
564582
data: json_encode(['@id' => '/admin/reviews/' . $id, '@type' => 'https://schema.org/Review']),
565583
),
566584
self::getMercureMessage()

0 commit comments

Comments
 (0)