|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Mockery |
|
4
|
|
|
* |
|
5
|
|
|
* LICENSE |
|
6
|
|
|
* |
|
7
|
|
|
* This source file is subject to the new BSD license that is bundled |
|
8
|
|
|
* with this package in the file LICENSE.txt. |
|
9
|
|
|
* It is also available through the world-wide-web at this URL: |
|
10
|
|
|
* http://github.com/padraic/mockery/blob/master/LICENSE |
|
11
|
|
|
* If you did not receive a copy of the license and are unable to |
|
12
|
|
|
* obtain it through the world-wide-web, please send an email |
|
13
|
|
|
* to [email protected] so we can send you a copy immediately. |
|
14
|
|
|
* |
|
15
|
|
|
* @category Mockery |
|
16
|
|
|
* @package Mockery |
|
17
|
|
|
* @copyright Copyright (c) 2010 Pádraic Brady (http://blog.astrumfutura.com) |
|
18
|
|
|
* @license http://github.com/padraic/mockery/blob/master/LICENSE New BSD License |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
namespace Mockery; |
|
22
|
|
|
|
|
23
|
|
|
class CompositeExpectation implements ExpectationInterface |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* Stores an array of all expectations for this composite |
|
27
|
|
|
* |
|
28
|
|
|
* @var array |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $_expectations = array(); |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Add an expectation to the composite |
|
34
|
|
|
* |
|
35
|
|
|
* @param \Mockery\Expectation|\Mockery\CompositeExpectation $expectation |
|
36
|
|
|
* @return void |
|
37
|
|
|
*/ |
|
38
|
307 |
|
public function add($expectation) |
|
39
|
|
|
{ |
|
40
|
307 |
|
$this->_expectations[] = $expectation; |
|
41
|
307 |
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param mixed ... |
|
45
|
|
|
*/ |
|
46
|
88 |
|
public function andReturn() |
|
47
|
|
|
{ |
|
48
|
88 |
|
return $this->__call(__FUNCTION__, func_get_args()); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Set a return value, or sequential queue of return values |
|
53
|
|
|
* |
|
54
|
|
|
* @param mixed ... |
|
55
|
|
|
* @return self |
|
56
|
|
|
*/ |
|
57
|
1 |
|
public function andReturns() |
|
58
|
|
|
{ |
|
59
|
1 |
|
return call_user_func_array([$this, 'andReturn'], func_get_args()); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Intercept any expectation calls and direct against all expectations |
|
64
|
|
|
* |
|
65
|
|
|
* @param string $method |
|
66
|
|
|
* @param array $args |
|
67
|
|
|
* @return self |
|
68
|
|
|
*/ |
|
69
|
300 |
|
public function __call($method, array $args) |
|
70
|
|
|
{ |
|
71
|
300 |
|
foreach ($this->_expectations as $expectation) { |
|
72
|
300 |
|
call_user_func_array(array($expectation, $method), $args); |
|
73
|
297 |
|
} |
|
74
|
297 |
|
return $this; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Return order number of the first expectation |
|
79
|
|
|
* |
|
80
|
|
|
* @return int |
|
81
|
|
|
*/ |
|
82
|
1 |
|
public function getOrderNumber() |
|
83
|
|
|
{ |
|
84
|
1 |
|
reset($this->_expectations); |
|
85
|
1 |
|
$first = current($this->_expectations); |
|
86
|
1 |
|
return $first->getOrderNumber(); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Return the parent mock of the first expectation |
|
91
|
|
|
* |
|
92
|
|
|
* @return \Mockery\MockInterface |
|
93
|
|
|
*/ |
|
94
|
3 |
|
public function getMock() |
|
95
|
|
|
{ |
|
96
|
3 |
|
reset($this->_expectations); |
|
97
|
3 |
|
$first = current($this->_expectations); |
|
98
|
3 |
|
return $first->getMock(); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Mockery API alias to getMock |
|
103
|
|
|
* |
|
104
|
|
|
* @return \Mockery\MockInterface |
|
105
|
|
|
*/ |
|
106
|
1 |
|
public function mock() |
|
107
|
|
|
{ |
|
108
|
1 |
|
return $this->getMock(); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Starts a new expectation addition on the first mock which is the primary |
|
113
|
|
|
* target outside of a demeter chain |
|
114
|
|
|
* |
|
115
|
|
|
* @param mixed ... |
|
116
|
|
|
* @return \Mockery\Expectation |
|
117
|
|
|
*/ |
|
118
|
|
|
public function shouldReceive() |
|
119
|
|
|
{ |
|
120
|
|
|
$args = func_get_args(); |
|
121
|
|
|
reset($this->_expectations); |
|
122
|
|
|
$first = current($this->_expectations); |
|
123
|
|
|
return call_user_func_array(array($first->getMock(), 'shouldReceive'), $args); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Return the string summary of this composite expectation |
|
128
|
|
|
* |
|
129
|
|
|
* @return string |
|
130
|
|
|
*/ |
|
131
|
3 |
|
public function __toString() |
|
132
|
|
|
{ |
|
133
|
3 |
|
$return = '['; |
|
134
|
3 |
|
$parts = array(); |
|
135
|
3 |
|
foreach ($this->_expectations as $exp) { |
|
136
|
3 |
|
$parts[] = (string) $exp; |
|
137
|
3 |
|
} |
|
138
|
3 |
|
$return .= implode(', ', $parts) . ']'; |
|
139
|
3 |
|
return $return; |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|