Skip to content

Commit b1eccee

Browse files
committed
Merge pull request #8 from clue-labs/block
Rely on clue/block-react in order to simplify tests
2 parents 188e603 + 59bf8b2 commit b1eccee

5 files changed

Lines changed: 25 additions & 35 deletions

File tree

.travis.yml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
language: php
2+
23
php:
34
- 5.3
45
- 5.4
@@ -7,7 +8,10 @@ php:
78
- 7
89
- hhvm
910

10-
before_script:
11-
- composer install --dev --prefer-source --no-interaction
11+
sudo: false
12+
13+
install:
14+
- composer install --prefer-source --no-interaction
15+
1216
script:
1317
- phpunit --coverage-text

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,8 @@
1919
"react/event-loop": ">=0.2, <0.5",
2020
"react/dns": ">=0.2, <0.5",
2121
"react/promise": "~2.0|~1.1"
22+
},
23+
"require-dev": {
24+
"clue/block-react": "~1.0"
2225
}
2326
}

tests/FactoryTest.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use React\Datagram\Socket;
4+
use Clue\React\Block;
45

56
class FactoryTest extends TestCase
67
{
@@ -16,7 +17,7 @@ public function testCreateClient()
1617
{
1718
$promise = $this->factory->createClient('127.0.0.1:12345');
1819

19-
$capturedClient = $this->getValueFromResolvedPromise($promise);
20+
$capturedClient = Block\await($promise, $this->loop);
2021
$this->assertInstanceOf('React\Datagram\Socket', $capturedClient);
2122

2223
$capturedClient->close();
@@ -26,7 +27,7 @@ public function testCreateClientLocalhost()
2627
{
2728
$promise = $this->factory->createClient('localhost:12345');
2829

29-
$capturedClient = $this->getValueFromResolvedPromise($promise);
30+
$capturedClient = Block\await($promise, $this->loop);
3031
$this->assertInstanceOf('React\Datagram\Socket', $capturedClient);
3132

3233
$capturedClient->close();
@@ -36,7 +37,7 @@ public function testCreateClientIpv6()
3637
{
3738
$promise = $this->factory->createClient('[::1]:12345');
3839

39-
$capturedClient = $this->getValueFromResolvedPromise($promise);
40+
$capturedClient = Block\await($promise, $this->loop);
4041
$this->assertInstanceOf('React\Datagram\Socket', $capturedClient);
4142

4243
$capturedClient->close();
@@ -46,7 +47,7 @@ public function testCreateServer()
4647
{
4748
$promise = $this->factory->createServer('127.0.0.1:12345');
4849

49-
$capturedServer = $this->getValueFromResolvedPromise($promise);
50+
$capturedServer = Block\await($promise, $this->loop);
5051
$this->assertInstanceOf('React\Datagram\Socket', $capturedServer);
5152

5253
$capturedServer->close();
@@ -56,7 +57,7 @@ public function testCreateServerRandomPort()
5657
{
5758
$promise = $this->factory->createServer('127.0.0.1:0');
5859

59-
$capturedServer = $this->getValueFromResolvedPromise($promise);
60+
$capturedServer = Block\await($promise, $this->loop);
6061
$this->assertInstanceOf('React\Datagram\Socket', $capturedServer);
6162

6263
$this->assertNotEquals('127.0.0.1:0', $capturedServer->getLocalAddress());

tests/SocketTest.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use React\Datagram\Socket;
4+
use Clue\React\Block;
45

56
class SocketTest extends TestCase
67
{
@@ -15,7 +16,7 @@ public function setUp()
1516
public function testCreateClientCloseWillNotBlock()
1617
{
1718
$promise = $this->factory->createClient('127.0.0.1:12345');
18-
$client = $this->getValueFromResolvedPromise($promise);
19+
$client = Block\await($promise, $this->loop);
1920

2021
$client->send('test');
2122
$client->close();
@@ -39,7 +40,7 @@ public function testClientCloseAgainWillNotBlock(Socket $client)
3940
public function testCreateClientEndWillNotBlock()
4041
{
4142
$promise = $this->factory->createClient('127.0.0.1:12345');
42-
$client = $this->getValueFromResolvedPromise($promise);
43+
$client = Block\await($promise, $this->loop);
4344

4445
$client->send('test');
4546
$client->end();
@@ -76,7 +77,7 @@ public function testClientSendAfterEndIsNoop(Socket $client)
7677
public function testClientSendHugeWillFail()
7778
{
7879
$promise = $this->factory->createClient('127.0.0.1:12345');
79-
$client = $this->getValueFromResolvedPromise($promise);
80+
$client = Block\await($promise, $this->loop);
8081

8182
$client->send(str_repeat(1, 1024 * 1024));
8283
$client->on('error', $this->expectCallableOnce());
@@ -88,7 +89,7 @@ public function testClientSendHugeWillFail()
8889
public function testClientSendNoServerWillFail()
8990
{
9091
$promise = $this->factory->createClient('127.0.0.1:1234');
91-
$client = $this->getValueFromResolvedPromise($promise);
92+
$client = Block\await($promise, $this->loop);
9293

9394
// send a message to a socket that is not actually listening
9495
// expect the remote end to reject this by sending an ICMP message
@@ -114,10 +115,10 @@ public function testClientSendNoServerWillFail()
114115
public function testCreatePair()
115116
{
116117
$promise = $this->factory->createServer('127.0.0.1:0');
117-
$server = $this->getValueFromResolvedPromise($promise);
118+
$server = Block\await($promise, $this->loop);
118119

119120
$promise = $this->factory->createClient($server->getLocalAddress());
120-
$client = $this->getValueFromResolvedPromise($promise);
121+
$client = Block\await($promise, $this->loop);
121122

122123
$that = $this;
123124
$server->on('message', function ($message, $remote, $server) use ($that) {
@@ -158,10 +159,10 @@ public function provideSanitizeAddress()
158159
public function testSanitizeAddress($address)
159160
{
160161
$promise = $this->factory->createServer($address);
161-
$server = $this->getValueFromResolvedPromise($promise);
162-
162+
$server = Block\await($promise, $this->loop);
163+
163164
$promise = $this->factory->createClient($server->getLocalAddress());
164-
$client = $this->getValueFromResolvedPromise($promise);
165+
$client = Block\await($promise, $this->loop);
165166

166167
$that = $this;
167168
$server->on('message', function ($message, $remote, $server) use ($that) {

tests/bootstrap.php

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,6 @@
44

55
abstract class TestCase extends PHPUnit_Framework_TestCase
66
{
7-
protected function getValueFromResolvedPromise($promise)
8-
{
9-
$this->assertInstanceOf('React\Promise\PromiseInterface', $promise);
10-
11-
$loop = $this->loop;
12-
$capturedValue = null;
13-
$promise->then(function ($value) use (&$capturedValue, $loop) {
14-
$capturedValue = $value;
15-
$loop->stop();
16-
}, $this->expectCallableNever());
17-
18-
// future-turn resolutions are not enforced, so the value MAY be known here already
19-
if ($capturedValue === null) {
20-
$loop->run();
21-
}
22-
23-
return $capturedValue;
24-
}
25-
267
protected function expectCallableOnce()
278
{
289
$mock = $this->createCallableMock();

0 commit comments

Comments
 (0)