Skip to content

Commit 81c03f7

Browse files
authored
Merge pull request #50 from SimonFrings/tests
Run tests on PHPUnit 9 and update PHPUnit configuration schema for PHPUnit 9.3
2 parents d4d1b16 + 7a03def commit 81c03f7

File tree

10 files changed

+77
-44
lines changed

10 files changed

+77
-44
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: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ language: php
33
# lock distro so new future defaults will not break the build
44
dist: trusty
55

6-
matrix:
6+
jobs:
77
include:
88
- php: 5.3
99
dist: precise
@@ -20,7 +20,8 @@ matrix:
2020
- php: hhvm-3.18
2121

2222
install:
23-
- composer install --no-interaction
23+
- composer install
2424

2525
script:
26-
- 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
@@ -23,6 +23,6 @@
2323
"react/promise-timer": "^1.5"
2424
},
2525
"require-dev": {
26-
"phpunit/phpunit": "^6.4 || ^5.7 || ^4.8.35"
26+
"phpunit/phpunit": "^9.3 || ^5.7 || ^4.8.35"
2727
}
2828
}

phpunit.xml.dist

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

3-
<phpunit bootstrap="vendor/autoload.php" colors="true">
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"
6+
bootstrap="vendor/autoload.php"
7+
colors="true"
8+
cacheResult="false">
49
<testsuites>
510
<testsuite name="Block React Test Suite">
611
<directory>./tests/</directory>
712
</testsuite>
813
</testsuites>
9-
<filter>
10-
<whitelist>
14+
<coverage>
15+
<include>
1116
<directory>./src/</directory>
12-
</whitelist>
13-
</filter>
17+
</include>
18+
</coverage>
1419
</phpunit>

phpunit.xml.legacy

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
bootstrap="vendor/autoload.php"
7+
colors="true">
8+
<testsuites>
9+
<testsuite name="Block React Test Suite">
10+
<directory>./tests/</directory>
11+
</testsuite>
12+
</testsuites>
13+
<filter>
14+
<whitelist>
15+
<directory>./src/</directory>
16+
</whitelist>
17+
</filter>
18+
</phpunit>

tests/FunctionAwaitAllTest.php

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,44 +23,36 @@ public function testAwaitAllAllResolved()
2323
$this->assertEquals(array('first' => 1, 'second' => 2), Block\awaitAll($all, $this->loop));
2424
}
2525

26-
/**
27-
* @expectedException Exception
28-
* @expectedExceptionMessage test
29-
*/
3026
public function testAwaitAllRejected()
3127
{
3228
$all = array(
3329
$this->createPromiseResolved(1),
3430
$this->createPromiseRejected(new \Exception('test'))
3531
);
3632

33+
$this->setExpectedException('Exception', 'test');
3734
Block\awaitAll($all, $this->loop);
3835
}
3936

40-
/**
41-
* @expectedException UnexpectedValueException
42-
*/
4337
public function testAwaitAllRejectedWithFalseWillWrapInUnexpectedValueException()
4438
{
4539
$all = array(
4640
$this->createPromiseResolved(1),
4741
Promise\reject(false)
4842
);
4943

44+
$this->setExpectedException('UnexpectedValueException');
5045
Block\awaitAll($all, $this->loop);
5146
}
5247

53-
/**
54-
* @expectedException Exception
55-
* @expectedExceptionMessage first
56-
*/
5748
public function testAwaitAllOnlyRejected()
5849
{
5950
$all = array(
6051
$this->createPromiseRejected(new \Exception('first')),
6152
$this->createPromiseRejected(new \Exception('second'))
6253
);
6354

55+
$this->setExpectedException('Exception', 'first');
6456
Block\awaitAll($all, $this->loop);
6557
}
6658

tests/FunctionAwaitAnyTest.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,9 @@
99

1010
class FunctionAwaitAnyTest extends TestCase
1111
{
12-
/**
13-
* @expectedException UnderflowException
14-
*/
1512
public function testAwaitAnyEmpty()
1613
{
14+
$this->setExpectedException('UnderflowException');
1715
Block\awaitAny(array(), $this->loop);
1816
}
1917

@@ -49,16 +47,14 @@ public function testAwaitAnyFirstResolvedConcurrently()
4947
$this->assertEquals(2, Block\awaitAny($all, $this->loop));
5048
}
5149

52-
/**
53-
* @expectedException UnderflowException
54-
*/
5550
public function testAwaitAnyAllRejected()
5651
{
5752
$all = array(
5853
$this->createPromiseRejected(1),
5954
$this->createPromiseRejected(2)
6055
);
6156

57+
$this->setExpectedException('UnderflowException');
6258
Block\awaitAny($all, $this->loop);
6359
}
6460

tests/FunctionAwaitTest.php

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,36 +8,27 @@
88

99
class FunctionAwaitTest extends TestCase
1010
{
11-
/**
12-
* @expectedException Exception
13-
* @expectedExceptionMessage test
14-
*/
1511
public function testAwaitOneRejected()
1612
{
1713
$promise = $this->createPromiseRejected(new \Exception('test'));
1814

15+
$this->setExpectedException('Exception', 'test');
1916
Block\await($promise, $this->loop);
2017
}
2118

22-
/**
23-
* @expectedException UnexpectedValueException
24-
* @expectedExceptionMessage Promise rejected with unexpected value of type bool
25-
*/
2619
public function testAwaitOneRejectedWithFalseWillWrapInUnexpectedValueException()
2720
{
2821
$promise = Promise\reject(false);
2922

23+
$this->setExpectedException('UnexpectedValueException', 'Promise rejected with unexpected value of type bool');
3024
Block\await($promise, $this->loop);
3125
}
3226

33-
/**
34-
* @expectedException UnexpectedValueException
35-
* @expectedExceptionMessage Promise rejected with unexpected value of type NULL
36-
*/
3727
public function testAwaitOneRejectedWithNullWillWrapInUnexpectedValueException()
3828
{
3929
$promise = Promise\reject(null);
4030

31+
$this->setExpectedException('UnexpectedValueException', 'Promise rejected with unexpected value of type NULL');
4132
Block\await($promise, $this->loop);
4233
}
4334

@@ -73,13 +64,11 @@ public function testAwaitOneInterrupted()
7364
$this->assertEquals(2, Block\await($promise, $this->loop));
7465
}
7566

76-
/**
77-
* @expectedException React\Promise\Timer\TimeoutException
78-
*/
7967
public function testAwaitOncePendingWillThrowOnTimeout()
8068
{
8169
$promise = new Promise\Promise(function () { });
8270

71+
$this->setExpectedException('React\Promise\Timer\TimeoutException');
8372
Block\await($promise, $this->loop, 0.001);
8473
}
8574

tests/FunctionSleepTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public function testSleep()
1212
Block\sleep(0.2, $this->loop);
1313
$time = microtime(true) - $time;
1414

15-
$this->assertEquals(0.2, $time, '', 0.1);
15+
$this->assertEqualsDelta(0.2, $time, 0.1);
1616
}
1717

1818
public function testSleepSmallTimerWillBeCappedReasonably()
@@ -21,6 +21,6 @@ public function testSleepSmallTimerWillBeCappedReasonably()
2121
Block\sleep(0.0000001, $this->loop);
2222
$time = microtime(true) - $time;
2323

24-
$this->assertEquals(0.1, $time, '', 0.1);
24+
$this->assertEqualsDelta(0.1, $time, 0.1);
2525
}
2626
}

tests/TestCase.php

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ class TestCase extends BaseTestCase
99
{
1010
protected $loop;
1111

12-
public function setUp()
12+
/**
13+
* @before
14+
*/
15+
public function setUpLoop()
1316
{
1417
$this->loop = \React\EventLoop\Factory::create();
1518
}
@@ -43,4 +46,32 @@ protected function createTimerInterrupt($delay = 0.01)
4346
$loop->stop();
4447
});
4548
}
49+
50+
public function setExpectedException($exception, $exceptionMessage = '', $exceptionCode = null)
51+
{
52+
if (method_exists($this, 'expectException')) {
53+
// PHPUnit 5+
54+
$this->expectException($exception);
55+
if ($exceptionMessage !== '') {
56+
$this->expectExceptionMessage($exceptionMessage);
57+
}
58+
if ($exceptionCode !== null) {
59+
$this->expectExceptionCode($exceptionCode);
60+
}
61+
} else {
62+
// legacy PHPUnit 4
63+
parent::setExpectedException($exception, $exceptionMessage, $exceptionCode);
64+
}
65+
}
66+
67+
public function assertEqualsDelta($expected, $actual, $delta)
68+
{
69+
if (method_exists($this, 'assertEqualsWithDelta')) {
70+
// PHPUnit 7.5+
71+
$this->assertEqualsWithDelta($expected, $actual, $delta);
72+
} else {
73+
// legacy PHPUnit 4 - PHPUnit 7.5
74+
$this->assertEquals($expected, $actual, '', $delta);
75+
}
76+
}
4677
}

0 commit comments

Comments
 (0)