Skip to content

Commit be83f99

Browse files
authored
Merge pull request #37 from SimonFrings/tests
Run tests on PHPUnit 9 and update PHPUnit configuration schema for PHPUnit 9.3
2 parents b4bda02 + e06cf84 commit be83f99

File tree

8 files changed

+101
-53
lines changed

8 files changed

+101
-53
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
/.travis.yml export-ignore
44
/examples export-ignore
55
/phpunit.xml.dist export-ignore
6+
/phpunit.xml.legacy export-ignore
67
/tests export-ignore

.travis.yml

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,27 @@
11
language: php
22

3-
php:
4-
# - 5.3 # requires old distro, see below
5-
- 5.4
6-
- 5.5
7-
- 5.6
8-
- 7.0
9-
- 7.1
10-
- 7.2
11-
- hhvm # ignore errors, see below
12-
133
# lock distro so new future defaults will not break the build
144
dist: trusty
155

16-
matrix:
6+
jobs:
177
include:
188
- php: 5.3
199
dist: precise
10+
- php: 5.4
11+
- php: 5.5
12+
- php: 5.6
13+
- php: 7.0
14+
- php: 7.1
15+
- php: 7.2
16+
- php: 7.3
17+
- php: 7.4
18+
- php: hhvm-3.18
2019
allow_failures:
21-
- php: hhvm
22-
23-
sudo: false
20+
- php: hhvm-3.18
2421

2522
install:
26-
- composer install --no-interaction
23+
- composer install
2724

2825
script:
29-
- vendor/bin/phpunit --coverage-text
26+
- if [[ "$TRAVIS_PHP_VERSION" > "7.2" ]]; then vendor/bin/phpunit --coverage-text; fi
27+
- if [[ "$TRAVIS_PHP_VERSION" < "7.3" ]]; then vendor/bin/phpunit --coverage-text -c phpunit.xml.legacy; fi

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,6 @@
4545
},
4646
"require-dev": {
4747
"clue/block-react": "~1.0",
48-
"phpunit/phpunit": "^7.0 || ^6.0 || ^5.0 || ^4.8.35"
48+
"phpunit/phpunit": "^9.3 || ^5.7 || ^4.8.35"
4949
}
5050
}

phpunit.xml.dist

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,19 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22

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"
3+
<!-- PHPUnit configuration file with new format for PHPUnit 9.3+ -->
4+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
116
bootstrap="vendor/autoload.php"
12-
>
7+
colors="true"
8+
cacheResult="false">
139
<testsuites>
1410
<testsuite name="Datagram Test Suite">
1511
<directory>./tests/</directory>
1612
</testsuite>
1713
</testsuites>
18-
<filter>
19-
<whitelist>
14+
<coverage>
15+
<include>
2016
<directory>./src/</directory>
21-
</whitelist>
22-
</filter>
17+
</include>
18+
</coverage>
2319
</phpunit>

phpunit.xml.legacy

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<!-- PHPUnit configuration file with old format for PHPUnit 9.2 or older -->
4+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/4.8/phpunit.xsd"
6+
backupGlobals="false"
7+
backupStaticAttributes="false"
8+
bootstrap="vendor/autoload.php"
9+
colors="true"
10+
convertErrorsToExceptions="true"
11+
convertNoticesToExceptions="true"
12+
convertWarningsToExceptions="true"
13+
processIsolation="false"
14+
stopOnFailure="false">
15+
<testsuites>
16+
<testsuite name="Datagram Test Suite">
17+
<directory>./tests/</directory>
18+
</testsuite>
19+
</testsuites>
20+
<filter>
21+
<whitelist>
22+
<directory>./src/</directory>
23+
</whitelist>
24+
</filter>
25+
</phpunit>

tests/FactoryTest.php

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ class FactoryTest extends TestCase
1313
private $resolver;
1414
private $factory;
1515

16-
public function setUp()
16+
/**
17+
* @before
18+
*/
19+
public function setUpFactory()
1720
{
1821
$this->loop = \React\EventLoop\Factory::create();
1922
$this->resolver = $this->createResolverMock();
@@ -31,7 +34,7 @@ public function testCreateClient()
3134

3235
$this->assertEquals('127.0.0.1:12345', $capturedClient->getRemoteAddress());
3336

34-
$this->assertContains('127.0.0.1:', $capturedClient->getLocalAddress());
37+
$this->assertContainsString('127.0.0.1:', $capturedClient->getLocalAddress());
3538
$this->assertNotEquals('127.0.0.1:12345', $capturedClient->getLocalAddress());
3639

3740
$capturedClient->close();
@@ -50,7 +53,7 @@ public function testCreateClientLocalhost()
5053

5154
$this->assertEquals('127.0.0.1:12345', $capturedClient->getRemoteAddress());
5255

53-
$this->assertContains('127.0.0.1:', $capturedClient->getLocalAddress());
56+
$this->assertContainsString('127.0.0.1:', $capturedClient->getLocalAddress());
5457
$this->assertNotEquals('127.0.0.1:12345', $capturedClient->getLocalAddress());
5558

5659
$capturedClient->close();
@@ -83,7 +86,7 @@ public function testCreateClientIpv6()
8386

8487
$this->assertEquals('[::1]:12345', $capturedClient->getRemoteAddress());
8588

86-
$this->assertContains('[::1]:', $capturedClient->getLocalAddress());
89+
$this->assertContainsString('[::1]:', $capturedClient->getLocalAddress());
8790
$this->assertNotEquals('[::1]:12345', $capturedClient->getLocalAddress());
8891

8992
$capturedClient->close();
@@ -133,37 +136,26 @@ public function testCreateClientWithHostnameWillUseResolver()
133136
$client->close();
134137
}
135138

136-
/**
137-
* @expectedException RuntimeException
138-
*/
139139
public function testCreateClientWithHostnameWillRejectIfResolverRejects()
140140
{
141141
$this->resolver->expects($this->once())->method('resolve')->with('example.com')->willReturn(Promise\reject(new \RuntimeException('test')));
142142

143+
$this->setExpectedException('RuntimeException');
143144
Block\await($this->factory->createClient('example.com:0'), $this->loop);
144145
}
145146

146-
/**
147-
* @expectedException Exception
148-
* @expectedExceptionMessage Unable to create client socket
149-
*/
150147
public function testCreateClientWithInvalidHostnameWillReject()
151148
{
149+
$this->setExpectedException('Exception', 'Unable to create client socket');
152150
Block\await($this->factory->createClient('/////'), $this->loop);
153151
}
154152

155-
/**
156-
* @expectedException Exception
157-
* @expectedExceptionMessage Unable to create server socket
158-
*/
159153
public function testCreateServerWithInvalidHostnameWillReject()
160154
{
155+
$this->setExpectedException('Exception', 'Unable to create server socket');
161156
Block\await($this->factory->createServer('/////'), $this->loop);
162157
}
163158

164-
/**
165-
* @expectedException RuntimeException
166-
*/
167159
public function testCancelCreateClientWithCancellableHostnameResolver()
168160
{
169161
$promise = new Promise\Promise(function () { }, $this->expectCallableOnce());
@@ -172,12 +164,10 @@ public function testCancelCreateClientWithCancellableHostnameResolver()
172164
$promise = $this->factory->createClient('example.com:0');
173165
$promise->cancel();
174166

167+
$this->setExpectedException('RuntimeException');
175168
Block\await($promise, $this->loop);
176169
}
177170

178-
/**
179-
* @expectedException RuntimeException
180-
*/
181171
public function testCancelCreateClientWithUncancellableHostnameResolver()
182172
{
183173
$promise = $this->getMockBuilder('React\Promise\PromiseInterface')->getMock();
@@ -186,6 +176,7 @@ public function testCancelCreateClientWithUncancellableHostnameResolver()
186176
$promise = $this->factory->createClient('example.com:0');
187177
$promise->cancel();
188178

179+
$this->setExpectedException('RuntimeException');
189180
Block\await($promise, $this->loop);
190181
}
191182
}

tests/SocketTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ class SocketTest extends TestCase
1010
private $loop;
1111
private $factory;
1212

13-
public function setUp()
13+
/**
14+
* @before
15+
*/
16+
public function setUpFactory()
1417
{
1518
$this->loop = \React\EventLoop\Factory::create();
1619
$this->factory = new \React\Datagram\Factory($this->loop, $this->createResolverMock());

tests/TestCase.php

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,45 @@ protected function expectCallableNever()
2828

2929
protected function createCallableMock()
3030
{
31-
return $this->getMockBuilder('stdClass')->setMethods(array('__invoke'))->getMock();
31+
if (method_exists('PHPUnit\Framework\MockObject\MockBuilder', 'addMethods')) {
32+
// PHPUnit 8.5+
33+
return $this->getMockBuilder('stdClass')->addMethods(array('__invoke'))->getMock();
34+
} else {
35+
// legacy PHPUnit 4 - PHPUnit 8.4
36+
return $this->getMockBuilder('stdClass')->setMethods(array('__invoke'))->getMock();
37+
}
3238
}
3339

3440
protected function createResolverMock()
3541
{
3642
return $this->getMockBuilder('React\Dns\Resolver\ResolverInterface')->getMock();
3743
}
44+
45+
public function setExpectedException($exception, $exceptionMessage = '', $exceptionCode = null)
46+
{
47+
if (method_exists($this, 'expectException')) {
48+
// PHPUnit 5.2+
49+
$this->expectException($exception);
50+
if ($exceptionMessage !== '') {
51+
$this->expectExceptionMessage($exceptionMessage);
52+
}
53+
if ($exceptionCode !== null) {
54+
$this->expectExceptionCode($exceptionCode);
55+
}
56+
} else {
57+
// legacy PHPUnit 4 - PHPUnit 5.1
58+
parent::setExpectedException($exception, $exceptionMessage, $exceptionCode);
59+
}
60+
}
61+
62+
public function assertContainsString($needle, $haystack)
63+
{
64+
if (method_exists($this, 'assertStringContainsString')) {
65+
// PHPUnit 7.5+
66+
$this->assertStringContainsString($needle, $haystack);
67+
} else {
68+
// legacy PHPUnit 4 - PHPUnit 7.5
69+
$this->assertContains($needle, $haystack);
70+
}
71+
}
3872
}

0 commit comments

Comments
 (0)