blob: 90a5fa2ed55b83684573c6d11056d890b251b4e3 [file] [log] [blame]
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02001<?php
2
3namespace LdapRecord\Testing;
4
5use LdapRecord\LdapRecordException;
6use PHPUnit\Framework\Constraint\Constraint;
7use PHPUnit\Framework\Constraint\IsEqual;
8use UnexpectedValueException;
9
10class LdapExpectation
11{
12 /**
13 * The value to return from the expectation.
14 *
15 * @var mixed
16 */
17 protected $value;
18
19 /**
20 * The exception to throw from the expectation.
21 *
22 * @var null|LdapRecordException|\Exception
23 */
24 protected $exception;
25
26 /**
27 * The amount of times the expectation should be called.
28 *
29 * @var int
30 */
31 protected $count = 1;
32
33 /**
34 * The method that the expectation belongs to.
35 *
36 * @var string
37 */
38 protected $method;
39
40 /**
41 * The methods argument's.
42 *
43 * @var array
44 */
45 protected $args = [];
46
47 /**
48 * Whether the same expectation should be returned indefinitely.
49 *
50 * @var bool
51 */
52 protected $indefinitely = true;
53
54 /**
55 * Whether the expectation should return errors.
56 *
57 * @var bool
58 */
59 protected $errors = false;
60
61 /**
62 * The error number to return.
63 *
64 * @var int
65 */
66 protected $errorCode = 1;
67
68 /**
69 * The last error string to return.
70 *
71 * @var string
72 */
73 protected $errorMessage = '';
74
75 /**
76 * The diagnostic message string to return.
77 *
78 * @var string
79 */
80 protected $errorDiagnosticMessage = '';
81
82 /**
83 * Constructor.
84 *
85 * @param string $method
86 */
87 public function __construct($method)
88 {
89 $this->method = $method;
90 }
91
92 /**
93 * Set the arguments that the operation should receive.
94 *
95 * @param mixed $args
96 *
97 * @return $this
98 */
99 public function with($args)
100 {
101 $args = is_array($args) ? $args : func_get_args();
102
103 foreach ($args as $key => $arg) {
104 if (! $arg instanceof Constraint) {
105 $args[$key] = new IsEqual($arg);
106 }
107 }
108
109 $this->args = $args;
110
111 return $this;
112 }
113
114 /**
115 * Set the expected value to return.
116 *
117 * @param mixed $value
118 *
119 * @return $this
120 */
121 public function andReturn($value)
122 {
123 $this->value = $value;
124
125 return $this;
126 }
127
128 /**
129 * The error message to return from the expectation.
130 *
131 * @param int $code
132 * @param string $error
133 * @param string $diagnosticMessage
134 *
135 * @return $this
136 */
137 public function andReturnError($code = 1, $error = '', $diagnosticMessage = '')
138 {
139 $this->errors = true;
140
141 $this->errorCode = $code;
142 $this->errorMessage = $error;
143 $this->errorDiagnosticMessage = $diagnosticMessage;
144
145 return $this;
146 }
147
148 /**
149 * Set the expected exception to throw.
150 *
151 * @param string|\Exception|LdapRecordException $exception
152 *
153 * @return $this
154 */
155 public function andThrow($exception)
156 {
157 if (is_string($exception)) {
158 $exception = new LdapRecordException($exception);
159 }
160
161 $this->exception = $exception;
162
163 return $this;
164 }
165
166 /**
167 * Set the expectation to be only called once.
168 *
169 * @return $this
170 */
171 public function once()
172 {
173 return $this->times(1);
174 }
175
176 /**
177 * Set the expectation to be only called twice.
178 *
179 * @return $this
180 */
181 public function twice()
182 {
183 return $this->times(2);
184 }
185
186 /**
187 * Set the expectation to be called the given number of times.
188 *
189 * @param int $count
190 *
191 * @return $this
192 */
193 public function times($count = 1)
194 {
195 $this->indefinitely = false;
196
197 $this->count = $count;
198
199 return $this;
200 }
201
202 /**
203 * Get the method the expectation belongs to.
204 *
205 * @return string
206 */
207 public function getMethod()
208 {
209 if (is_null($this->method)) {
210 throw new UnexpectedValueException('An expectation must have a method.');
211 }
212
213 return $this->method;
214 }
215
216 /**
217 * Get the expected call count.
218 *
219 * @return int
220 */
221 public function getExpectedCount()
222 {
223 return $this->count;
224 }
225
226 /**
227 * Get the expected arguments.
228 *
229 * @return Constraint[]
230 */
231 public function getExpectedArgs()
232 {
233 return $this->args;
234 }
235
236 /**
237 * Get the expected exception.
238 *
239 * @return null|\Exception|LdapRecordException
240 */
241 public function getExpectedException()
242 {
243 return $this->exception;
244 }
245
246 /**
247 * Get the expected value.
248 *
249 * @return mixed
250 */
251 public function getExpectedValue()
252 {
253 return $this->value;
254 }
255
256 /**
257 * Determine whether the expectation is returning an error.
258 *
259 * @return bool
260 */
261 public function isReturningError()
262 {
263 return $this->errors;
264 }
265
266 /**
267 * @return int
268 */
269 public function getExpectedErrorCode()
270 {
271 return $this->errorCode;
272 }
273
274 /**
275 * @return string
276 */
277 public function getExpectedErrorMessage()
278 {
279 return $this->errorMessage;
280 }
281
282 /**
283 * @return string
284 */
285 public function getExpectedErrorDiagnosticMessage()
286 {
287 return $this->errorDiagnosticMessage;
288 }
289
290 /**
291 * Decrement the call count of the expectation.
292 *
293 * @return $this
294 */
295 public function decrementCallCount()
296 {
297 if (! $this->indefinitely) {
298 $this->count -= 1;
299 }
300
301 return $this;
302 }
303}