Skip to content
This repository has been archived by the owner on Mar 15, 2020. It is now read-only.

Commit

Permalink
Add PHPUnit 6 support
Browse files Browse the repository at this point in the history
  • Loading branch information
theofidry authored Sep 10, 2017
2 parents b378c19 + b4e3c49 commit 64717e5
Show file tree
Hide file tree
Showing 15 changed files with 261 additions and 172 deletions.
24 changes: 13 additions & 11 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,25 @@ language: php
sudo: false

php:
- nightly
- '7.1'
- '7.0'
- '5.6'
- '5.5'
- '5.4'
- hhvm
- hhvm-nightly
- '7.0'
- '7.1'
- nightly
- hhvm

matrix:
fast_finish: true
include:
- php: '7.0'
env: COMPOSER_FLAGS="--prefer-lowest"
- php: '7.1'
env: COMPOSER_FLAGS="--prefer-lowest"
allow_failures:
- php: hhvm-nightly
- php: nightly
- php: hhvm

cache:
directories:
- $HOME/.composer/cache
directories:
- $HOME/.composer/cache

before_install:
- composer self-update
Expand Down
9 changes: 3 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,15 @@
}
],
"require": {
"php": ">=5.4.0",
"phpunit/phpunit": "^4.0|^5.0"
},
"require-dev": {
"mockery/mockery": "^0.9"
"php": "^7.0",
"phpunit/phpunit": "^6.0"
},
"autoload": {
"psr-0": { "Humbug\\": "src/" }
},
"extra": {
"branch-alias": {
"dev-master": "1.0-dev"
"dev-master": "2.0-dev"
}
}
}
8 changes: 3 additions & 5 deletions src/Humbug/Phpunit/Filter/FilterInterface.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php
/**
* Humbug
* Humbug.
*
* @category Humbug
* @package Humbug
*
* @copyright Copyright (c) 2015 Pádraic Brady (http://blog.astrumfutura.com)
* @license https://github.com/padraic/humbug/blob/master/LICENSE New BSD License
*/
Expand All @@ -12,7 +12,5 @@

interface FilterInterface
{

public function filter(array $array);

}
}
22 changes: 14 additions & 8 deletions src/Humbug/Phpunit/Filter/TestSuite/FastestFirstFilter.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
<?php
/**
* Humbug
* Humbug.
*
* @category Humbug
* @package Humbug
*
* @copyright Copyright (c) 2015 Pádraic Brady (http://blog.astrumfutura.com)
* @license https://github.com/padraic/humbug/blob/master/LICENSE New BSD License
*/

namespace Humbug\Phpunit\Filter\TestSuite;

use Exception;
use PHPUnit\Framework\TestSuite as PHPUnitTestSuite;
use RuntimeException;

class FastestFirstFilter extends AbstractFilter
{

private $log;

public function __construct($log)
Expand All @@ -23,11 +26,11 @@ public function __construct($log)
public function filter(array $array)
{
$times = $this->loadTimes();
@usort($array, function (\PHPUnit_Framework_TestSuite $a, \PHPUnit_Framework_TestSuite $b) use ($times) {
@usort($array, function (PHPUnitTestSuite $a, PHPUnitTestSuite $b) use ($times) {
$na = $a->getName();
$nb = $b->getName();
if (!isset($times['suites'][$na]) || !isset($times['suites'][$nb])) {
throw new \RuntimeException(
throw new RuntimeException(
'FastestFirstFilter has encountered an unlogged test suite which cannot be sorted'
);
}
Expand All @@ -37,21 +40,24 @@ public function filter(array $array)
if ($times['suites'][$na] < $times['suites'][$nb]) {
return -1;
}

return 1;
});

return $array;
}

private function loadTimes()
{
if (!file_exists($this->log)) {
throw new \Exception(sprintf(
throw new Exception(sprintf(
'Log file for collected times does not exist: %s. '
. 'Use the Humbug\Phpunit\Listener\TimeCollectorListener listener prior '
. 'to using the FastestFirstFilter filter at least once',
.'Use the Humbug\Phpunit\Listener\TimeCollectorListener listener prior '
.'to using the FastestFirstFilter filter at least once',
$this->log
));
}

return json_decode(file_get_contents($this->log), true);
}
}
8 changes: 3 additions & 5 deletions src/Humbug/Phpunit/Filter/TestSuite/IncludeOnlyFilter.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php
/**
* Humbug
* Humbug.
*
* @category Humbug
* @package Humbug
*
* @copyright Copyright (c) 2015 Pádraic Brady (http://blog.astrumfutura.com)
* @license https://github.com/padraic/humbug/blob/master/LICENSE New BSD License
*/
Expand All @@ -12,7 +12,6 @@

class IncludeOnlyFilter extends AbstractFilter
{

private $exclusivelyInclude;

public function __construct()
Expand All @@ -30,8 +29,7 @@ public function filter(array $array)
$return[] = $suite;
}
}

return $return;
}


}
30 changes: 15 additions & 15 deletions src/Humbug/Phpunit/Listener/FilterListener.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
<?php
/**
* Humbug
* Humbug.
*
* @category Humbug
* @package Humbug
*
* @copyright Copyright (c) 2015 Pádraic Brady (http://blog.astrumfutura.com)
* @license https://github.com/padraic/humbug/blob/master/LICENSE New BSD License
*/

namespace Humbug\Phpunit\Listener;

use Exception;
use Humbug\Phpunit\Filter\FilterInterface;
use Humbug\Phpunit\Filter\TestSuite\AbstractFilter as TestSuiteFilter;
use PHPUnit\Framework\BaseTestListener;
use PHPUnit\Framework\TestSuite;

class FilterListener extends \PHPUnit_Framework_BaseTestListener
class FilterListener extends BaseTestListener
{

protected $rootSuiteName;

protected $currentSuiteName;
Expand All @@ -35,16 +36,17 @@ public function __construct($rootSuiteNestingLevel)
$args = func_get_args();
array_shift($args);
if (empty($args)) {
throw new \Exception(
'No Humbug\Filter\FilterInterface objects assigned to FilterListener'
);
throw new Exception(sprintf(
'No %s objects assigned to FilterListener',
FilterInterface::class
));
}
foreach ($args as $filter) {
$this->addFilter($filter);
}
}

public function startTestSuite(\PHPUnit_Framework_TestSuite $suite)
public function startTestSuite(TestSuite $suite)
{
$this->suiteLevel++;
$this->currentSuiteName = $suite->getName();
Expand All @@ -56,7 +58,7 @@ public function startTestSuite(\PHPUnit_Framework_TestSuite $suite)
}
}

public function endTestSuite(\PHPUnit_Framework_TestSuite $suite)
public function endTestSuite(TestSuite $suite)
{
$this->suiteLevel--;
}
Expand All @@ -72,14 +74,12 @@ protected function filterSuites(array $suites)
foreach ($this->suiteFilters as $filter) {
$filtered = $filter->filter($filtered);
}

return $filtered;
}

protected function addFilter(FilterInterface $filter)
{
if ($filter instanceof TestSuiteFilter) {
$this->suiteFilters[] = $filter;
}
$this->suiteFilters[] = $filter;
}

}
}
37 changes: 29 additions & 8 deletions src/Humbug/Phpunit/Listener/TimeCollectorListener.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
<?php
/**
* Humbug
* Humbug.
*
* @category Humbug
* @package Humbug
*
* @copyright Copyright (c) 2015 Pádraic Brady (http://blog.astrumfutura.com)
* @license https://github.com/padraic/humbug/blob/master/LICENSE New BSD License
*/

namespace Humbug\Phpunit\Listener;

use Humbug\Phpunit\Logger\JsonLogger;
use PHPUnit\Framework\BaseTestListener;
use PHPUnit\Framework\Test;
use PHPUnit\Framework\TestSuite;

class TimeCollectorListener extends \PHPUnit_Framework_BaseTestListener
class TimeCollectorListener extends BaseTestListener
{
private $logger;

private $rootSuiteNestingLevel = 0;

Expand All @@ -31,7 +35,13 @@ public function __construct(JsonLogger $logger, $rootSuiteNestingLevel = 0)
$this->rootSuiteNestingLevel = $rootSuiteNestingLevel;
}

public function startTestSuite(\PHPUnit_Framework_TestSuite $suite)
public function __destruct()
{
$this->rootSuiteNestingLevel = null;
$this->logger = null;
}

public function startTestSuite(TestSuite $suite)
{
$this->suiteLevel++;
if (!isset($this->rootSuiteName)) {
Expand All @@ -40,7 +50,17 @@ public function startTestSuite(\PHPUnit_Framework_TestSuite $suite)
$this->currentSuiteName = $suite->getName();
}

public function endTest(\PHPUnit_Framework_Test $test, $time)
/**
* Logs the end of the test.
*
* This method hints Test for its first parameter but then uses getName(), which does not exist on that interface.
* getName() exists on TestCase though, which inherits from Test. So here we assume that $test is always an instance
* of TestCase rather than test.
*
* @param Test $test
* @param float $time
*/
public function endTest(Test $test, $time)
{
$this->currentSuiteTime += $time;
$this->logger->logTest(
Expand All @@ -50,15 +70,16 @@ public function endTest(\PHPUnit_Framework_Test $test, $time)
);
}

public function endTestSuite(\PHPUnit_Framework_TestSuite $suite)
public function endTestSuite(TestSuite $suite)
{
/**
/*
* Only log Level 2 test suites, i.e. your actual test classes. Level 1
* is the parent root suite(s) defined in the XML config and Level 3 are
* those hosting data provider tests.
*/
if ($this->suiteLevel !== (2 + $this->rootSuiteNestingLevel)) {
$this->suiteLevel--;

return;
}
$this->suiteLevel--;
Expand All @@ -68,4 +89,4 @@ public function endTestSuite(\PHPUnit_Framework_TestSuite $suite)
);
$this->currentSuiteTime = 0;
}
}
}
Loading

0 comments on commit 64717e5

Please sign in to comment.