minor fix to prior commit
[openemr.git] / vendor / phpunit / phpunit-mock-objects / tests / MockObjectTest.php
blob302dae7ea709403b63e884f4a3f466746aaec309
1 <?php
2 /*
3 * This file is part of the PHPUnit_MockObject package.
5 * (c) Sebastian Bergmann <sebastian@phpunit.de>
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
11 class Framework_MockObjectTest extends PHPUnit_Framework_TestCase
13 public function testMockedMethodIsNeverCalled()
15 $mock = $this->getMockBuilder(AnInterface::class)
16 ->getMock();
18 $mock->expects($this->never())
19 ->method('doSomething');
22 public function testMockedMethodIsNeverCalledWithParameter()
24 $mock = $this->getMockBuilder(SomeClass::class)
25 ->getMock();
27 $mock->expects($this->never())
28 ->method('doSomething')
29 ->with('someArg');
32 /**
33 * @doesNotPerformAssertions
35 public function testMockedMethodIsNotCalledWhenExpectsAnyWithParameter()
37 $mock = $this->getMockBuilder(SomeClass::class)
38 ->getMock();
40 $mock->expects($this->any())
41 ->method('doSomethingElse')
42 ->with('someArg');
45 /**
46 * @doesNotPerformAssertions
48 public function testMockedMethodIsNotCalledWhenMethodSpecifiedDirectlyWithParameter()
50 $mock = $this->getMockBuilder(SomeClass::class)
51 ->getMock();
53 $mock->method('doSomethingElse')
54 ->with('someArg');
57 public function testMockedMethodIsCalledAtLeastOnce()
59 $mock = $this->getMockBuilder(AnInterface::class)
60 ->getMock();
62 $mock->expects($this->atLeastOnce())
63 ->method('doSomething');
65 $mock->doSomething();
68 public function testMockedMethodIsCalledAtLeastOnce2()
70 $mock = $this->getMockBuilder(AnInterface::class)
71 ->getMock();
73 $mock->expects($this->atLeastOnce())
74 ->method('doSomething');
76 $mock->doSomething();
77 $mock->doSomething();
80 public function testMockedMethodIsCalledAtLeastTwice()
82 $mock = $this->getMockBuilder(AnInterface::class)
83 ->getMock();
85 $mock->expects($this->atLeast(2))
86 ->method('doSomething');
88 $mock->doSomething();
89 $mock->doSomething();
92 public function testMockedMethodIsCalledAtLeastTwice2()
94 $mock = $this->getMockBuilder(AnInterface::class)
95 ->getMock();
97 $mock->expects($this->atLeast(2))
98 ->method('doSomething');
100 $mock->doSomething();
101 $mock->doSomething();
102 $mock->doSomething();
105 public function testMockedMethodIsCalledAtMostTwice()
107 $mock = $this->getMockBuilder(AnInterface::class)
108 ->getMock();
110 $mock->expects($this->atMost(2))
111 ->method('doSomething');
113 $mock->doSomething();
114 $mock->doSomething();
117 public function testMockedMethodIsCalledAtMosttTwice2()
119 $mock = $this->getMockBuilder(AnInterface::class)
120 ->getMock();
122 $mock->expects($this->atMost(2))
123 ->method('doSomething');
125 $mock->doSomething();
128 public function testMockedMethodIsCalledOnce()
130 $mock = $this->getMockBuilder(AnInterface::class)
131 ->getMock();
133 $mock->expects($this->once())
134 ->method('doSomething');
136 $mock->doSomething();
139 public function testMockedMethodIsCalledOnceWithParameter()
141 $mock = $this->getMockBuilder(SomeClass::class)
142 ->getMock();
144 $mock->expects($this->once())
145 ->method('doSomethingElse')
146 ->with($this->equalTo('something'));
148 $mock->doSomethingElse('something');
151 public function testMockedMethodIsCalledExactly()
153 $mock = $this->getMockBuilder(AnInterface::class)
154 ->getMock();
156 $mock->expects($this->exactly(2))
157 ->method('doSomething');
159 $mock->doSomething();
160 $mock->doSomething();
163 public function testStubbedException()
165 $mock = $this->getMockBuilder(AnInterface::class)
166 ->getMock();
168 $mock->expects($this->any())
169 ->method('doSomething')
170 ->will($this->throwException(new Exception));
172 $this->expectException(Exception::class);
174 $mock->doSomething();
177 public function testStubbedWillThrowException()
179 $mock = $this->getMockBuilder(AnInterface::class)
180 ->getMock();
182 $mock->expects($this->any())
183 ->method('doSomething')
184 ->willThrowException(new Exception);
186 $this->expectException(Exception::class);
188 $mock->doSomething();
191 public function testStubbedReturnValue()
193 $mock = $this->getMockBuilder(AnInterface::class)
194 ->getMock();
196 $mock->expects($this->any())
197 ->method('doSomething')
198 ->will($this->returnValue('something'));
200 $this->assertEquals('something', $mock->doSomething());
202 $mock = $this->getMockBuilder(AnInterface::class)
203 ->getMock();
205 $mock->expects($this->any())
206 ->method('doSomething')
207 ->willReturn('something');
209 $this->assertEquals('something', $mock->doSomething());
212 public function testStubbedReturnValueMap()
214 $map = [
215 ['a', 'b', 'c', 'd'],
216 ['e', 'f', 'g', 'h']
219 $mock = $this->getMockBuilder(AnInterface::class)
220 ->getMock();
222 $mock->expects($this->any())
223 ->method('doSomething')
224 ->will($this->returnValueMap($map));
226 $this->assertEquals('d', $mock->doSomething('a', 'b', 'c'));
227 $this->assertEquals('h', $mock->doSomething('e', 'f', 'g'));
228 $this->assertEquals(null, $mock->doSomething('foo', 'bar'));
230 $mock = $this->getMockBuilder(AnInterface::class)
231 ->getMock();
233 $mock->expects($this->any())
234 ->method('doSomething')
235 ->willReturnMap($map);
237 $this->assertEquals('d', $mock->doSomething('a', 'b', 'c'));
238 $this->assertEquals('h', $mock->doSomething('e', 'f', 'g'));
239 $this->assertEquals(null, $mock->doSomething('foo', 'bar'));
242 public function testStubbedReturnArgument()
244 $mock = $this->getMockBuilder(AnInterface::class)
245 ->getMock();
247 $mock->expects($this->any())
248 ->method('doSomething')
249 ->will($this->returnArgument(1));
251 $this->assertEquals('b', $mock->doSomething('a', 'b'));
253 $mock = $this->getMockBuilder(AnInterface::class)
254 ->getMock();
256 $mock->expects($this->any())
257 ->method('doSomething')
258 ->willReturnArgument(1);
260 $this->assertEquals('b', $mock->doSomething('a', 'b'));
263 public function testFunctionCallback()
265 $mock = $this->getMockBuilder(SomeClass::class)
266 ->setMethods(['doSomething'])
267 ->getMock();
269 $mock->expects($this->once())
270 ->method('doSomething')
271 ->will($this->returnCallback('functionCallback'));
273 $this->assertEquals('pass', $mock->doSomething('foo', 'bar'));
275 $mock = $this->getMockBuilder(SomeClass::class)
276 ->setMethods(['doSomething'])
277 ->getMock();
279 $mock->expects($this->once())
280 ->method('doSomething')
281 ->willReturnCallback('functionCallback');
283 $this->assertEquals('pass', $mock->doSomething('foo', 'bar'));
286 public function testStubbedReturnSelf()
288 $mock = $this->getMockBuilder(AnInterface::class)
289 ->getMock();
291 $mock->expects($this->any())
292 ->method('doSomething')
293 ->will($this->returnSelf());
295 $this->assertEquals($mock, $mock->doSomething());
297 $mock = $this->getMockBuilder(AnInterface::class)
298 ->getMock();
300 $mock->expects($this->any())
301 ->method('doSomething')
302 ->willReturnSelf();
304 $this->assertEquals($mock, $mock->doSomething());
307 public function testStubbedReturnOnConsecutiveCalls()
309 $mock = $this->getMockBuilder(AnInterface::class)
310 ->getMock();
312 $mock->expects($this->any())
313 ->method('doSomething')
314 ->will($this->onConsecutiveCalls('a', 'b', 'c'));
316 $this->assertEquals('a', $mock->doSomething());
317 $this->assertEquals('b', $mock->doSomething());
318 $this->assertEquals('c', $mock->doSomething());
320 $mock = $this->getMockBuilder(AnInterface::class)
321 ->getMock();
323 $mock->expects($this->any())
324 ->method('doSomething')
325 ->willReturnOnConsecutiveCalls('a', 'b', 'c');
327 $this->assertEquals('a', $mock->doSomething());
328 $this->assertEquals('b', $mock->doSomething());
329 $this->assertEquals('c', $mock->doSomething());
332 public function testStaticMethodCallback()
334 $mock = $this->getMockBuilder(SomeClass::class)
335 ->setMethods(['doSomething'])
336 ->getMock();
338 $mock->expects($this->once())
339 ->method('doSomething')
340 ->will($this->returnCallback(['MethodCallback', 'staticCallback']));
342 $this->assertEquals('pass', $mock->doSomething('foo', 'bar'));
345 public function testPublicMethodCallback()
347 $mock = $this->getMockBuilder(SomeClass::class)
348 ->setMethods(['doSomething'])
349 ->getMock();
351 $mock->expects($this->once())
352 ->method('doSomething')
353 ->will($this->returnCallback([new MethodCallback, 'nonStaticCallback']));
355 $this->assertEquals('pass', $mock->doSomething('foo', 'bar'));
358 public function testMockClassOnlyGeneratedOnce()
360 $mock1 = $this->getMockBuilder(AnInterface::class)
361 ->getMock();
363 $mock2 = $this->getMockBuilder(AnInterface::class)
364 ->getMock();
366 $this->assertEquals(get_class($mock1), get_class($mock2));
369 public function testMockClassDifferentForPartialMocks()
371 $mock1 = $this->getMockBuilder(PartialMockTestClass::class)
372 ->getMock();
374 $mock2 = $this->getMockBuilder(PartialMockTestClass::class)
375 ->setMethods(['doSomething'])
376 ->getMock();
378 $mock3 = $this->getMockBuilder(PartialMockTestClass::class)
379 ->setMethods(['doSomething'])
380 ->getMock();
382 $mock4 = $this->getMockBuilder(PartialMockTestClass::class)
383 ->setMethods(['doAnotherThing'])
384 ->getMock();
386 $mock5 = $this->getMockBuilder(PartialMockTestClass::class)
387 ->setMethods(['doAnotherThing'])
388 ->getMock();
390 $this->assertNotEquals(get_class($mock1), get_class($mock2));
391 $this->assertNotEquals(get_class($mock1), get_class($mock3));
392 $this->assertNotEquals(get_class($mock1), get_class($mock4));
393 $this->assertNotEquals(get_class($mock1), get_class($mock5));
394 $this->assertEquals(get_class($mock2), get_class($mock3));
395 $this->assertNotEquals(get_class($mock2), get_class($mock4));
396 $this->assertNotEquals(get_class($mock2), get_class($mock5));
397 $this->assertEquals(get_class($mock4), get_class($mock5));
400 public function testMockClassStoreOverrulable()
402 $mock1 = $this->getMockBuilder(PartialMockTestClass::class)
403 ->getMock();
405 $mock2 = $this->getMockBuilder(PartialMockTestClass::class)
406 ->setMockClassName('MyMockClassNameForPartialMockTestClass1')
407 ->getMock();
409 $mock3 = $this->getMockBuilder(PartialMockTestClass::class)
410 ->getMock();
412 $mock4 = $this->getMockBuilder(PartialMockTestClass::class)
413 ->setMethods(['doSomething'])
414 ->setMockClassName('AnotherMockClassNameForPartialMockTestClass')
415 ->getMock();
417 $mock5 = $this->getMockBuilder(PartialMockTestClass::class)
418 ->setMockClassName('MyMockClassNameForPartialMockTestClass2')
419 ->getMock();
421 $this->assertNotEquals(get_class($mock1), get_class($mock2));
422 $this->assertEquals(get_class($mock1), get_class($mock3));
423 $this->assertNotEquals(get_class($mock1), get_class($mock4));
424 $this->assertNotEquals(get_class($mock2), get_class($mock3));
425 $this->assertNotEquals(get_class($mock2), get_class($mock4));
426 $this->assertNotEquals(get_class($mock2), get_class($mock5));
427 $this->assertNotEquals(get_class($mock3), get_class($mock4));
428 $this->assertNotEquals(get_class($mock3), get_class($mock5));
429 $this->assertNotEquals(get_class($mock4), get_class($mock5));
432 public function testGetMockWithFixedClassNameCanProduceTheSameMockTwice()
434 $mock = $this->getMockBuilder(stdClass::class)->setMockClassName('FixedName')->getMock();
435 $mock = $this->getMockBuilder(stdClass::class)->setMockClassName('FixedName')->getMock();
436 $this->assertInstanceOf(stdClass::class, $mock);
439 public function testOriginalConstructorSettingConsidered()
441 $mock1 = $this->getMockBuilder(PartialMockTestClass::class)
442 ->getMock();
444 $mock2 = $this->getMockBuilder(PartialMockTestClass::class)
445 ->disableOriginalConstructor()
446 ->getMock();
448 $this->assertTrue($mock1->constructorCalled);
449 $this->assertFalse($mock2->constructorCalled);
452 public function testOriginalCloneSettingConsidered()
454 $mock1 = $this->getMockBuilder(PartialMockTestClass::class)
455 ->getMock();
457 $mock2 = $this->getMockBuilder(PartialMockTestClass::class)
458 ->disableOriginalClone()
459 ->getMock();
461 $this->assertNotEquals(get_class($mock1), get_class($mock2));
464 public function testGetMockForAbstractClass()
466 $mock = $this->getMockBuilder(AbstractMockTestClass::class)
467 ->getMock();
469 $mock->expects($this->never())
470 ->method('doSomething');
474 * @dataProvider traversableProvider
476 public function testGetMockForTraversable($type)
478 $mock = $this->getMockBuilder($type)
479 ->getMock();
481 $this->assertInstanceOf(Traversable::class, $mock);
484 public function testMultipleInterfacesCanBeMockedInSingleObject()
486 $mock = $this->getMockBuilder([AnInterface::class, AnotherInterface::class])
487 ->getMock();
489 $this->assertInstanceOf(AnInterface::class, $mock);
490 $this->assertInstanceOf(AnotherInterface::class, $mock);
493 public function testGetMockForTrait()
495 $mock = $this->getMockForTrait(AbstractTrait::class);
497 $mock->expects($this->never())
498 ->method('doSomething');
500 $parent = get_parent_class($mock);
501 $traits = class_uses($parent, false);
503 $this->assertContains(AbstractTrait::class, $traits);
506 public function testClonedMockObjectShouldStillEqualTheOriginal()
508 $a = $this->getMockBuilder(stdClass::class)
509 ->getMock();
511 $b = clone $a;
513 $this->assertEquals($a, $b);
516 public function testMockObjectsConstructedIndepentantlyShouldBeEqual()
518 $a = $this->getMockBuilder(stdClass::class)
519 ->getMock();
521 $b = $this->getMockBuilder(stdClass::class)
522 ->getMock();
524 $this->assertEquals($a, $b);
527 public function testMockObjectsConstructedIndepentantlyShouldNotBeTheSame()
529 $a = $this->getMockBuilder(stdClass::class)
530 ->getMock();
532 $b = $this->getMockBuilder(stdClass::class)
533 ->getMock();
535 $this->assertNotSame($a, $b);
538 public function testClonedMockObjectCanBeUsedInPlaceOfOriginalOne()
540 $x = $this->getMockBuilder(stdClass::class)
541 ->getMock();
543 $y = clone $x;
545 $mock = $this->getMockBuilder(stdClass::class)
546 ->setMethods(['foo'])
547 ->getMock();
549 $mock->expects($this->once())
550 ->method('foo')
551 ->with($this->equalTo($x));
553 $mock->foo($y);
556 public function testClonedMockObjectIsNotIdenticalToOriginalOne()
558 $x = $this->getMockBuilder(stdClass::class)
559 ->getMock();
561 $y = clone $x;
563 $mock = $this->getMockBuilder(stdClass::class)
564 ->setMethods(['foo'])
565 ->getMock();
567 $mock->expects($this->once())
568 ->method('foo')
569 ->with($this->logicalNot($this->identicalTo($x)));
571 $mock->foo($y);
574 public function testObjectMethodCallWithArgumentCloningEnabled()
576 $expectedObject = new stdClass;
578 $mock = $this->getMockBuilder('SomeClass')
579 ->setMethods(['doSomethingElse'])
580 ->enableArgumentCloning()
581 ->getMock();
583 $actualArguments = [];
585 $mock->expects($this->any())
586 ->method('doSomethingElse')
587 ->will(
588 $this->returnCallback(
589 function () use (&$actualArguments) {
590 $actualArguments = func_get_args();
595 $mock->doSomethingElse($expectedObject);
597 $this->assertEquals(1, count($actualArguments));
598 $this->assertEquals($expectedObject, $actualArguments[0]);
599 $this->assertNotSame($expectedObject, $actualArguments[0]);
602 public function testObjectMethodCallWithArgumentCloningDisabled()
604 $expectedObject = new stdClass;
606 $mock = $this->getMockBuilder('SomeClass')
607 ->setMethods(['doSomethingElse'])
608 ->disableArgumentCloning()
609 ->getMock();
611 $actualArguments = [];
613 $mock->expects($this->any())
614 ->method('doSomethingElse')
615 ->will(
616 $this->returnCallback(
617 function () use (&$actualArguments) {
618 $actualArguments = func_get_args();
623 $mock->doSomethingElse($expectedObject);
625 $this->assertEquals(1, count($actualArguments));
626 $this->assertSame($expectedObject, $actualArguments[0]);
629 public function testArgumentCloningOptionGeneratesUniqueMock()
631 $mockWithCloning = $this->getMockBuilder('SomeClass')
632 ->setMethods(['doSomethingElse'])
633 ->enableArgumentCloning()
634 ->getMock();
636 $mockWithoutCloning = $this->getMockBuilder('SomeClass')
637 ->setMethods(['doSomethingElse'])
638 ->disableArgumentCloning()
639 ->getMock();
641 $this->assertNotEquals($mockWithCloning, $mockWithoutCloning);
644 public function testVerificationOfMethodNameFailsWithoutParameters()
646 $mock = $this->getMockBuilder(SomeClass::class)
647 ->setMethods(['right', 'wrong'])
648 ->getMock();
650 $mock->expects($this->once())
651 ->method('right');
653 $mock->wrong();
655 try {
656 $mock->__phpunit_verify();
657 $this->fail('Expected exception');
658 } catch (PHPUnit_Framework_ExpectationFailedException $e) {
659 $this->assertSame(
660 "Expectation failed for method name is equal to <string:right> when invoked 1 time(s).\n"
661 . "Method was expected to be called 1 times, actually called 0 times.\n",
662 $e->getMessage()
666 $this->resetMockObjects();
669 public function testVerificationOfMethodNameFailsWithParameters()
671 $mock = $this->getMockBuilder(SomeClass::class)
672 ->setMethods(['right', 'wrong'])
673 ->getMock();
675 $mock->expects($this->once())
676 ->method('right');
678 $mock->wrong();
680 try {
681 $mock->__phpunit_verify();
682 $this->fail('Expected exception');
683 } catch (PHPUnit_Framework_ExpectationFailedException $e) {
684 $this->assertSame(
685 "Expectation failed for method name is equal to <string:right> when invoked 1 time(s).\n"
686 . "Method was expected to be called 1 times, actually called 0 times.\n",
687 $e->getMessage()
691 $this->resetMockObjects();
694 public function testVerificationOfMethodNameFailsWithWrongParameters()
696 $mock = $this->getMockBuilder(SomeClass::class)
697 ->setMethods(['right', 'wrong'])
698 ->getMock();
700 $mock->expects($this->once())
701 ->method('right')
702 ->with(['first', 'second']);
704 try {
705 $mock->right(['second']);
706 } catch (PHPUnit_Framework_ExpectationFailedException $e) {
707 $this->assertSame(
708 "Expectation failed for method name is equal to <string:right> when invoked 1 time(s)\n"
709 . "Parameter 0 for invocation SomeClass::right(Array (...)) does not match expected value.\n"
710 . 'Failed asserting that two arrays are equal.',
711 $e->getMessage()
715 try {
716 $mock->__phpunit_verify();
717 $this->fail('Expected exception');
718 } catch (PHPUnit_Framework_ExpectationFailedException $e) {
719 $this->assertSame(
720 "Expectation failed for method name is equal to <string:right> when invoked 1 time(s).\n"
721 . "Parameter 0 for invocation SomeClass::right(Array (...)) does not match expected value.\n"
722 . "Failed asserting that two arrays are equal.\n"
723 . "--- Expected\n"
724 . "+++ Actual\n"
725 . "@@ @@\n"
726 . " Array (\n"
727 . "- 0 => 'first'\n"
728 . "- 1 => 'second'\n"
729 . "+ 0 => 'second'\n"
730 . " )\n",
731 $e->getMessage()
735 $this->resetMockObjects();
738 public function testVerificationOfNeverFailsWithEmptyParameters()
740 $mock = $this->getMockBuilder(SomeClass::class)
741 ->setMethods(['right', 'wrong'])
742 ->getMock();
744 $mock->expects($this->never())
745 ->method('right')
746 ->with();
748 try {
749 $mock->right();
750 $this->fail('Expected exception');
751 } catch (PHPUnit_Framework_ExpectationFailedException $e) {
752 $this->assertSame(
753 'SomeClass::right() was not expected to be called.',
754 $e->getMessage()
758 $this->resetMockObjects();
761 public function testVerificationOfNeverFailsWithAnyParameters()
763 $mock = $this->getMockBuilder(SomeClass::class)
764 ->setMethods(['right', 'wrong'])
765 ->getMock();
767 $mock->expects($this->never())
768 ->method('right')
769 ->withAnyParameters();
771 try {
772 $mock->right();
773 $this->fail('Expected exception');
774 } catch (PHPUnit_Framework_ExpectationFailedException $e) {
775 $this->assertSame(
776 'SomeClass::right() was not expected to be called.',
777 $e->getMessage()
781 $this->resetMockObjects();
784 public function testWithAnythingInsteadOfWithAnyParameters()
786 $mock = $this->getMockBuilder(SomeClass::class)
787 ->setMethods(['right', 'wrong'])
788 ->getMock();
790 $mock->expects($this->once())
791 ->method('right')
792 ->with($this->anything());
794 try {
795 $mock->right();
796 $this->fail('Expected exception');
797 } catch (PHPUnit_Framework_ExpectationFailedException $e) {
798 $this->assertSame(
799 "Expectation failed for method name is equal to <string:right> when invoked 1 time(s)\n" .
800 "Parameter count for invocation SomeClass::right() is too low.\n" .
801 'To allow 0 or more parameters with any value, omit ->with() or use ->withAnyParameters() instead.',
802 $e->getMessage()
806 $this->resetMockObjects();
810 * See https://github.com/sebastianbergmann/phpunit-mock-objects/issues/81
812 public function testMockArgumentsPassedByReference()
814 $foo = $this->getMockBuilder('MethodCallbackByReference')
815 ->setMethods(['bar'])
816 ->disableOriginalConstructor()
817 ->disableArgumentCloning()
818 ->getMock();
820 $foo->expects($this->any())
821 ->method('bar')
822 ->will($this->returnCallback([$foo, 'callback']));
824 $a = $b = $c = 0;
826 $foo->bar($a, $b, $c);
828 $this->assertEquals(1, $b);
832 * See https://github.com/sebastianbergmann/phpunit-mock-objects/issues/81
834 public function testMockArgumentsPassedByReference2()
836 $foo = $this->getMockBuilder('MethodCallbackByReference')
837 ->disableOriginalConstructor()
838 ->disableArgumentCloning()
839 ->getMock();
841 $foo->expects($this->any())
842 ->method('bar')
843 ->will($this->returnCallback(
844 function (&$a, &$b, $c) {
845 $b = 1;
849 $a = $b = $c = 0;
851 $foo->bar($a, $b, $c);
853 $this->assertEquals(1, $b);
857 * @see https://github.com/sebastianbergmann/phpunit-mock-objects/issues/116
859 public function testMockArgumentsPassedByReference3()
861 $foo = $this->getMockBuilder('MethodCallbackByReference')
862 ->setMethods(['bar'])
863 ->disableOriginalConstructor()
864 ->disableArgumentCloning()
865 ->getMock();
867 $a = new stdClass;
868 $b = $c = 0;
870 $foo->expects($this->any())
871 ->method('bar')
872 ->with($a, $b, $c)
873 ->will($this->returnCallback([$foo, 'callback']));
875 $this->assertNull($foo->bar($a, $b, $c));
879 * @see https://github.com/sebastianbergmann/phpunit/issues/796
881 public function testMockArgumentsPassedByReference4()
883 $foo = $this->getMockBuilder('MethodCallbackByReference')
884 ->setMethods(['bar'])
885 ->disableOriginalConstructor()
886 ->disableArgumentCloning()
887 ->getMock();
889 $a = new stdClass;
890 $b = $c = 0;
892 $foo->expects($this->any())
893 ->method('bar')
894 ->with($this->isInstanceOf(stdClass::class), $b, $c)
895 ->will($this->returnCallback([$foo, 'callback']));
897 $this->assertNull($foo->bar($a, $b, $c));
901 * @requires extension soap
903 public function testCreateMockFromWsdl()
905 $mock = $this->getMockFromWsdl(__DIR__ . '/_fixture/GoogleSearch.wsdl', 'WsdlMock');
907 $this->assertStringStartsWith(
908 'Mock_WsdlMock_',
909 get_class($mock)
914 * @requires extension soap
916 public function testCreateNamespacedMockFromWsdl()
918 $mock = $this->getMockFromWsdl(__DIR__ . '/_fixture/GoogleSearch.wsdl', 'My\\Space\\WsdlMock');
920 $this->assertStringStartsWith(
921 'Mock_WsdlMock_',
922 get_class($mock)
927 * @requires extension soap
929 public function testCreateTwoMocksOfOneWsdlFile()
931 $a = $this->getMockFromWsdl(__DIR__ . '/_fixture/GoogleSearch.wsdl');
932 $b = $this->getMockFromWsdl(__DIR__ . '/_fixture/GoogleSearch.wsdl');
934 $this->assertStringStartsWith('Mock_GoogleSearch_', get_class($a));
935 $this->assertEquals(get_class($a), get_class($b));
939 * @see https://github.com/sebastianbergmann/phpunit-mock-objects/issues/156
940 * @ticket 156
942 public function testInterfaceWithStaticMethodCanBeStubbed()
944 $this->assertInstanceOf(
945 InterfaceWithStaticMethod::class,
946 $this->getMockBuilder(InterfaceWithStaticMethod::class)->getMock()
951 * @expectedException PHPUnit_Framework_MockObject_BadMethodCallException
953 public function testInvokingStubbedStaticMethodRaisesException()
955 $mock = $this->getMockBuilder(ClassWithStaticMethod::class)->getMock();
957 $mock->staticMethod();
961 * @see https://github.com/sebastianbergmann/phpunit-mock-objects/issues/171
962 * @ticket 171
964 public function testStubForClassThatImplementsSerializableCanBeCreatedWithoutInvokingTheConstructor()
966 $this->assertInstanceOf(
967 ClassThatImplementsSerializable::class,
968 $this->getMockBuilder(ClassThatImplementsSerializable::class)
969 ->disableOriginalConstructor()
970 ->getMock()
974 public function testGetMockForClassWithSelfTypeHint()
976 $this->assertInstanceOf(
977 ClassWithSelfTypeHint::class,
978 $this->getMockBuilder(ClassWithSelfTypeHint::class)->getMock()
982 private function resetMockObjects()
984 $refl = new ReflectionObject($this);
985 $refl = $refl->getParentClass();
986 $prop = $refl->getProperty('mockObjects');
987 $prop->setAccessible(true);
988 $prop->setValue($this, []);
991 public function testStringableClassDoesNotThrow()
993 $mock = $this->getMockBuilder(StringableClass::class)->getMock();
995 $this->assertInternalType('string', (string) $mock);
998 public function testStringableClassCanBeMocked()
1000 $mock = $this->getMockBuilder(StringableClass::class)->getMock();
1002 $mock->method('__toString')->willReturn('foo');
1004 $this->assertSame('foo', (string) $mock);
1007 public function traversableProvider()
1009 return [
1010 ['Traversable'],
1011 ['\Traversable'],
1012 ['TraversableMockTestInterface'],
1013 [['Traversable']],
1014 [['Iterator','Traversable']],
1015 [['\Iterator','\Traversable']]
1019 public function testParameterCallbackConstraintOnlyEvaluatedOnce()
1021 $mock = $this->getMockBuilder(Foo::class)->setMethods(['bar'])->getMock();
1022 $expectedNumberOfCalls = 1;
1023 $callCount = 0;
1025 $mock->expects($this->exactly($expectedNumberOfCalls))->method('bar')
1026 ->with($this->callback(function ($argument) use (&$callCount) {
1027 return $argument === 'call_' . $callCount++;
1028 }));
1030 for ($i = 0; $i < $expectedNumberOfCalls; $i++) {
1031 $mock->bar('call_' . $i);