Skip to content

Commit

Permalink
test(symfony): implement deprecated publisherinterface
Browse files Browse the repository at this point in the history
  • Loading branch information
soyuka committed Jun 4, 2024
1 parent b3d6baa commit fad65d6
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 45 deletions.
15 changes: 10 additions & 5 deletions tests/Behat/MercureContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace ApiPlatform\Tests\Behat;

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

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

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

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

$updateHandler = $this->driverContainer->get('mercure.hub.default.message_handler');

$updateHandler = $this->getMercureTestHub();
foreach ($updateHandler->getUpdates() as $sentUpdate) {
$toMatchTopics = \count($topics);
foreach ($sentUpdate->getTopics() as $sentTopic) {
Expand All @@ -136,4 +136,9 @@ public function theFollowingMercureUpdateShouldHaveBeenSent(string $topics, PySt

throw new \RuntimeException('Mercure update has not been sent.');
}

private function getMercureTestHub(): TestHub
{
return $this->driverContainer->get('mercure.hub.default.test_hub');
}
}
36 changes: 0 additions & 36 deletions tests/Fixtures/DummyMercureUpdateHandler.php

This file was deleted.

66 changes: 66 additions & 0 deletions tests/Fixtures/TestBundle/Mercure/TestHub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Tests\Fixtures\TestBundle\Mercure;

use Symfony\Component\Mercure\HubInterface;
use Symfony\Component\Mercure\Jwt\TokenFactoryInterface;
use Symfony\Component\Mercure\Jwt\TokenProviderInterface;
use Symfony\Component\Mercure\Update;

final class TestHub implements HubInterface
{
/**
* @var Update[]
*/
private array $updates = [];

public function __construct(private readonly HubInterface $hub)
{
}

/**
* @return array<Update>
*/
public function getUpdates(): array
{
return $this->updates;
}

public function getUrl(): string
{
return $this->hub->getUrl();
}

public function getPublicUrl(): string
{
return $this->hub->getPublicUrl();
}

public function getProvider(): TokenProviderInterface
{
return $this->hub->getProvider();
}

public function getFactory(): ?TokenFactoryInterface
{
return $this->hub->getFactory();
}

public function publish(Update $update): string
{
$this->updates[] = $update;

return $this->hub->publish($update);
}
}
12 changes: 8 additions & 4 deletions tests/Fixtures/app/config/config_common.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ mercure:
default:
url: '%env(MERCURE_URL)%'
jwt: '%env(MERCURE_JWT_SECRET)%'
debug:
url: '%env(MERCURE_URL)%'
jwt: '%env(MERCURE_JWT_SECRET)%'

api_platform:
title: 'My Dummy API'
Expand Down Expand Up @@ -268,11 +271,12 @@ services:
tags:
- name: 'api_platform.validation_groups_generator'

mercure.hub.default.message_handler:
class: ApiPlatform\Tests\Fixtures\DummyMercureUpdateHandler
mercure.hub.default.test_hub:
class: ApiPlatform\Tests\Fixtures\TestBundle\Mercure\TestHub
arguments:
$hub: '@mercure.hub.debug'
public: true
tags: ['messenger.message_handler']
mercure.hub.default.publisher: '@mercure.hub.default.message_handler'
mercure.hub.default: '@mercure.hub.default.test_hub'

ApiPlatform\Tests\Fixtures\TestBundle\State\RecoverPasswordProcessor:
tags:
Expand Down

0 comments on commit fad65d6

Please sign in to comment.