|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Part of ci-phpunit-test |
|
4
|
|
|
* |
|
5
|
|
|
* @author Kenji Suzuki <https://github.com/kenjis> |
|
6
|
|
|
* @license MIT License |
|
7
|
|
|
* @copyright 2015 Kenji Suzuki |
|
8
|
|
|
* @link https://github.com/kenjis/ci-phpunit-test |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
class CIPHPUnitTestDouble |
|
|
|
|
|
|
12
|
|
|
{ |
|
13
|
|
|
protected $testCase; |
|
14
|
|
|
|
|
15
|
|
|
public function __construct(PHPUnit_Framework_TestCase $testCase) |
|
16
|
|
|
{ |
|
17
|
|
|
$this->testCase = $testCase; |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Get Mock Object |
|
22
|
|
|
* |
|
23
|
|
|
* $email = $this->getMockBuilder('CI_Email') |
|
24
|
|
|
* ->disableOriginalConstructor() |
|
25
|
|
|
* ->setMethods(['send']) |
|
26
|
|
|
* ->getMock(); |
|
27
|
|
|
* $email->method('send')->willReturn(TRUE); |
|
28
|
|
|
* |
|
29
|
|
|
* will be |
|
30
|
|
|
* |
|
31
|
|
|
* $email = $this->getDouble('CI_Email', ['send' => TRUE]); |
|
32
|
|
|
* |
|
33
|
|
|
* @param string $classname |
|
34
|
|
|
* @param array $params [method_name => return_value] |
|
35
|
|
|
* @param mixed $constructor_params false: disable construntor, array: construntor params |
|
36
|
|
|
* |
|
37
|
|
|
* @return object PHPUnit mock object |
|
38
|
|
|
*/ |
|
39
|
|
|
public function getDouble($classname, $params, $constructor_params = false) |
|
40
|
|
|
{ |
|
41
|
|
|
$methods = array_keys($params); |
|
42
|
|
|
|
|
43
|
|
|
// `disableOriginalConstructor()` is the default, because if we call |
|
44
|
|
|
// construnctor, it may call `$this->load->...` or other CodeIgniter |
|
45
|
|
|
// methods in it. But we can't use them in |
|
46
|
|
|
// `$this->request->setCallablePreConstructor()` |
|
|
|
|
|
|
47
|
|
|
$mock = $this->testCase->getMockBuilder($classname); |
|
48
|
|
|
if ($constructor_params === false) |
|
49
|
|
|
{ |
|
50
|
|
|
$mock->disableOriginalConstructor(); |
|
51
|
|
|
} |
|
52
|
|
|
elseif (is_array($constructor_params)) |
|
53
|
|
|
{ |
|
54
|
|
|
$mock->setConstructorArgs($constructor_params); |
|
55
|
|
|
} |
|
56
|
|
|
$mock = $mock->setMethods($methods)->getMock(); |
|
57
|
|
|
|
|
58
|
|
|
foreach ($params as $method => $return) |
|
59
|
|
|
{ |
|
60
|
|
|
if (is_object($return) && $return instanceof PHPUnit_Framework_MockObject_Stub) { |
|
|
|
|
|
|
61
|
|
|
$mock->expects($this->testCase->any())->method($method) |
|
62
|
|
|
->will($return); |
|
63
|
|
|
} elseif (is_object($return) && $return instanceof Closure) { |
|
64
|
|
|
$mock->expects($this->testCase->any())->method($method) |
|
65
|
|
|
->willReturnCallback($return); |
|
66
|
|
|
} else { |
|
67
|
|
|
$mock->expects($this->testCase->any())->method($method) |
|
68
|
|
|
->willReturn($return); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return $mock; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
protected function _verify($mock, $method, $params = null, $expects, $with) |
|
76
|
|
|
{ |
|
77
|
|
|
$invocation = $mock->expects($expects) |
|
78
|
|
|
->method($method); |
|
79
|
|
|
|
|
80
|
|
|
call_user_func_array([$invocation, $with], $params); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Verifies that method was called exactly $times times |
|
85
|
|
|
* |
|
86
|
|
|
* $loader->expects($this->exactly(2)) |
|
87
|
|
|
* ->method('view') |
|
88
|
|
|
* ->withConsecutive( |
|
89
|
|
|
* ['shop_confirm', $this->anything(), TRUE], |
|
90
|
|
|
* ['shop_tmpl_checkout', $this->anything()] |
|
91
|
|
|
* ); |
|
92
|
|
|
* |
|
93
|
|
|
* will be |
|
94
|
|
|
* |
|
95
|
|
|
* $this->verifyInvokedMultipleTimes( |
|
96
|
|
|
* $loader, |
|
97
|
|
|
* 'view', |
|
98
|
|
|
* 2, |
|
99
|
|
|
* [ |
|
100
|
|
|
* ['shop_confirm', $this->anything(), TRUE], |
|
101
|
|
|
* ['shop_tmpl_checkout', $this->anything()] |
|
102
|
|
|
* ] |
|
103
|
|
|
* ); |
|
104
|
|
|
* |
|
105
|
|
|
* @param object $mock PHPUnit mock object |
|
106
|
|
|
* @param string $method |
|
107
|
|
|
* @param int $times |
|
108
|
|
|
* @param array $params arguments |
|
109
|
|
|
*/ |
|
110
|
|
|
public function verifyInvokedMultipleTimes($mock, $method, $times, $params = null) |
|
111
|
|
|
{ |
|
112
|
|
|
$this->_verify( |
|
113
|
|
|
$mock, $method, $params, $this->testCase->exactly($times), 'withConsecutive' |
|
114
|
|
|
); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Verifies a method was invoked at least once |
|
119
|
|
|
* |
|
120
|
|
|
* @param object $mock PHPUnit mock object |
|
121
|
|
|
* @param string $method |
|
122
|
|
|
* @param array $params arguments |
|
123
|
|
|
*/ |
|
124
|
|
|
public function verifyInvoked($mock, $method, $params = null) |
|
125
|
|
|
{ |
|
126
|
|
|
$this->_verify( |
|
127
|
|
|
$mock, $method, $params, $this->testCase->atLeastOnce(), 'with' |
|
128
|
|
|
); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Verifies that method was invoked only once |
|
133
|
|
|
* |
|
134
|
|
|
* @param object $mock PHPUnit mock object |
|
135
|
|
|
* @param string $method |
|
136
|
|
|
* @param array $params arguments |
|
137
|
|
|
*/ |
|
138
|
|
|
public function verifyInvokedOnce($mock, $method, $params = null) |
|
139
|
|
|
{ |
|
140
|
|
|
$this->_verify( |
|
141
|
|
|
$mock, $method, $params, $this->testCase->once(), 'with' |
|
142
|
|
|
); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Verifies that method was not called |
|
147
|
|
|
* |
|
148
|
|
|
* @param object $mock PHPUnit mock object |
|
149
|
|
|
* @param string $method |
|
150
|
|
|
* @param array $params arguments |
|
151
|
|
|
*/ |
|
152
|
|
|
public function verifyNeverInvoked($mock, $method, $params = null) |
|
153
|
|
|
{ |
|
154
|
|
|
$this->_verify( |
|
155
|
|
|
$mock, $method, $params, $this->testCase->never(), 'with' |
|
156
|
|
|
); |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.