Skip to content

Commit 332aaa1

Browse files
committed
test(symfony): implement deprecated publisherinterface
1 parent 98e9cac commit 332aaa1

File tree

4 files changed

+84
-45
lines changed

4 files changed

+84
-45
lines changed

tests/Behat/MercureContext.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace ApiPlatform\Tests\Behat;
1515

16+
use ApiPlatform\Tests\Fixtures\TestBundle\Mercure\TestHub;
1617
use Behat\Behat\Context\Context;
1718
use Behat\Gherkin\Node\PyStringNode;
1819
use Behat\Gherkin\Node\TableNode;
@@ -37,7 +38,7 @@ public function __construct(private readonly ContainerInterface $driverContainer
3738
*/
3839
public function mercureUpdatesShouldHaveBeenSent(int $number): void
3940
{
40-
$updateHandler = $this->driverContainer->get('mercure.hub.default.message_handler');
41+
$updateHandler = $this->getMercureTestHub();
4142
$total = \count($updateHandler->getUpdates());
4243

4344
if (0 === $total) {
@@ -70,7 +71,7 @@ public function firstMercureUpdateShouldHaveData(PyStringNode $data): void
7071
*/
7172
public function mercureUpdateShouldHaveTopics(int $index, TableNode $table): void
7273
{
73-
$updateHandler = $this->driverContainer->get('mercure.hub.default.message_handler');
74+
$updateHandler = $this->getMercureTestHub();
7475
$updates = $updateHandler->getUpdates();
7576

7677
if (0 === \count($updates)) {
@@ -90,7 +91,7 @@ public function mercureUpdateShouldHaveTopics(int $index, TableNode $table): voi
9091
*/
9192
public function mercureUpdateShouldHaveData(int $index, PyStringNode $data): void
9293
{
93-
$updateHandler = $this->driverContainer->get('mercure.hub.default.message_handler');
94+
$updateHandler = $this->getMercureTestHub();
9495
$updates = $updateHandler->getUpdates();
9596

9697
if (0 === \count($updates)) {
@@ -113,8 +114,7 @@ public function theFollowingMercureUpdateShouldHaveBeenSent(string $topics, PySt
113114
$topics = explode(',', $topics);
114115
$update = json_decode($update->getRaw(), true, 512, \JSON_THROW_ON_ERROR);
115116

116-
$updateHandler = $this->driverContainer->get('mercure.hub.default.message_handler');
117-
117+
$updateHandler = $this->getMercureTestHub();
118118
foreach ($updateHandler->getUpdates() as $sentUpdate) {
119119
$toMatchTopics = \count($topics);
120120
foreach ($sentUpdate->getTopics() as $sentTopic) {
@@ -136,4 +136,9 @@ public function theFollowingMercureUpdateShouldHaveBeenSent(string $topics, PySt
136136

137137
throw new \RuntimeException('Mercure update has not been sent.');
138138
}
139+
140+
private function getMercureTestHub(): TestHub
141+
{
142+
return $this->driverContainer->get('mercure.hub.default.test_hub');
143+
}
139144
}

tests/Fixtures/DummyMercureUpdateHandler.php

Lines changed: 0 additions & 36 deletions
This file was deleted.
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Tests\Fixtures\TestBundle\Mercure;
15+
16+
use Symfony\Component\Mercure\HubInterface;
17+
use Symfony\Component\Mercure\Jwt\TokenFactoryInterface;
18+
use Symfony\Component\Mercure\Jwt\TokenProviderInterface;
19+
use Symfony\Component\Mercure\Update;
20+
21+
final class TestHub implements HubInterface
22+
{
23+
/**
24+
* @var Update[]
25+
*/
26+
private array $updates = [];
27+
28+
public function __construct(private readonly HubInterface $hub)
29+
{
30+
}
31+
32+
/**
33+
* @return array<Update>
34+
*/
35+
public function getUpdates(): array
36+
{
37+
return $this->updates;
38+
}
39+
40+
public function getUrl(): string
41+
{
42+
return $this->hub->getUrl();
43+
}
44+
45+
public function getPublicUrl(): string
46+
{
47+
return $this->hub->getPublicUrl();
48+
}
49+
50+
public function getProvider(): TokenProviderInterface
51+
{
52+
return $this->hub->getProvider();
53+
}
54+
55+
public function getFactory(): ?TokenFactoryInterface
56+
{
57+
return $this->hub->getFactory();
58+
}
59+
60+
public function publish(Update $update): string
61+
{
62+
$this->updates[] = $update;
63+
64+
return $this->hub->publish($update);
65+
}
66+
}

tests/Fixtures/app/config/config_common.yml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ mercure:
2727
default:
2828
url: '%env(MERCURE_URL)%'
2929
jwt: '%env(MERCURE_JWT_SECRET)%'
30+
debug:
31+
url: '%env(MERCURE_URL)%'
32+
jwt: '%env(MERCURE_JWT_SECRET)%'
3033

3134
api_platform:
3235
title: 'My Dummy API'
@@ -268,11 +271,12 @@ services:
268271
tags:
269272
- name: 'api_platform.validation_groups_generator'
270273

271-
mercure.hub.default.message_handler:
272-
class: ApiPlatform\Tests\Fixtures\DummyMercureUpdateHandler
274+
mercure.hub.default.test_hub:
275+
class: ApiPlatform\Tests\Fixtures\TestBundle\Mercure\TestHub
276+
arguments:
277+
$hub: '@mercure.hub.debug'
273278
public: true
274-
tags: ['messenger.message_handler']
275-
mercure.hub.default.publisher: '@mercure.hub.default.message_handler'
279+
mercure.hub.default: '@mercure.hub.default.test_hub'
276280

277281
ApiPlatform\Tests\Fixtures\TestBundle\State\RecoverPasswordProcessor:
278282
tags:

0 commit comments

Comments
 (0)