PageRenderTime 83ms CodeModel.GetById 17ms app.highlight 59ms RepoModel.GetById 2ms app.codeStats 0ms

/Server/SWXPHP/2.00/php/tests/simpletest/test/mock_objects_test.php

http://swx-format.googlecode.com/
PHP | 691 lines | 585 code | 101 blank | 5 comment | 0 complexity | ef0d594de691346ace90735419cfe691 MD5 | raw file
  1<?php
  2    // $Id: mock_objects_test.php,v 1.24 2006/11/20 23:10:18 lastcraft Exp $
  3    require_once(dirname(__FILE__) . '/../expectation.php');
  4
  5    class TestOfAnythingExpectation extends UnitTestCase {
  6
  7        function testSimpleInteger() {
  8            $expectation = new AnythingExpectation();
  9            $this->assertTrue($expectation->test(33));
 10            $this->assertTrue($expectation->test(false));
 11            $this->assertTrue($expectation->test(null));
 12        }
 13    }
 14
 15    class TestOfParametersExpectation extends UnitTestCase {
 16
 17        function testEmptyMatch() {
 18            $expectation = new ParametersExpectation(array());
 19            $this->assertTrue($expectation->test(array()));
 20            $this->assertFalse($expectation->test(array(33)));
 21        }
 22
 23        function testSingleMatch() {
 24            $expectation = new ParametersExpectation(array(0));
 25            $this->assertFalse($expectation->test(array(1)));
 26            $this->assertTrue($expectation->test(array(0)));
 27        }
 28
 29        function testAnyMatch() {
 30            $expectation = new ParametersExpectation(false);
 31            $this->assertTrue($expectation->test(array()));
 32            $this->assertTrue($expectation->test(array(1, 2)));
 33        }
 34
 35        function testMissingParameter() {
 36            $expectation = new ParametersExpectation(array(0));
 37            $this->assertFalse($expectation->test(array()));
 38        }
 39
 40        function testNullParameter() {
 41            $expectation = new ParametersExpectation(array(null));
 42            $this->assertTrue($expectation->test(array(null)));
 43            $this->assertFalse($expectation->test(array()));
 44        }
 45
 46        function testAnythingExpectations() {
 47            $expectation = new ParametersExpectation(array(new AnythingExpectation()));
 48            $this->assertFalse($expectation->test(array()));
 49            $this->assertIdentical($expectation->test(array(null)), true);
 50            $this->assertIdentical($expectation->test(array(13)), true);
 51        }
 52
 53        function testOtherExpectations() {
 54            $expectation = new ParametersExpectation(
 55                    array(new PatternExpectation('/hello/i')));
 56            $this->assertFalse($expectation->test(array('Goodbye')));
 57            $this->assertTrue($expectation->test(array('hello')));
 58            $this->assertTrue($expectation->test(array('Hello')));
 59        }
 60
 61        function testIdentityOnly() {
 62            $expectation = new ParametersExpectation(array("0"));
 63            $this->assertFalse($expectation->test(array(0)));
 64            $this->assertTrue($expectation->test(array("0")));
 65        }
 66
 67        function testLongList() {
 68            $expectation = new ParametersExpectation(
 69                    array("0", 0, new AnythingExpectation(), false));
 70            $this->assertTrue($expectation->test(array("0", 0, 37, false)));
 71            $this->assertFalse($expectation->test(array("0", 0, 37, true)));
 72            $this->assertFalse($expectation->test(array("0", 0, 37)));
 73        }
 74    }
 75
 76    class TestOfCallMap extends UnitTestCase {
 77
 78        function testEmpty() {
 79            $map = new CallMap();
 80            $this->assertFalse($map->isMatch("any", array()));
 81            $this->assertNull($map->findFirstMatch("any", array()));
 82        }
 83
 84        function testExactValue() {
 85            $map = new CallMap();
 86            $map->addValue(array(0), "Fred");
 87            $map->addValue(array(1), "Jim");
 88            $map->addValue(array("1"), "Tom");
 89            $this->assertTrue($map->isMatch(array(0)));
 90            $this->assertEqual($map->findFirstMatch(array(0)), "Fred");
 91            $this->assertTrue($map->isMatch(array(1)));
 92            $this->assertEqual($map->findFirstMatch(array(1)), "Jim");
 93            $this->assertEqual($map->findFirstMatch(array("1")), "Tom");
 94        }
 95
 96        function testExactReference() {
 97            $map = new CallMap();
 98            $ref = "Fred";
 99            $map->addReference(array(0), $ref);
100            $this->assertEqual($map->findFirstMatch(array(0)), "Fred");
101            $ref2 = &$map->findFirstMatch(array(0));
102            $this->assertReference($ref2, $ref);
103        }
104
105        function testWildcard() {
106            $map = new CallMap();
107            $map->addValue(array(new AnythingExpectation(), 1, 3), "Fred");
108            $this->assertTrue($map->isMatch(array(2, 1, 3)));
109            $this->assertEqual($map->findFirstMatch(array(2, 1, 3)), "Fred");
110        }
111
112        function testAllWildcard() {
113            $map = new CallMap();
114            $this->assertFalse($map->isMatch(array(2, 1, 3)));
115            $map->addValue("", "Fred");
116            $this->assertTrue($map->isMatch(array(2, 1, 3)));
117            $this->assertEqual($map->findFirstMatch(array(2, 1, 3)), "Fred");
118        }
119
120        function testOrdering() {
121            $map = new CallMap();
122            $map->addValue(array(1, 2), "1, 2");
123            $map->addValue(array(1, 3), "1, 3");
124            $map->addValue(array(1), "1");
125            $map->addValue(array(1, 4), "1, 4");
126            $map->addValue(array(new AnythingExpectation()), "Any");
127            $map->addValue(array(2), "2");
128            $map->addValue("", "Default");
129            $map->addValue(array(), "None");
130            $this->assertEqual($map->findFirstMatch(array(1, 2)), "1, 2");
131            $this->assertEqual($map->findFirstMatch(array(1, 3)), "1, 3");
132            $this->assertEqual($map->findFirstMatch(array(1, 4)), "1, 4");
133            $this->assertEqual($map->findFirstMatch(array(1)), "1");
134            $this->assertEqual($map->findFirstMatch(array(2)), "Any");
135            $this->assertEqual($map->findFirstMatch(array(3)), "Any");
136            $this->assertEqual($map->findFirstMatch(array()), "Default");
137        }
138    }
139
140    class Dummy {
141        function Dummy() {
142        }
143
144        function aMethod() {
145            return true;
146        }
147
148        function anotherMethod() {
149            return true;
150        }
151    }
152
153    Stub::generate('Dummy', 'StubDummy');
154    Stub::generate('Dummy', 'AnotherStubDummy');
155    Stub::generate('Dummy', 'StubDummyWithExtraMethods', array('extraMethod'));
156
157    class SpecialSimpleStub extends SimpleMock {
158        function SpecialSimpleStub() {
159            $this->SimpleMock();
160        }
161    }
162    SimpleTest::setMockBaseClass('SpecialSimpleStub');
163    Stub::generate('Dummy', 'SpecialStubDummy');
164    SimpleTest::setMockBaseClass('SimpleMock');
165
166    class TestOfStubGeneration extends UnitTestCase {
167
168        function testCloning() {
169            $stub = &new StubDummy();
170            $this->assertTrue(method_exists($stub, "aMethod"));
171            $this->assertNull($stub->aMethod(null));
172        }
173
174        function testCloningWithExtraMethod() {
175            $stub = &new StubDummyWithExtraMethods();
176            $this->assertTrue(method_exists($stub, "extraMethod"));
177        }
178
179        function testCloningWithChosenClassName() {
180            $stub = &new AnotherStubDummy();
181            $this->assertTrue(method_exists($stub, "aMethod"));
182        }
183
184        function testCloningWithDifferentBaseClass() {
185            $stub = &new SpecialStubDummy();
186            $this->assertIsA($stub, "SpecialSimpleStub");
187            $this->assertTrue(method_exists($stub, "aMethod"));
188        }
189    }
190
191    class TestOfServerStubReturns extends UnitTestCase {
192
193        function testDefaultReturn() {
194            $stub = &new StubDummy();
195            $stub->setReturnValue("aMethod", "aaa");
196            $this->assertIdentical($stub->aMethod(), "aaa");
197            $this->assertIdentical($stub->aMethod(), "aaa");
198        }
199
200        function testParameteredReturn() {
201            $stub = &new StubDummy();
202            $stub->setReturnValue("aMethod", "aaa", array(1, 2, 3));
203            $this->assertNull($stub->aMethod());
204            $this->assertIdentical($stub->aMethod(1, 2, 3), "aaa");
205        }
206
207        function testReferenceReturned() {
208            $stub = &new StubDummy();
209            $object = new Dummy();
210            $stub->setReturnReference("aMethod", $object, array(1, 2, 3));
211            $this->assertReference($zref =& $stub->aMethod(1, 2, 3), $object);
212        }
213
214        function testCallCount() {
215            $stub = &new StubDummy();
216            $this->assertEqual($stub->getCallCount("aMethod"), 0);
217            $stub->aMethod();
218            $this->assertEqual($stub->getCallCount("aMethod"), 1);
219            $stub->aMethod();
220            $this->assertEqual($stub->getCallCount("aMethod"), 2);
221        }
222
223        function testMultipleMethods() {
224            $stub = &new StubDummy();
225            $stub->setReturnValue("aMethod", 100, array(1));
226            $stub->setReturnValue("aMethod", 200, array(2));
227            $stub->setReturnValue("anotherMethod", 10, array(1));
228            $stub->setReturnValue("anotherMethod", 20, array(2));
229            $this->assertIdentical($stub->aMethod(1), 100);
230            $this->assertIdentical($stub->anotherMethod(1), 10);
231            $this->assertIdentical($stub->aMethod(2), 200);
232            $this->assertIdentical($stub->anotherMethod(2), 20);
233        }
234
235        function testReturnSequence() {
236            $stub = &new StubDummy();
237            $stub->setReturnValueAt(0, "aMethod", "aaa");
238            $stub->setReturnValueAt(1, "aMethod", "bbb");
239            $stub->setReturnValueAt(3, "aMethod", "ddd");
240            $this->assertIdentical($stub->aMethod(), "aaa");
241            $this->assertIdentical($stub->aMethod(), "bbb");
242            $this->assertNull($stub->aMethod());
243            $this->assertIdentical($stub->aMethod(), "ddd");
244        }
245
246        function testReturnReferenceSequence() {
247            $stub = &new StubDummy();
248            $object = new Dummy();
249            $stub->setReturnReferenceAt(1, "aMethod", $object);
250            $this->assertNull($stub->aMethod());
251            $this->assertReference($zref =& $stub->aMethod(), $object);
252            $this->assertNull($stub->aMethod());
253        }
254
255        function testComplicatedReturnSequence() {
256            $stub = &new StubDummy();
257            $object = new Dummy();
258            $stub->setReturnValueAt(1, "aMethod", "aaa", array("a"));
259            $stub->setReturnValueAt(1, "aMethod", "bbb");
260            $stub->setReturnReferenceAt(2, "aMethod", $object, array('*', 2));
261            $stub->setReturnValueAt(2, "aMethod", "value", array('*', 3));
262            $stub->setReturnValue("aMethod", 3, array(3));
263            $this->assertNull($stub->aMethod());
264            $this->assertEqual($stub->aMethod("a"), "aaa");
265            $this->assertReference($zref =& $stub->aMethod(1, 2), $object);
266            $this->assertEqual($stub->aMethod(3), 3);
267            $this->assertNull($stub->aMethod());
268        }
269
270        function testMultipleMethodSequences() {
271            $stub = &new StubDummy();
272            $stub->setReturnValueAt(0, "aMethod", "aaa");
273            $stub->setReturnValueAt(1, "aMethod", "bbb");
274            $stub->setReturnValueAt(0, "anotherMethod", "ccc");
275            $stub->setReturnValueAt(1, "anotherMethod", "ddd");
276            $this->assertIdentical($stub->aMethod(), "aaa");
277            $this->assertIdentical($stub->anotherMethod(), "ccc");
278            $this->assertIdentical($stub->aMethod(), "bbb");
279            $this->assertIdentical($stub->anotherMethod(), "ddd");
280        }
281
282        function testSequenceFallback() {
283            $stub = &new StubDummy();
284            $stub->setReturnValueAt(0, "aMethod", "aaa", array('a'));
285            $stub->setReturnValueAt(1, "aMethod", "bbb", array('a'));
286            $stub->setReturnValue("aMethod", "AAA");
287            $this->assertIdentical($stub->aMethod('a'), "aaa");
288            $this->assertIdentical($stub->aMethod('b'), "AAA");
289        }
290
291        function testMethodInterference() {
292            $stub = &new StubDummy();
293            $stub->setReturnValueAt(0, "anotherMethod", "aaa");
294            $stub->setReturnValue("aMethod", "AAA");
295            $this->assertIdentical($stub->aMethod(), "AAA");
296            $this->assertIdentical($stub->anotherMethod(), "aaa");
297        }
298    }
299
300    Mock::generate('Dummy');
301    Mock::generate('Dummy', 'AnotherMockDummy');
302    Mock::generate('Dummy', 'MockDummyWithExtraMethods', array('extraMethod'));
303
304    class SpecialSimpleMock extends SimpleMock { }
305
306    SimpleTest::setMockBaseClass("SpecialSimpleMock");
307    Mock::generate("Dummy", "SpecialMockDummy");
308    SimpleTest::setMockBaseClass("SimpleMock");
309
310    class TestOfMockGeneration extends UnitTestCase {
311
312        function testCloning() {
313            $mock = &new MockDummy();
314            $this->assertTrue(method_exists($mock, "aMethod"));
315            $this->assertNull($mock->aMethod());
316        }
317
318        function testCloningWithExtraMethod() {
319            $mock = &new MockDummyWithExtraMethods();
320            $this->assertTrue(method_exists($mock, "extraMethod"));
321        }
322
323        function testCloningWithChosenClassName() {
324            $mock = &new AnotherMockDummy();
325            $this->assertTrue(method_exists($mock, "aMethod"));
326        }
327
328        function testCloningWithDifferentBaseClass() {
329            $mock = &new SpecialMockDummy();
330            $this->assertIsA($mock, "SpecialSimpleMock");
331            $this->assertTrue(method_exists($mock, "aMethod"));
332        }
333    }
334
335    class TestOfMockReturns extends UnitTestCase {
336
337        function testParameteredReturn() {
338            $mock = &new MockDummy();
339            $mock->setReturnValue('aMethod', 'aaa', array(1, 2, 3));
340            $this->assertNull($mock->aMethod());
341            $this->assertIdentical($mock->aMethod(1, 2, 3), 'aaa');
342        }
343
344        function testReferenceReturned() {
345            $mock = &new MockDummy();
346            $object = new Dummy();
347            $mock->setReturnReference("aMethod", $object, array(1, 2, 3));
348            $this->assertReference($zref =& $mock->aMethod(1, 2, 3), $object);
349        }
350
351        function testWildcardReturn() {
352            $mock = &new MockDummy();
353            $mock->setWildcard('wild');
354            $mock->setReturnValue('aMethod', 'a', array(1, 'wild', 3));
355            $this->assertIdentical($mock->aMethod(1, 'something', 3), 'a');
356            $this->assertIdentical($mock->aMethod(1, 'anything', 3), 'a');
357        }
358
359        function testPatternMatchReturn() {
360            $mock = &new MockDummy();
361            $mock->setReturnValue(
362                    "aMethod",
363                    "aaa",
364                    array(new PatternExpectation('/hello/i')));
365            $this->assertIdentical($mock->aMethod('Hello'), "aaa");
366            $this->assertNull($mock->aMethod('Goodbye'));
367        }
368
369        function testCallCount() {
370            $mock = &new MockDummy();
371            $this->assertEqual($mock->getCallCount("aMethod"), 0);
372            $mock->aMethod();
373            $this->assertEqual($mock->getCallCount("aMethod"), 1);
374            $mock->aMethod();
375            $this->assertEqual($mock->getCallCount("aMethod"), 2);
376        }
377
378        function testReturnReferenceSequence() {
379            $mock = &new MockDummy();
380            $object = new Dummy();
381            $mock->setReturnReferenceAt(1, 'aMethod', $object);
382            $this->assertNull($mock->aMethod());
383            $this->assertReference($zref =& $mock->aMethod(), $object);
384            $this->assertNull($mock->aMethod());
385            $this->swallowErrors();
386        }
387    }
388
389    class ClassWithSpecialMethods {
390        function __get($name) { }
391        function __set($name, $value) { }
392        function __isset($name) { }
393        function __unset($name) { }
394        function __call($method, $arguments) { }
395        function __toString() { }
396    }
397    Mock::generate('ClassWithSpecialMethods');
398
399    class TestOfSpecialMethods extends UnitTestCase {
400        function skip() {
401            $this->skipIf(version_compare(phpversion(), '5', '<='), 'Overloading not tested for PHP 4');
402        }
403        
404        function testCanMockTheThingAtAll() {
405            $mock = new MockClassWithSpecialMethods();
406        }
407        
408        function testReturnFromSpecialAccessor() {
409            $mock = &new MockClassWithSpecialMethods();
410            $mock->setReturnValue('__get', '1st Return', array('first'));
411            $mock->setReturnValue('__get', '2nd Return', array('second'));
412            $this->assertEqual($mock->first, '1st Return');
413            $this->assertEqual($mock->second, '2nd Return');
414        }
415        
416        function testcanExpectTheSettingOfValue() {
417            $mock = &new MockClassWithSpecialMethods();
418            $mock->expectOnce('__set', array('a', 'A'));
419            $mock->a = 'A';
420        }
421        
422        function testCanSimulateAnOverloadmethod() {
423            $mock = &new MockClassWithSpecialMethods();
424            $mock->expectOnce('__call', array('amOverloaded', array('A')));
425            $mock->setReturnValue('__call', 'aaa');
426            $this->assertIdentical($mock->amOverloaded('A'), 'aaa');
427        }
428        
429        function testCanEmulateIsset() {
430            $mock = &new MockClassWithSpecialMethods();
431            $mock->setReturnValue('__isset', true);
432            $this->assertIdentical(isset($mock->a), true);
433        }
434        
435        function testCanExpectUnset() {
436            $mock = &new MockClassWithSpecialMethods();
437            $mock->expectOnce('__unset', array('a'));
438            unset($mock->a);
439        }
440        
441        function testToStringMagic() {
442            $mock = &new MockClassWithSpecialMethods();
443            $mock->expectOnce('__toString');
444            $mock->setReturnValue('__toString', 'AAA');
445            ob_start();
446            print $mock;
447            $output = ob_get_contents();
448            ob_end_clean();
449            $this->assertEqual($output, 'AAA');
450        }
451    }
452
453    class TestOfMockTally extends UnitTestCase {
454
455        function testZeroCallCount() {
456            $mock = &new MockDummy();
457            $mock->expectCallCount('aMethod', 0);
458        }
459
460        function testExpectedCallCount() {
461            $mock = &new MockDummy();
462            $mock->expectCallCount('aMethod', 2);
463            $mock->aMethod();
464            $mock->aMethod();
465        }
466    }
467
468    class MockDummyWithInjectedTestCase extends MockDummy {
469        function &_getCurrentTestCase() {
470            $context = &SimpleTest::getContext();
471            $test = &$context->getTest();
472            return $test->test;
473        }
474    }
475
476    Mock::generate('SimpleTestCase');
477
478    class TestOfMockExpectations extends UnitTestCase {
479        var $_test;
480
481        function setUp() {
482            $this->test = &new MockSimpleTestCase();
483        }
484
485        function testSettingExpectationOnNonMethodThrowsError() {
486            $mock = &new MockDummyWithInjectedTestCase();
487            $mock->expectMaximumCallCount('aMissingMethod', 2);
488            $this->assertError();
489        }
490
491        function testMaxCallsDetectsOverrun() {
492            $this->test->expectOnce('assert', array(new IsAExpectation('MaximumCallCountExpectation'), 3));
493            $mock = &new MockDummyWithInjectedTestCase();
494            $mock->expectMaximumCallCount('aMethod', 2);
495            $mock->aMethod();
496            $mock->aMethod();
497            $mock->aMethod();
498            $mock->atTestEnd('testSomething', $this->test);
499        }
500
501        function testTallyOnMaxCallsSendsPassOnUnderrun() {
502            $this->test->expectOnce('assert', array(new IsAExpectation('MaximumCallCountExpectation'), 2));
503            $mock = &new MockDummyWithInjectedTestCase();
504            $mock->expectMaximumCallCount("aMethod", 2);
505            $mock->aMethod();
506            $mock->aMethod();
507            $mock->atTestEnd('testSomething', $this->test);
508        }
509
510        function testExpectNeverDetectsOverrun() {
511            $this->test->expectOnce('assert', array(new IsAExpectation('MaximumCallCountExpectation'), 1));
512            $mock = &new MockDummyWithInjectedTestCase();
513            $mock->expectNever('aMethod');
514            $mock->aMethod();
515            $mock->atTestEnd('testSomething', $this->test);
516        }
517
518        function testTallyOnExpectNeverSendsPassOnUnderrun() {
519            $this->test->expectOnce('assert', array(new IsAExpectation('MaximumCallCountExpectation'), 0));
520            $mock = &new MockDummyWithInjectedTestCase();
521            $mock->expectNever('aMethod');
522            $mock->atTestEnd('testSomething', $this->test);
523        }
524
525        function testMinCalls() {
526            $this->test->expectOnce('assert', array(new IsAExpectation('MinimumCallCountExpectation'), 2));
527            $mock = &new MockDummyWithInjectedTestCase();
528            $mock->expectMinimumCallCount('aMethod', 2);
529            $mock->aMethod();
530            $mock->aMethod();
531            $mock->atTestEnd('testSomething', $this->test);
532        }
533
534        function testFailedNever() {
535            $this->test->expectOnce('assert', array(new IsAExpectation('MaximumCallCountExpectation'), 1));
536            $mock = &new MockDummyWithInjectedTestCase();
537            $mock->expectNever('aMethod');
538            $mock->aMethod();
539            $mock->atTestEnd('testSomething', $this->test);
540        }
541
542        function testUnderOnce() {
543            $this->test->expectOnce('assert', array(new IsAExpectation('CallCountExpectation'), 0));
544            $mock = &new MockDummyWithInjectedTestCase();
545            $mock->expectOnce('aMethod');
546            $mock->atTestEnd('testSomething', $this->test);
547        }
548
549        function testOverOnce() {
550            $this->test->expectOnce('assert', array(new IsAExpectation('CallCountExpectation'), 2));
551            $mock = &new MockDummyWithInjectedTestCase();
552            $mock->expectOnce('aMethod');
553            $mock->aMethod();
554            $mock->aMethod();
555            $mock->atTestEnd('testSomething', $this->test);
556            $this->swallowErrors();
557        }
558
559        function testUnderAtLeastOnce() {
560            $this->test->expectOnce('assert', array(new IsAExpectation('MinimumCallCountExpectation'), 0));
561            $mock = &new MockDummyWithInjectedTestCase();
562            $mock->expectAtLeastOnce("aMethod");
563            $mock->atTestEnd('testSomething', $this->test);
564        }
565
566        function testZeroArguments() {
567            $mock = &new MockDummyWithInjectedTestCase();
568            $mock->expectArguments("aMethod", array());
569            $mock->aMethod();
570            $mock->atTestEnd('testSomething', $this->test);
571        }
572
573        function testExpectedArguments() {
574            $mock = &new MockDummyWithInjectedTestCase();
575            $mock->expectArguments('aMethod', array(1, 2, 3));
576            $mock->aMethod(1, 2, 3);
577            $mock->atTestEnd('testSomething', $this->test);
578        }
579
580        function testFailedArguments() {
581            $this->test->expectOnce('assert', array(new IsAExpectation('ParametersExpectation'), array('that'), '*'));
582            $mock = &new MockDummyWithInjectedTestCase();
583            $mock->expectArguments('aMethod', array('this'));
584            $mock->aMethod('that');
585            $mock->atTestEnd('testSomething', $this->test);
586        }
587
588        function testWildcardArguments() {
589            $mock = &new MockDummyWithInjectedTestCase($this, "wild");
590            $mock->expectArguments("aMethod", array("wild", 123, "wild"));
591            $mock->aMethod(100, 123, 101);
592            $mock->atTestEnd('testSomething', $this->test);
593        }
594
595        function testSpecificSequence() {
596            $mock = &new MockDummyWithInjectedTestCase();
597            $mock->expectArgumentsAt(1, "aMethod", array(1, 2, 3));
598            $mock->expectArgumentsAt(2, "aMethod", array("Hello"));
599            $mock->aMethod();
600            $mock->aMethod(1, 2, 3);
601            $mock->aMethod("Hello");
602            $mock->aMethod();
603            $mock->atTestEnd('testSomething', $this->test);
604        }
605
606        function testBadArgParameter() {
607            $mock = &new MockDummyWithInjectedTestCase();
608            $mock->expectArguments("aMethod", "foo");
609            $this->assertErrorPattern('/\$args.*not an array/i');
610            $mock->aMethod();
611            $mock->tally();
612            $mock->atTestEnd('testSomething', $this->test);
613       }
614    }
615
616    class TestOfMockComparisons extends UnitTestCase {
617
618        function testEqualComparisonOfMocksDoesNotCrash() {
619            $expectation = &new EqualExpectation(new MockDummy());
620            $this->assertTrue($expectation->test(new MockDummy(), true));
621        }
622
623        function testIdenticalComparisonOfMocksDoesNotCrash() {
624            $expectation = &new IdenticalExpectation(new MockDummy());
625            $this->assertTrue($expectation->test(new MockDummy()));
626        }
627    }
628
629    Mock::generatePartial('Dummy', 'TestDummy', array('anotherMethod'));
630
631    class TestOfPartialMocks extends UnitTestCase {
632
633        function testMethodReplacement() {
634            $mock = &new TestDummy();
635            $this->assertEqual($mock->aMethod(99), 99);
636            $this->assertNull($mock->anotherMethod());
637        }
638
639        function testSettingReturns() {
640            $mock = &new TestDummy();
641            $mock->setReturnValue('anotherMethod', 33, array(3));
642            $mock->setReturnValue('anotherMethod', 22);
643            $mock->setReturnValueAt(2, 'anotherMethod', 44, array(3));
644            $this->assertEqual($mock->anotherMethod(), 22);
645            $this->assertEqual($mock->anotherMethod(3), 33);
646            $this->assertEqual($mock->anotherMethod(3), 44);
647        }
648
649        function testReferences() {
650            $mock = &new TestDummy();
651            $object = new Dummy();
652            $mock->setReturnReferenceAt(0, 'anotherMethod', $object, array(3));
653            $this->assertReference($zref =& $mock->anotherMethod(3), $object);
654        }
655
656        function testExpectations() {
657            $mock = &new TestDummy();
658            $mock->expectCallCount('anotherMethod', 2);
659            $mock->expectArguments('anotherMethod', array(77));
660            $mock->expectArgumentsAt(1, 'anotherMethod', array(66));
661            $mock->anotherMethod(77);
662            $mock->anotherMethod(66);
663        }
664
665        function testSettingExpectationOnMissingMethodThrowsError() {
666            $mock = &new TestDummy();
667            $mock->expectCallCount('aMissingMethod', 2);
668            $this->assertError();
669        }
670    }
671
672    class ConstructorSuperClass {
673        function ConstructorSuperClass() { }
674    }
675
676    class ConstructorSubClass extends ConstructorSuperClass {
677    }
678
679    class TestOfPHP4StyleSuperClassConstruct extends UnitTestCase {
680        /*
681         * This addresses issue #1231401.  Without the fix in place, this will
682		 * generate a fatal PHP error.
683		 */
684        function testBasicConstruct() {
685            Mock::generate('ConstructorSubClass');
686            $mock = &new MockConstructorSubClass();
687            $this->assertIsA($mock, 'SimpleMock');
688            $this->assertTrue(method_exists($mock, 'ConstructorSuperClass'));
689        }
690    }
691?>