Skip to content

Commit 8abcf52

Browse files
committed
Brought factory tests up to spec to test the entire factory
1 parent 981ca9c commit 8abcf52

File tree

2 files changed

+42
-21
lines changed

2 files changed

+42
-21
lines changed

src/Factory.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use ApiClients\Tools\CommandBus\Factory as CommandBusFactory;
1111
use DI\ContainerBuilder;
1212
use Interop\Container\ContainerInterface;
13+
use InvalidArgumentException;
1314
use League\Event\Emitter;
1415
use League\Event\EmitterInterface;
1516
use React\EventLoop\LoopInterface;
@@ -51,17 +52,17 @@ private static function createTransport(
5152
LoopInterface $loop = null,
5253
array $options = []
5354
): TransportClient {
55+
if (!isset($options[Options::TRANSPORT_OPTIONS])) {
56+
throw new InvalidArgumentException('Missing Transport options');
57+
}
58+
5459
return TransportFactory::create($container, $loop, $options[Options::TRANSPORT_OPTIONS]);
5560
}
5661

5762
private static function createHydrator(ContainerInterface $container, array $options = [])
5863
{
59-
if (isset($options[Options::HYDRATOR]) && $options[Options::HYDRATOR] instanceof Hydrator) {
60-
return $options[Options::HYDRATOR];
61-
}
62-
6364
if (!isset($options[Options::HYDRATOR_OPTIONS])) {
64-
throw new \Exception('Missing Hydrator options');
65+
throw new InvalidArgumentException('Missing Hydrator options');
6566
}
6667

6768
return HydratorFactory::create($container, $options[Options::HYDRATOR_OPTIONS]);

tests/FactoryTest.php

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
namespace ApiClients\Tests\Foundation;
44

55
use ApiClients\Foundation\Client;
6-
use ApiClients\Foundation\Events\CommandLocatorEvent;
76
use ApiClients\Foundation\Factory;
7+
use ApiClients\Foundation\Hydrator\Hydrator;
8+
use ApiClients\Foundation\Transport\Client as TransportClient;
89
use ApiClients\Foundation\Options;
910
use ApiClients\Tools\TestUtilities\TestCase;
10-
use League\Event\CallbackListener;
11-
use League\Event\EmitterInterface;
11+
use InvalidArgumentException;
1212
use League\Tactician\Exception\MissingHandlerException;
1313
use React\EventLoop\Factory as LoopFactory;
14+
use React\EventLoop\LoopInterface;
1415
use Throwable;
1516
use function Clue\React\Block\await;
1617

@@ -31,27 +32,46 @@ public function testCreate()
3132
$this->assertInstanceOf(Client::class, $client);
3233

3334
$container = $client->getContainer();
34-
35-
$called = false;
36-
$container->get(EmitterInterface::class)->addListener(
37-
CommandLocatorEvent::NAME,
38-
CallbackListener::fromCallable(
39-
function (CommandLocatorEvent $event) use (&$called) {
40-
$called = true;
41-
}
42-
)
43-
);
44-
45-
$this->assertFalse($called);
35+
$this->assertInstanceOf(LoopInterface::class, $container->get(LoopInterface::class));
36+
$this->assertSame($loop, $container->get(LoopInterface::class));
37+
$this->assertInstanceOf(Hydrator::class, $container->get(Hydrator::class));
38+
$this->assertInstanceOf(TransportClient::class, $container->get(TransportClient::class));
4639

4740
try {
4841
await($client->handle(new class() {}), $loop);
4942
} catch (Throwable $exception) {
5043

5144
}
5245

53-
$this->assertTrue($called);
5446
$this->assertTrue(isset($exception));
5547
$this->assertInstanceOf(MissingHandlerException::class, $exception);
5648
}
49+
50+
/**
51+
* @expectedException InvalidArgumentException
52+
* @expectedExceptionMessage Missing Hydrator options
53+
*/
54+
public function testCreateMissingHydratorOptions()
55+
{
56+
Factory::create(
57+
LoopFactory::create(),
58+
[
59+
Options::TRANSPORT_OPTIONS => [],
60+
]
61+
)->getContainer()->get(Hydrator::class);
62+
}
63+
64+
/**
65+
* @expectedException InvalidArgumentException
66+
* @expectedExceptionMessage Missing Transport options
67+
*/
68+
public function testCreateMissingTransportOptions()
69+
{
70+
Factory::create(
71+
LoopFactory::create(),
72+
[
73+
Options::HYDRATOR_OPTIONS => [],
74+
]
75+
)->getContainer()->get(TransportClient::class);
76+
}
5777
}

0 commit comments

Comments
 (0)