Skip to content

Commit

Permalink
Update test suite and remove legacy PHPUnit workarounds
Browse files Browse the repository at this point in the history
  • Loading branch information
WyriHaximus committed Feb 24, 2024
1 parent d23f22a commit 65f7f6a
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 126 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"evenement/evenement": "^3.0 || ^2.0 || ^1.0"
},
"require-dev": {
"phpunit/phpunit": "^9.6 || ^5.7",
"phpunit/phpunit": "^9.6 || ^7.5",
"clue/stream-filter": "~1.2"
},
"autoload": {
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml.legacy
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<!-- PHPUnit configuration file with old format for legacy PHPUnit -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/5.7/phpunit.xsd"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/7.5/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true">
<testsuites>
Expand Down
72 changes: 34 additions & 38 deletions tests/DuplexResourceStreamIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,51 +6,47 @@
use React\Stream\DuplexResourceStream;
use React\Stream\ReadableResourceStream;
use React\EventLoop\ExtEventLoop;
use React\EventLoop\ExtLibeventLoop;
use React\EventLoop\ExtLibevLoop;
use React\EventLoop\ExtEvLoop;
use React\EventLoop\ExtUvLoop;
use React\EventLoop\LoopInterface;
use React\EventLoop\LibEventLoop;
use React\EventLoop\LibEvLoop;
use React\EventLoop\StreamSelectLoop;

class DuplexResourceStreamIntegrationTest extends TestCase
{
public function loopProvider()
{
return array(
array(
function() {
return true;
},
function () {
return new StreamSelectLoop();
}
),
array(
function () {
return function_exists('event_base_new');
},
function () {
return class_exists('React\EventLoop\ExtLibeventLoop') ? new ExtLibeventLoop() : new LibEventLoop();
}
),
array(
function () {
return class_exists('libev\EventLoop');
},
function () {
return class_exists('React\EventLoop\ExtLibevLoop') ? new ExtLibevLoop() : new LibEvLoop();
}
),
array(
function () {
return class_exists('EventBase') && class_exists('React\EventLoop\ExtEventLoop');
},
function () {
return new ExtEventLoop();
}
)
);
yield [
function() {
return true;
},
function () {
return new StreamSelectLoop();
}
];
yield [
function () {
return class_exists('EvLoop');
},
function () {
return new ExtEvLoop();
}
];
yield [
function () {
return \function_exists('uv_loop_new');
},
function () {
return new ExtUvLoop();
}
];
yield [
function () {
return class_exists('EventBase') && class_exists('React\EventLoop\ExtEventLoop');
},
function () {
return new ExtEventLoop();
}
];
}

/**
Expand Down
14 changes: 5 additions & 9 deletions tests/DuplexResourceStreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function testConstructorThrowsExceptionOnInvalidStream()
{
$loop = $this->createLoopMock();

$this->setExpectedException('InvalidArgumentException');
$this->expectException('InvalidArgumentException');
new DuplexResourceStream('breakme', $loop);
}

Expand All @@ -65,13 +65,9 @@ public function testConstructorThrowsExceptionOnInvalidStream()
*/
public function testConstructorThrowsExceptionOnWriteOnlyStream()
{
if (defined('HHVM_VERSION')) {
$this->markTestSkipped('HHVM does not report fopen mode for STDOUT');
}

$loop = $this->createLoopMock();

$this->setExpectedException('InvalidArgumentException');
$this->expectException('InvalidArgumentException');
new DuplexResourceStream(STDOUT, $loop);
}

Expand All @@ -86,7 +82,7 @@ public function testConstructorThrowsExceptionOnWriteOnlyStreamWithExcessiveMode
unlink($name);

$loop = $this->createLoopMock();
$this->setExpectedException('InvalidArgumentException');
$this->expectException('InvalidArgumentException');
new DuplexResourceStream($stream, $loop);
}

Expand All @@ -102,7 +98,7 @@ public function testConstructorThrowsExceptionIfStreamDoesNotSupportNonBlocking(
$stream = fopen('blocking://test', 'r+');
$loop = $this->createLoopMock();

$this->setExpectedException('RunTimeException');
$this->expectException('RunTimeException');
new DuplexResourceStream($stream, $loop);
}

Expand Down Expand Up @@ -134,7 +130,7 @@ public function testConstructorThrowsExceptionIfStreamDoesNotSupportNonBlockingW

$buffer = $this->getMockBuilder('React\Stream\WritableStreamInterface')->getMock();

$this->setExpectedException('RunTimeException');
$this->expectException('RunTimeException');
new DuplexResourceStream($stream, $loop, null, $buffer);
}

Expand Down
9 changes: 4 additions & 5 deletions tests/FunctionalInternetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ public function testUploadBiggerBlockSecure()

$stream = stream_socket_client('ssl://httpbin.org:443');

// PHP < 7.1.4 (and PHP < 7.0.18) suffers from a bug when writing big
// chunks of data over TLS streams at once.
// PHP < 7.1.4 suffers from a bug when writing big chunks of data over
// TLS streams at once.
// We work around this by limiting the write chunk size to 8192 bytes
// here to also support older PHP versions.
// See https://github.com/reactphp/socket/issues/105
Expand Down Expand Up @@ -120,10 +120,9 @@ private function awaitStreamClose(DuplexResourceStream $stream, LoopInterface $l
$loop->stop();
});

$that = $this;
$loop->addTimer($timeout, function () use ($loop, $that) {
$loop->addTimer($timeout, function () use ($loop) {
$loop->stop();
$that->fail('Timed out while waiting for stream to close');
$this->fail('Timed out while waiting for stream to close');
});

$loop->run();
Expand Down
12 changes: 4 additions & 8 deletions tests/ReadableResourceStreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function testConstructorThrowsExceptionOnInvalidStream()
{
$loop = $this->createLoopMock();

$this->setExpectedException('InvalidArgumentException');
$this->expectException('InvalidArgumentException');
new ReadableResourceStream(false, $loop);
}

Expand All @@ -64,13 +64,9 @@ public function testConstructorThrowsExceptionOnInvalidStream()
*/
public function testConstructorThrowsExceptionOnWriteOnlyStream()
{
if (defined('HHVM_VERSION')) {
$this->markTestSkipped('HHVM does not report fopen mode for STDOUT');
}

$loop = $this->createLoopMock();

$this->setExpectedException('InvalidArgumentException');
$this->expectException('InvalidArgumentException');
new ReadableResourceStream(STDOUT, $loop);
}

Expand All @@ -85,7 +81,7 @@ public function testConstructorThrowsExceptionOnWriteOnlyStreamWithExcessiveMode
unlink($name);

$loop = $this->createLoopMock();
$this->setExpectedException('InvalidArgumentException');
$this->expectException('InvalidArgumentException');
new ReadableResourceStream($stream, $loop);
}

Expand All @@ -101,7 +97,7 @@ public function testConstructorThrowsExceptionIfStreamDoesNotSupportNonBlocking(
$stream = fopen('blocking://test', 'r+');
$loop = $this->createLoopMock();

$this->setExpectedException('RuntimeException');
$this->expectException('RuntimeException');
new ReadableResourceStream($stream, $loop);
}

Expand Down
59 changes: 5 additions & 54 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,62 +39,13 @@ protected function expectCallableNever()

protected function createCallableMock()
{
if (method_exists('PHPUnit\Framework\MockObject\MockBuilder', 'addMethods')) {
$builder = $this->getMockBuilder(\stdClass::class);
if (method_exists($builder, 'addMethods')) {
// PHPUnit 9+
return $this->getMockBuilder('stdClass')->addMethods(array('__invoke'))->getMock();
return $builder->addMethods(['__invoke'])->getMock();
} else {
// legacy PHPUnit 4 - PHPUnit 9
return $this->getMockBuilder('stdClass')->setMethods(array('__invoke'))->getMock();
}
}

public function setExpectedException($exception, $exceptionMessage = '', $exceptionCode = null)
{
if (method_exists($this, 'expectException')) {
// PHPUnit 5.2+
$this->expectException($exception);
if ($exceptionMessage !== '') {
$this->expectExceptionMessage($exceptionMessage);
}
if ($exceptionCode !== null) {
$this->expectExceptionCode($exceptionCode);
}
} else {
// legacy PHPUnit 4 - PHPUnit 5.1
parent::setExpectedException($exception, $exceptionMessage, $exceptionCode);
}
}

public function assertContainsString($needle, $haystack)
{
if (method_exists($this, 'assertStringContainsString')) {
// PHPUnit 7.5+
$this->assertStringContainsString($needle, $haystack);
} else {
// legacy PHPUnit 4 - PHPUnit 7.5
$this->assertContains($needle, $haystack);
}
}

public function assertContainsStringIgnoringCase($needle, $haystack)
{
if (method_exists($this, 'assertStringContainsStringIgnoringCase')) {
// PHPUnit 7.5+
$this->assertStringContainsStringIgnoringCase($needle, $haystack);
} else {
// legacy PHPUnit 4 - PHPUnit 7.5
$this->assertContains($needle, $haystack, '', true);
}
}

public function assertSameIgnoringCase($expected, $actual)
{
if (method_exists($this, 'assertEqualsIgnoringCase')) {
// PHPUnit 7.5+
$this->assertEqualsIgnoringCase($expected, $actual);
} else {
// legacy PHPUnit 4 - PHPUnit 7.5
$this->assertSame($expected, $actual);
// legacy PHPUnit
return $builder->setMethods(['__invoke'])->getMock();
}
}
}
2 changes: 1 addition & 1 deletion tests/ThroughStreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class ThroughStreamTest extends TestCase
*/
public function itShouldRejectInvalidCallback()
{
$this->setExpectedException('InvalidArgumentException');
$this->expectException('InvalidArgumentException');
new ThroughStream(123);
}

Expand Down
14 changes: 5 additions & 9 deletions tests/WritableResourceStreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function testConstructorThrowsIfNotAValidStreamResource()
$stream = null;
$loop = $this->createLoopMock();

$this->setExpectedException('InvalidArgumentException');
$this->expectException('InvalidArgumentException');
new WritableResourceStream($stream, $loop);
}

Expand All @@ -68,7 +68,7 @@ public function testConstructorThrowsExceptionOnReadOnlyStream()
$stream = fopen('php://temp', 'r');
$loop = $this->createLoopMock();

$this->setExpectedException('InvalidArgumentException');
$this->expectException('InvalidArgumentException');
new WritableResourceStream($stream, $loop);
}

Expand All @@ -83,7 +83,7 @@ public function testConstructorThrowsExceptionOnReadOnlyStreamWithExcessiveMode(
unlink($name);

$loop = $this->createLoopMock();
$this->setExpectedException('InvalidArgumentException');
$this->expectException('InvalidArgumentException');
new WritableResourceStream($stream, $loop);
}

Expand All @@ -99,7 +99,7 @@ public function testConstructorThrowsExceptionIfStreamDoesNotSupportNonBlocking(
$stream = fopen('blocking://test', 'r+');
$loop = $this->createLoopMock();

$this->setExpectedException('RuntimeException');
$this->expectException('RuntimeException');
new WritableResourceStream($stream, $loop);
}

Expand Down Expand Up @@ -347,10 +347,6 @@ public function testEndWithoutDataDoesNotCloseIfWritableResourceStreamIsFull()
*/
public function testEndWithDataClosesImmediatelyIfWritableResourceStreamFlushes()
{
if (defined('HHVM_VERSION')) {
$this->markTestSkipped('Not supported on HHVM');
}

$stream = fopen('php://temp', 'r+');
$filterBuffer = '';
$loop = $this->createLoopMock();
Expand Down Expand Up @@ -507,7 +503,7 @@ public function testWritingToClosedStream()
$buffer->handleWrite();

$this->assertInstanceOf('Exception', $error);
$this->assertSameIgnoringCase('Unable to write to stream: fwrite(): send of 3 bytes failed with errno=32 Broken pipe', $error->getMessage());
$this->assertEqualsIgnoringCase('Unable to write to stream: fwrite(): send of 3 bytes failed with errno=32 Broken pipe', $error->getMessage());
}

private function createWriteableLoopMock()
Expand Down

0 comments on commit 65f7f6a

Please sign in to comment.