Skip to content

Commit

Permalink
PHPUnit 6 compatibility (#11)
Browse files Browse the repository at this point in the history
* ⬆️ Update phpunit/phpunit dependency to support `^6.0`

Addresses #9

* ⬆️ Update travis.yml to remove PHP 5.x and HHVM

* ⬆️ TestListener compatible with PHPUnit 6

Addresses #9

* 📝 Update README
  • Loading branch information
ascii-soup authored and sjparkinson committed Jul 27, 2017
1 parent 5a80fbe commit 3356cd0
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 60 deletions.
5 changes: 1 addition & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@ language: php
sudo: false

php:
- 5.5
- 5.6
- 7.0
- hhvm

matrix:
include:
- php: 5.5
- php: 7.0
env: 'COMPOSER_FLAGS="--prefer-stable --prefer-lowest"'

install:
Expand Down
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ A PHPUnit test listener for the hamcrest assertion library.

## Installation

NOTE:

For PHPUnit >= 6, please use version >= 3.0

For PHPUnit < 6, please use version < 3.0

```bash
~$ composer require --dev graze/hamcrest-test-listener
```
Expand All @@ -13,7 +19,9 @@ A PHPUnit test listener for the hamcrest assertion library.
In your **phpunit.xml** file, add the following:

```xml
<phpunit>
<phpunit
beStrictAboutTestsThatDoNotTestAnything="false"> <!-- PHPUnit will not consider Hamcrest assertions -->

<listeners>
<listener class="\Hamcrest\Adapter\PHPUnit\TestListener"></listener>
</listeners>
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
},

"require": {
"php": "^5.5 || ^7.0",
"phpunit/phpunit": "^3.3.3 || ^4.0 || ^5.0",
"php": "^7.0",
"phpunit/phpunit": "^6.0",
"hamcrest/hamcrest-php": "^2.0"
},

Expand Down
3 changes: 0 additions & 3 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@
</logging>

<listeners>

<listener class="\Mockery\Adapter\Phpunit\TestListener"></listener>

<!-- A basic integration test. -->
<listener class="\Hamcrest\Adapter\PHPUnit\TestListener"></listener>

Expand Down
53 changes: 7 additions & 46 deletions src/TestListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,65 +15,26 @@
namespace Hamcrest\Adapter\PHPUnit;

use Hamcrest\MatcherAssert;
use PHPUnit\Framework\BaseTestListener;
use PHPUnit\Framework\Test;
use PHPUnit\Framework\TestCase;

class TestListener implements \PHPUnit_Framework_TestListener
class TestListener extends BaseTestListener
{
/**
* @param PHPUnit_Framework_Test $test
*/
public function startTest(\PHPUnit_Framework_Test $test)
public function startTest(Test $test)
{
MatcherAssert::resetCount();
}

/**
* @param PHPUnit_Framework_Test $test
* @param float $time
*/
public function endTest(\PHPUnit_Framework_Test $test, $time)
public function endTest(Test $test, $time)
{
try {
if ($test instanceof \PHPUnit_Framework_TestCase) {
if ($test instanceof TestCase) {
$test->addToAssertionCount(MatcherAssert::getCount());
}
} catch (\Exception $e) {
$result = $test->getTestResultObject();
$result->addError($test, $e, $time);
}
}

public function startTestSuite(\PHPUnit_Framework_TestSuite $suite)
{
}

public function addError(\PHPUnit_Framework_Test $test, \Exception $e, $time)
{
}

/**
* @see https://github.com/sebastianbergmann/phpunit/commit/073c4de6d013353df368ccb25f9de37f13f61d2d
*/
// public function addWarning(\PHPUnit_Framework_Test $test, \PHPUnit_Framework_Warning $e, $time)
// {
// }

public function addFailure(\PHPUnit_Framework_Test $test, \PHPUnit_Framework_AssertionFailedError $e, $time)
{
}

public function addIncompleteTest(\PHPUnit_Framework_Test $test, \Exception $e, $time)
{
}

public function addSkippedTest(\PHPUnit_Framework_Test $test, \Exception $e, $time)
{
}

public function addRiskyTest(\PHPUnit_Framework_Test $test, \Exception $e, $time)
{
}

public function endTestSuite(\PHPUnit_Framework_TestSuite $suite)
{
}
}
23 changes: 19 additions & 4 deletions tests/unit/TestListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,24 @@
use Hamcrest\Adapter\PHPUnit\TestListener;
use Hamcrest\MatcherAssert;
use Hamcrest\Util;
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Test;

class TestListenerTest extends \PHPUnit_Framework_TestCase
use \Mockery as m;

class TestListenerTest extends TestCase
{
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;

public static function setUpBeforeClass()
{
// Require the Hamcrest global functions.
Util::registerGlobalFunctions();
}

/**
* @covers Hamcrest\Adapter\PHPUnit\TestListener::startTest()
*/
public function testShouldResetAssertionCountOnTestStart()
{
$listener = new TestListener();
Expand All @@ -33,16 +42,19 @@ public function testShouldResetAssertionCountOnTestStart()

$this->assertSame(1, MatcherAssert::getCount(), 'Hamcrest is not counting assertions correctly.');

$listener->startTest(Mockery::mock(PHPUnit_Framework_Test::class));
$listener->startTest(m::mock(Test::class));

$this->assertSame(0, MatcherAssert::getCount(), 'The listener did not reset the hamcrest assertion count.');
}

/**
* @covers Hamcrest\Adapter\PHPUnit\TestListener::endTest()
*/
public function testShouldCallAddToAssertionCountOnTestEnd()
{
$listener = new TestListener();

$test = Mockery::mock(PHPUnit_Framework_TestCase::class);
$test = m::mock(TestCase::class);
$test->shouldReceive('addToAssertionCount')->with(1)->once();

// Bump the hamcrest assertion count by 1.
Expand All @@ -51,11 +63,14 @@ public function testShouldCallAddToAssertionCountOnTestEnd()
$listener->endTest($test, 0.0);
}

/**
* @covers Hamcrest\Adapter\PHPUnit\TestListener::endTest()
*/
public function testShouldOnlyCallAddToAssertionCountOnTestCases()
{
$listener = new TestListener();

$test = Mockery::mock(PHPUnit_Framework_Test::class);
$test = m::mock(Test::class);
$test->shouldReceive('addToAssertionCount')->never();

// Bump the hamcrest assertion count by 1.
Expand Down

0 comments on commit 3356cd0

Please sign in to comment.