Skip to content

Commit 3165c06

Browse files
authored
Merge pull request #23 from SimonFrings/tests
Run tests on PHP 8 and PHPUnit 9 and update PHPUnit configuration schema for PHPUnit 9.3
2 parents dcaa8f9 + 25de1ea commit 3165c06

File tree

9 files changed

+77
-18
lines changed

9 files changed

+77
-18
lines changed

.gitattributes

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

.github/workflows/ci.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ jobs:
1010
strategy:
1111
matrix:
1212
php:
13+
- 8.0
1314
- 7.4
1415
- 7.3
1516
- 7.2
@@ -27,6 +28,9 @@ jobs:
2728
php-version: ${{ matrix.php }}
2829
- run: composer install
2930
- run: vendor/bin/phpunit --coverage-text
31+
if: ${{ matrix.php >= 7.3 }}
32+
- run: vendor/bin/phpunit --coverage-text -c phpunit.xml.legacy
33+
if: ${{ matrix.php < 7.3 }}
3034

3135
PHPUnit-hhvm:
3236
name: PHPUnit (HHVM)
@@ -36,6 +40,5 @@ jobs:
3640
- uses: azjezz/setup-hhvm@v1
3741
with:
3842
version: lts-3.30
39-
- run: hhvm $(which composer) require phpunit/phpunit:^5 --dev --no-interaction # requires legacy phpunit
4043
- run: hhvm $(which composer) install
4144
- run: hhvm vendor/bin/phpunit

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ $ composer require clue/ndjson-react:^1.1
303303
See also the [CHANGELOG](CHANGELOG.md) for details about version upgrades.
304304

305305
This project aims to run on any platform and thus does not require any PHP
306-
extensions and supports running on legacy PHP 5.3 through current PHP 7+ and
306+
extensions and supports running on legacy PHP 5.3 through current PHP 8+ and
307307
HHVM.
308308
It's *highly recommended to use PHP 7+* for this project.
309309

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@
2222
},
2323
"require-dev": {
2424
"react/event-loop": "^1.0 || ^0.5 || ^0.4 || ^0.3",
25-
"phpunit/phpunit": "^7.0 || ^6.0 || ^5.7 || ^4.8.35"
25+
"phpunit/phpunit": "^9.3 || ^5.7 || ^4.8.35"
2626
}
2727
}

phpunit.xml.dist

Lines changed: 11 additions & 6 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="NDJSON 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>
14-
</phpunit>
17+
</include>
18+
</coverage>
19+
</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="NDJSON 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/DecoderTest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ class DecoderTest extends TestCase
1010
private $input;
1111
private $decoder;
1212

13-
public function setUp()
13+
/**
14+
* @before
15+
*/
16+
public function setUpDecoder()
1417
{
1518
$this->input = new ThroughStream();
1619
$this->decoder = new Decoder($this->input);
@@ -76,7 +79,7 @@ public function testEmitDataErrorWillForwardError()
7679
$this->input->emit('data', array("invalid\n"));
7780

7881
$this->assertInstanceOf('RuntimeException', $error);
79-
$this->assertContains('Syntax error', $error->getMessage());
82+
$this->assertContainsString('Syntax error', $error->getMessage());
8083
$this->assertEquals(JSON_ERROR_SYNTAX, $error->getCode());
8184
}
8285

tests/EncoderTest.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ class EncoderTest extends TestCase
1010
private $output;
1111
private $encoder;
1212

13-
public function setUp()
13+
/**
14+
* @before
15+
*/
16+
public function setUpEncoder()
1417
{
1518
$stream = fopen('php://temp', 'r+');
1619
$loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
@@ -19,15 +22,13 @@ public function setUp()
1922
$this->encoder = new Encoder($this->output);
2023
}
2124

22-
/**
23-
* @expectedException InvalidArgumentException
24-
*/
2525
public function testPrettyPrintDoesNotMakeSenseForNDJson()
2626
{
2727
if (!defined('JSON_PRETTY_PRINT')) {
2828
$this->markTestSkipped('Const JSON_PRETTY_PRINT only available in PHP 5.4+');
2929
}
3030

31+
$this->setExpectedException('InvalidArgumentException');
3132
$this->encoder = new Encoder($this->output, JSON_PRETTY_PRINT);
3233
}
3334

@@ -77,11 +78,11 @@ public function testWriteInfiniteWillEmitErrorAndClose()
7778
$this->assertInstanceOf('RuntimeException', $error);
7879
if (PHP_VERSION_ID >= 50500) {
7980
// PHP 5.5+ reports error with proper code
80-
$this->assertContains('Inf and NaN cannot be JSON encoded', $error->getMessage());
81+
$this->assertContainsString('Inf and NaN cannot be JSON encoded', $error->getMessage());
8182
$this->assertEquals(JSON_ERROR_INF_OR_NAN, $error->getCode());
8283
} else {
8384
// PHP < 5.5 reports error message without code
84-
$this->assertContains('double INF does not conform to the JSON spec', $error->getMessage());
85+
$this->assertContainsString('double INF does not conform to the JSON spec', $error->getMessage());
8586
$this->assertEquals(0, $error->getCode());
8687
}
8788
}
@@ -109,7 +110,7 @@ public function testWriteInvalidUtf8WillEmitErrorAndClose()
109110
$this->assertInstanceOf('RuntimeException', $error);
110111
if (PHP_VERSION_ID >= 50500) {
111112
// PHP 5.5+ reports error with proper code
112-
$this->assertContains('Malformed UTF-8 characters, possibly incorrectly encoded', $error->getMessage());
113+
$this->assertContainsString('Malformed UTF-8 characters, possibly incorrectly encoded', $error->getMessage());
113114
$this->assertEquals(JSON_ERROR_UTF8, $error->getCode());
114115
} elseif (PHP_VERSION_ID >= 50303) {
115116
// PHP 5.3.3+ reports error with proper code (const JSON_ERROR_UTF8 added in PHP 5.3.3)

tests/TestCase.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,32 @@ protected function createCallableMock()
4141
{
4242
return $this->getMockBuilder('stdClass')->setMethods(array('__invoke'))->getMock();
4343
}
44+
45+
public function assertContainsString($needle, $haystack)
46+
{
47+
if (method_exists($this, 'assertStringContainsString')) {
48+
// PHPUnit 7.5+
49+
$this->assertStringContainsString($needle, $haystack);
50+
} else {
51+
// legacy PHPUnit 4 - PHPUnit 7.5
52+
$this->assertContains($needle, $haystack);
53+
}
54+
}
55+
56+
public function setExpectedException($exception, $exceptionMessage = '', $exceptionCode = null)
57+
{
58+
if (method_exists($this, 'expectException')) {
59+
// PHPUnit 5+
60+
$this->expectException($exception);
61+
if ($exceptionMessage !== '') {
62+
$this->expectExceptionMessage($exceptionMessage);
63+
}
64+
if ($exceptionCode !== null) {
65+
$this->expectExceptionCode($exceptionCode);
66+
}
67+
} else {
68+
// legacy PHPUnit 4
69+
parent::setExpectedException($exception, $exceptionMessage, $exceptionCode);
70+
}
71+
}
4472
}

0 commit comments

Comments
 (0)