Skip to content

Commit 8daca4a

Browse files
authored
Add HubRegistry::all method and HubRegistry tests (#50)
1 parent ce22b0d commit 8daca4a

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

src/HubRegistry.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,12 @@ public function getHub(string $name = null): HubInterface
4141

4242
return $this->hubs[$name];
4343
}
44+
45+
/**
46+
* @return array<string, HubInterface>
47+
*/
48+
public function all(): array
49+
{
50+
return $this->hubs;
51+
}
4452
}

tests/HubRegistryTest.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Mercure Component 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 Symfony\Component\Mercure\Tests;
15+
16+
use PHPUnit\Framework\TestCase;
17+
use Symfony\Component\Mercure\Exception\InvalidArgumentException;
18+
use Symfony\Component\Mercure\HubRegistry;
19+
use Symfony\Component\Mercure\Jwt\StaticTokenProvider;
20+
use Symfony\Component\Mercure\MockHub;
21+
22+
class HubRegistryTest extends TestCase
23+
{
24+
public function testGetHubByName(): void
25+
{
26+
$fooHub = new MockHub('fooUrl', new StaticTokenProvider('fooToken'), static function (): string { return 'foo'; });
27+
$barHub = new MockHub('barUrl', new StaticTokenProvider('barToken'), static function (): string { return 'bar'; });
28+
$registry = new HubRegistry($fooHub, ['foo' => $fooHub, 'bar' => $barHub]);
29+
30+
$this->assertSame($fooHub, $registry->getHub('foo'));
31+
}
32+
33+
public function testGetDefaultHub(): void
34+
{
35+
$fooHub = new MockHub('fooUrl', new StaticTokenProvider('fooToken'), static function (): string { return 'foo'; });
36+
$barHub = new MockHub('barUrl', new StaticTokenProvider('barToken'), static function (): string { return 'bar'; });
37+
$registry = new HubRegistry($fooHub, ['foo' => $fooHub, 'bar' => $barHub]);
38+
39+
$this->assertSame($fooHub, $registry->getHub());
40+
}
41+
42+
public function testGetMissingHubThrows(): void
43+
{
44+
$fooHub = new MockHub('fooUrl', new StaticTokenProvider('fooToken'), static function (): string { return 'foo'; });
45+
$registry = new HubRegistry($fooHub, ['foo' => $fooHub]);
46+
47+
$this->expectException(InvalidArgumentException::class);
48+
$registry->getHub('bar');
49+
}
50+
51+
public function testGetAllHubs(): void
52+
{
53+
$fooHub = new MockHub('fooUrl', new StaticTokenProvider('fooToken'), static function (): string { return 'foo'; });
54+
$barHub = new MockHub('barUrl', new StaticTokenProvider('barToken'), static function (): string { return 'bar'; });
55+
$registry = new HubRegistry($fooHub, ['foo' => $fooHub, 'bar' => $barHub]);
56+
57+
$this->assertSame(['foo' => $fooHub, 'bar' => $barHub], $registry->all());
58+
}
59+
}

0 commit comments

Comments
 (0)