Skip to content

Commit 3cad6e5

Browse files
fix: some cleans about Mercure (#396)
1 parent a81a33a commit 3cad6e5

File tree

6 files changed

+20
-20
lines changed

6 files changed

+20
-20
lines changed

api/src/State/Processor/MercureProcessor.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function __construct(
3535
}
3636

3737
/**
38-
* @param array{item_uri_template?: string, topics?: array, mercure_data?: string} $context
38+
* @param array{id?: string, type?: string, private?: bool, retry?: int, item_uri_template?: string, topics?: array, mercure_data?: string} $context
3939
*
4040
* @throws InvalidArgumentException
4141
* @throws ResourceClassNotFoundException
@@ -61,7 +61,11 @@ public function process(mixed $data, Operation $operation, array $uriVariables =
6161

6262
$this->hubRegistry->getHub()->publish(new Update(
6363
topics: $context['topics'],
64-
data: $context[self::DATA]
64+
data: $context[self::DATA],
65+
id: $context['id'] ?? null,
66+
type: $context['type'] ?? null,
67+
private: $context['private'] ?? false,
68+
retry: $context['retry'] ?? null,
6569
));
6670

6771
return $data;

api/tests/Api/Admin/BookTest.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -627,18 +627,17 @@ public function asAdminUserICanDeleteABook(): void
627627
self::assertEmpty($response->getContent());
628628
BookFactory::assert()->notExists(['title' => 'Hyperion']);
629629
self::assertCount(2, self::getMercureMessages());
630-
// todo how to ensure it's a delete update
631630
self::assertEquals(
632631
new Update(
633632
topics: ['http://localhost/admin/books/' . $id],
634-
data: json_encode(['@id' => 'http://localhost/admin/books/' . $id])
633+
data: json_encode(['@id' => 'http://localhost/admin/books/' . $id]),
635634
),
636635
self::getMercureMessage()
637636
);
638637
self::assertEquals(
639638
new Update(
640639
topics: ['http://localhost/books/' . $id],
641-
data: json_encode(['@id' => 'http://localhost/books/' . $id])
640+
data: json_encode(['@id' => 'http://localhost/books/' . $id]),
642641
),
643642
self::getMercureMessage(1)
644643
);

api/tests/Api/Admin/ReviewTest.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -376,18 +376,17 @@ public function asAdminUserICanDeleteAReview(): void
376376
self::assertEmpty($response->getContent());
377377
ReviewFactory::assert()->notExists(['body' => 'Best book ever!']);
378378
self::assertCount(2, self::getMercureMessages());
379-
// todo how to ensure it's a delete update
380379
self::assertEquals(
381380
new Update(
382381
topics: ['http://localhost/admin/reviews/' . $id],
383-
data: json_encode(['@id' => 'http://localhost/admin/reviews/' . $id])
382+
data: json_encode(['@id' => 'http://localhost/admin/reviews/' . $id]),
384383
),
385384
self::getMercureMessage()
386385
);
387386
self::assertEquals(
388387
new Update(
389388
topics: ['http://localhost/books/' . $bookId . '/reviews/' . $id],
390-
data: json_encode(['@id' => 'http://localhost/books/' . $bookId . '/reviews/' . $id])
389+
data: json_encode(['@id' => 'http://localhost/books/' . $bookId . '/reviews/' . $id]),
391390
),
392391
self::getMercureMessage(1)
393392
);

api/tests/Api/BookmarkTest.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ public function asAUserICanCreateABookmark(): void
184184
$object,
185185
'jsonld',
186186
self::getOperationNormalizationContext(Bookmark::class, '/bookmarks/{id}{._format}')
187-
)
187+
),
188188
)
189189
);
190190
}
@@ -302,11 +302,10 @@ public function asAUserICanDeleteMyBookmark(): void
302302
self::assertEmpty($response->getContent());
303303
BookmarkFactory::assert()->notExists(['book' => $book]);
304304
self::assertCount(1, self::getMercureMessages());
305-
// todo how to ensure it's a delete update
306305
self::assertEquals(
307306
new Update(
308307
topics: ['http://localhost/bookmarks/' . $id],
309-
data: json_encode(['@id' => '/bookmarks/' . $id, '@type' => 'https://schema.org/BookmarkAction'])
308+
data: json_encode(['@id' => '/bookmarks/' . $id, '@type' => 'https://schema.org/BookmarkAction']),
310309
),
311310
self::getMercureMessage()
312311
);

api/tests/Api/ReviewTest.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -568,18 +568,17 @@ public function asAUserICanDeleteMyBookReview(): void
568568
self::assertEmpty($response->getContent());
569569
ReviewFactory::assert()->notExists(['body' => 'Best book ever!']);
570570
self::assertCount(2, self::getMercureMessages());
571-
// todo how to ensure it's a delete update
572571
self::assertEquals(
573572
new Update(
574573
topics: ['http://localhost/admin/reviews/' . $id],
575-
data: json_encode(['@id' => 'http://localhost/admin/reviews/' . $id])
574+
data: json_encode(['@id' => 'http://localhost/admin/reviews/' . $id]),
576575
),
577576
self::getMercureMessage()
578577
);
579578
self::assertEquals(
580579
new Update(
581580
topics: ['http://localhost/books/' . $bookId . '/reviews/' . $id],
582-
data: json_encode(['@id' => 'http://localhost/books/' . $bookId . '/reviews/' . $id])
581+
data: json_encode(['@id' => 'http://localhost/books/' . $bookId . '/reviews/' . $id]),
583582
),
584583
self::getMercureMessage(1)
585584
);

api/tests/State/Processor/MercureProcessorTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace App\Tests\State\Processor;
66

77
use ApiPlatform\Api\IriConverterInterface;
8-
use ApiPlatform\Api\UrlGeneratorInterface;
98
use ApiPlatform\Metadata\ApiResource;
109
use ApiPlatform\Metadata\Get;
1110
use ApiPlatform\Metadata\Operation;
@@ -63,8 +62,9 @@ public function itSendsAMercureUpdate(): void
6362
$this->iriConverterMock
6463
->expects($this->once())
6564
->method('getIriFromResource')
66-
->with($this->objectMock, UrlGeneratorInterface::ABS_URL, $this->operationMock)
67-
->willReturn('/books/9aff4b91-31cf-4e91-94b0-1d52bbe23fe6')
65+
->willReturnOnConsecutiveCalls(
66+
'https://example.com/books/9aff4b91-31cf-4e91-94b0-1d52bbe23fe6',
67+
)
6868
;
6969
$this->operationMock
7070
->expects($this->once())
@@ -81,7 +81,7 @@ public function itSendsAMercureUpdate(): void
8181
->expects($this->once())
8282
->method('publish')
8383
->with($this->equalTo(new Update(
84-
topics: ['/books/9aff4b91-31cf-4e91-94b0-1d52bbe23fe6'],
84+
topics: ['https://example.com/books/9aff4b91-31cf-4e91-94b0-1d52bbe23fe6'],
8585
data: json_encode(['foo' => 'bar']),
8686
)))
8787
;
@@ -105,14 +105,14 @@ public function itSendsAMercureUpdateWithContextOptions(): void
105105
->expects($this->once())
106106
->method('publish')
107107
->with($this->equalTo(new Update(
108-
topics: ['/admin/books/9aff4b91-31cf-4e91-94b0-1d52bbe23fe6'],
108+
topics: ['https://example.com/admin/books/9aff4b91-31cf-4e91-94b0-1d52bbe23fe6'],
109109
data: json_encode(['bar' => 'baz']),
110110
)))
111111
;
112112

113113
$this->processor->process($this->objectMock, $this->operationMock, [], [
114114
'item_uri_template' => '/admin/books/{id}{._format}',
115-
'topics' => ['/admin/books/9aff4b91-31cf-4e91-94b0-1d52bbe23fe6'],
115+
'topics' => ['https://example.com/admin/books/9aff4b91-31cf-4e91-94b0-1d52bbe23fe6'],
116116
MercureProcessor::DATA => json_encode(['bar' => 'baz']),
117117
]);
118118
}

0 commit comments

Comments
 (0)