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

Commit 64717e5

Browse files
authored
Add PHPUnit 6 support
2 parents b378c19 + b4e3c49 commit 64717e5

File tree

15 files changed

+261
-172
lines changed

15 files changed

+261
-172
lines changed

.travis.yml

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,25 @@ language: php
22
sudo: false
33

44
php:
5-
- nightly
6-
- '7.1'
7-
- '7.0'
8-
- '5.6'
9-
- '5.5'
10-
- '5.4'
11-
- hhvm
12-
- hhvm-nightly
5+
- '7.0'
6+
- '7.1'
7+
- nightly
8+
- hhvm
139

1410
matrix:
1511
fast_finish: true
12+
include:
13+
- php: '7.0'
14+
env: COMPOSER_FLAGS="--prefer-lowest"
15+
- php: '7.1'
16+
env: COMPOSER_FLAGS="--prefer-lowest"
1617
allow_failures:
17-
- php: hhvm-nightly
18+
- php: nightly
19+
- php: hhvm
1820

1921
cache:
20-
directories:
21-
- $HOME/.composer/cache
22+
directories:
23+
- $HOME/.composer/cache
2224

2325
before_install:
2426
- composer self-update

composer.json

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,15 @@
1212
}
1313
],
1414
"require": {
15-
"php": ">=5.4.0",
16-
"phpunit/phpunit": "^4.0|^5.0"
17-
},
18-
"require-dev": {
19-
"mockery/mockery": "^0.9"
15+
"php": "^7.0",
16+
"phpunit/phpunit": "^6.0"
2017
},
2118
"autoload": {
2219
"psr-0": { "Humbug\\": "src/" }
2320
},
2421
"extra": {
2522
"branch-alias": {
26-
"dev-master": "1.0-dev"
23+
"dev-master": "2.0-dev"
2724
}
2825
}
2926
}
Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?php
22
/**
3-
* Humbug
3+
* Humbug.
44
*
55
* @category Humbug
6-
* @package Humbug
6+
*
77
* @copyright Copyright (c) 2015 Pádraic Brady (http://blog.astrumfutura.com)
88
* @license https://github.com/padraic/humbug/blob/master/LICENSE New BSD License
99
*/
@@ -12,7 +12,5 @@
1212

1313
interface FilterInterface
1414
{
15-
1615
public function filter(array $array);
17-
18-
}
16+
}
Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
<?php
22
/**
3-
* Humbug
3+
* Humbug.
44
*
55
* @category Humbug
6-
* @package Humbug
6+
*
77
* @copyright Copyright (c) 2015 Pádraic Brady (http://blog.astrumfutura.com)
88
* @license https://github.com/padraic/humbug/blob/master/LICENSE New BSD License
99
*/
1010

1111
namespace Humbug\Phpunit\Filter\TestSuite;
1212

13+
use Exception;
14+
use PHPUnit\Framework\TestSuite as PHPUnitTestSuite;
15+
use RuntimeException;
16+
1317
class FastestFirstFilter extends AbstractFilter
1418
{
15-
1619
private $log;
1720

1821
public function __construct($log)
@@ -23,11 +26,11 @@ public function __construct($log)
2326
public function filter(array $array)
2427
{
2528
$times = $this->loadTimes();
26-
@usort($array, function (\PHPUnit_Framework_TestSuite $a, \PHPUnit_Framework_TestSuite $b) use ($times) {
29+
@usort($array, function (PHPUnitTestSuite $a, PHPUnitTestSuite $b) use ($times) {
2730
$na = $a->getName();
2831
$nb = $b->getName();
2932
if (!isset($times['suites'][$na]) || !isset($times['suites'][$nb])) {
30-
throw new \RuntimeException(
33+
throw new RuntimeException(
3134
'FastestFirstFilter has encountered an unlogged test suite which cannot be sorted'
3235
);
3336
}
@@ -37,21 +40,24 @@ public function filter(array $array)
3740
if ($times['suites'][$na] < $times['suites'][$nb]) {
3841
return -1;
3942
}
43+
4044
return 1;
4145
});
46+
4247
return $array;
4348
}
4449

4550
private function loadTimes()
4651
{
4752
if (!file_exists($this->log)) {
48-
throw new \Exception(sprintf(
53+
throw new Exception(sprintf(
4954
'Log file for collected times does not exist: %s. '
50-
. 'Use the Humbug\Phpunit\Listener\TimeCollectorListener listener prior '
51-
. 'to using the FastestFirstFilter filter at least once',
55+
.'Use the Humbug\Phpunit\Listener\TimeCollectorListener listener prior '
56+
.'to using the FastestFirstFilter filter at least once',
5257
$this->log
5358
));
5459
}
60+
5561
return json_decode(file_get_contents($this->log), true);
5662
}
5763
}

src/Humbug/Phpunit/Filter/TestSuite/IncludeOnlyFilter.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?php
22
/**
3-
* Humbug
3+
* Humbug.
44
*
55
* @category Humbug
6-
* @package Humbug
6+
*
77
* @copyright Copyright (c) 2015 Pádraic Brady (http://blog.astrumfutura.com)
88
* @license https://github.com/padraic/humbug/blob/master/LICENSE New BSD License
99
*/
@@ -12,7 +12,6 @@
1212

1313
class IncludeOnlyFilter extends AbstractFilter
1414
{
15-
1615
private $exclusivelyInclude;
1716

1817
public function __construct()
@@ -30,8 +29,7 @@ public function filter(array $array)
3029
$return[] = $suite;
3130
}
3231
}
32+
3333
return $return;
3434
}
35-
36-
3735
}

src/Humbug/Phpunit/Listener/FilterListener.php

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
<?php
22
/**
3-
* Humbug
3+
* Humbug.
44
*
55
* @category Humbug
6-
* @package Humbug
6+
*
77
* @copyright Copyright (c) 2015 Pádraic Brady (http://blog.astrumfutura.com)
88
* @license https://github.com/padraic/humbug/blob/master/LICENSE New BSD License
99
*/
1010

1111
namespace Humbug\Phpunit\Listener;
1212

13+
use Exception;
1314
use Humbug\Phpunit\Filter\FilterInterface;
14-
use Humbug\Phpunit\Filter\TestSuite\AbstractFilter as TestSuiteFilter;
15+
use PHPUnit\Framework\BaseTestListener;
16+
use PHPUnit\Framework\TestSuite;
1517

16-
class FilterListener extends \PHPUnit_Framework_BaseTestListener
18+
class FilterListener extends BaseTestListener
1719
{
18-
1920
protected $rootSuiteName;
2021

2122
protected $currentSuiteName;
@@ -35,16 +36,17 @@ public function __construct($rootSuiteNestingLevel)
3536
$args = func_get_args();
3637
array_shift($args);
3738
if (empty($args)) {
38-
throw new \Exception(
39-
'No Humbug\Filter\FilterInterface objects assigned to FilterListener'
40-
);
39+
throw new Exception(sprintf(
40+
'No %s objects assigned to FilterListener',
41+
FilterInterface::class
42+
));
4143
}
4244
foreach ($args as $filter) {
4345
$this->addFilter($filter);
4446
}
4547
}
4648

47-
public function startTestSuite(\PHPUnit_Framework_TestSuite $suite)
49+
public function startTestSuite(TestSuite $suite)
4850
{
4951
$this->suiteLevel++;
5052
$this->currentSuiteName = $suite->getName();
@@ -56,7 +58,7 @@ public function startTestSuite(\PHPUnit_Framework_TestSuite $suite)
5658
}
5759
}
5860

59-
public function endTestSuite(\PHPUnit_Framework_TestSuite $suite)
61+
public function endTestSuite(TestSuite $suite)
6062
{
6163
$this->suiteLevel--;
6264
}
@@ -72,14 +74,12 @@ protected function filterSuites(array $suites)
7274
foreach ($this->suiteFilters as $filter) {
7375
$filtered = $filter->filter($filtered);
7476
}
77+
7578
return $filtered;
7679
}
7780

7881
protected function addFilter(FilterInterface $filter)
7982
{
80-
if ($filter instanceof TestSuiteFilter) {
81-
$this->suiteFilters[] = $filter;
82-
}
83+
$this->suiteFilters[] = $filter;
8384
}
84-
85-
}
85+
}

src/Humbug/Phpunit/Listener/TimeCollectorListener.php

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
<?php
22
/**
3-
* Humbug
3+
* Humbug.
44
*
55
* @category Humbug
6-
* @package Humbug
6+
*
77
* @copyright Copyright (c) 2015 Pádraic Brady (http://blog.astrumfutura.com)
88
* @license https://github.com/padraic/humbug/blob/master/LICENSE New BSD License
99
*/
1010

1111
namespace Humbug\Phpunit\Listener;
1212

1313
use Humbug\Phpunit\Logger\JsonLogger;
14+
use PHPUnit\Framework\BaseTestListener;
15+
use PHPUnit\Framework\Test;
16+
use PHPUnit\Framework\TestSuite;
1417

15-
class TimeCollectorListener extends \PHPUnit_Framework_BaseTestListener
18+
class TimeCollectorListener extends BaseTestListener
1619
{
20+
private $logger;
1721

1822
private $rootSuiteNestingLevel = 0;
1923

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

34-
public function startTestSuite(\PHPUnit_Framework_TestSuite $suite)
38+
public function __destruct()
39+
{
40+
$this->rootSuiteNestingLevel = null;
41+
$this->logger = null;
42+
}
43+
44+
public function startTestSuite(TestSuite $suite)
3545
{
3646
$this->suiteLevel++;
3747
if (!isset($this->rootSuiteName)) {
@@ -40,7 +50,17 @@ public function startTestSuite(\PHPUnit_Framework_TestSuite $suite)
4050
$this->currentSuiteName = $suite->getName();
4151
}
4252

43-
public function endTest(\PHPUnit_Framework_Test $test, $time)
53+
/**
54+
* Logs the end of the test.
55+
*
56+
* This method hints Test for its first parameter but then uses getName(), which does not exist on that interface.
57+
* getName() exists on TestCase though, which inherits from Test. So here we assume that $test is always an instance
58+
* of TestCase rather than test.
59+
*
60+
* @param Test $test
61+
* @param float $time
62+
*/
63+
public function endTest(Test $test, $time)
4464
{
4565
$this->currentSuiteTime += $time;
4666
$this->logger->logTest(
@@ -50,15 +70,16 @@ public function endTest(\PHPUnit_Framework_Test $test, $time)
5070
);
5171
}
5272

53-
public function endTestSuite(\PHPUnit_Framework_TestSuite $suite)
73+
public function endTestSuite(TestSuite $suite)
5474
{
55-
/**
75+
/*
5676
* Only log Level 2 test suites, i.e. your actual test classes. Level 1
5777
* is the parent root suite(s) defined in the XML config and Level 3 are
5878
* those hosting data provider tests.
5979
*/
6080
if ($this->suiteLevel !== (2 + $this->rootSuiteNestingLevel)) {
6181
$this->suiteLevel--;
82+
6283
return;
6384
}
6485
$this->suiteLevel--;
@@ -68,4 +89,4 @@ public function endTestSuite(\PHPUnit_Framework_TestSuite $suite)
6889
);
6990
$this->currentSuiteTime = 0;
7091
}
71-
}
92+
}

0 commit comments

Comments
 (0)