|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Kambo\Testing\ClassOpener\PHPUnit; |
|
4
|
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
|
6
|
|
|
|
|
7
|
|
|
use Kambo\Testing\ClassOpener\ClassOpener; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* A TestCase which allows to mock final classes with annotation "@disableFinal" |
|
11
|
|
|
* eg.: "@disableFinal \Foo\Bar" will allows to mock class \Foo\Bar |
|
12
|
|
|
* |
|
13
|
|
|
* @author Bohuslav Simek <[email protected]> |
|
14
|
|
|
* @license MIT |
|
15
|
|
|
*/ |
|
16
|
|
|
class ClassOpenerTestCase extends TestCase |
|
17
|
|
|
{ |
|
18
|
|
|
|
|
19
|
|
|
const ANNOTATION_NAME = 'disableFinal'; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Open all class which are defined in the @disableFinal annotation |
|
23
|
|
|
* |
|
24
|
|
|
* @return void |
|
25
|
|
|
*/ |
|
26
|
3 |
|
protected function checkRequirements() |
|
27
|
|
|
{ |
|
28
|
3 |
|
parent::checkRequirements(); |
|
29
|
|
|
|
|
30
|
3 |
|
$classesNames = $this->extractDisableFinalNamesAnnotations($this->getAnnotations()); |
|
31
|
|
|
|
|
32
|
3 |
|
if (!empty($classesNames)) { |
|
33
|
2 |
|
$this->openClasses($classesNames); |
|
34
|
|
|
} |
|
35
|
3 |
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Open the provided final classes for the further modification |
|
39
|
|
|
* |
|
40
|
|
|
* @param array $classesNames Name of the classes which should be opened, a fully |
|
41
|
|
|
* qualified class names must be provided in the array |
|
42
|
|
|
* eg.: ['foo\bar\qaz', ...] |
|
43
|
|
|
* |
|
44
|
|
|
* @return void |
|
45
|
|
|
*/ |
|
46
|
2 |
|
protected function openClasses(array $classesNames) |
|
47
|
|
|
{ |
|
48
|
2 |
|
$ClassOpener = ClassOpener::create(); |
|
49
|
2 |
|
foreach ($classesNames as $name) { |
|
50
|
2 |
|
$ClassOpener->open($name); |
|
51
|
|
|
} |
|
52
|
2 |
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Extract specific annotations from the class |
|
56
|
|
|
* |
|
57
|
|
|
* @param array $annotations Annotations for this test. |
|
58
|
|
|
* |
|
59
|
|
|
* @return array Extract annotation, empty array, if there are not any annotations |
|
60
|
|
|
*/ |
|
61
|
3 |
|
private function extractDisableFinalNamesAnnotations(array $annotations) : array |
|
62
|
|
|
{ |
|
63
|
3 |
|
if (isset($annotations['method'][self::ANNOTATION_NAME])) { |
|
64
|
2 |
|
return $annotations['method'][self::ANNOTATION_NAME]; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
1 |
|
return []; |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|