|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Fwolf\Wrapper\PHPUnit; |
|
4
|
|
|
|
|
5
|
|
|
use Fwolf\Wrapper\PHPUnit\Helper\BuildEasyMockTrait; |
|
6
|
|
|
use PHPUnit\Framework\TestCase as ParentTestCase; |
|
7
|
|
|
use ReflectionMethod; |
|
8
|
|
|
use ReflectionProperty; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Wrapper for PHPUnit\Framework\TestCase |
|
12
|
|
|
* |
|
13
|
|
|
* Added some helper methods, keep tiny for easy distribute, and should only |
|
14
|
|
|
* use for test propose. |
|
15
|
|
|
* |
|
16
|
|
|
* @copyright Copyright 2013-2019 Fwolf |
|
17
|
|
|
* @license http://opensource.org/licenses/MIT MIT |
|
18
|
|
|
*/ |
|
19
|
|
|
abstract class TestCase extends ParentTestCase |
|
20
|
|
|
{ |
|
21
|
|
|
use BuildEasyMockTrait; |
|
22
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Asserts that two array are equal and same element sequence |
|
26
|
|
|
* |
|
27
|
|
|
* @param mixed $expected |
|
28
|
|
|
* @param mixed $actual |
|
29
|
|
|
*/ |
|
30
|
|
|
public function assertEqualArray($expected, $actual) |
|
31
|
|
|
{ |
|
32
|
|
|
self::assertEquals( |
|
33
|
|
|
var_export($expected, true), |
|
34
|
|
|
var_export($actual, true) |
|
35
|
|
|
); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Call private or protected method for test using reflection |
|
41
|
|
|
* |
|
42
|
|
|
* @param mixed $classOrInstance |
|
43
|
|
|
* @param string $name |
|
44
|
|
|
* @param array $argument |
|
45
|
|
|
* @return mixed |
|
46
|
|
|
*/ |
|
47
|
|
|
protected function reflectionCall( |
|
48
|
|
|
$classOrInstance, |
|
49
|
|
|
$name, |
|
50
|
|
|
array $argument = [] |
|
51
|
|
|
) { |
|
52
|
|
|
$ref = new ReflectionMethod($classOrInstance, $name); |
|
53
|
|
|
|
|
54
|
|
|
$ref->setAccessible(true); |
|
55
|
|
|
|
|
56
|
|
|
return $ref->invokeArgs($classOrInstance, $argument); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Get private or protected property for test using reflection |
|
62
|
|
|
* |
|
63
|
|
|
* @param mixed $classOrInstance |
|
64
|
|
|
* @param string $name |
|
65
|
|
|
* @return mixed |
|
66
|
|
|
*/ |
|
67
|
|
|
protected function reflectionGet($classOrInstance, $name) |
|
68
|
|
|
{ |
|
69
|
|
|
$ref = new ReflectionProperty($classOrInstance, $name); |
|
70
|
|
|
|
|
71
|
|
|
$ref->setAccessible(true); |
|
72
|
|
|
|
|
73
|
|
|
return $ref->getValue($classOrInstance); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Set private or protected property for test using reflection |
|
79
|
|
|
* |
|
80
|
|
|
* @param mixed $classOrInstance |
|
81
|
|
|
* @param string $name |
|
82
|
|
|
* @param mixed $value |
|
83
|
|
|
*/ |
|
84
|
|
|
protected function reflectionSet($classOrInstance, $name, $value) |
|
85
|
|
|
{ |
|
86
|
|
|
$ref = new ReflectionProperty($classOrInstance, $name); |
|
87
|
|
|
|
|
88
|
|
|
$ref->setAccessible(true); |
|
89
|
|
|
|
|
90
|
|
|
if ($ref->isStatic()) { |
|
91
|
|
|
$ref->setValue($value); |
|
92
|
|
|
} else { |
|
93
|
|
|
$ref->setValue($classOrInstance, $value); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|