Completed
Branch development (b1b115)
by Johannes
10:28
created

ExpectationTest   F

Complexity

Total Complexity 210

Size/Duplication

Total Lines 1920
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 210
c 0
b 0
f 0
dl 0
loc 1920
rs 0.8

How to fix   Complexity   

Complex Class

Complex classes like ExpectationTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use ExpectationTest, and based on these observations, apply Extract Interface, too.

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/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
 * @subpackage UnitTests
18
 * @copyright  Copyright (c) 2010-2014 Pádraic Brady (http://blog.astrumfutura.com)
19
 * @license    http://github.com/padraic/mockery/blob/master/LICENSE New BSD License
20
 */
21
22
use Mockery\Adapter\Phpunit\MockeryTestCase;
23
24
class ExpectationTest extends MockeryTestCase
25
{
26
27
    public function setup()
28
    {
29
        $this->container = new \Mockery\Container(\Mockery::getDefaultGenerator(), \Mockery::getDefaultLoader());
30
        $this->mock = $this->container->mock('foo');
31
    }
32
33
    public function teardown()
34
    {
35
        \Mockery::getConfiguration()->allowMockingNonExistentMethods(true);
36
        $this->container->mockery_close();
37
    }
38
39
    public function testReturnsNullWhenNoArgs()
40
    {
41
        $this->mock->shouldReceive('foo');
42
        $this->assertNull($this->mock->foo());
43
    }
44
45
    public function testReturnsNullWhenSingleArg()
46
    {
47
        $this->mock->shouldReceive('foo');
48
        $this->assertNull($this->mock->foo(1));
49
    }
50
51
    public function testReturnsNullWhenManyArgs()
52
    {
53
        $this->mock->shouldReceive('foo');
54
        $this->assertNull($this->mock->foo('foo', array(), new stdClass));
55
    }
56
57
    public function testReturnsNullIfNullIsReturnValue()
58
    {
59
        $this->mock->shouldReceive('foo')->andReturn(null);
60
        $this->assertNull($this->mock->foo());
61
    }
62
63
    public function testReturnsNullForMockedExistingClassIfAndreturnnullCalled()
64
    {
65
        $mock = $this->container->mock('MockeryTest_Foo');
66
        $mock->shouldReceive('foo')->andReturn(null);
67
        $this->assertNull($mock->foo());
68
    }
69
70
    public function testReturnsNullForMockedExistingClassIfNullIsReturnValue()
71
    {
72
        $mock = $this->container->mock('MockeryTest_Foo');
73
        $mock->shouldReceive('foo')->andReturnNull();
74
        $this->assertNull($mock->foo());
75
    }
76
77
    public function testReturnsSameValueForAllIfNoArgsExpectationAndNoneGiven()
78
    {
79
        $this->mock->shouldReceive('foo')->andReturn(1);
80
        $this->assertEquals(1, $this->mock->foo());
81
    }
82
83
    public function testSetsPublicPropertyWhenRequested()
84
    {
85
        $this->mock->bar = null;
86
        $this->mock->shouldReceive('foo')->andSet('bar', 'baz');
87
        $this->assertNull($this->mock->bar);
88
        $this->mock->foo();
89
        $this->assertEquals('baz', $this->mock->bar);
90
    }
91
92
    public function testSetsPublicPropertyWhenRequestedUsingAlias()
93
    {
94
        $this->mock->bar = null;
95
        $this->mock->shouldReceive('foo')->set('bar', 'baz');
96
        $this->assertNull($this->mock->bar);
97
        $this->mock->foo();
98
        $this->assertEquals('baz', $this->mock->bar);
99
    }
100
101
    public function testSetsPublicPropertiesWhenRequested()
102
    {
103
        $this->mock->bar = null;
104
        $this->mock->shouldReceive('foo')->andSet('bar', 'baz', 'bazz', 'bazzz');
105
        $this->assertNull($this->mock->bar);
106
        $this->mock->foo();
107
        $this->assertEquals('baz', $this->mock->bar);
108
        $this->mock->foo();
109
        $this->assertEquals('bazz', $this->mock->bar);
110
        $this->mock->foo();
111
        $this->assertEquals('bazzz', $this->mock->bar);
112
    }
113
114
    public function testSetsPublicPropertiesWhenRequestedUsingAlias()
115
    {
116
        $this->mock->bar = null;
117
        $this->mock->shouldReceive('foo')->set('bar', 'baz', 'bazz', 'bazzz');
118
        $this->assertAttributeEmpty('bar', $this->mock);
119
        $this->mock->foo();
120
        $this->assertEquals('baz', $this->mock->bar);
121
        $this->mock->foo();
122
        $this->assertEquals('bazz', $this->mock->bar);
123
        $this->mock->foo();
124
        $this->assertEquals('bazzz', $this->mock->bar);
125
    }
126
    
127
    public function testSetsPublicPropertiesWhenRequestedMoreTimesThanSetValues()
128
    {
129
        $this->mock->bar = null;
130
        $this->mock->shouldReceive('foo')->andSet('bar', 'baz', 'bazz');
131
        $this->assertNull($this->mock->bar);
132
        $this->mock->foo();
133
        $this->assertEquals('baz', $this->mock->bar);
134
        $this->mock->foo();
135
        $this->assertEquals('bazz', $this->mock->bar);
136
        $this->mock->foo();
137
        $this->assertEquals('bazz', $this->mock->bar);
138
    }
139
140
    public function testSetsPublicPropertiesWhenRequestedMoreTimesThanSetValuesUsingAlias()
141
    {
142
        $this->mock->bar = null;
143
        $this->mock->shouldReceive('foo')->andSet('bar', 'baz', 'bazz');
144
        $this->assertNull($this->mock->bar);
145
        $this->mock->foo();
146
        $this->assertEquals('baz', $this->mock->bar);
147
        $this->mock->foo();
148
        $this->assertEquals('bazz', $this->mock->bar);
149
        $this->mock->foo();
150
        $this->assertEquals('bazz', $this->mock->bar);
151
    }
152
153
    public function testSetsPublicPropertiesWhenRequestedMoreTimesThanSetValuesWithDirectSet()
154
    {
155
        $this->mock->bar = null;
156
        $this->mock->shouldReceive('foo')->andSet('bar', 'baz', 'bazz');
157
        $this->assertNull($this->mock->bar);
158
        $this->mock->foo();
159
        $this->assertEquals('baz', $this->mock->bar);
160
        $this->mock->foo();
161
        $this->assertEquals('bazz', $this->mock->bar);
162
        $this->mock->bar = null;
163
        $this->mock->foo();
164
        $this->assertNull($this->mock->bar);
165
    }
166
167
    public function testSetsPublicPropertiesWhenRequestedMoreTimesThanSetValuesWithDirectSetUsingAlias()
168
    {
169
        $this->mock->bar = null;
170
        $this->mock->shouldReceive('foo')->set('bar', 'baz', 'bazz');
171
        $this->assertNull($this->mock->bar);
172
        $this->mock->foo();
173
        $this->assertEquals('baz', $this->mock->bar);
174
        $this->mock->foo();
175
        $this->assertEquals('bazz', $this->mock->bar);
176
        $this->mock->bar = null;
177
        $this->mock->foo();
178
        $this->assertNull($this->mock->bar);
179
    }
180
181
    public function testReturnsSameValueForAllIfNoArgsExpectationAndSomeGiven()
182
    {
183
        $this->mock->shouldReceive('foo')->andReturn(1);
184
        $this->assertEquals(1, $this->mock->foo('foo'));
185
    }
186
187
    public function testReturnsValueFromSequenceSequentially()
188
    {
189
        $this->mock->shouldReceive('foo')->andReturn(1, 2, 3);
190
        $this->mock->foo('foo');
191
        $this->assertEquals(2, $this->mock->foo('foo'));
192
    }
193
194
    public function testReturnsValueFromSequenceSequentiallyAndRepeatedlyReturnsFinalValueOnExtraCalls()
195
    {
196
        $this->mock->shouldReceive('foo')->andReturn(1, 2, 3);
197
        $this->mock->foo('foo');
198
        $this->mock->foo('foo');
199
        $this->assertEquals(3, $this->mock->foo('foo'));
200
        $this->assertEquals(3, $this->mock->foo('foo'));
201
    }
202
203
    public function testReturnsValueFromSequenceSequentiallyAndRepeatedlyReturnsFinalValueOnExtraCallsWithManyAndReturnCalls()
204
    {
205
        $this->mock->shouldReceive('foo')->andReturn(1)->andReturn(2, 3);
206
        $this->mock->foo('foo');
207
        $this->mock->foo('foo');
208
        $this->assertEquals(3, $this->mock->foo('foo'));
209
        $this->assertEquals(3, $this->mock->foo('foo'));
210
    }
211
212
    public function testReturnsValueOfClosure()
213
    {
214
        $this->mock->shouldReceive('foo')->with(5)->andReturnUsing(function ($v) {return $v+1;});
215
        $this->assertEquals(6, $this->mock->foo(5));
216
    }
217
218
    public function testReturnsUndefined()
219
    {
220
        $this->mock->shouldReceive('foo')->andReturnUndefined();
221
        $this->assertTrue($this->mock->foo() instanceof \Mockery\Undefined);
222
    }
223
224
    public function testReturnsValuesSetAsArray()
225
    {
226
        $this->mock->shouldReceive('foo')->andReturnValues(array(1, 2, 3));
227
        $this->assertEquals(1, $this->mock->foo());
228
        $this->assertEquals(2, $this->mock->foo());
229
        $this->assertEquals(3, $this->mock->foo());
230
    }
231
232
    /**
233
     * @expectedException OutOfBoundsException
234
     */
235
    public function testThrowsException()
236
    {
237
        $this->mock->shouldReceive('foo')->andThrow(new OutOfBoundsException);
238
        $this->mock->foo();
239
    }
240
241
    /**
242
     * @expectedException OutOfBoundsException
243
     */
244
    public function testThrowsExceptionBasedOnArgs()
245
    {
246
        $this->mock->shouldReceive('foo')->andThrow('OutOfBoundsException');
247
        $this->mock->foo();
248
    }
249
250
    public function testThrowsExceptionBasedOnArgsWithMessage()
251
    {
252
        $this->mock->shouldReceive('foo')->andThrow('OutOfBoundsException', 'foo');
253
        try {
254
            $this->mock->foo();
255
        } catch (OutOfBoundsException $e) {
256
            $this->assertEquals('foo', $e->getMessage());
257
        }
258
    }
259
260
    /**
261
     * @expectedException OutOfBoundsException
262
     */
263
    public function testThrowsExceptionSequentially()
264
    {
265
        $this->mock->shouldReceive('foo')->andThrow(new Exception)->andThrow(new OutOfBoundsException);
266
        try {
267
            $this->mock->foo();
268
        } catch (Exception $e) {
269
        }
270
        $this->mock->foo();
271
    }
272
273
    public function testAndThrowExceptions()
274
    {
275
        $this->mock->shouldReceive('foo')->andThrowExceptions(array(
276
            new OutOfBoundsException,
277
            new InvalidArgumentException,
278
        ));
279
280
        try {
281
            $this->mock->foo();
282
            throw new Exception("Expected OutOfBoundsException, non thrown");
283
        } catch (\Exception $e) {
284
            $this->assertInstanceOf("OutOfBoundsException", $e, "Wrong or no exception thrown: {$e->getMessage()}");
285
        }
286
287
        try {
288
            $this->mock->foo();
289
            throw new Exception("Expected InvalidArgumentException, non thrown");
290
        } catch (\Exception $e) {
291
            $this->assertInstanceOf("InvalidArgumentException", $e, "Wrong or no exception thrown: {$e->getMessage()}");
292
        }
293
    }
294
295
    /**
296
     * @expectedException Mockery\Exception
297
     * @expectedExceptionMessage You must pass an array of exception objects to andThrowExceptions
298
     */
299
    public function testAndThrowExceptionsCatchNonExceptionArgument()
300
    {
301
        $this->mock
302
            ->shouldReceive('foo')
303
            ->andThrowExceptions(array('NotAnException'));
304
    }
305
306
    public function testMultipleExpectationsWithReturns()
307
    {
308
        $this->mock->shouldReceive('foo')->with(1)->andReturn(10);
309
        $this->mock->shouldReceive('bar')->with(2)->andReturn(20);
310
        $this->assertEquals(10, $this->mock->foo(1));
311
        $this->assertEquals(20, $this->mock->bar(2));
312
    }
313
314
    public function testExpectsNoArguments()
315
    {
316
        $this->mock->shouldReceive('foo')->withNoArgs();
317
        $this->mock->foo();
318
    }
319
320
    /**
321
     * @expectedException \Mockery\Exception
322
     */
323
    public function testExpectsNoArgumentsThrowsExceptionIfAnyPassed()
324
    {
325
        $this->mock->shouldReceive('foo')->withNoArgs();
326
        $this->mock->foo(1);
327
    }
328
329
    public function testExpectsArgumentsArray()
330
    {
331
        $this->mock->shouldReceive('foo')->withArgs(array(1, 2));
332
        $this->mock->foo(1, 2);
333
    }
334
335
    /**
336
     * @expectedException \Mockery\Exception
337
     */
338
    public function testExpectsArgumentsArrayThrowsExceptionIfPassedEmptyArray()
339
    {
340
        $this->mock->shouldReceive('foo')->withArgs(array());
341
        $this->mock->foo(1, 2);
342
    }
343
344
    /**
345
     * @expectedException \Mockery\Exception
346
     */
347
    public function testExpectsArgumentsArrayThrowsExceptionIfNoArgumentsPassed()
348
    {
349
        $this->mock->shouldReceive('foo')->with();
350
        $this->mock->foo(1);
351
    }
352
353
    /**
354
     * @expectedException \Mockery\Exception
355
     */
356
    public function testExpectsArgumentsArrayThrowsExceptionIfPassedWrongArguments()
357
    {
358
        $this->mock->shouldReceive('foo')->withArgs(array(1, 2));
359
        $this->mock->foo(3, 4);
360
    }
361
362
    /**
363
     * @expectedException \Mockery\Exception
364
     * @expectedExceptionMessageRegExp /foo\(NULL\)/
365
     */
366
    public function testExpectsStringArgumentExceptionMessageDifferentiatesBetweenNullAndEmptyString()
367
    {
368
        $this->mock->shouldReceive('foo')->withArgs(array('a string'));
369
        $this->mock->foo(null);
370
    }
371
372
    public function testExpectsAnyArguments()
373
    {
374
        $this->mock->shouldReceive('foo')->withAnyArgs();
375
        $this->mock->foo();
376
        $this->mock->foo(1);
377
        $this->mock->foo(1, 'k', new stdClass);
378
    }
379
380
    public function testExpectsArgumentMatchingRegularExpression()
381
    {
382
        $this->mock->shouldReceive('foo')->with('/bar/i');
383
        $this->mock->foo('xxBARxx');
384
    }
385
386
    public function testExpectsArgumentMatchingObjectType()
387
    {
388
        $this->mock->shouldReceive('foo')->with('\stdClass');
389
        $this->mock->foo(new stdClass);
390
    }
391
392
    /**
393
     * @expectedException \Mockery\Exception
394
     */
395
    public function testThrowsExceptionOnNoArgumentMatch()
396
    {
397
        $this->mock->shouldReceive('foo')->with(1);
398
        $this->mock->foo(2);
399
    }
400
401
    public function testNeverCalled()
402
    {
403
        $this->mock->shouldReceive('foo')->never();
404
        $this->container->mockery_verify();
405
    }
406
407
    public function testShouldNotReceive()
408
    {
409
        $this->mock->shouldNotReceive('foo');
410
        $this->container->mockery_verify();
411
    }
412
413
    /**
414
     * @expectedException \Mockery\Exception\InvalidCountException
415
     */
416
    public function testShouldNotReceiveThrowsExceptionIfMethodCalled()
417
    {
418
        $this->mock->shouldNotReceive('foo');
419
        $this->mock->foo();
420
        $this->container->mockery_verify();
421
    }
422
423
    /**
424
     * @expectedException \Mockery\Exception\InvalidCountException
425
     */
426
    public function testShouldNotReceiveWithArgumentThrowsExceptionIfMethodCalled()
427
    {
428
        $this->mock->shouldNotReceive('foo')->with(2);
429
        $this->mock->foo(2);
430
        $this->container->mockery_verify();
431
    }
432
433
    /**
434
     * @expectedException \Mockery\CountValidator\Exception
435
     */
436
    public function testNeverCalledThrowsExceptionOnCall()
437
    {
438
        $this->mock->shouldReceive('foo')->never();
439
        $this->mock->foo();
440
        $this->container->mockery_verify();
441
    }
442
443
    public function testCalledOnce()
444
    {
445
        $this->mock->shouldReceive('foo')->once();
446
        $this->mock->foo();
447
        $this->container->mockery_verify();
448
    }
449
450
    /**
451
     * @expectedException \Mockery\CountValidator\Exception
452
     */
453
    public function testCalledOnceThrowsExceptionIfNotCalled()
454
    {
455
        $this->mock->shouldReceive('foo')->once();
456
        $this->container->mockery_verify();
457
    }
458
459
    /**
460
     * @expectedException \Mockery\CountValidator\Exception
461
     */
462
    public function testCalledOnceThrowsExceptionIfCalledTwice()
463
    {
464
        $this->mock->shouldReceive('foo')->once();
465
        $this->mock->foo();
466
        $this->mock->foo();
467
        $this->container->mockery_verify();
468
    }
469
470
    public function testCalledTwice()
471
    {
472
        $this->mock->shouldReceive('foo')->twice();
473
        $this->mock->foo();
474
        $this->mock->foo();
475
        $this->container->mockery_verify();
476
    }
477
478
    /**
479
     * @expectedException \Mockery\CountValidator\Exception
480
     */
481
    public function testCalledTwiceThrowsExceptionIfNotCalled()
482
    {
483
        $this->mock->shouldReceive('foo')->twice();
484
        $this->container->mockery_verify();
485
    }
486
487
    /**
488
     * @expectedException \Mockery\CountValidator\Exception
489
     */
490
    public function testCalledOnceThrowsExceptionIfCalledThreeTimes()
491
    {
492
        $this->mock->shouldReceive('foo')->twice();
493
        $this->mock->foo();
494
        $this->mock->foo();
495
        $this->mock->foo();
496
        $this->container->mockery_verify();
497
    }
498
499
    public function testCalledZeroOrMoreTimesAtZeroCalls()
500
    {
501
        $this->mock->shouldReceive('foo')->zeroOrMoreTimes();
502
        $this->container->mockery_verify();
503
    }
504
505
    public function testCalledZeroOrMoreTimesAtThreeCalls()
506
    {
507
        $this->mock->shouldReceive('foo')->zeroOrMoreTimes();
508
        $this->mock->foo();
509
        $this->mock->foo();
510
        $this->mock->foo();
511
        $this->container->mockery_verify();
512
    }
513
514
    public function testTimesCountCalls()
515
    {
516
        $this->mock->shouldReceive('foo')->times(4);
517
        $this->mock->foo();
518
        $this->mock->foo();
519
        $this->mock->foo();
520
        $this->mock->foo();
521
        $this->container->mockery_verify();
522
    }
523
524
    /**
525
     * @expectedException \Mockery\CountValidator\Exception
526
     */
527
    public function testTimesCountCallThrowsExceptionOnTooFewCalls()
528
    {
529
        $this->mock->shouldReceive('foo')->times(2);
530
        $this->mock->foo();
531
        $this->container->mockery_verify();
532
    }
533
534
    /**
535
     * @expectedException \Mockery\CountValidator\Exception
536
     */
537
    public function testTimesCountCallThrowsExceptionOnTooManyCalls()
538
    {
539
        $this->mock->shouldReceive('foo')->times(2);
540
        $this->mock->foo();
541
        $this->mock->foo();
542
        $this->mock->foo();
543
        $this->container->mockery_verify();
544
    }
545
546
    public function testCalledAtLeastOnceAtExactlyOneCall()
547
    {
548
        $this->mock->shouldReceive('foo')->atLeast()->once();
549
        $this->mock->foo();
550
        $this->container->mockery_verify();
551
    }
552
553
    public function testCalledAtLeastOnceAtExactlyThreeCalls()
554
    {
555
        $this->mock->shouldReceive('foo')->atLeast()->times(3);
556
        $this->mock->foo();
557
        $this->mock->foo();
558
        $this->mock->foo();
559
        $this->container->mockery_verify();
560
    }
561
562
    /**
563
     * @expectedException \Mockery\CountValidator\Exception
564
     */
565
    public function testCalledAtLeastThrowsExceptionOnTooFewCalls()
566
    {
567
        $this->mock->shouldReceive('foo')->atLeast()->twice();
568
        $this->mock->foo();
569
        $this->container->mockery_verify();
570
    }
571
572
    public function testCalledAtMostOnceAtExactlyOneCall()
573
    {
574
        $this->mock->shouldReceive('foo')->atMost()->once();
575
        $this->mock->foo();
576
        $this->container->mockery_verify();
577
    }
578
579
    public function testCalledAtMostAtExactlyThreeCalls()
580
    {
581
        $this->mock->shouldReceive('foo')->atMost()->times(3);
582
        $this->mock->foo();
583
        $this->mock->foo();
584
        $this->mock->foo();
585
        $this->container->mockery_verify();
586
    }
587
588
    /**
589
     * @expectedException \Mockery\CountValidator\Exception
590
     */
591
    public function testCalledAtLeastThrowsExceptionOnTooManyCalls()
592
    {
593
        $this->mock->shouldReceive('foo')->atMost()->twice();
594
        $this->mock->foo();
595
        $this->mock->foo();
596
        $this->mock->foo();
597
        $this->container->mockery_verify();
598
    }
599
600
    /**
601
     * @expectedException \Mockery\CountValidator\Exception
602
     */
603
    public function testExactCountersOverrideAnyPriorSetNonExactCounters()
604
    {
605
        $this->mock->shouldReceive('foo')->atLeast()->once()->once();
606
        $this->mock->foo();
607
        $this->mock->foo();
608
        $this->container->mockery_verify();
609
    }
610
611
    public function testComboOfLeastAndMostCallsWithOneCall()
612
    {
613
        $this->mock->shouldReceive('foo')->atleast()->once()->atMost()->twice();
614
        $this->mock->foo();
615
        $this->container->mockery_verify();
616
    }
617
618
    public function testComboOfLeastAndMostCallsWithTwoCalls()
619
    {
620
        $this->mock->shouldReceive('foo')->atleast()->once()->atMost()->twice();
621
        $this->mock->foo();
622
        $this->mock->foo();
623
        $this->container->mockery_verify();
624
    }
625
626
    /**
627
     * @expectedException \Mockery\CountValidator\Exception
628
     */
629
    public function testComboOfLeastAndMostCallsThrowsExceptionAtTooFewCalls()
630
    {
631
        $this->mock->shouldReceive('foo')->atleast()->once()->atMost()->twice();
632
        $this->container->mockery_verify();
633
    }
634
635
    /**
636
     * @expectedException \Mockery\CountValidator\Exception
637
     */
638
    public function testComboOfLeastAndMostCallsThrowsExceptionAtTooManyCalls()
639
    {
640
        $this->mock->shouldReceive('foo')->atleast()->once()->atMost()->twice();
641
        $this->mock->foo();
642
        $this->mock->foo();
643
        $this->mock->foo();
644
        $this->container->mockery_verify();
645
    }
646
647
    public function testCallCountingOnlyAppliesToMatchedExpectations()
648
    {
649
        $this->mock->shouldReceive('foo')->with(1)->once();
650
        $this->mock->shouldReceive('foo')->with(2)->twice();
651
        $this->mock->shouldReceive('foo')->with(3);
652
        $this->mock->foo(1);
653
        $this->mock->foo(2);
654
        $this->mock->foo(2);
655
        $this->mock->foo(3);
656
        $this->container->mockery_verify();
657
    }
658
659
    /**
660
     * @expectedException \Mockery\CountValidator\Exception
661
     */
662
    public function testCallCountingThrowsExceptionOnAnyMismatch()
663
    {
664
        $this->mock->shouldReceive('foo')->with(1)->once();
665
        $this->mock->shouldReceive('foo')->with(2)->twice();
666
        $this->mock->shouldReceive('foo')->with(3);
667
        $this->mock->shouldReceive('bar');
668
        $this->mock->foo(1);
669
        $this->mock->foo(2);
670
        $this->mock->foo(3);
671
        $this->mock->bar();
672
        $this->container->mockery_verify();
673
    }
674
675
    public function testOrderedCallsWithoutError()
676
    {
677
        $this->mock->shouldReceive('foo')->ordered();
678
        $this->mock->shouldReceive('bar')->ordered();
679
        $this->mock->foo();
680
        $this->mock->bar();
681
        $this->container->mockery_verify();
682
    }
683
684
    /**
685
     * @expectedException \Mockery\Exception
686
     */
687
    public function testOrderedCallsWithOutOfOrderError()
688
    {
689
        $this->mock->shouldReceive('foo')->ordered();
690
        $this->mock->shouldReceive('bar')->ordered();
691
        $this->mock->bar();
692
        $this->mock->foo();
693
        $this->container->mockery_verify();
694
    }
695
696
    public function testDifferentArgumentsAndOrderingsPassWithoutException()
697
    {
698
        $this->mock->shouldReceive('foo')->with(1)->ordered();
699
        $this->mock->shouldReceive('foo')->with(2)->ordered();
700
        $this->mock->foo(1);
701
        $this->mock->foo(2);
702
        $this->container->mockery_verify();
703
    }
704
705
    /**
706
     * @expectedException \Mockery\Exception
707
     */
708
    public function testDifferentArgumentsAndOrderingsThrowExceptionWhenInWrongOrder()
709
    {
710
        $this->mock->shouldReceive('foo')->with(1)->ordered();
711
        $this->mock->shouldReceive('foo')->with(2)->ordered();
712
        $this->mock->foo(2);
713
        $this->mock->foo(1);
714
        $this->container->mockery_verify();
715
    }
716
717
    public function testUnorderedCallsIgnoredForOrdering()
718
    {
719
        $this->mock->shouldReceive('foo')->with(1)->ordered();
720
        $this->mock->shouldReceive('foo')->with(2);
721
        $this->mock->shouldReceive('foo')->with(3)->ordered();
722
        $this->mock->foo(2);
723
        $this->mock->foo(1);
724
        $this->mock->foo(2);
725
        $this->mock->foo(3);
726
        $this->mock->foo(2);
727
        $this->container->mockery_verify();
728
    }
729
730
    public function testOrderingOfDefaultGrouping()
731
    {
732
        $this->mock->shouldReceive('foo')->ordered();
733
        $this->mock->shouldReceive('bar')->ordered();
734
        $this->mock->foo();
735
        $this->mock->bar();
736
        $this->container->mockery_verify();
737
    }
738
739
    /**
740
     * @expectedException \Mockery\Exception
741
     */
742
    public function testOrderingOfDefaultGroupingThrowsExceptionOnWrongOrder()
743
    {
744
        $this->mock->shouldReceive('foo')->ordered();
745
        $this->mock->shouldReceive('bar')->ordered();
746
        $this->mock->bar();
747
        $this->mock->foo();
748
        $this->container->mockery_verify();
749
    }
750
751
    public function testOrderingUsingNumberedGroups()
752
    {
753
        $this->mock->shouldReceive('start')->ordered(1);
754
        $this->mock->shouldReceive('foo')->ordered(2);
755
        $this->mock->shouldReceive('bar')->ordered(2);
756
        $this->mock->shouldReceive('final')->ordered();
757
        $this->mock->start();
758
        $this->mock->bar();
759
        $this->mock->foo();
760
        $this->mock->bar();
761
        $this->mock->final();
762
        $this->container->mockery_verify();
763
    }
764
765
    public function testOrderingUsingNamedGroups()
766
    {
767
        $this->mock->shouldReceive('start')->ordered('start');
768
        $this->mock->shouldReceive('foo')->ordered('foobar');
769
        $this->mock->shouldReceive('bar')->ordered('foobar');
770
        $this->mock->shouldReceive('final')->ordered();
771
        $this->mock->start();
772
        $this->mock->bar();
773
        $this->mock->foo();
774
        $this->mock->bar();
775
        $this->mock->final();
776
        $this->container->mockery_verify();
777
    }
778
779
    /**
780
     * @group 2A
781
     */
782
    public function testGroupedUngroupedOrderingDoNotOverlap()
783
    {
784
        $s = $this->mock->shouldReceive('start')->ordered();
785
        $m = $this->mock->shouldReceive('mid')->ordered('foobar');
786
        $e = $this->mock->shouldReceive('end')->ordered();
787
        $this->assertTrue($s->getOrderNumber() < $m->getOrderNumber());
788
        $this->assertTrue($m->getOrderNumber() < $e->getOrderNumber());
789
    }
790
791
    /**
792
     * @expectedException \Mockery\Exception
793
     */
794
    public function testGroupedOrderingThrowsExceptionWhenCallsDisordered()
795
    {
796
        $this->mock->shouldReceive('foo')->ordered('first');
797
        $this->mock->shouldReceive('bar')->ordered('second');
798
        $this->mock->bar();
799
        $this->mock->foo();
800
        $this->container->mockery_verify();
801
    }
802
803
    public function testExpectationMatchingWithNoArgsOrderings()
804
    {
805
        $this->mock->shouldReceive('foo')->withNoArgs()->once()->ordered();
806
        $this->mock->shouldReceive('bar')->withNoArgs()->once()->ordered();
807
        $this->mock->shouldReceive('foo')->withNoArgs()->once()->ordered();
808
        $this->mock->foo();
809
        $this->mock->bar();
810
        $this->mock->foo();
811
        $this->container->mockery_verify();
812
    }
813
814
    public function testExpectationMatchingWithAnyArgsOrderings()
815
    {
816
        $this->mock->shouldReceive('foo')->withAnyArgs()->once()->ordered();
817
        $this->mock->shouldReceive('bar')->withAnyArgs()->once()->ordered();
818
        $this->mock->shouldReceive('foo')->withAnyArgs()->once()->ordered();
819
        $this->mock->foo();
820
        $this->mock->bar();
821
        $this->mock->foo();
822
        $this->container->mockery_verify();
823
    }
824
825
    public function testEnsuresOrderingIsNotCrossMockByDefault()
826
    {
827
        $this->mock->shouldReceive('foo')->ordered();
828
        $mock2 = $this->container->mock('bar');
829
        $mock2->shouldReceive('bar')->ordered();
830
        $mock2->bar();
831
        $this->mock->foo();
832
    }
833
834
    /**
835
     * @expectedException \Mockery\Exception
836
     */
837
    public function testEnsuresOrderingIsCrossMockWhenGloballyFlagSet()
838
    {
839
        $this->mock->shouldReceive('foo')->globally()->ordered();
840
        $mock2 = $this->container->mock('bar');
841
        $mock2->shouldReceive('bar')->globally()->ordered();
842
        $mock2->bar();
843
        $this->mock->foo();
844
    }
845
846
    public function testExpectationCastToStringFormatting()
847
    {
848
        $exp = $this->mock->shouldReceive('foo')->with(1, 'bar', new stdClass, array('Spam' => 'Ham', 'Bar' => 'Baz'));
849
        $this->assertEquals('[foo(1, "bar", object(stdClass), array(\'Spam\'=>\'Ham\',\'Bar\'=>\'Baz\',))]', (string) $exp);
850
    }
851
852
    public function testLongExpectationCastToStringFormatting()
853
    {
854
        $exp = $this->mock->shouldReceive('foo')->with(array('Spam' => 'Ham', 'Bar' => 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'Bar', 'Baz', 'End'));
855
        $this->assertEquals("[foo(array('Spam'=>'Ham','Bar'=>'Baz',0=>'Bar',1=>'Baz',2=>'Bar',3=>'Baz',4=>'Bar',5=>'Baz',6=>'Bar',7=>'Baz',8=>'Bar',9=>'Baz',10=>'Bar',11=>'Baz',12=>'Bar',13=>'Baz',14=>'Bar',15=>'Baz',16=>'Bar',17=>'Baz',18=>'Bar',19=>'Baz',20=>'Bar',21=>'Baz',22=>'Bar',23=>'Baz',24=>'Bar',25=>'Baz',26=>'Bar',27=>'Baz',28=>'Bar',29=>'Baz',30=>'Bar',31=>'Baz',32=>'Bar',33=>'Baz',34=>'Bar',35=>'Baz',36=>'Bar',37=>'Baz',38=>'Bar',39=>'Baz',40=>'Bar',41=>'Baz',42=>'Bar',43=>'Baz',44=>'Bar',45=>'Baz',46=>'Baz',47=>'Bar',48=>'Baz',49=>'Bar',50=>'Baz',51=>'Bar',52=>'Baz',53=>'Bar',54=>'Baz',55=>'Bar',56=>'Baz',57=>'Baz',58=>'Bar',59=>'Baz',60=>'Bar',61=>'Baz',62=>'Bar',63=>'Baz',64=>'Bar',65=>'Baz',66=>'Bar',67=>'Baz',68=>'Baz',69=>'Bar',70=>'Baz',71=>'Bar',72=>'Baz',73=>'Bar',74=>'Baz',75=>'Bar',76=>'Baz',77=>'Bar',78=>'Baz',79=>'Baz',80=>'Bar',81=>'Baz',82=>'Bar',83=>'Baz',84=>'Bar',85=>'Baz',86=>'Bar',87=>'Baz',88=>'Bar',89=>'Baz',90=>'Baz',91=>'Bar',92=>'Baz',93=>'Bar',94=>'Baz',95=>'Bar',96=>'Baz',97=>'Ba...))]", (string) $exp);
856
    }
857
858
    public function testMultipleExpectationCastToStringFormatting()
859
    {
860
        $exp = $this->mock->shouldReceive('foo', 'bar')->with(1);
861
        $this->assertEquals('[foo(1), bar(1)]', (string) $exp);
862
    }
863
864
    public function testGroupedOrderingWithLimitsAllowsMultipleReturnValues()
865
    {
866
        $this->mock->shouldReceive('foo')->with(2)->once()->andReturn('first');
867
        $this->mock->shouldReceive('foo')->with(2)->twice()->andReturn('second/third');
868
        $this->mock->shouldReceive('foo')->with(2)->andReturn('infinity');
869
        $this->assertEquals('first', $this->mock->foo(2));
870
        $this->assertEquals('second/third', $this->mock->foo(2));
871
        $this->assertEquals('second/third', $this->mock->foo(2));
872
        $this->assertEquals('infinity', $this->mock->foo(2));
873
        $this->assertEquals('infinity', $this->mock->foo(2));
874
        $this->assertEquals('infinity', $this->mock->foo(2));
875
        $this->container->mockery_verify();
876
    }
877
878
    public function testExpectationsCanBeMarkedAsDefaults()
879
    {
880
        $this->mock->shouldReceive('foo')->andReturn('bar')->byDefault();
881
        $this->assertEquals('bar', $this->mock->foo());
882
        $this->container->mockery_verify();
883
    }
884
885
    public function testDefaultExpectationsValidatedInCorrectOrder()
886
    {
887
        $this->mock->shouldReceive('foo')->with(1)->once()->andReturn('first')->byDefault();
888
        $this->mock->shouldReceive('foo')->with(2)->once()->andReturn('second')->byDefault();
889
        $this->assertEquals('first', $this->mock->foo(1));
890
        $this->assertEquals('second', $this->mock->foo(2));
891
        $this->container->mockery_verify();
892
    }
893
894
    public function testDefaultExpectationsAreReplacedByLaterConcreteExpectations()
895
    {
896
        $this->mock->shouldReceive('foo')->andReturn('bar')->once()->byDefault();
897
        $this->mock->shouldReceive('foo')->andReturn('bar')->twice();
898
        $this->mock->foo();
899
        $this->mock->foo();
900
        $this->container->mockery_verify();
901
    }
902
903
    public function testDefaultExpectationsCanBeChangedByLaterExpectations()
904
    {
905
        $this->mock->shouldReceive('foo')->with(1)->andReturn('bar')->once()->byDefault();
906
        $this->mock->shouldReceive('foo')->with(2)->andReturn('baz')->once();
907
        try {
908
            $this->mock->foo(1);
909
            $this->fail('Expected exception not thrown');
910
        } catch (\Mockery\Exception $e) {
911
        }
912
        $this->mock->foo(2);
913
        $this->container->mockery_verify();
914
    }
915
916
    /**
917
     * @expectedException \Mockery\Exception
918
     */
919
    public function testDefaultExpectationsCanBeOrdered()
920
    {
921
        $this->mock->shouldReceive('foo')->ordered()->byDefault();
922
        $this->mock->shouldReceive('bar')->ordered()->byDefault();
923
        $this->mock->bar();
924
        $this->mock->foo();
925
        $this->container->mockery_verify();
926
    }
927
928
    public function testDefaultExpectationsCanBeOrderedAndReplaced()
929
    {
930
        $this->mock->shouldReceive('foo')->ordered()->byDefault();
931
        $this->mock->shouldReceive('bar')->ordered()->byDefault();
932
        $this->mock->shouldReceive('bar')->ordered();
933
        $this->mock->shouldReceive('foo')->ordered();
934
        $this->mock->bar();
935
        $this->mock->foo();
936
        $this->container->mockery_verify();
937
    }
938
939
    public function testByDefaultOperatesFromMockConstruction()
940
    {
941
        $container = new \Mockery\Container(\Mockery::getDefaultGenerator(), \Mockery::getDefaultLoader());
942
        $mock = $container->mock('f', array('foo'=>'rfoo', 'bar'=>'rbar', 'baz'=>'rbaz'))->byDefault();
943
        $mock->shouldReceive('foo')->andReturn('foobar');
944
        $this->assertEquals('foobar', $mock->foo());
945
        $this->assertEquals('rbar', $mock->bar());
946
        $this->assertEquals('rbaz', $mock->baz());
947
        $mock->mockery_verify();
948
    }
949
950
    public function testByDefaultOnAMockDoesSquatWithoutExpectations()
951
    {
952
        $container = new \Mockery\Container(\Mockery::getDefaultGenerator(), \Mockery::getDefaultLoader());
953
        $mock = $container->mock('f')->byDefault();
954
    }
955
956
    public function testDefaultExpectationsCanBeOverridden()
957
    {
958
        $this->mock->shouldReceive('foo')->with('test')->andReturn('bar')->byDefault();
959
        $this->mock->shouldReceive('foo')->with('test')->andReturn('newbar')->byDefault();
960
        $this->mock->foo('test');
961
        $this->assertEquals('newbar', $this->mock->foo('test'));
962
    }
963
964
    /**
965
     * @expectedException \Mockery\Exception
966
     */
967
    public function testByDefaultPreventedFromSettingDefaultWhenDefaultingExpectationWasReplaced()
968
    {
969
        $exp = $this->mock->shouldReceive('foo')->andReturn(1);
970
        $this->mock->shouldReceive('foo')->andReturn(2);
971
        $exp->byDefault();
972
    }
973
974
    /**
975
     * Argument Constraint Tests
976
     */
977
978
    public function testAnyConstraintMatchesAnyArg()
979
    {
980
        $this->mock->shouldReceive('foo')->with(1, Mockery::any())->twice();
981
        $this->mock->foo(1, 2);
982
        $this->mock->foo(1, 'str');
983
        $this->container->mockery_verify();
984
    }
985
986
    public function testAnyConstraintNonMatchingCase()
987
    {
988
        $this->mock->shouldReceive('foo')->times(3);
989
        $this->mock->shouldReceive('foo')->with(1, Mockery::any())->never();
990
        $this->mock->foo();
991
        $this->mock->foo(1);
992
        $this->mock->foo(1, 2, 3);
993
        $this->container->mockery_verify();
994
    }
995
996
    public function testArrayConstraintMatchesArgument()
997
    {
998
        $this->mock->shouldReceive('foo')->with(Mockery::type('array'))->once();
999
        $this->mock->foo(array());
1000
        $this->container->mockery_verify();
1001
    }
1002
1003
    public function testArrayConstraintNonMatchingCase()
1004
    {
1005
        $this->mock->shouldReceive('foo')->times(3);
1006
        $this->mock->shouldReceive('foo')->with(1, Mockery::type('array'))->never();
1007
        $this->mock->foo();
1008
        $this->mock->foo(1);
1009
        $this->mock->foo(1, 2, 3);
1010
        $this->container->mockery_verify();
1011
    }
1012
1013
    /**
1014
     * @expectedException \Mockery\Exception
1015
     */
1016
    public function testArrayConstraintThrowsExceptionWhenConstraintUnmatched()
1017
    {
1018
        $this->mock->shouldReceive('foo')->with(Mockery::type('array'))->once();
1019
        $this->mock->foo(1);
1020
        $this->container->mockery_verify();
1021
    }
1022
1023
    public function testBoolConstraintMatchesArgument()
1024
    {
1025
        $this->mock->shouldReceive('foo')->with(Mockery::type('bool'))->once();
1026
        $this->mock->foo(true);
1027
        $this->container->mockery_verify();
1028
    }
1029
1030
    public function testBoolConstraintNonMatchingCase()
1031
    {
1032
        $this->mock->shouldReceive('foo')->times(3);
1033
        $this->mock->shouldReceive('foo')->with(1, Mockery::type('bool'))->never();
1034
        $this->mock->foo();
1035
        $this->mock->foo(1);
1036
        $this->mock->foo(1, 2, 3);
1037
        $this->container->mockery_verify();
1038
    }
1039
1040
    /**
1041
     * @expectedException \Mockery\Exception
1042
     */
1043
    public function testBoolConstraintThrowsExceptionWhenConstraintUnmatched()
1044
    {
1045
        $this->mock->shouldReceive('foo')->with(Mockery::type('bool'))->once();
1046
        $this->mock->foo(1);
1047
        $this->container->mockery_verify();
1048
    }
1049
1050
    public function testCallableConstraintMatchesArgument()
1051
    {
1052
        $this->mock->shouldReceive('foo')->with(Mockery::type('callable'))->once();
1053
        $this->mock->foo(function () {return 'f';});
1054
        $this->container->mockery_verify();
1055
    }
1056
1057
    public function testCallableConstraintNonMatchingCase()
1058
    {
1059
        $this->mock->shouldReceive('foo')->times(3);
1060
        $this->mock->shouldReceive('foo')->with(1, Mockery::type('callable'))->never();
1061
        $this->mock->foo();
1062
        $this->mock->foo(1);
1063
        $this->mock->foo(1, 2, 3);
1064
        $this->container->mockery_verify();
1065
    }
1066
1067
    /**
1068
     * @expectedException \Mockery\Exception
1069
     */
1070
    public function testCallableConstraintThrowsExceptionWhenConstraintUnmatched()
1071
    {
1072
        $this->mock->shouldReceive('foo')->with(Mockery::type('callable'))->once();
1073
        $this->mock->foo(1);
1074
        $this->container->mockery_verify();
1075
    }
1076
1077
    public function testDoubleConstraintMatchesArgument()
1078
    {
1079
        $this->mock->shouldReceive('foo')->with(Mockery::type('double'))->once();
1080
        $this->mock->foo(2.25);
1081
        $this->container->mockery_verify();
1082
    }
1083
1084
    public function testDoubleConstraintNonMatchingCase()
1085
    {
1086
        $this->mock->shouldReceive('foo')->times(3);
1087
        $this->mock->shouldReceive('foo')->with(1, Mockery::type('double'))->never();
1088
        $this->mock->foo();
1089
        $this->mock->foo(1);
1090
        $this->mock->foo(1, 2, 3);
1091
        $this->container->mockery_verify();
1092
    }
1093
1094
    /**
1095
     * @expectedException \Mockery\Exception
1096
     */
1097
    public function testDoubleConstraintThrowsExceptionWhenConstraintUnmatched()
1098
    {
1099
        $this->mock->shouldReceive('foo')->with(Mockery::type('double'))->once();
1100
        $this->mock->foo(1);
1101
        $this->container->mockery_verify();
1102
    }
1103
1104
    public function testFloatConstraintMatchesArgument()
1105
    {
1106
        $this->mock->shouldReceive('foo')->with(Mockery::type('float'))->once();
1107
        $this->mock->foo(2.25);
1108
        $this->container->mockery_verify();
1109
    }
1110
1111
    public function testFloatConstraintNonMatchingCase()
1112
    {
1113
        $this->mock->shouldReceive('foo')->times(3);
1114
        $this->mock->shouldReceive('foo')->with(1, Mockery::type('float'))->never();
1115
        $this->mock->foo();
1116
        $this->mock->foo(1);
1117
        $this->mock->foo(1, 2, 3);
1118
        $this->container->mockery_verify();
1119
    }
1120
1121
    /**
1122
     * @expectedException \Mockery\Exception
1123
     */
1124
    public function testFloatConstraintThrowsExceptionWhenConstraintUnmatched()
1125
    {
1126
        $this->mock->shouldReceive('foo')->with(Mockery::type('float'))->once();
1127
        $this->mock->foo(1);
1128
        $this->container->mockery_verify();
1129
    }
1130
1131
    public function testIntConstraintMatchesArgument()
1132
    {
1133
        $this->mock->shouldReceive('foo')->with(Mockery::type('int'))->once();
1134
        $this->mock->foo(2);
1135
        $this->container->mockery_verify();
1136
    }
1137
1138
    public function testIntConstraintNonMatchingCase()
1139
    {
1140
        $this->mock->shouldReceive('foo')->times(3);
1141
        $this->mock->shouldReceive('foo')->with(1, Mockery::type('int'))->never();
1142
        $this->mock->foo();
1143
        $this->mock->foo(1);
1144
        $this->mock->foo(1, 2, 3);
1145
        $this->container->mockery_verify();
1146
    }
1147
1148
    /**
1149
     * @expectedException \Mockery\Exception
1150
     */
1151
    public function testIntConstraintThrowsExceptionWhenConstraintUnmatched()
1152
    {
1153
        $this->mock->shouldReceive('foo')->with(Mockery::type('int'))->once();
1154
        $this->mock->foo('f');
1155
        $this->container->mockery_verify();
1156
    }
1157
1158
    public function testLongConstraintMatchesArgument()
1159
    {
1160
        $this->mock->shouldReceive('foo')->with(Mockery::type('long'))->once();
1161
        $this->mock->foo(2);
1162
        $this->container->mockery_verify();
1163
    }
1164
1165
    public function testLongConstraintNonMatchingCase()
1166
    {
1167
        $this->mock->shouldReceive('foo')->times(3);
1168
        $this->mock->shouldReceive('foo')->with(1, Mockery::type('long'))->never();
1169
        $this->mock->foo();
1170
        $this->mock->foo(1);
1171
        $this->mock->foo(1, 2, 3);
1172
        $this->container->mockery_verify();
1173
    }
1174
1175
    /**
1176
     * @expectedException \Mockery\Exception
1177
     */
1178
    public function testLongConstraintThrowsExceptionWhenConstraintUnmatched()
1179
    {
1180
        $this->mock->shouldReceive('foo')->with(Mockery::type('long'))->once();
1181
        $this->mock->foo('f');
1182
        $this->container->mockery_verify();
1183
    }
1184
1185
    public function testNullConstraintMatchesArgument()
1186
    {
1187
        $this->mock->shouldReceive('foo')->with(Mockery::type('null'))->once();
1188
        $this->mock->foo(null);
1189
        $this->container->mockery_verify();
1190
    }
1191
1192
    public function testNullConstraintNonMatchingCase()
1193
    {
1194
        $this->mock->shouldReceive('foo')->times(3);
1195
        $this->mock->shouldReceive('foo')->with(1, Mockery::type('null'))->never();
1196
        $this->mock->foo();
1197
        $this->mock->foo(1);
1198
        $this->mock->foo(1, 2, 3);
1199
        $this->container->mockery_verify();
1200
    }
1201
1202
    /**
1203
     * @expectedException \Mockery\Exception
1204
     */
1205
    public function testNullConstraintThrowsExceptionWhenConstraintUnmatched()
1206
    {
1207
        $this->mock->shouldReceive('foo')->with(Mockery::type('null'))->once();
1208
        $this->mock->foo('f');
1209
        $this->container->mockery_verify();
1210
    }
1211
1212
    public function testNumericConstraintMatchesArgument()
1213
    {
1214
        $this->mock->shouldReceive('foo')->with(Mockery::type('numeric'))->once();
1215
        $this->mock->foo('2');
1216
        $this->container->mockery_verify();
1217
    }
1218
1219
    public function testNumericConstraintNonMatchingCase()
1220
    {
1221
        $this->mock->shouldReceive('foo')->times(3);
1222
        $this->mock->shouldReceive('foo')->with(1, Mockery::type('numeric'))->never();
1223
        $this->mock->foo();
1224
        $this->mock->foo(1);
1225
        $this->mock->foo(1, 2, 3);
1226
        $this->container->mockery_verify();
1227
    }
1228
1229
    /**
1230
     * @expectedException \Mockery\Exception
1231
     */
1232
    public function testNumericConstraintThrowsExceptionWhenConstraintUnmatched()
1233
    {
1234
        $this->mock->shouldReceive('foo')->with(Mockery::type('numeric'))->once();
1235
        $this->mock->foo('f');
1236
        $this->container->mockery_verify();
1237
    }
1238
1239
    public function testObjectConstraintMatchesArgument()
1240
    {
1241
        $this->mock->shouldReceive('foo')->with(Mockery::type('object'))->once();
1242
        $this->mock->foo(new stdClass);
1243
        $this->container->mockery_verify();
1244
    }
1245
1246
    public function testObjectConstraintNonMatchingCase()
1247
    {
1248
        $this->mock->shouldReceive('foo')->times(3);
1249
        $this->mock->shouldReceive('foo')->with(1, Mockery::type('object`'))->never();
1250
        $this->mock->foo();
1251
        $this->mock->foo(1);
1252
        $this->mock->foo(1, 2, 3);
1253
        $this->container->mockery_verify();
1254
    }
1255
1256
    /**
1257
     * @expectedException \Mockery\Exception
1258
     */
1259
    public function testObjectConstraintThrowsExceptionWhenConstraintUnmatched()
1260
    {
1261
        $this->mock->shouldReceive('foo')->with(Mockery::type('object'))->once();
1262
        $this->mock->foo('f');
1263
        $this->container->mockery_verify();
1264
    }
1265
1266
    public function testRealConstraintMatchesArgument()
1267
    {
1268
        $this->mock->shouldReceive('foo')->with(Mockery::type('real'))->once();
1269
        $this->mock->foo(2.25);
1270
        $this->container->mockery_verify();
1271
    }
1272
1273
    public function testRealConstraintNonMatchingCase()
1274
    {
1275
        $this->mock->shouldReceive('foo')->times(3);
1276
        $this->mock->shouldReceive('foo')->with(1, Mockery::type('real'))->never();
1277
        $this->mock->foo();
1278
        $this->mock->foo(1);
1279
        $this->mock->foo(1, 2, 3);
1280
        $this->container->mockery_verify();
1281
    }
1282
1283
    /**
1284
     * @expectedException \Mockery\Exception
1285
     */
1286
    public function testRealConstraintThrowsExceptionWhenConstraintUnmatched()
1287
    {
1288
        $this->mock->shouldReceive('foo')->with(Mockery::type('real'))->once();
1289
        $this->mock->foo('f');
1290
        $this->container->mockery_verify();
1291
    }
1292
1293
    public function testResourceConstraintMatchesArgument()
1294
    {
1295
        $this->mock->shouldReceive('foo')->with(Mockery::type('resource'))->once();
1296
        $r = fopen(dirname(__FILE__) . '/_files/file.txt', 'r');
1297
        $this->mock->foo($r);
1298
        $this->container->mockery_verify();
1299
    }
1300
1301
    public function testResourceConstraintNonMatchingCase()
1302
    {
1303
        $this->mock->shouldReceive('foo')->times(3);
1304
        $this->mock->shouldReceive('foo')->with(1, Mockery::type('resource'))->never();
1305
        $this->mock->foo();
1306
        $this->mock->foo(1);
1307
        $this->mock->foo(1, 2, 3);
1308
        $this->container->mockery_verify();
1309
    }
1310
1311
    /**
1312
     * @expectedException \Mockery\Exception
1313
     */
1314
    public function testResourceConstraintThrowsExceptionWhenConstraintUnmatched()
1315
    {
1316
        $this->mock->shouldReceive('foo')->with(Mockery::type('resource'))->once();
1317
        $this->mock->foo('f');
1318
        $this->container->mockery_verify();
1319
    }
1320
1321
    public function testScalarConstraintMatchesArgument()
1322
    {
1323
        $this->mock->shouldReceive('foo')->with(Mockery::type('scalar'))->once();
1324
        $this->mock->foo(2);
1325
        $this->container->mockery_verify();
1326
    }
1327
1328
    public function testScalarConstraintNonMatchingCase()
1329
    {
1330
        $this->mock->shouldReceive('foo')->times(3);
1331
        $this->mock->shouldReceive('foo')->with(1, Mockery::type('scalar'))->never();
1332
        $this->mock->foo();
1333
        $this->mock->foo(1);
1334
        $this->mock->foo(1, 2, 3);
1335
        $this->container->mockery_verify();
1336
    }
1337
1338
    /**
1339
     * @expectedException \Mockery\Exception
1340
     */
1341
    public function testScalarConstraintThrowsExceptionWhenConstraintUnmatched()
1342
    {
1343
        $this->mock->shouldReceive('foo')->with(Mockery::type('scalar'))->once();
1344
        $this->mock->foo(array());
1345
        $this->container->mockery_verify();
1346
    }
1347
1348
    public function testStringConstraintMatchesArgument()
1349
    {
1350
        $this->mock->shouldReceive('foo')->with(Mockery::type('string'))->once();
1351
        $this->mock->foo('2');
1352
        $this->container->mockery_verify();
1353
    }
1354
1355
    public function testStringConstraintNonMatchingCase()
1356
    {
1357
        $this->mock->shouldReceive('foo')->times(3);
1358
        $this->mock->shouldReceive('foo')->with(1, Mockery::type('string'))->never();
1359
        $this->mock->foo();
1360
        $this->mock->foo(1);
1361
        $this->mock->foo(1, 2, 3);
1362
        $this->container->mockery_verify();
1363
    }
1364
1365
    /**
1366
     * @expectedException \Mockery\Exception
1367
     */
1368
    public function testStringConstraintThrowsExceptionWhenConstraintUnmatched()
1369
    {
1370
        $this->mock->shouldReceive('foo')->with(Mockery::type('string'))->once();
1371
        $this->mock->foo(1);
1372
        $this->container->mockery_verify();
1373
    }
1374
1375
    public function testClassConstraintMatchesArgument()
1376
    {
1377
        $this->mock->shouldReceive('foo')->with(Mockery::type('stdClass'))->once();
1378
        $this->mock->foo(new stdClass);
1379
        $this->container->mockery_verify();
1380
    }
1381
1382
    public function testClassConstraintNonMatchingCase()
1383
    {
1384
        $this->mock->shouldReceive('foo')->times(3);
1385
        $this->mock->shouldReceive('foo')->with(1, Mockery::type('stdClass'))->never();
1386
        $this->mock->foo();
1387
        $this->mock->foo(1);
1388
        $this->mock->foo(1, 2, 3);
1389
        $this->container->mockery_verify();
1390
    }
1391
1392
    /**
1393
     * @expectedException \Mockery\Exception
1394
     */
1395
    public function testClassConstraintThrowsExceptionWhenConstraintUnmatched()
1396
    {
1397
        $this->mock->shouldReceive('foo')->with(Mockery::type('stdClass'))->once();
1398
        $this->mock->foo(new Exception);
1399
        $this->container->mockery_verify();
1400
    }
1401
1402
    public function testDucktypeConstraintMatchesArgument()
1403
    {
1404
        $this->mock->shouldReceive('foo')->with(Mockery::ducktype('quack', 'swim'))->once();
1405
        $this->mock->foo(new Mockery_Duck);
1406
        $this->container->mockery_verify();
1407
    }
1408
1409
    public function testDucktypeConstraintNonMatchingCase()
1410
    {
1411
        $this->mock->shouldReceive('foo')->times(3);
1412
        $this->mock->shouldReceive('foo')->with(1, Mockery::ducktype('quack', 'swim'))->never();
1413
        $this->mock->foo();
1414
        $this->mock->foo(1);
1415
        $this->mock->foo(1, 2, 3);
1416
        $this->container->mockery_verify();
1417
    }
1418
1419
    /**
1420
     * @expectedException \Mockery\Exception
1421
     */
1422
    public function testDucktypeConstraintThrowsExceptionWhenConstraintUnmatched()
1423
    {
1424
        $this->mock->shouldReceive('foo')->with(Mockery::ducktype('quack', 'swim'))->once();
1425
        $this->mock->foo(new Mockery_Duck_Nonswimmer);
1426
        $this->container->mockery_verify();
1427
    }
1428
1429
    public function testArrayContentConstraintMatchesArgument()
1430
    {
1431
        $this->mock->shouldReceive('foo')->with(Mockery::subset(array('a'=>1, 'b'=>2)))->once();
1432
        $this->mock->foo(array('a'=>1, 'b'=>2, 'c'=>3));
1433
        $this->container->mockery_verify();
1434
    }
1435
1436
    public function testArrayContentConstraintNonMatchingCase()
1437
    {
1438
        $this->mock->shouldReceive('foo')->times(3);
1439
        $this->mock->shouldReceive('foo')->with(1, Mockery::subset(array('a'=>1, 'b'=>2)))->never();
1440
        $this->mock->foo();
1441
        $this->mock->foo(1);
1442
        $this->mock->foo(1, 2, 3);
1443
        $this->container->mockery_verify();
1444
    }
1445
1446
    /**
1447
     * @expectedException \Mockery\Exception
1448
     */
1449
    public function testArrayContentConstraintThrowsExceptionWhenConstraintUnmatched()
1450
    {
1451
        $this->mock->shouldReceive('foo')->with(Mockery::subset(array('a'=>1, 'b'=>2)))->once();
1452
        $this->mock->foo(array('a'=>1, 'c'=>3));
1453
        $this->container->mockery_verify();
1454
    }
1455
1456
    public function testContainsConstraintMatchesArgument()
1457
    {
1458
        $this->mock->shouldReceive('foo')->with(Mockery::contains(1, 2))->once();
1459
        $this->mock->foo(array('a'=>1, 'b'=>2, 'c'=>3));
1460
        $this->container->mockery_verify();
1461
    }
1462
1463
    public function testContainsConstraintNonMatchingCase()
1464
    {
1465
        $this->mock->shouldReceive('foo')->times(3);
1466
        $this->mock->shouldReceive('foo')->with(1, Mockery::contains(1, 2))->never();
1467
        $this->mock->foo();
1468
        $this->mock->foo(1);
1469
        $this->mock->foo(1, 2, 3);
1470
        $this->container->mockery_verify();
1471
    }
1472
1473
    /**
1474
     * @expectedException \Mockery\Exception
1475
     */
1476
    public function testContainsConstraintThrowsExceptionWhenConstraintUnmatched()
1477
    {
1478
        $this->mock->shouldReceive('foo')->with(Mockery::contains(1, 2))->once();
1479
        $this->mock->foo(array('a'=>1, 'c'=>3));
1480
        $this->container->mockery_verify();
1481
    }
1482
1483
    public function testHasKeyConstraintMatchesArgument()
1484
    {
1485
        $this->mock->shouldReceive('foo')->with(Mockery::hasKey('c'))->once();
1486
        $this->mock->foo(array('a'=>1, 'b'=>2, 'c'=>3));
1487
        $this->container->mockery_verify();
1488
    }
1489
1490
    public function testHasKeyConstraintNonMatchingCase()
1491
    {
1492
        $this->mock->shouldReceive('foo')->times(3);
1493
        $this->mock->shouldReceive('foo')->with(1, Mockery::hasKey('a'))->never();
1494
        $this->mock->foo();
1495
        $this->mock->foo(1);
1496
        $this->mock->foo(1, array('a'=>1), 3);
1497
        $this->container->mockery_verify();
1498
    }
1499
1500
    /**
1501
     * @expectedException \Mockery\Exception
1502
     */
1503
    public function testHasKeyConstraintThrowsExceptionWhenConstraintUnmatched()
1504
    {
1505
        $this->mock->shouldReceive('foo')->with(Mockery::hasKey('c'))->once();
1506
        $this->mock->foo(array('a'=>1, 'b'=>3));
1507
        $this->container->mockery_verify();
1508
    }
1509
1510
    public function testHasValueConstraintMatchesArgument()
1511
    {
1512
        $this->mock->shouldReceive('foo')->with(Mockery::hasValue(1))->once();
1513
        $this->mock->foo(array('a'=>1, 'b'=>2, 'c'=>3));
1514
        $this->container->mockery_verify();
1515
    }
1516
1517
    public function testHasValueConstraintNonMatchingCase()
1518
    {
1519
        $this->mock->shouldReceive('foo')->times(3);
1520
        $this->mock->shouldReceive('foo')->with(1, Mockery::hasValue(1))->never();
1521
        $this->mock->foo();
1522
        $this->mock->foo(1);
1523
        $this->mock->foo(1, array('a'=>1), 3);
1524
        $this->container->mockery_verify();
1525
    }
1526
1527
    /**
1528
     * @expectedException \Mockery\Exception
1529
     */
1530
    public function testHasValueConstraintThrowsExceptionWhenConstraintUnmatched()
1531
    {
1532
        $this->mock->shouldReceive('foo')->with(Mockery::hasValue(2))->once();
1533
        $this->mock->foo(array('a'=>1, 'b'=>3));
1534
        $this->container->mockery_verify();
1535
    }
1536
1537
    public function testOnConstraintMatchesArgument_ClosureEvaluatesToTrue()
1538
    {
1539
        $function = function ($arg) {return $arg % 2 == 0;};
1540
        $this->mock->shouldReceive('foo')->with(Mockery::on($function))->once();
1541
        $this->mock->foo(4);
1542
        $this->container->mockery_verify();
1543
    }
1544
1545
    /**
1546
     * @expectedException \Mockery\Exception
1547
     */
1548
    public function testOnConstraintThrowsExceptionWhenConstraintUnmatched_ClosureEvaluatesToFalse()
1549
    {
1550
        $function = function ($arg) {return $arg % 2 == 0;};
1551
        $this->mock->shouldReceive('foo')->with(Mockery::on($function))->once();
1552
        $this->mock->foo(5);
1553
        $this->container->mockery_verify();
1554
    }
1555
1556
    public function testMustBeConstraintMatchesArgument()
1557
    {
1558
        $this->mock->shouldReceive('foo')->with(Mockery::mustBe(2))->once();
1559
        $this->mock->foo(2);
1560
        $this->container->mockery_verify();
1561
    }
1562
1563
    public function testMustBeConstraintNonMatchingCase()
1564
    {
1565
        $this->mock->shouldReceive('foo')->times(3);
1566
        $this->mock->shouldReceive('foo')->with(1, Mockery::mustBe(2))->never();
1567
        $this->mock->foo();
1568
        $this->mock->foo(1);
1569
        $this->mock->foo(1, 2, 3);
1570
        $this->container->mockery_verify();
1571
    }
1572
1573
    /**
1574
     * @expectedException \Mockery\Exception
1575
     */
1576
    public function testMustBeConstraintThrowsExceptionWhenConstraintUnmatched()
1577
    {
1578
        $this->mock->shouldReceive('foo')->with(Mockery::mustBe(2))->once();
1579
        $this->mock->foo('2');
1580
        $this->container->mockery_verify();
1581
    }
1582
1583
    public function testMustBeConstraintMatchesObjectArgumentWithEqualsComparisonNotIdentical()
1584
    {
1585
        $a = new stdClass;
1586
        $a->foo = 1;
1587
        $b = new stdClass;
1588
        $b->foo = 1;
1589
        $this->mock->shouldReceive('foo')->with(Mockery::mustBe($a))->once();
1590
        $this->mock->foo($b);
1591
        $this->container->mockery_verify();
1592
    }
1593
1594
    public function testMustBeConstraintNonMatchingCaseWithObject()
1595
    {
1596
        $a = new stdClass;
1597
        $a->foo = 1;
1598
        $this->mock->shouldReceive('foo')->times(3);
1599
        $this->mock->shouldReceive('foo')->with(1, Mockery::mustBe($a))->never();
1600
        $this->mock->foo();
1601
        $this->mock->foo(1);
1602
        $this->mock->foo(1, $a, 3);
1603
        $this->container->mockery_verify();
1604
    }
1605
1606
    /**
1607
     * @expectedException \Mockery\Exception
1608
     */
1609
    public function testMustBeConstraintThrowsExceptionWhenConstraintUnmatchedWithObject()
1610
    {
1611
        $a = new stdClass;
1612
        $a->foo = 1;
1613
        $b = new stdClass;
1614
        $b->foo = 2;
1615
        $this->mock->shouldReceive('foo')->with(Mockery::mustBe($a))->once();
1616
        $this->mock->foo($b);
1617
        $this->container->mockery_verify();
1618
    }
1619
1620
    public function testMatchPrecedenceBasedOnExpectedCallsFavouringExplicitMatch()
1621
    {
1622
        $this->mock->shouldReceive('foo')->with(1)->once();
1623
        $this->mock->shouldReceive('foo')->with(Mockery::any())->never();
1624
        $this->mock->foo(1);
1625
        $this->container->mockery_verify();
1626
    }
1627
1628
    public function testMatchPrecedenceBasedOnExpectedCallsFavouringAnyMatch()
1629
    {
1630
        $this->mock->shouldReceive('foo')->with(Mockery::any())->once();
1631
        $this->mock->shouldReceive('foo')->with(1)->never();
1632
        $this->mock->foo(1);
1633
        $this->container->mockery_verify();
1634
    }
1635
1636
    public function testReturnNullIfIgnoreMissingMethodsSet()
1637
    {
1638
        $this->mock->shouldIgnoreMissing();
1639
        $this->assertNull($this->mock->g(1, 2));
1640
    }
1641
1642
    public function testReturnUndefinedIfIgnoreMissingMethodsSet()
1643
    {
1644
        $this->mock->shouldIgnoreMissing()->asUndefined();
1645
        $this->assertTrue($this->mock->g(1, 2) instanceof \Mockery\Undefined);
1646
    }
1647
1648
    public function testReturnAsUndefinedAllowsForInfiniteSelfReturningChain()
1649
    {
1650
        $this->mock->shouldIgnoreMissing()->asUndefined();
1651
        $this->assertTrue($this->mock->g(1, 2)->a()->b()->c() instanceof \Mockery\Undefined);
1652
    }
1653
1654
    public function testShouldIgnoreMissingFluentInterface()
1655
    {
1656
        $this->assertTrue($this->mock->shouldIgnoreMissing() instanceof \Mockery\MockInterface);
1657
    }
1658
1659
    public function testShouldIgnoreMissingAsUndefinedFluentInterface()
1660
    {
1661
        $this->assertTrue($this->mock->shouldIgnoreMissing()->asUndefined() instanceof \Mockery\MockInterface);
1662
    }
1663
1664
    public function testShouldIgnoreMissingAsDefinedProxiesToUndefinedAllowingToString()
1665
    {
1666
        $this->mock->shouldIgnoreMissing()->asUndefined();
1667
        $string = "Method call: {$this->mock->g()}";
1668
        $string = "Mock: {$this->mock}";
1669
    }
1670
1671
    public function testShouldIgnoreMissingDefaultReturnValue()
1672
    {
1673
        $this->mock->shouldIgnoreMissing(1);
1674
        $this->assertEquals(1, $this->mock->a());
1675
    }
1676
1677
    /** @issue #253 */
1678
    public function testShouldIgnoreMissingDefaultSelfAndReturnsSelf()
1679
    {
1680
        $this->mock->shouldIgnoreMissing($this->container->self());
1681
        $this->assertSame($this->mock, $this->mock->a()->b());
1682
    }
1683
1684
    public function testToStringMagicMethodCanBeMocked()
1685
    {
1686
        $this->mock->shouldReceive("__toString")->andReturn('dave');
1687
        $this->assertEquals("{$this->mock}", "dave");
1688
    }
1689
1690
    public function testOptionalMockRetrieval()
1691
    {
1692
        $m = $this->container->mock('f')->shouldReceive('foo')->with(1)->andReturn(3)->mock();
1693
        $this->assertTrue($m instanceof \Mockery\MockInterface);
1694
    }
1695
1696
    public function testNotConstraintMatchesArgument()
1697
    {
1698
        $this->mock->shouldReceive('foo')->with(Mockery::not(1))->once();
1699
        $this->mock->foo(2);
1700
        $this->container->mockery_verify();
1701
    }
1702
1703
    public function testNotConstraintNonMatchingCase()
1704
    {
1705
        $this->mock->shouldReceive('foo')->times(3);
1706
        $this->mock->shouldReceive('foo')->with(1, Mockery::not(2))->never();
1707
        $this->mock->foo();
1708
        $this->mock->foo(1);
1709
        $this->mock->foo(1, 2, 3);
1710
        $this->container->mockery_verify();
1711
    }
1712
1713
    /**
1714
     * @expectedException \Mockery\Exception
1715
     */
1716
    public function testNotConstraintThrowsExceptionWhenConstraintUnmatched()
1717
    {
1718
        $this->mock->shouldReceive('foo')->with(Mockery::not(2))->once();
1719
        $this->mock->foo(2);
1720
        $this->container->mockery_verify();
1721
    }
1722
1723
    public function testAnyOfConstraintMatchesArgument()
1724
    {
1725
        $this->mock->shouldReceive('foo')->with(Mockery::anyOf(1, 2))->twice();
1726
        $this->mock->foo(2);
1727
        $this->mock->foo(1);
1728
        $this->container->mockery_verify();
1729
    }
1730
1731
    public function testAnyOfConstraintNonMatchingCase()
1732
    {
1733
        $this->mock->shouldReceive('foo')->times(3);
1734
        $this->mock->shouldReceive('foo')->with(1, Mockery::anyOf(1, 2))->never();
1735
        $this->mock->foo();
1736
        $this->mock->foo(1);
1737
        $this->mock->foo(1, 2, 3);
1738
        $this->container->mockery_verify();
1739
    }
1740
1741
    /**
1742
     * @expectedException \Mockery\Exception
1743
     */
1744
    public function testAnyOfConstraintThrowsExceptionWhenConstraintUnmatched()
1745
    {
1746
        $this->mock->shouldReceive('foo')->with(Mockery::anyOf(1, 2))->once();
1747
        $this->mock->foo(3);
1748
        $this->container->mockery_verify();
1749
    }
1750
1751
    public function testNotAnyOfConstraintMatchesArgument()
1752
    {
1753
        $this->mock->shouldReceive('foo')->with(Mockery::notAnyOf(1, 2))->once();
1754
        $this->mock->foo(3);
1755
        $this->container->mockery_verify();
1756
    }
1757
1758
    public function testNotAnyOfConstraintNonMatchingCase()
1759
    {
1760
        $this->mock->shouldReceive('foo')->times(3);
1761
        $this->mock->shouldReceive('foo')->with(1, Mockery::notAnyOf(1, 2))->never();
1762
        $this->mock->foo();
1763
        $this->mock->foo(1);
1764
        $this->mock->foo(1, 4, 3);
1765
        $this->container->mockery_verify();
1766
    }
1767
1768
    /**
1769
     * @expectedException \Mockery\Exception
1770
     */
1771
    public function testNotAnyOfConstraintThrowsExceptionWhenConstraintUnmatched()
1772
    {
1773
        $this->mock->shouldReceive('foo')->with(Mockery::notAnyOf(1, 2))->once();
1774
        $this->mock->foo(2);
1775
        $this->container->mockery_verify();
1776
    }
1777
1778
    /**
1779
     * @expectedException \Mockery\Exception
1780
     */
1781
    public function testGlobalConfigMayForbidMockingNonExistentMethodsOnClasses()
1782
    {
1783
        \Mockery::getConfiguration()->allowMockingNonExistentMethods(false);
1784
        $mock = $this->container->mock('stdClass');
1785
        $mock->shouldReceive('foo');
1786
    }
1787
1788
    /**
1789
     * @expectedException \Mockery\Exception
1790
     * @expectedExceptionMessage Mockery's configuration currently forbids mocking
1791
     */
1792
    public function testGlobalConfigMayForbidMockingNonExistentMethodsOnAutoDeclaredClasses()
1793
    {
1794
        \Mockery::getConfiguration()->allowMockingNonExistentMethods(false);
1795
        $mock = $this->container->mock('SomeMadeUpClass');
1796
        $mock->shouldReceive('foo');
1797
    }
1798
1799
    /**
1800
     * @expectedException \Mockery\Exception
1801
     */
1802
    public function testGlobalConfigMayForbidMockingNonExistentMethodsOnObjects()
1803
    {
1804
        \Mockery::getConfiguration()->allowMockingNonExistentMethods(false);
1805
        $mock = $this->container->mock(new stdClass);
1806
        $mock->shouldReceive('foo');
1807
    }
1808
1809
    public function testAnExampleWithSomeExpectationAmends()
1810
    {
1811
        $service = $this->container->mock('MyService');
1812
        $service->shouldReceive('login')->with('user', 'pass')->once()->andReturn(true);
1813
        $service->shouldReceive('hasBookmarksTagged')->with('php')->once()->andReturn(false);
1814
        $service->shouldReceive('addBookmark')->with('/^http:/', \Mockery::type('string'))->times(3)->andReturn(true);
1815
        $service->shouldReceive('hasBookmarksTagged')->with('php')->once()->andReturn(true);
1816
1817
        $this->assertTrue($service->login('user', 'pass'));
1818
        $this->assertFalse($service->hasBookmarksTagged('php'));
1819
        $this->assertTrue($service->addBookmark('http://example.com/1', 'some_tag1'));
1820
        $this->assertTrue($service->addBookmark('http://example.com/2', 'some_tag2'));
1821
        $this->assertTrue($service->addBookmark('http://example.com/3', 'some_tag3'));
1822
        $this->assertTrue($service->hasBookmarksTagged('php'));
1823
1824
        $this->container->mockery_verify();
1825
    }
1826
1827
    public function testAnExampleWithSomeExpectationAmendsOnCallCounts()
1828
    {
1829
        $service = $this->container->mock('MyService');
1830
        $service->shouldReceive('login')->with('user', 'pass')->once()->andReturn(true);
1831
        $service->shouldReceive('hasBookmarksTagged')->with('php')->once()->andReturn(false);
1832
        $service->shouldReceive('addBookmark')->with('/^http:/', \Mockery::type('string'))->times(3)->andReturn(true);
1833
        $service->shouldReceive('hasBookmarksTagged')->with('php')->twice()->andReturn(true);
1834
1835
        $this->assertTrue($service->login('user', 'pass'));
1836
        $this->assertFalse($service->hasBookmarksTagged('php'));
1837
        $this->assertTrue($service->addBookmark('http://example.com/1', 'some_tag1'));
1838
        $this->assertTrue($service->addBookmark('http://example.com/2', 'some_tag2'));
1839
        $this->assertTrue($service->addBookmark('http://example.com/3', 'some_tag3'));
1840
        $this->assertTrue($service->hasBookmarksTagged('php'));
1841
        $this->assertTrue($service->hasBookmarksTagged('php'));
1842
1843
        $this->container->mockery_verify();
1844
    }
1845
1846
    public function testAnExampleWithSomeExpectationAmendsOnCallCounts_PHPUnitTest()
1847
    {
1848
        $service = $this->getMock('MyService2');
1849
        $service->expects($this->once())->method('login')->with('user', 'pass')->will($this->returnValue(true));
1850
        $service->expects($this->exactly(3))->method('hasBookmarksTagged')->with('php')
1851
            ->will($this->onConsecutiveCalls(false, true, true));
1852
        $service->expects($this->exactly(3))->method('addBookmark')
1853
            ->with($this->matchesRegularExpression('/^http:/'), $this->isType('string'))
1854
            ->will($this->returnValue(true));
1855
1856
        $this->assertTrue($service->login('user', 'pass'));
1857
        $this->assertFalse($service->hasBookmarksTagged('php'));
1858
        $this->assertTrue($service->addBookmark('http://example.com/1', 'some_tag1'));
1859
        $this->assertTrue($service->addBookmark('http://example.com/2', 'some_tag2'));
1860
        $this->assertTrue($service->addBookmark('http://example.com/3', 'some_tag3'));
1861
        $this->assertTrue($service->hasBookmarksTagged('php'));
1862
        $this->assertTrue($service->hasBookmarksTagged('php'));
1863
    }
1864
1865
    public function testMockedMethodsCallableFromWithinOriginalClass()
1866
    {
1867
        $mock = $this->container->mock('MockeryTest_InterMethod1[doThird]');
1868
        $mock->shouldReceive('doThird')->andReturn(true);
1869
        $this->assertTrue($mock->doFirst());
1870
    }
1871
1872
    /**
1873
     * @group issue #20
1874
     */
1875
    public function testMockingDemeterChainsPassesMockeryExpectationToCompositeExpectation()
1876
    {
1877
        $mock = $this->container->mock('Mockery_Demeterowski');
1878
        $mock->shouldReceive('foo->bar->baz')->andReturn('Spam!');
1879
        $demeter = new Mockery_UseDemeter($mock);
1880
        $this->assertSame('Spam!', $demeter->doit());
1881
    }
1882
1883
    /**
1884
     * @group issue #20 - with args in demeter chain
1885
     */
1886
    public function testMockingDemeterChainsPassesMockeryExpectationToCompositeExpectationWithArgs()
1887
    {
1888
        $mock = $this->container->mock('Mockery_Demeterowski');
1889
        $mock->shouldReceive('foo->bar->baz')->andReturn('Spam!');
1890
        $demeter = new Mockery_UseDemeter($mock);
1891
        $this->assertSame('Spam!', $demeter->doitWithArgs());
1892
    }
1893
1894
    public function testPassthruEnsuresRealMethodCalledForReturnValues()
1895
    {
1896
        $mock = $this->container->mock('MockeryTest_SubjectCall1');
1897
        $mock->shouldReceive('foo')->once()->passthru();
1898
        $this->assertEquals('bar', $mock->foo());
1899
        $this->container->mockery_verify();
1900
    }
1901
1902
    public function testShouldIgnoreMissingExpectationBasedOnArgs()
1903
    {
1904
        $mock = $this->container->mock("MyService2")->shouldIgnoreMissing();
1905
        $mock->shouldReceive("hasBookmarksTagged")->with("dave")->once();
1906
        $mock->hasBookmarksTagged("dave");
1907
        $mock->hasBookmarksTagged("padraic");
1908
        $this->container->mockery_verify();
1909
    }
1910
1911
    public function testShouldDeferMissingExpectationBasedOnArgs()
1912
    {
1913
        $mock = $this->container->mock("MockeryTest_SubjectCall1")->shouldDeferMissing();
1914
1915
        $this->assertEquals('bar', $mock->foo());
1916
        $this->assertEquals('bar', $mock->foo("baz"));
1917
        $this->assertEquals('bar', $mock->foo("qux"));
1918
1919
        $mock->shouldReceive("foo")->with("baz")->twice()->andReturn('123');
1920
        $this->assertEquals('bar', $mock->foo());
1921
        $this->assertEquals('123', $mock->foo("baz"));
1922
        $this->assertEquals('bar', $mock->foo("qux"));
1923
1924
        $mock->shouldReceive("foo")->withNoArgs()->once()->andReturn('456');
1925
        $this->assertEquals('456', $mock->foo());
1926
        $this->assertEquals('123', $mock->foo("baz"));
1927
        $this->assertEquals('bar', $mock->foo("qux"));
1928
1929
        $this->container->mockery_verify();
1930
    }
1931
1932
    public function testCanReturnSelf()
1933
    {
1934
        $this->mock->shouldReceive("foo")->andReturnSelf();
1935
        $this->assertSame($this->mock, $this->mock->foo());
1936
    }
1937
1938
    public function testExpectationCanBeOverridden()
1939
    {
1940
        $this->mock->shouldReceive('foo')->once()->andReturn('green');
1941
        $this->mock->shouldReceive('foo')->andReturn('blue');
1942
        $this->assertEquals($this->mock->foo(), 'green');
1943
        $this->assertEquals($this->mock->foo(), 'blue');
1944
    }
1945
}
1946
1947
class MockeryTest_SubjectCall1
1948
{
1949
    public function foo()
1950
    {
1951
        return 'bar';
1952
    }
1953
}
1954
1955
class MockeryTest_InterMethod1
1956
{
1957
    public function doFirst()
1958
    {
1959
        return $this->doSecond();
1960
    }
1961
1962
    private function doSecond()
1963
    {
1964
        return $this->doThird();
1965
    }
1966
1967
    public function doThird()
1968
    {
1969
        return false;
1970
    }
1971
}
1972
1973
class MyService2
1974
{
1975
    public function login($user, $pass)
1976
    {
1977
    }
1978
    public function hasBookmarksTagged($tag)
1979
    {
1980
    }
1981
    public function addBookmark($uri, $tag)
1982
    {
1983
    }
1984
}
1985
1986
class Mockery_Duck
1987
{
1988
    public function quack()
1989
    {
1990
    }
1991
    public function swim()
1992
    {
1993
    }
1994
}
1995
1996
class Mockery_Duck_Nonswimmer
1997
{
1998
    public function quack()
1999
    {
2000
    }
2001
}
2002
2003
class Mockery_Demeterowski
2004
{
2005
    public function foo()
2006
    {
2007
        return $this;
2008
    }
2009
    public function bar()
2010
    {
2011
        return $this;
2012
    }
2013
    public function baz()
2014
    {
2015
        return 'Ham!';
2016
    }
2017
}
2018
2019
class Mockery_UseDemeter
2020
{
2021
    public function __construct($demeter)
2022
    {
2023
        $this->demeter = $demeter;
2024
    }
2025
    public function doit()
2026
    {
2027
        return $this->demeter->foo()->bar()->baz();
2028
    }
2029
    public function doitWithArgs()
2030
    {
2031
        return $this->demeter->foo("foo")->bar("bar")->baz("baz");
2032
    }
2033
}
2034
2035
class MockeryTest_Foo
2036
{
2037
    public function foo()
2038
    {
2039
    }
2040
}
2041