Skip to content

Commit d3e9d75

Browse files
authored
Improve test suite, add test namespaces, simplify test bootstrap… (#31)
Improve test suite, add test namespaces, simplify test bootstrap logic and add forward compatibility with PHPUnit 7 and PHPUnit 6
2 parents 86586b6 + 6b97970 commit d3e9d75

File tree

5 files changed

+51
-22
lines changed

5 files changed

+51
-22
lines changed

composer.json

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,14 @@
1111
}
1212
],
1313
"autoload": {
14-
"psr-4": {"React\\Datagram\\": "src"}
14+
"psr-4": {
15+
"React\\Datagram\\": "src"
16+
}
17+
},
18+
"autoload-dev": {
19+
"psr-4": {
20+
"React\\Tests\\Datagram\\": "tests"
21+
}
1522
},
1623
"require": {
1724
"php": ">=5.3",
@@ -22,6 +29,6 @@
2229
},
2330
"require-dev": {
2431
"clue/block-react": "~1.0",
25-
"phpunit/phpunit": "^5.0 || ^4.8"
32+
"phpunit/phpunit": "^7.0 || ^6.0 || ^5.0 || ^4.8.35"
2633
}
2734
}

phpunit.xml.dist

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22

3-
<phpunit colors="true" bootstrap="./tests/bootstrap.php">
3+
<phpunit backupGlobals="false"
4+
backupStaticAttributes="false"
5+
colors="true"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
processIsolation="false"
10+
stopOnFailure="false"
11+
bootstrap="vendor/autoload.php"
12+
>
413
<testsuites>
514
<testsuite name="Datagram Test Suite">
615
<directory>./tests/</directory>

tests/FactoryTest.php

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

3+
namespace React\Tests\Datagram;
4+
35
use React\Datagram\Socket;
46
use React\Datagram\Factory;
57
use Clue\React\Block;
@@ -13,7 +15,7 @@ class FactoryTest extends TestCase
1315

1416
public function setUp()
1517
{
16-
$this->loop = React\EventLoop\Factory::create();
18+
$this->loop = \React\EventLoop\Factory::create();
1719
$this->resolver = $this->createResolverMock();
1820
$this->factory = new Factory($this->loop, $this->resolver);
1921
}
@@ -62,6 +64,8 @@ public function testCreateClientLocalhostWithDefaultResolver()
6264
$promise = $this->factory->createClient('localhost:12345');
6365

6466
$capturedClient = Block\await($promise, $this->loop);
67+
$this->assertInstanceOf('React\Datagram\SocketInterface', $capturedClient);
68+
6569
$capturedClient->close();
6670
}
6771

@@ -129,11 +133,13 @@ public function testCreateClientWithHostnameWillUseResolver()
129133
$client->close();
130134
}
131135

136+
/**
137+
* @expectedException RuntimeException
138+
*/
132139
public function testCreateClientWithHostnameWillRejectIfResolverRejects()
133140
{
134141
$this->resolver->expects($this->once())->method('resolve')->with('example.com')->willReturn(Promise\reject(new \RuntimeException('test')));
135142

136-
$this->setExpectedException('RuntimeException');
137143
Block\await($this->factory->createClient('example.com:0'), $this->loop);
138144
}
139145

@@ -155,6 +161,9 @@ public function testCreateServerWithInvalidHostnameWillReject()
155161
Block\await($this->factory->createServer('/////'), $this->loop);
156162
}
157163

164+
/**
165+
* @expectedException RuntimeException
166+
*/
158167
public function testCancelCreateClientWithCancellableHostnameResolver()
159168
{
160169
$promise = new Promise\Promise(function () { }, $this->expectCallableOnce());
@@ -163,10 +172,12 @@ public function testCancelCreateClientWithCancellableHostnameResolver()
163172
$promise = $this->factory->createClient('example.com:0');
164173
$promise->cancel();
165174

166-
$this->setExpectedException('RuntimeException');
167175
Block\await($promise, $this->loop);
168176
}
169177

178+
/**
179+
* @expectedException RuntimeException
180+
*/
170181
public function testCancelCreateClientWithUncancellableHostnameResolver()
171182
{
172183
$promise = $this->getMockBuilder('React\Promise\PromiseInterface')->getMock();
@@ -175,7 +186,6 @@ public function testCancelCreateClientWithUncancellableHostnameResolver()
175186
$promise = $this->factory->createClient('example.com:0');
176187
$promise->cancel();
177188

178-
$this->setExpectedException('RuntimeException');
179189
Block\await($promise, $this->loop);
180190
}
181191
}

tests/SocketTest.php

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

3+
namespace React\Tests\Datagram;
4+
35
use React\Datagram\Socket;
46
use Clue\React\Block;
57

@@ -10,10 +12,13 @@ class SocketTest extends TestCase
1012

1113
public function setUp()
1214
{
13-
$this->loop = React\EventLoop\Factory::create();
14-
$this->factory = new React\Datagram\Factory($this->loop, $this->createResolverMock());
15+
$this->loop = \React\EventLoop\Factory::create();
16+
$this->factory = new \React\Datagram\Factory($this->loop, $this->createResolverMock());
1517
}
1618

19+
/**
20+
* @doesNotPerformAssertions
21+
*/
1722
public function testCreateClientCloseWillNotBlock()
1823
{
1924
$promise = $this->factory->createClient('127.0.0.1:12345');
@@ -28,7 +33,7 @@ public function testCreateClientCloseWillNotBlock()
2833
}
2934

3035
/**
31-
*
36+
* @doesNotPerformAssertions
3237
* @param Socket $client
3338
* @depends testCreateClientCloseWillNotBlock
3439
*/
@@ -38,6 +43,9 @@ public function testClientCloseAgainWillNotBlock(Socket $client)
3843
$this->loop->run();
3944
}
4045

46+
/**
47+
* @doesNotPerformAssertions
48+
*/
4149
public function testCreateClientEndWillNotBlock()
4250
{
4351
$promise = $this->factory->createClient('127.0.0.1:12345');
@@ -52,7 +60,7 @@ public function testCreateClientEndWillNotBlock()
5260
}
5361

5462
/**
55-
*
63+
* @doesNotPerformAssertions
5664
* @param Socket $client
5765
* @depends testCreateClientEndWillNotBlock
5866
*/
@@ -65,7 +73,7 @@ public function testClientEndAgainWillNotBlock(Socket $client)
6573
}
6674

6775
/**
68-
*
76+
* @doesNotPerformAssertions
6977
* @param Socket $client
7078
* @depends testClientEndAgainWillNotBlock
7179
*/
Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
<?php
22

3-
require __DIR__ . '/../vendor/autoload.php';
3+
namespace React\Tests\Datagram;
44

5-
abstract class TestCase extends PHPUnit_Framework_TestCase
5+
use PHPUnit\Framework\TestCase as BaseTestCase;
6+
7+
abstract class TestCase extends BaseTestCase
68
{
79
protected function expectCallableOnce()
810
{
@@ -26,18 +28,11 @@ protected function expectCallableNever()
2628

2729
protected function createCallableMock()
2830
{
29-
return $this->getMockBuilder('CallableStub')->getMock();
31+
return $this->getMockBuilder('stdClass')->setMethods(array('__invoke'))->getMock();
3032
}
3133

3234
protected function createResolverMock()
3335
{
3436
return $this->getMockBuilder('React\Dns\Resolver\ResolverInterface')->getMock();
3537
}
3638
}
37-
38-
class CallableStub
39-
{
40-
public function __invoke()
41-
{
42-
}
43-
}

0 commit comments

Comments
 (0)