@@ -159,6 +159,24 @@ public function testStubbedException()
159
159
$ this ->fail ();
160
160
}
161
161
162
+ public function testStubbedWillThrowException ()
163
+ {
164
+ $ mock = $ this ->getMock ('AnInterface ' );
165
+ $ mock ->expects ($ this ->any ())
166
+ ->method ('doSomething ' )
167
+ ->willThrowException (new Exception );
168
+
169
+ try {
170
+ $ mock ->doSomething ();
171
+ }
172
+
173
+ catch (Exception $ e ) {
174
+ return ;
175
+ }
176
+
177
+ $ this ->fail ();
178
+ }
179
+
162
180
public function testStubbedReturnValue ()
163
181
{
164
182
$ mock = $ this ->getMock ('AnInterface ' );
@@ -167,6 +185,13 @@ public function testStubbedReturnValue()
167
185
->will ($ this ->returnValue ('something ' ));
168
186
169
187
$ this ->assertEquals ('something ' , $ mock ->doSomething ());
188
+
189
+ $ mock = $ this ->getMock ('AnInterface ' );
190
+ $ mock ->expects ($ this ->any ())
191
+ ->method ('doSomething ' )
192
+ ->willReturn ('something ' );
193
+
194
+ $ this ->assertEquals ('something ' , $ mock ->doSomething ());
170
195
}
171
196
172
197
public function testStubbedReturnValueMap ()
@@ -184,6 +209,32 @@ public function testStubbedReturnValueMap()
184
209
$ this ->assertEquals ('d ' , $ mock ->doSomething ('a ' , 'b ' , 'c ' ));
185
210
$ this ->assertEquals ('h ' , $ mock ->doSomething ('e ' , 'f ' , 'g ' ));
186
211
$ this ->assertEquals (NULL , $ mock ->doSomething ('foo ' , 'bar ' ));
212
+
213
+ $ mock = $ this ->getMock ('AnInterface ' );
214
+ $ mock ->expects ($ this ->any ())
215
+ ->method ('doSomething ' )
216
+ ->willReturnMap ($ map );
217
+
218
+ $ this ->assertEquals ('d ' , $ mock ->doSomething ('a ' , 'b ' , 'c ' ));
219
+ $ this ->assertEquals ('h ' , $ mock ->doSomething ('e ' , 'f ' , 'g ' ));
220
+ $ this ->assertEquals (NULL , $ mock ->doSomething ('foo ' , 'bar ' ));
221
+ }
222
+
223
+ public function testStubbedReturnArgument ()
224
+ {
225
+ $ mock = $ this ->getMock ('AnInterface ' );
226
+ $ mock ->expects ($ this ->any ())
227
+ ->method ('doSomething ' )
228
+ ->will ($ this ->returnArgument (1 ));
229
+
230
+ $ this ->assertEquals ('b ' , $ mock ->doSomething ('a ' , 'b ' ));
231
+
232
+ $ mock = $ this ->getMock ('AnInterface ' );
233
+ $ mock ->expects ($ this ->any ())
234
+ ->method ('doSomething ' )
235
+ ->willReturnArgument (1 );
236
+
237
+ $ this ->assertEquals ('b ' , $ mock ->doSomething ('a ' , 'b ' ));
187
238
}
188
239
189
240
public function testFunctionCallback ()
@@ -194,6 +245,51 @@ public function testFunctionCallback()
194
245
->will ($ this ->returnCallback ('functionCallback ' ));
195
246
196
247
$ this ->assertEquals ('pass ' , $ mock ->doSomething ('foo ' , 'bar ' ));
248
+
249
+ $ mock = $ this ->getMock ('SomeClass ' , array ('doSomething ' ), array (), '' , FALSE );
250
+ $ mock ->expects ($ this ->once ())
251
+ ->method ('doSomething ' )
252
+ ->willReturnCallback ('functionCallback ' );
253
+
254
+ $ this ->assertEquals ('pass ' , $ mock ->doSomething ('foo ' , 'bar ' ));
255
+ }
256
+
257
+ public function testStubbedReturnSelf ()
258
+ {
259
+ $ mock = $ this ->getMock ('AnInterface ' );
260
+ $ mock ->expects ($ this ->any ())
261
+ ->method ('doSomething ' )
262
+ ->will ($ this ->returnSelf ());
263
+
264
+ $ this ->assertEquals ($ mock , $ mock ->doSomething ());
265
+
266
+ $ mock = $ this ->getMock ('AnInterface ' );
267
+ $ mock ->expects ($ this ->any ())
268
+ ->method ('doSomething ' )
269
+ ->willReturnSelf ();
270
+
271
+ $ this ->assertEquals ($ mock , $ mock ->doSomething ());
272
+ }
273
+
274
+ public function testStubbedReturnOnConsecutiveCalls ()
275
+ {
276
+ $ mock = $ this ->getMock ('AnInterface ' );
277
+ $ mock ->expects ($ this ->any ())
278
+ ->method ('doSomething ' )
279
+ ->will ($ this ->onConsecutiveCalls ('a ' , 'b ' , 'c ' ));
280
+
281
+ $ this ->assertEquals ('a ' , $ mock ->doSomething ());
282
+ $ this ->assertEquals ('b ' , $ mock ->doSomething ());
283
+ $ this ->assertEquals ('c ' , $ mock ->doSomething ());
284
+
285
+ $ mock = $ this ->getMock ('AnInterface ' );
286
+ $ mock ->expects ($ this ->any ())
287
+ ->method ('doSomething ' )
288
+ ->willReturnOnConsecutiveCalls ('a ' , 'b ' , 'c ' );
289
+
290
+ $ this ->assertEquals ('a ' , $ mock ->doSomething ());
291
+ $ this ->assertEquals ('b ' , $ mock ->doSomething ());
292
+ $ this ->assertEquals ('c ' , $ mock ->doSomething ());
197
293
}
198
294
199
295
public function testStaticMethodCallback ()
@@ -304,6 +400,21 @@ public function testStubbedReturnValueForStaticMethod()
304
400
$ this ->assertEquals (
305
401
'something ' , StaticMockTestClassMock::doSomething ()
306
402
);
403
+
404
+ $ this ->getMockClass (
405
+ 'StaticMockTestClass ' ,
406
+ array ('doSomething ' ),
407
+ array (),
408
+ 'StaticMockTestClassMock '
409
+ );
410
+
411
+ StaticMockTestClassMock::staticExpects ($ this ->any ())
412
+ ->method ('doSomething ' )
413
+ ->willReturn ('something ' );
414
+
415
+ $ this ->assertEquals (
416
+ 'something ' , StaticMockTestClassMock::doSomething ()
417
+ );
307
418
}
308
419
309
420
public function testStubbedReturnValueForStaticMethod2 ()
@@ -322,6 +433,21 @@ public function testStubbedReturnValueForStaticMethod2()
322
433
$ this ->assertEquals (
323
434
'something ' , StaticMockTestClassMock2::doSomethingElse ()
324
435
);
436
+
437
+ $ this ->getMockClass (
438
+ 'StaticMockTestClass ' ,
439
+ array ('doSomething ' ),
440
+ array (),
441
+ 'StaticMockTestClassMock2 '
442
+ );
443
+
444
+ StaticMockTestClassMock2::staticExpects ($ this ->any ())
445
+ ->method ('doSomething ' )
446
+ ->willReturn ('something ' );
447
+
448
+ $ this ->assertEquals (
449
+ 'something ' , StaticMockTestClassMock2::doSomethingElse ()
450
+ );
325
451
}
326
452
327
453
public function testGetMockForAbstractClass ()
0 commit comments