Skip to content

Commit

Permalink
fix: some cleans about Mercure (#396)
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentchalamon committed Mar 29, 2024
1 parent a81a33a commit 3cad6e5
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 20 deletions.
8 changes: 6 additions & 2 deletions api/src/State/Processor/MercureProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function __construct(
}

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

$this->hubRegistry->getHub()->publish(new Update(
topics: $context['topics'],
data: $context[self::DATA]
data: $context[self::DATA],
id: $context['id'] ?? null,
type: $context['type'] ?? null,
private: $context['private'] ?? false,
retry: $context['retry'] ?? null,
));

return $data;
Expand Down
5 changes: 2 additions & 3 deletions api/tests/Api/Admin/BookTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -627,18 +627,17 @@ public function asAdminUserICanDeleteABook(): void
self::assertEmpty($response->getContent());
BookFactory::assert()->notExists(['title' => 'Hyperion']);
self::assertCount(2, self::getMercureMessages());
// todo how to ensure it's a delete update
self::assertEquals(
new Update(
topics: ['http://localhost/admin/books/' . $id],
data: json_encode(['@id' => 'http://localhost/admin/books/' . $id])
data: json_encode(['@id' => 'http://localhost/admin/books/' . $id]),
),
self::getMercureMessage()
);
self::assertEquals(
new Update(
topics: ['http://localhost/books/' . $id],
data: json_encode(['@id' => 'http://localhost/books/' . $id])
data: json_encode(['@id' => 'http://localhost/books/' . $id]),
),
self::getMercureMessage(1)
);
Expand Down
5 changes: 2 additions & 3 deletions api/tests/Api/Admin/ReviewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -376,18 +376,17 @@ public function asAdminUserICanDeleteAReview(): void
self::assertEmpty($response->getContent());
ReviewFactory::assert()->notExists(['body' => 'Best book ever!']);
self::assertCount(2, self::getMercureMessages());
// todo how to ensure it's a delete update
self::assertEquals(
new Update(
topics: ['http://localhost/admin/reviews/' . $id],
data: json_encode(['@id' => 'http://localhost/admin/reviews/' . $id])
data: json_encode(['@id' => 'http://localhost/admin/reviews/' . $id]),
),
self::getMercureMessage()
);
self::assertEquals(
new Update(
topics: ['http://localhost/books/' . $bookId . '/reviews/' . $id],
data: json_encode(['@id' => 'http://localhost/books/' . $bookId . '/reviews/' . $id])
data: json_encode(['@id' => 'http://localhost/books/' . $bookId . '/reviews/' . $id]),
),
self::getMercureMessage(1)
);
Expand Down
5 changes: 2 additions & 3 deletions api/tests/Api/BookmarkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ public function asAUserICanCreateABookmark(): void
$object,
'jsonld',
self::getOperationNormalizationContext(Bookmark::class, '/bookmarks/{id}{._format}')
)
),
)
);
}
Expand Down Expand Up @@ -302,11 +302,10 @@ public function asAUserICanDeleteMyBookmark(): void
self::assertEmpty($response->getContent());
BookmarkFactory::assert()->notExists(['book' => $book]);
self::assertCount(1, self::getMercureMessages());
// todo how to ensure it's a delete update
self::assertEquals(
new Update(
topics: ['http://localhost/bookmarks/' . $id],
data: json_encode(['@id' => '/bookmarks/' . $id, '@type' => 'https://schema.org/BookmarkAction'])
data: json_encode(['@id' => '/bookmarks/' . $id, '@type' => 'https://schema.org/BookmarkAction']),
),
self::getMercureMessage()
);
Expand Down
5 changes: 2 additions & 3 deletions api/tests/Api/ReviewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -568,18 +568,17 @@ public function asAUserICanDeleteMyBookReview(): void
self::assertEmpty($response->getContent());
ReviewFactory::assert()->notExists(['body' => 'Best book ever!']);
self::assertCount(2, self::getMercureMessages());
// todo how to ensure it's a delete update
self::assertEquals(
new Update(
topics: ['http://localhost/admin/reviews/' . $id],
data: json_encode(['@id' => 'http://localhost/admin/reviews/' . $id])
data: json_encode(['@id' => 'http://localhost/admin/reviews/' . $id]),
),
self::getMercureMessage()
);
self::assertEquals(
new Update(
topics: ['http://localhost/books/' . $bookId . '/reviews/' . $id],
data: json_encode(['@id' => 'http://localhost/books/' . $bookId . '/reviews/' . $id])
data: json_encode(['@id' => 'http://localhost/books/' . $bookId . '/reviews/' . $id]),
),
self::getMercureMessage(1)
);
Expand Down
12 changes: 6 additions & 6 deletions api/tests/State/Processor/MercureProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace App\Tests\State\Processor;

use ApiPlatform\Api\IriConverterInterface;
use ApiPlatform\Api\UrlGeneratorInterface;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\Operation;
Expand Down Expand Up @@ -63,8 +62,9 @@ public function itSendsAMercureUpdate(): void
$this->iriConverterMock
->expects($this->once())
->method('getIriFromResource')
->with($this->objectMock, UrlGeneratorInterface::ABS_URL, $this->operationMock)
->willReturn('/books/9aff4b91-31cf-4e91-94b0-1d52bbe23fe6')
->willReturnOnConsecutiveCalls(
'https://example.com/books/9aff4b91-31cf-4e91-94b0-1d52bbe23fe6',
)
;
$this->operationMock
->expects($this->once())
Expand All @@ -81,7 +81,7 @@ public function itSendsAMercureUpdate(): void
->expects($this->once())
->method('publish')
->with($this->equalTo(new Update(
topics: ['/books/9aff4b91-31cf-4e91-94b0-1d52bbe23fe6'],
topics: ['https://example.com/books/9aff4b91-31cf-4e91-94b0-1d52bbe23fe6'],
data: json_encode(['foo' => 'bar']),
)))
;
Expand All @@ -105,14 +105,14 @@ public function itSendsAMercureUpdateWithContextOptions(): void
->expects($this->once())
->method('publish')
->with($this->equalTo(new Update(
topics: ['/admin/books/9aff4b91-31cf-4e91-94b0-1d52bbe23fe6'],
topics: ['https://example.com/admin/books/9aff4b91-31cf-4e91-94b0-1d52bbe23fe6'],
data: json_encode(['bar' => 'baz']),
)))
;

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

0 comments on commit 3cad6e5

Please sign in to comment.