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.
11 class Framework_MockObjectTest
extends PHPUnit_Framework_TestCase
13 public function testMockedMethodIsNeverCalled()
15 $mock = $this->getMockBuilder(AnInterface
::class)
18 $mock->expects($this->never())
19 ->method('doSomething');
22 public function testMockedMethodIsNeverCalledWithParameter()
24 $mock = $this->getMockBuilder(SomeClass
::class)
27 $mock->expects($this->never())
28 ->method('doSomething')
33 * @doesNotPerformAssertions
35 public function testMockedMethodIsNotCalledWhenExpectsAnyWithParameter()
37 $mock = $this->getMockBuilder(SomeClass
::class)
40 $mock->expects($this->any())
41 ->method('doSomethingElse')
46 * @doesNotPerformAssertions
48 public function testMockedMethodIsNotCalledWhenMethodSpecifiedDirectlyWithParameter()
50 $mock = $this->getMockBuilder(SomeClass
::class)
53 $mock->method('doSomethingElse')
57 public function testMockedMethodIsCalledAtLeastOnce()
59 $mock = $this->getMockBuilder(AnInterface
::class)
62 $mock->expects($this->atLeastOnce())
63 ->method('doSomething');
68 public function testMockedMethodIsCalledAtLeastOnce2()
70 $mock = $this->getMockBuilder(AnInterface
::class)
73 $mock->expects($this->atLeastOnce())
74 ->method('doSomething');
80 public function testMockedMethodIsCalledAtLeastTwice()
82 $mock = $this->getMockBuilder(AnInterface
::class)
85 $mock->expects($this->atLeast(2))
86 ->method('doSomething');
92 public function testMockedMethodIsCalledAtLeastTwice2()
94 $mock = $this->getMockBuilder(AnInterface
::class)
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)
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)
122 $mock->expects($this->atMost(2))
123 ->method('doSomething');
125 $mock->doSomething();
128 public function testMockedMethodIsCalledOnce()
130 $mock = $this->getMockBuilder(AnInterface
::class)
133 $mock->expects($this->once())
134 ->method('doSomething');
136 $mock->doSomething();
139 public function testMockedMethodIsCalledOnceWithParameter()
141 $mock = $this->getMockBuilder(SomeClass
::class)
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)
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)
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)
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)
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)
205 $mock->expects($this->any())
206 ->method('doSomething')
207 ->willReturn('something');
209 $this->assertEquals('something', $mock->doSomething());
212 public function testStubbedReturnValueMap()
215 ['a', 'b', 'c', 'd'],
219 $mock = $this->getMockBuilder(AnInterface
::class)
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)
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)
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)
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'])
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'])
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)
291 $mock->expects($this->any())
292 ->method('doSomething')
293 ->will($this->returnSelf());
295 $this->assertEquals($mock, $mock->doSomething());
297 $mock = $this->getMockBuilder(AnInterface
::class)
300 $mock->expects($this->any())
301 ->method('doSomething')
304 $this->assertEquals($mock, $mock->doSomething());
307 public function testStubbedReturnOnConsecutiveCalls()
309 $mock = $this->getMockBuilder(AnInterface
::class)
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)
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'])
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'])
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)
363 $mock2 = $this->getMockBuilder(AnInterface
::class)
366 $this->assertEquals(get_class($mock1), get_class($mock2));
369 public function testMockClassDifferentForPartialMocks()
371 $mock1 = $this->getMockBuilder(PartialMockTestClass
::class)
374 $mock2 = $this->getMockBuilder(PartialMockTestClass
::class)
375 ->setMethods(['doSomething'])
378 $mock3 = $this->getMockBuilder(PartialMockTestClass
::class)
379 ->setMethods(['doSomething'])
382 $mock4 = $this->getMockBuilder(PartialMockTestClass
::class)
383 ->setMethods(['doAnotherThing'])
386 $mock5 = $this->getMockBuilder(PartialMockTestClass
::class)
387 ->setMethods(['doAnotherThing'])
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)
405 $mock2 = $this->getMockBuilder(PartialMockTestClass
::class)
406 ->setMockClassName('MyMockClassNameForPartialMockTestClass1')
409 $mock3 = $this->getMockBuilder(PartialMockTestClass
::class)
412 $mock4 = $this->getMockBuilder(PartialMockTestClass
::class)
413 ->setMethods(['doSomething'])
414 ->setMockClassName('AnotherMockClassNameForPartialMockTestClass')
417 $mock5 = $this->getMockBuilder(PartialMockTestClass
::class)
418 ->setMockClassName('MyMockClassNameForPartialMockTestClass2')
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)
444 $mock2 = $this->getMockBuilder(PartialMockTestClass
::class)
445 ->disableOriginalConstructor()
448 $this->assertTrue($mock1->constructorCalled
);
449 $this->assertFalse($mock2->constructorCalled
);
452 public function testOriginalCloneSettingConsidered()
454 $mock1 = $this->getMockBuilder(PartialMockTestClass
::class)
457 $mock2 = $this->getMockBuilder(PartialMockTestClass
::class)
458 ->disableOriginalClone()
461 $this->assertNotEquals(get_class($mock1), get_class($mock2));
464 public function testGetMockForAbstractClass()
466 $mock = $this->getMockBuilder(AbstractMockTestClass
::class)
469 $mock->expects($this->never())
470 ->method('doSomething');
474 * @dataProvider traversableProvider
476 public function testGetMockForTraversable($type)
478 $mock = $this->getMockBuilder($type)
481 $this->assertInstanceOf(Traversable
::class, $mock);
484 public function testMultipleInterfacesCanBeMockedInSingleObject()
486 $mock = $this->getMockBuilder([AnInterface
::class, AnotherInterface
::class])
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)
513 $this->assertEquals($a, $b);
516 public function testMockObjectsConstructedIndepentantlyShouldBeEqual()
518 $a = $this->getMockBuilder(stdClass
::class)
521 $b = $this->getMockBuilder(stdClass
::class)
524 $this->assertEquals($a, $b);
527 public function testMockObjectsConstructedIndepentantlyShouldNotBeTheSame()
529 $a = $this->getMockBuilder(stdClass
::class)
532 $b = $this->getMockBuilder(stdClass
::class)
535 $this->assertNotSame($a, $b);
538 public function testClonedMockObjectCanBeUsedInPlaceOfOriginalOne()
540 $x = $this->getMockBuilder(stdClass
::class)
545 $mock = $this->getMockBuilder(stdClass
::class)
546 ->setMethods(['foo'])
549 $mock->expects($this->once())
551 ->with($this->equalTo($x));
556 public function testClonedMockObjectIsNotIdenticalToOriginalOne()
558 $x = $this->getMockBuilder(stdClass
::class)
563 $mock = $this->getMockBuilder(stdClass
::class)
564 ->setMethods(['foo'])
567 $mock->expects($this->once())
569 ->with($this->logicalNot($this->identicalTo($x)));
574 public function testObjectMethodCallWithArgumentCloningEnabled()
576 $expectedObject = new stdClass
;
578 $mock = $this->getMockBuilder('SomeClass')
579 ->setMethods(['doSomethingElse'])
580 ->enableArgumentCloning()
583 $actualArguments = [];
585 $mock->expects($this->any())
586 ->method('doSomethingElse')
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()
611 $actualArguments = [];
613 $mock->expects($this->any())
614 ->method('doSomethingElse')
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()
636 $mockWithoutCloning = $this->getMockBuilder('SomeClass')
637 ->setMethods(['doSomethingElse'])
638 ->disableArgumentCloning()
641 $this->assertNotEquals($mockWithCloning, $mockWithoutCloning);
644 public function testVerificationOfMethodNameFailsWithoutParameters()
646 $mock = $this->getMockBuilder(SomeClass
::class)
647 ->setMethods(['right', 'wrong'])
650 $mock->expects($this->once())
656 $mock->__phpunit_verify();
657 $this->fail('Expected exception');
658 } catch (PHPUnit_Framework_ExpectationFailedException
$e) {
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",
666 $this->resetMockObjects();
669 public function testVerificationOfMethodNameFailsWithParameters()
671 $mock = $this->getMockBuilder(SomeClass
::class)
672 ->setMethods(['right', 'wrong'])
675 $mock->expects($this->once())
681 $mock->__phpunit_verify();
682 $this->fail('Expected exception');
683 } catch (PHPUnit_Framework_ExpectationFailedException
$e) {
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",
691 $this->resetMockObjects();
694 public function testVerificationOfMethodNameFailsWithWrongParameters()
696 $mock = $this->getMockBuilder(SomeClass
::class)
697 ->setMethods(['right', 'wrong'])
700 $mock->expects($this->once())
702 ->with(['first', 'second']);
705 $mock->right(['second']);
706 } catch (PHPUnit_Framework_ExpectationFailedException
$e) {
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.',
716 $mock->__phpunit_verify();
717 $this->fail('Expected exception');
718 } catch (PHPUnit_Framework_ExpectationFailedException
$e) {
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"
728 . "- 1 => 'second'\n"
729 . "+ 0 => 'second'\n"
735 $this->resetMockObjects();
738 public function testVerificationOfNeverFailsWithEmptyParameters()
740 $mock = $this->getMockBuilder(SomeClass
::class)
741 ->setMethods(['right', 'wrong'])
744 $mock->expects($this->never())
750 $this->fail('Expected exception');
751 } catch (PHPUnit_Framework_ExpectationFailedException
$e) {
753 'SomeClass::right() was not expected to be called.',
758 $this->resetMockObjects();
761 public function testVerificationOfNeverFailsWithAnyParameters()
763 $mock = $this->getMockBuilder(SomeClass
::class)
764 ->setMethods(['right', 'wrong'])
767 $mock->expects($this->never())
769 ->withAnyParameters();
773 $this->fail('Expected exception');
774 } catch (PHPUnit_Framework_ExpectationFailedException
$e) {
776 'SomeClass::right() was not expected to be called.',
781 $this->resetMockObjects();
784 public function testWithAnythingInsteadOfWithAnyParameters()
786 $mock = $this->getMockBuilder(SomeClass
::class)
787 ->setMethods(['right', 'wrong'])
790 $mock->expects($this->once())
792 ->with($this->anything());
796 $this->fail('Expected exception');
797 } catch (PHPUnit_Framework_ExpectationFailedException
$e) {
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.',
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()
820 $foo->expects($this->any())
822 ->will($this->returnCallback([$foo, 'callback']));
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()
841 $foo->expects($this->any())
843 ->will($this->returnCallback(
844 function (&$a, &$b, $c) {
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()
870 $foo->expects($this->any())
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()
892 $foo->expects($this->any())
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(
914 * @requires extension soap
916 public function testCreateNamespacedMockFromWsdl()
918 $mock = $this->getMockFromWsdl(__DIR__
. '/_fixture/GoogleSearch.wsdl', 'My\\Space\\WsdlMock');
920 $this->assertStringStartsWith(
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
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
964 public function testStubForClassThatImplementsSerializableCanBeCreatedWithoutInvokingTheConstructor()
966 $this->assertInstanceOf(
967 ClassThatImplementsSerializable
::class,
968 $this->getMockBuilder(ClassThatImplementsSerializable
::class)
969 ->disableOriginalConstructor()
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()
1012 ['TraversableMockTestInterface'],
1014 [['Iterator','Traversable']],
1015 [['\Iterator','\Traversable']]
1019 public function testParameterCallbackConstraintOnlyEvaluatedOnce()
1021 $mock = $this->getMockBuilder(Foo
::class)->setMethods(['bar'])->getMock();
1022 $expectedNumberOfCalls = 1;
1025 $mock->expects($this->exactly($expectedNumberOfCalls))->method('bar')
1026 ->with($this->callback(function ($argument) use (&$callCount) {
1027 return $argument === 'call_' . $callCount++
;
1030 for ($i = 0; $i < $expectedNumberOfCalls; $i++
) {
1031 $mock->bar('call_' . $i);