Skip to content

Commit

Permalink
Improve test suite, add test namespaces, simplify test bootstrap… (#31)
Browse files Browse the repository at this point in the history
Improve test suite, add test namespaces, simplify test bootstrap logic and add forward compatibility with PHPUnit 7 and PHPUnit 6
  • Loading branch information
WyriHaximus authored Jul 19, 2019
2 parents 86586b6 + 6b97970 commit d3e9d75
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 22 deletions.
11 changes: 9 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@
}
],
"autoload": {
"psr-4": {"React\\Datagram\\": "src"}
"psr-4": {
"React\\Datagram\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"React\\Tests\\Datagram\\": "tests"
}
},
"require": {
"php": ">=5.3",
Expand All @@ -22,6 +29,6 @@
},
"require-dev": {
"clue/block-react": "~1.0",
"phpunit/phpunit": "^5.0 || ^4.8"
"phpunit/phpunit": "^7.0 || ^6.0 || ^5.0 || ^4.8.35"
}
}
11 changes: 10 additions & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit colors="true" bootstrap="./tests/bootstrap.php">
<phpunit backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
bootstrap="vendor/autoload.php"
>
<testsuites>
<testsuite name="Datagram Test Suite">
<directory>./tests/</directory>
Expand Down
18 changes: 14 additions & 4 deletions tests/FactoryTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

namespace React\Tests\Datagram;

use React\Datagram\Socket;
use React\Datagram\Factory;
use Clue\React\Block;
Expand All @@ -13,7 +15,7 @@ class FactoryTest extends TestCase

public function setUp()
{
$this->loop = React\EventLoop\Factory::create();
$this->loop = \React\EventLoop\Factory::create();
$this->resolver = $this->createResolverMock();
$this->factory = new Factory($this->loop, $this->resolver);
}
Expand Down Expand Up @@ -62,6 +64,8 @@ public function testCreateClientLocalhostWithDefaultResolver()
$promise = $this->factory->createClient('localhost:12345');

$capturedClient = Block\await($promise, $this->loop);
$this->assertInstanceOf('React\Datagram\SocketInterface', $capturedClient);

$capturedClient->close();
}

Expand Down Expand Up @@ -129,11 +133,13 @@ public function testCreateClientWithHostnameWillUseResolver()
$client->close();
}

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

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

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

/**
* @expectedException RuntimeException
*/
public function testCancelCreateClientWithCancellableHostnameResolver()
{
$promise = new Promise\Promise(function () { }, $this->expectCallableOnce());
Expand All @@ -163,10 +172,12 @@ public function testCancelCreateClientWithCancellableHostnameResolver()
$promise = $this->factory->createClient('example.com:0');
$promise->cancel();

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

/**
* @expectedException RuntimeException
*/
public function testCancelCreateClientWithUncancellableHostnameResolver()
{
$promise = $this->getMockBuilder('React\Promise\PromiseInterface')->getMock();
Expand All @@ -175,7 +186,6 @@ public function testCancelCreateClientWithUncancellableHostnameResolver()
$promise = $this->factory->createClient('example.com:0');
$promise->cancel();

$this->setExpectedException('RuntimeException');
Block\await($promise, $this->loop);
}
}
18 changes: 13 additions & 5 deletions tests/SocketTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

namespace React\Tests\Datagram;

use React\Datagram\Socket;
use Clue\React\Block;

Expand All @@ -10,10 +12,13 @@ class SocketTest extends TestCase

public function setUp()
{
$this->loop = React\EventLoop\Factory::create();
$this->factory = new React\Datagram\Factory($this->loop, $this->createResolverMock());
$this->loop = \React\EventLoop\Factory::create();
$this->factory = new \React\Datagram\Factory($this->loop, $this->createResolverMock());
}

/**
* @doesNotPerformAssertions
*/
public function testCreateClientCloseWillNotBlock()
{
$promise = $this->factory->createClient('127.0.0.1:12345');
Expand All @@ -28,7 +33,7 @@ public function testCreateClientCloseWillNotBlock()
}

/**
*
* @doesNotPerformAssertions
* @param Socket $client
* @depends testCreateClientCloseWillNotBlock
*/
Expand All @@ -38,6 +43,9 @@ public function testClientCloseAgainWillNotBlock(Socket $client)
$this->loop->run();
}

/**
* @doesNotPerformAssertions
*/
public function testCreateClientEndWillNotBlock()
{
$promise = $this->factory->createClient('127.0.0.1:12345');
Expand All @@ -52,7 +60,7 @@ public function testCreateClientEndWillNotBlock()
}

/**
*
* @doesNotPerformAssertions
* @param Socket $client
* @depends testCreateClientEndWillNotBlock
*/
Expand All @@ -65,7 +73,7 @@ public function testClientEndAgainWillNotBlock(Socket $client)
}

/**
*
* @doesNotPerformAssertions
* @param Socket $client
* @depends testClientEndAgainWillNotBlock
*/
Expand Down
15 changes: 5 additions & 10 deletions tests/bootstrap.php → tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<?php

require __DIR__ . '/../vendor/autoload.php';
namespace React\Tests\Datagram;

abstract class TestCase extends PHPUnit_Framework_TestCase
use PHPUnit\Framework\TestCase as BaseTestCase;

abstract class TestCase extends BaseTestCase
{
protected function expectCallableOnce()
{
Expand All @@ -26,18 +28,11 @@ protected function expectCallableNever()

protected function createCallableMock()
{
return $this->getMockBuilder('CallableStub')->getMock();
return $this->getMockBuilder('stdClass')->setMethods(array('__invoke'))->getMock();
}

protected function createResolverMock()
{
return $this->getMockBuilder('React\Dns\Resolver\ResolverInterface')->getMock();
}
}

class CallableStub
{
public function __invoke()
{
}
}

0 comments on commit d3e9d75

Please sign in to comment.