Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add HubRegistry::all method #50

Merged
merged 1 commit into from Apr 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/HubRegistry.php
Expand Up @@ -41,4 +41,12 @@ public function getHub(string $name = null): HubInterface

return $this->hubs[$name];
}

/**
* @return array<string, HubInterface>
*/
public function all(): array
{
return $this->hubs;
}
}
59 changes: 59 additions & 0 deletions tests/HubRegistryTest.php
@@ -0,0 +1,59 @@
<?php

/*
* This file is part of the Mercure Component 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 Symfony\Component\Mercure\Tests;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Mercure\Exception\InvalidArgumentException;
use Symfony\Component\Mercure\HubRegistry;
use Symfony\Component\Mercure\Jwt\StaticTokenProvider;
use Symfony\Component\Mercure\MockHub;

class HubRegistryTest extends TestCase
{
public function testGetHubByName(): void
{
$fooHub = new MockHub('fooUrl', new StaticTokenProvider('fooToken'), static function (): string { return 'foo'; });
$barHub = new MockHub('barUrl', new StaticTokenProvider('barToken'), static function (): string { return 'bar'; });
$registry = new HubRegistry($fooHub, ['foo' => $fooHub, 'bar' => $barHub]);

$this->assertSame($fooHub, $registry->getHub('foo'));
}

public function testGetDefaultHub(): void
{
$fooHub = new MockHub('fooUrl', new StaticTokenProvider('fooToken'), static function (): string { return 'foo'; });
$barHub = new MockHub('barUrl', new StaticTokenProvider('barToken'), static function (): string { return 'bar'; });
$registry = new HubRegistry($fooHub, ['foo' => $fooHub, 'bar' => $barHub]);

$this->assertSame($fooHub, $registry->getHub());
}

public function testGetMissingHubThrows(): void
{
$fooHub = new MockHub('fooUrl', new StaticTokenProvider('fooToken'), static function (): string { return 'foo'; });
$registry = new HubRegistry($fooHub, ['foo' => $fooHub]);

$this->expectException(InvalidArgumentException::class);
$registry->getHub('bar');
}

public function testGetAllHubs(): void
{
$fooHub = new MockHub('fooUrl', new StaticTokenProvider('fooToken'), static function (): string { return 'foo'; });
$barHub = new MockHub('barUrl', new StaticTokenProvider('barToken'), static function (): string { return 'bar'; });
$registry = new HubRegistry($fooHub, ['foo' => $fooHub, 'bar' => $barHub]);

$this->assertSame(['foo' => $fooHub, 'bar' => $barHub], $registry->all());
}
}