Skip to content

Commit 97c1c6d

Browse files
committed
Refactor tests into reusable components
1 parent a0d8f44 commit 97c1c6d

File tree

1 file changed

+27
-16
lines changed

1 file changed

+27
-16
lines changed

tests/FactoryTest.php

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,36 +10,47 @@ class FactoryTest extends PHPUnit_Framework_TestCase
1010
{
1111
private $factory;
1212

13-
public function testClientSuccess()
13+
public function setUp()
1414
{
15-
$loop = React\EventLoop\Factory::create();
16-
$factory = new Datagram\Factory($loop, $this->createResolverMock());
15+
$this->loop = React\EventLoop\Factory::create();
16+
$this->factory = new Datagram\Factory($this->loop, $this->createResolverMock());
17+
}
18+
19+
public function testCreateClient()
20+
{
21+
$promise = $this->factory->createClient('127.0.0.1', 12345);
22+
23+
$capturedClient = $this->getValueFromResolvedPromise($promise);
24+
$this->assertInstanceOf('Datagram\Socket', $capturedClient);
25+
26+
$capturedClient->close();
27+
}
1728

18-
$promise = $factory->createClient('127.0.0.1', 12345);
29+
protected function getValueFromResolvedPromise($promise)
30+
{
1931
$this->assertInstanceOf('React\Promise\PromiseInterface', $promise);
2032

21-
$capturedClient = null;
22-
$promise->then(function ($client) use (&$capturedClient, $loop) {
23-
$capturedClient = $client;
33+
$loop = $this->loop;
34+
$capturedValue = null;
35+
$promise->then(function ($value) use (&$capturedValue, $loop) {
36+
$capturedValue = $value;
2437
$loop->stop();
2538
}, $this->expectCallableNever());
2639

27-
// future-turn resolutions are not enforced, so the client MAY be known here already
28-
if ($capturedClient === null) {
40+
// future-turn resolutions are not enforced, so the value MAY be known here already
41+
if ($capturedValue === null) {
2942
$loop->run();
3043
}
3144

32-
$this->assertInstanceOf('Datagram\Socket', $capturedClient);
33-
34-
$capturedClient->close();
45+
return $capturedValue;
3546
}
3647

3748
protected function expectCallableOnce()
3849
{
3950
$mock = $this->createCallableMock();
4051
$mock
41-
->expects($this->once())
42-
->method('__invoke');
52+
->expects($this->once())
53+
->method('__invoke');
4354

4455
return $mock;
4556
}
@@ -48,8 +59,8 @@ protected function expectCallableNever()
4859
{
4960
$mock = $this->createCallableMock();
5061
$mock
51-
->expects($this->never())
52-
->method('__invoke');
62+
->expects($this->never())
63+
->method('__invoke');
5364

5465
return $mock;
5566
}

0 commit comments

Comments
 (0)