-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTUnit.php
211 lines (185 loc) · 5.15 KB
/
TUnit.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<?php
namespace Dazzle\Redis\Test;
use Dazzle\Loop\LoopInterface;
use ReflectionClass;
class TUnit extends \PHPUnit_Framework_TestCase
{
/**
* Return project root.
*
* @return string
*/
public function basePath()
{
return realpath(__DIR__ . '/..');
}
/**
* @return TUnit
*/
public function getTest()
{
return $this;
}
/**
* @return \PHPUnit_Framework_MockObject_Matcher_InvokedCount
*/
public function twice()
{
return $this->exactly(2);
}
/**
* Creates a callback that must be called $amount times or the test will fail.
*
* @param $amount
* @return callable|\PHPUnit_Framework_MockObject_MockObject
*/
public function expectCallableExactly($amount)
{
$mock = $this->createCallableMock();
$mock
->expects($this->exactly($amount))
->method('__invoke');
return $mock;
}
/**
* Creates a callback that must be called once.
*
* @return callable|\PHPUnit_Framework_MockObject_MockObject
*/
public function expectCallableOnce()
{
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke');
return $mock;
}
/**
* Creates a callback that must be called twice.
*
* @return callable|\PHPUnit_Framework_MockObject_MockObject
*/
public function expectCallableTwice()
{
$mock = $this->createCallableMock();
$mock
->expects($this->exactly(2))
->method('__invoke');
return $mock;
}
/**
* Creates a callable that must not be called once.
*
* @return callable|\PHPUnit_Framework_MockObject_MockObject
*/
public function expectCallableNever()
{
$mock = $this->createCallableMock();
$mock
->expects($this->never())
->method('__invoke');
return $mock;
}
/**
* Creates a callable mock.
*
* @return callable|\PHPUnit_Framework_MockObject_MockObject
*/
public function createCallableMock()
{
return $this->getMock(Callback::class);
}
/**
* @return LoopInterface|\PHPUnit_Framework_MockObject_MockObject
*/
public function createLoopMock()
{
return $this->getMock('Dazzle\Loop\LoopInterface');
}
/**
* @return LoopInterface|\PHPUnit_Framework_MockObject_MockObject
*/
public function createWritableLoopMock()
{
$loop = $this->createLoopMock();
$loop
->expects($this->once())
->method('addWriteStream')
->will($this->returnCallback(function($stream, $listener) {
call_user_func($listener, $stream);
}));
return $loop;
}
/**
* @return LoopInterface|\PHPUnit_Framework_MockObject_MockObject
*/
public function createReadableLoopMock()
{
$loop = $this->createLoopMock();
$loop
->expects($this->once())
->method('addReadStream')
->will($this->returnCallback(function($stream, $listener) {
call_user_func($listener, $stream);
}));
return $loop;
}
/**
* Check if protected property exists.
*
* @param object $object
* @param string $property
* @return bool
*/
public function existsProtectedProperty($object, $property)
{
$reflection = new ReflectionClass($object);
return $reflection->hasProperty($property);
}
/**
* Get protected property from given object via reflection.
*
* @param object $object
* @param string $property
* @return mixed
*/
public function getProtectedProperty($object, $property)
{
$reflection = new ReflectionClass($object);
$reflection_property = $reflection->getProperty($property);
$reflection_property->setAccessible(true);
return $reflection_property->getValue($object);
}
/**
* Set protected property on a given object via reflection.
*
* @param object $object
* @param string $property
* @param mixed $value
* @return object
*/
public function setProtectedProperty($object, $property, $value)
{
$reflection = new ReflectionClass($object);
$reflection_property = $reflection->getProperty($property);
$reflection_property->setAccessible(true);
$reflection_property->setValue($object, $value);
return $object;
}
/**
* Call protected method on a given object via reflection.
*
* @param object|string $objectOrClass
* @param string $method
* @param mixed[] $args
* @return mixed
*/
public function callProtectedMethod($objectOrClass, $method, $args = [])
{
$reflection = new ReflectionClass($objectOrClass);
$reflectionMethod = $reflection->getMethod($method);
$reflectionMethod->setAccessible(true);
$reflectionTarget = is_object($objectOrClass) ? $objectOrClass : null;
return $reflectionMethod->invokeArgs($reflectionTarget, $args);
}
}