|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace MyBuilder\PhpunitAccelerator; |
|
4
|
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestListener as BaseTestListener; |
|
6
|
|
|
use PHPUnit\Framework\TestListenerDefaultImplementation; |
|
7
|
|
|
|
|
8
|
|
|
class TestListener implements BaseTestListener |
|
9
|
|
|
{ |
|
10
|
|
|
use TestListenerDefaultImplementation; |
|
11
|
|
|
|
|
12
|
|
|
private $ignorePolicy; |
|
13
|
|
|
|
|
14
|
|
|
const PHPUNIT_PROPERTY_PREFIX = 'PHPUnit_'; |
|
15
|
|
|
|
|
16
|
|
|
public function __construct(IgnoreTestPolicy $ignorePolicy = null) |
|
17
|
|
|
{ |
|
18
|
|
|
$this->ignorePolicy = ($ignorePolicy) ?: new NeverIgnoreTestPolicy(); |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
public function endTest(\PHPUnit\Framework\Test $test, float $time): void |
|
22
|
|
|
{ |
|
23
|
|
|
$testReflection = new \ReflectionObject($test); |
|
24
|
|
|
|
|
25
|
|
|
if ($this->ignorePolicy->shouldIgnore($testReflection)) { |
|
26
|
|
|
return; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
$this->safelyFreeProperties($test, $testReflection->getProperties()); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
private function safelyFreeProperties(\PHPUnit\Framework\Test $test, array $properties) |
|
33
|
|
|
{ |
|
34
|
|
|
foreach ($properties as $property) { |
|
35
|
|
|
if ($this->isSafeToFreeProperty($property)) { |
|
36
|
|
|
$this->freeProperty($test, $property); |
|
37
|
|
|
} |
|
38
|
|
|
} |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
private function isSafeToFreeProperty(\ReflectionProperty $property) |
|
42
|
|
|
{ |
|
43
|
|
|
return !$property->isStatic() && $this->isNotPhpUnitProperty($property); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
private function isNotPhpUnitProperty(\ReflectionProperty $property) |
|
47
|
|
|
{ |
|
48
|
|
|
return 0 !== strpos($property->getDeclaringClass()->getName(), self::PHPUNIT_PROPERTY_PREFIX); |
|
|
|
|
|
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
private function freeProperty(\PHPUnit\Framework\Test $test, \ReflectionProperty $property) |
|
52
|
|
|
{ |
|
53
|
|
|
$property->setAccessible(true); |
|
54
|
|
|
$property->setValue($test, null); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
class NeverIgnoreTestPolicy implements IgnoreTestPolicy |
|
59
|
|
|
{ |
|
60
|
|
|
public function shouldIgnore(\ReflectionObject $testReflection) |
|
61
|
|
|
{ |
|
62
|
|
|
return false; |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|