Best Phake code snippet using PhakeTest_MockedClass.fooWithReturnValue
PhakeTest.php
Source:PhakeTest.php
...182 public function testMultipleStubs()183 {184 $mock = Phake::mock('PhakeTest_MockedClass');185 Phake::when($mock)->foo()->thenReturn(24);186 Phake::when($mock)->fooWithReturnValue()->thenReturn(42);187 $this->assertEquals(24, $mock->foo());188 $this->assertEquals(42, $mock->fooWithReturnValue());189 }190 /**191 * Tests using multiple stubs.192 */193 public function testConsecutiveCalls()194 {195 $mock = Phake::mock('PhakeTest_MockedClass');196 Phake::when($mock)->foo()->thenReturn(24)->thenReturn(42);197 $this->assertEquals(24, $mock->foo());198 $this->assertEquals(42, $mock->foo());199 }200 /**201 * Tests passing a basic equals matcher to the verify method will correctly verify a call.202 */203 public function testVerifyCallWithEqualsMatcher()204 {205 $mock = Phake::mock('PhakeTest_MockedClass');206 $mock->fooWithArgument('bar');207 Phake::verify($mock)->fooWithArgument(Phake::equalTo('bar'));208 }209 /**210 * Tests passing a basic equals matcher to the verify method will correctly fail when matcher is not satisfied.211 *212 * @expectedException Phake_Exception_VerificationException213 */214 public function testVerifyCallWithEqualsMatcherFails()215 {216 $mock = Phake::mock('PhakeTest_MockedClass');217 $mock->fooWithArgument('test');218 Phake::verify($mock)->fooWithArgument(Phake::equalTo('bar'));219 }220 /**221 * Tests that we can implicitely indicate an equalTo matcher when we pass in a non-matcher value.222 */223 public function testVerifyCallWithDefaultMatcher()224 {225 $mock = Phake::mock('PhakeTest_MockedClass');226 $mock->fooWithArgument('bar');227 Phake::verify($mock)->fooWithArgument('bar');228 }229 /**230 * Tests passing a default matcher type to the verify method will correctly fail when matcher is not satisfied.231 *232 * @expectedException Phake_Exception_VerificationException233 */234 public function testVerifyCallWithDefaultMatcherFails()235 {236 $mock = Phake::mock('PhakeTest_MockedClass');237 $mock->fooWithArgument('test');238 Phake::verify($mock)->fooWithArgument('bar');239 }240 /**241 * Tests passing in a PHPUnit constraint to the verifier242 */243 public function testVerifyCallWithPHPUnitMatcher()244 {245 $mock = Phake::mock('PhakeTest_MockedClass');246 $mock->fooWithArgument('bar');247 Phake::verify($mock)->fooWithArgument($this->equalTo('bar'));248 }249 /**250 * Tests passing in a PHPUnit constraint to the verifier fails when constraint not met.251 *252 * @expectedException Phake_Exception_VerificationException253 */254 public function testVerifyCallWithPHPUnitMatcherFails()255 {256 $mock = Phake::mock('PhakeTest_MockedClass');257 $mock->fooWithArgument('test');258 Phake::verify($mock)->fooWithArgument($this->equalTo('bar'));259 }260 /**261 * Tests passing in a Hamcrest matcher to the verifier262 */263 public function testVerifyCallWithHamcrestMatcher()264 {265 $mock = Phake::mock('PhakeTest_MockedClass');266 $mock->fooWithArgument('bar');267 Phake::verify($mock)->fooWithArgument(equalTo('bar'));268 }269 /**270 * Tests passing in a Hamcrest matcher to the verifier fails when constraint not met.271 *272 * @expectedException Phake_Exception_VerificationException273 */274 public function testVerifyCallWithHamcrestMatcherFails()275 {276 $mock = Phake::mock('PhakeTest_MockedClass');277 $mock->fooWithArgument('test');278 Phake::verify($mock)->fooWithArgument(equalTo('bar'));279 }280 /**281 * Tests using an equalTo argument matcher with a method stub282 */283 public function testStubWithEqualsMatcher()284 {285 $mock = Phake::mock('PhakeTest_MockedClass');286 Phake::when($mock)->fooWithArgument(Phake::equalTo('bar'))->thenReturn(42);287 $this->assertEquals(42, $mock->fooWithArgument('bar'));288 $this->assertNull($mock->fooWithArgument('test'));289 }290 /**291 * Tests using an implicit equalTo argument matcher with a method stub292 */293 public function testStubWithDefaultMatcher()294 {295 $mock = Phake::mock('PhakeTest_MockedClass');296 Phake::when($mock)->fooWithArgument('bar')->thenReturn(42);297 $this->assertEquals(42, $mock->fooWithArgument('bar'));298 $this->assertNull($mock->fooWithArgument('test'));299 }300 /**301 * Tests using a phpunit constraint with a method stub302 */303 public function testStubWithPHPUnitConstraint()304 {305 $mock = Phake::mock('PhakeTest_MockedClass');306 Phake::when($mock)->fooWithArgument($this->equalTo('bar'))->thenReturn(42);307 $this->assertEquals(42, $mock->fooWithArgument('bar'));308 $this->assertNull($mock->fooWithArgument('test'));309 }310 /**311 * Tests using a hamcrest matcher with a method stub312 */313 public function testStubWithHamcrestConstraint()314 {315 $mock = Phake::mock('PhakeTest_MockedClass');316 Phake::when($mock)->fooWithArgument(equalTo('bar'))->thenReturn(42);317 $this->assertEquals(42, $mock->fooWithArgument('bar'));318 $this->assertNull($mock->fooWithArgument('test'));319 }320 /**321 * Tests that resetting a mock clears the call recorder322 */323 public function testResettingCallRecorder()324 {325 $mock = Phake::mock('PhakeTest_MockedClass');326 $mock->foo();327 Phake::verify($mock)->foo();328 Phake::reset($mock);329 $this->setExpectedException('Phake_Exception_VerificationException');330 Phake::verify($mock)->foo();331 }332 /**333 * Tests that resetting a mock clears the stubber334 */335 public function testResettingStubMapper()336 {337 $mock = Phake::mock('PhakeTest_MockedClass');338 Phake::when($mock)->foo()->thenReturn(42);339 $this->assertEquals(42, $mock->foo());340 Phake::reset($mock);341 $this->assertNull($mock->foo());342 }343 /**344 * Tests that resetting a mock clears the call recorder345 */346 public function testResettingStaticCallRecorder()347 {348 $mock = Phake::mock('PhakeTest_StaticInterface');349 $mock::staticMethod();350 Phake::verifyStatic($mock)->staticMethod();351 Phake::resetStatic($mock);352 $this->setExpectedException('Phake_Exception_VerificationException');353 Phake::verifyStatic($mock)->staticMethod();354 }355 public function testMockingPhar()356 {357 if (!class_exists('Phar'))358 {359 $this->markTestSkipped('Phar class does not exist');360 }361 $phar = Phake::mock('Phar');362 $this->assertInstanceOf('Phar', $phar);363 }364 /**365 * Tests that resetting a mock clears the stubber366 */367 public function testResettingStaticStubMapper()368 {369 $mock = Phake::mock('PhakeTest_StaticInterface');370 Phake::whenStatic($mock)->staticMethod()->thenReturn(42);371 $this->assertEquals(42, $mock::staticMethod());372 Phake::resetStatic($mock);373 $this->assertNull($mock::staticMethod());374 }375 /**376 * Tests setting a default answer for stubs377 */378 public function testDefaultAnswerForStubs()379 {380 $mock = Phake::mock('PhakeTest_MockedClass', Phake::ifUnstubbed()->thenReturn(42));381 $this->assertEquals(42, $mock->foo());382 }383 /**384 * Tests setting a default answer for stubs385 */386 public function testDefaultAnswerForInterfaces()387 {388 $mock = Phake::mock('PhakeTest_MockedInterface', Phake::ifUnstubbed()->thenReturn(42));389 $this->assertEquals(42, $mock->foo());390 }391 /**392 * Tests setting a default answer for only the __call magic method393 */394 public function testDefaultAnswerForStubsOfCall()395 {396 $mock = Phake::mock('PhakeTest_MagicClass');397 Phake::whenCallMethodWith(Phake::anyParameters())->isCalledOn($mock)->thenReturn(42);398 $this->assertEquals(42, $mock->foo());399 }400 /**401 * Tests setting a default answer for only the __call magic method402 */403 public function testDefaultAnswerForStaticStubsOfCall()404 {405 $mock = Phake::mock('PhakeTest_MagicClass');406 Phake::whenStaticCallMethodWith(Phake::anyParameters())->isCalledOn($mock)->thenReturn(42);407 $this->assertEquals(42, $mock::foo());408 }409 /**410 * Tests validating calls to __call411 */412 public function testVerificationOfCall()413 {414 $mock = Phake::mock('PhakeTest_MagicClass');415 $mock->foo();416 Phake::verifyCallMethodWith(Phake::anyParameters())->isCalledOn($mock);417 }418 /**419 * Tests validating calls to __callStatic420 */421 public function testVerificationOfStaticCall()422 {423 $mock = Phake::mock('PhakeTest_MagicClass');424 $mock::foo();425 Phake::verifyStaticCallMethodWith(Phake::anyParameters())->isCalledOn($mock);426 }427 /**428 * Tests stubbing a mocked method to call its parent.429 */430 public function testStubbingMethodToCallParent()431 {432 $mock = Phake::mock('PhakeTest_MockedClass');433 Phake::when($mock)->fooWithReturnValue()->thenCallParent();434 $this->assertEquals('blah', $mock->fooWithReturnValue());435 }436 /**437 * Tests calling through a chain of calls438 */439 public function testStubbingChainedMethodsToCallParent()440 {441 $mock = Phake::mock('PhakeTest_MockedClass', Phake::ifUnstubbed()->thenCallParent());442 $this->assertEquals('test', $mock->callInnerFunc());443 }444 /**445 * Tests partial mock functionality to make sure original method is called.446 */447 public function testPartialMockCallsOriginal()448 {449 $pmock = Phake::partialMock('PhakeTest_MockedClass');450 $this->assertEquals('blah', $pmock->fooWithReturnValue());451 }452 /**453 * Tests partial mock calls are recorded454 */455 public function testPartialMockRecordsCall()456 {457 $pmock = Phake::partialMock('PhakeTest_MockedClass');458 $pmock->foo();459 Phake::verify($pmock)->foo();460 }461 /**462 * Tests that partial mock calls can chain properly463 */464 public function testPartialMockInternalMethodCalls()465 {466 $pmock = Phake::partialMock('PhakeTest_MockedClass');467 Phake::when($pmock)->innerFunc()->thenReturn('blah');468 $this->assertEquals('blah', $pmock->chainedCall());469 }470 /**471 * Tests that partial mock can overwrite methods472 * so that they don't do anything when they get called473 */474 public function testPartialMockCanReturnNothing()475 {476 $pmock = Phake::partialMock('PhakeTest_MockedClass');477 Phake::when($pmock)->innerFunc()->thenDoNothing();478 $this->assertNull($pmock->chainedCall());479 }480 /**481 * Tests that partial mocks listen to the constructor args given482 */483 public function testPartialMockCallsConstructor()484 {485 $pmock = Phake::partialMock('PhakeTest_MockedConstructedClass', 'val1', 'val2', 'val3');486 $this->assertEquals('val1', $pmock->getProp1());487 $this->assertEquals('val2', $pmock->getProp2());488 $this->assertEquals('val3', $pmock->getProp3());489 }490 /**491 * Tests that partial mocks with constructors higher in the chain have their constructors called492 */493 public function testPartialMockCallsParentConstructor()494 {495 $pmock = Phake::partialMock('PhakeTest_ExtendedMockedConstructedClass', 'val1', 'val2', 'val3');496 $this->assertEquals('val1', $pmock->getProp1());497 $this->assertEquals('val2', $pmock->getProp2());498 $this->assertEquals('val3', $pmock->getProp3());499 }500 /**501 * Tests that the deprecated partMock works502 */503 public function testPartMock()504 {505 $pmock = Phake::partMock('PhakeTest_ExtendedMockedConstructedClass', 'val1', 'val2', 'val3');506 $this->assertEquals('val1', $pmock->getProp1());507 $this->assertEquals('val2', $pmock->getProp2());508 $this->assertEquals('val3', $pmock->getProp3());509 }510 /**511 * Tests mocking of an interface512 */513 public function testMockingInterface()514 {515 $mock = Phake::mock('PhakeTest_MockedInterface');516 Phake::when($mock)->foo()->thenReturn('bar');517 $this->assertEquals('bar', $mock->foo());518 }519 /**520 * Tests mocking of an abstract class521 */522 public function testMockingAbstract()523 {524 $mock = Phake::mock('PhakeTest_AbstractClass');525 Phake::when($mock)->foo()->thenReturn('bar');526 $this->assertEquals('bar', $mock->foo());527 }528 /**529 * Tests verifying the call order of particular methods within an object530 */531 public function testCallOrderInObject()532 {533 $mock = Phake::mock('PhakeTest_MockedClass');534 $mock->foo();535 $mock->fooWithReturnValue();536 $mock->callInnerFunc();537 Phake::inOrder(538 Phake::verify($mock)->foo(),539 Phake::verify($mock)->fooWithReturnValue(),540 Phake::verify($mock)->callInnerFunc()541 );542 }543 /**544 * Tests verifying the call order of particular methods within an object545 */546 public function testCallOrderInObjectFails()547 {548 $mock = Phake::mock('PhakeTest_MockedClass');549 $mock->foo();550 $mock->callInnerFunc();551 $mock->fooWithReturnValue();552 $this->setExpectedException('Phake_Exception_VerificationException');553 Phake::inOrder(554 Phake::verify($mock)->foo(),555 Phake::verify($mock)->fooWithReturnValue(),556 Phake::verify($mock)->callInnerFunc()557 );558 }559 /**560 * Tests verifying the call order of particular methods across objects561 */562 public function testCallOrderAccrossObjects()563 {564 $mock1 = Phake::mock('PhakeTest_MockedClass');565 $mock2 = Phake::mock('PhakeTest_MockedClass');566 $mock1->foo();567 $mock2->foo();568 $mock1->fooWithReturnValue();569 $mock2->fooWithReturnValue();570 $mock1->callInnerFunc();571 $mock2->callInnerFunc();572 Phake::inOrder(573 Phake::verify($mock1)->foo(),574 Phake::verify($mock2)->foo(),575 Phake::verify($mock2)->fooWithReturnValue(),576 Phake::verify($mock1)->callInnerFunc()577 );578 }579 /**580 * Tests verifying the call order of particular methods across objects581 */582 public function testCallOrderAccrossObjectsFail()583 {584 $mock1 = Phake::mock('PhakeTest_MockedClass');585 $mock2 = Phake::mock('PhakeTest_MockedClass');586 $mock1->foo();587 $mock2->foo();588 $mock1->fooWithReturnValue();589 $mock1->callInnerFunc();590 $mock2->fooWithReturnValue();591 $mock2->callInnerFunc();592 $this->setExpectedException('Phake_Exception_VerificationException');593 Phake::inOrder(594 Phake::verify($mock2)->fooWithReturnValue(),595 Phake::verify($mock1)->callInnerFunc()596 );597 }598 public function testCallOrderWithStatics()599 {600 $mock1 = Phake::mock('PhakeTest_MockedClass');601 $mock2 = Phake::mock('PhakeTest_StaticInterface');602 $mock1->foo();603 $mock2::staticMethod();604 $mock1->fooWithReturnValue();605 $mock1->callInnerFunc();606 Phake::inOrder(607 Phake::verify($mock1)->foo(),608 Phake::verifyStatic($mock2)->staticMethod(),609 Phake::verify($mock1)->callInnerFunc()610 );611 }612 /**613 * Tests freezing mocks614 */615 public function testMockFreezing()616 {617 $mock = Phake::mock('PhakeTest_MockedClass');618 $mock->foo();619 Phake::verifyNoFurtherInteraction($mock);620 $this->setExpectedException('Phake_Exception_VerificationException');621 $mock->foo();622 }623 public function testStaticMockFreezing()624 {625 $mock = Phake::mock('PhakeTest_StaticInterface');626 $mock::staticMethod();627 Phake::verifyNoFurtherInteraction($mock);628 $this->setExpectedException('Phake_Exception_VerificationException');629 $mock::staticMethod();630 }631 /**632 * Tests freezing mocks633 */634 public function testMockFreezingWithMultipleMocks()635 {636 $mock1 = Phake::mock('PhakeTest_MockedClass');637 $mock2 = Phake::mock('PhakeTest_MockedClass');638 $mock1->foo();639 $mock2->foo();640 Phake::verifyNoFurtherInteraction($mock1, $mock2);641 $this->setExpectedException('Phake_Exception_VerificationException');642 $mock2->foo();643 }644 /**645 * Tests verifying that no interaction occured646 */647 public function testVerifyingZeroInteraction()648 {649 $mock = Phake::mock('PhakeTest_MockedClass');650 Phake::verifyNoInteraction($mock);651 $mock->foo();652 $this->setExpectedException('Phake_Exception_VerificationException');653 Phake::verifyNoInteraction($mock);654 }655 /**656 * Tests verifying that no interaction occured657 */658 public function testVerifyingZeroInteractionIncludesStatic()659 {660 $mock = Phake::mock('PhakeTest_StaticInterface');661 Phake::verifyNoInteraction($mock);662 $mock::staticMethod();663 $this->setExpectedException('Phake_Exception_VerificationException');664 Phake::verifyNoInteraction($mock);665 }666 /**667 * Tests verifying that no interaction occured668 */669 public function testVerifyingZeroInteractionWithMultipleArgs()670 {671 $mock1 = Phake::mock('PhakeTest_MockedClass');672 $mock2 = Phake::mock('PhakeTest_MockedClass');673 Phake::verifyNoInteraction($mock1, $mock2);674 $mock2->foo();675 $this->setExpectedException('Phake_Exception_VerificationException');676 Phake::verifyNoInteraction($mock1, $mock2);677 }678 /**679 * Tests argument capturing680 */681 public function testArugmentCapturing()682 {683 $mock = Phake::mock('PhakeTest_MockedClass');684 $mock->fooWithArgument('TEST');685 Phake::verify($mock)->fooWithArgument(Phake::capture($toArgument));686 $this->assertSame('TEST', $toArgument);687 }688 /**689 * Tests conditional argument capturing690 */691 public function testConditionalArugmentCapturing()692 {693 $mock = Phake::mock('PhakeTest_MockedClass');694 $mock->fooWithArgument('FOO');695 $mock->fooWithArgument('BAR');696 Phake::verify($mock)->fooWithArgument(Phake::capture($toArgument)->when('BAR'));697 $this->assertSame('BAR', $toArgument);698 }699 /**700 * Make sure arguments aren't captured if the conditions don't match701 */702 public function testConditionalArugmentCapturingFails()703 {704 $mock = Phake::mock('PhakeTest_MockedClass');705 $mock->fooWithArgument('FOO');706 $this->setExpectedException('Phake_Exception_VerificationException');707 Phake::verify($mock)->fooWithArgument(Phake::capture($toArgument)->when('BAR'));708 }709 /**710 * Make sure arguments are captured with no issues711 */712 public function testArgumentCapturingWorksOnObjects()713 {714 $mock = Phake::mock('PhakeTest_MockedClass');715 $obj = new stdClass;716 $mock->fooWithArgument($obj);717 Phake::verify($mock)->fooWithArgument(Phake::capture($toArgument));718 $this->assertSame($obj, $toArgument);719 }720 /**721 * Make sure arguments are captured with no issues722 */723 public function testArgumentCapturingWorksOnStubbing()724 {725 $mock = Phake::mock('PhakeTest_MockedClass');726 $obj = new stdClass;727 Phake::when($mock)->fooWithArgument(Phake::capture($toArgument))->thenReturn(true);728 $mock->fooWithArgument($obj);729 $this->assertSame($obj, $toArgument);730 }731 public function testArgumentCapturingAllValls()732 {733 $mock = Phake::mock('PhakeTest_MockedClass');734 $obj1 = new stdClass;735 $obj2 = new stdClass;736 $obj3 = new stdClass;737 $mock->fooWithArgument($obj1);738 $mock->fooWithArgument($obj2);739 $mock->fooWithArgument($obj3);740 Phake::verify($mock, Phake::atLeast(1))->fooWithArgument(Phake::captureAll($toArgument));741 $this->assertSame(array($obj1, $obj2, $obj3), $toArgument);742 }743 /**744 * Make sure stub return value capturing returns the parent value745 */746 public function testCaptureAnswerReturnsParentValue()747 {748 $mock = Phake::mock('PhakeTest_MockedClass');749 Phake::when($mock)->fooWithReturnValue()->captureReturnTo($return);750 $this->assertEquals('blah', $mock->fooWithReturnValue());751 }752 /**753 * Make sure stub return value capturing returns the parent value754 */755 public function testCaptureAnswerCapturesParentValue()756 {757 $mock = Phake::mock('PhakeTest_MockedClass');758 Phake::when($mock)->fooWithReturnValue()->captureReturnTo($return);759 $mock->fooWithReturnValue();760 $this->assertEquals('blah', $return);761 }762 /**763 * Tests setting reference parameters764 */765 public function testSettingReferenceParameters()766 {767 $mock = Phake::mock('PhakeTest_MockedClass');768 Phake::when($mock)->fooWithRefParm('test', Phake::setReference(42))->thenReturn(null);769 $mock->fooWithRefParm('test', $value);770 $this->assertSame(42, $value);771 }772 /**773 * Tests conditional reference parameter setting774 */775 public function testConditionalReferenceParameterSetting()776 {777 $mock = Phake::mock('PhakeTest_MockedClass');778 Phake::when($mock)->fooWithRefParm('test', Phake::setReference(42)->when(24))->thenReturn(null);779 $value = 24;780 $mock->fooWithRefParm('test', $value);781 $this->assertSame(42, $value);782 }783 /**784 * Make sure reference parameters aren't set if the conditions don't match785 */786 public function testConditionalReferenceParameterSettingFails()787 {788 $mock = Phake::mock('PhakeTest_MockedClass');789 Phake::when($mock)->fooWithRefParm('test', Phake::setReference(42)->when(24))->thenReturn(null);790 $value = 25;791 $mock->fooWithRefParm('test', $value);792 $this->assertSame(25, $value);793 }794 /**795 * Make sure paremeters are set to objects with no issues796 */797 public function testReferenceParameterSettingWorksOnObjects()798 {799 $mock = Phake::mock('PhakeTest_MockedClass');800 $obj = new stdClass;801 Phake::when($mock)->fooWithRefParm('test', Phake::setReference($obj))->thenReturn(null);802 $value = 25;803 $mock->fooWithRefParm('test', $value);804 $this->assertSame($obj, $value);805 }806 /**807 * Tests times matches exactly808 */809 public function testVerifyTimesExact()810 {811 $mock = Phake::mock('PhakeTest_MockedClass');812 $mock->foo();813 $mock->foo();814 Phake::verify($mock, Phake::times(2))->foo();815 }816 /**817 * Tests times doesn't match818 * @expectedException Phake_Exception_VerificationException819 */820 public function testVerifyTimesMismatch()821 {822 $mock = Phake::mock('PhakeTest_MockedClass');823 $mock->foo();824 $mock->foo();825 Phake::verify($mock)->foo();826 }827 /**828 * Tests at least matches with exact calls829 */830 public function testVerifyAtLeastExact()831 {832 $mock = Phake::mock('PhakeTest_MockedClass');833 $mock->foo();834 Phake::verify($mock, Phake::atLeast(1))->foo();835 }836 /**837 * Tests at least matches with greater calls838 */839 public function testVerifyAtLeastGreater()840 {841 $mock = Phake::mock('PhakeTest_MockedClass');842 $mock->foo();843 $mock->foo();844 Phake::verify($mock, Phake::atLeast(1))->foo();845 }846 /**847 * Tests that at least doesn't match848 * @expectedException Phake_Exception_VerificationException849 */850 public function testVerifyAtLeastMismatch()851 {852 $mock = Phake::mock('PhakeTest_MockedClass');853 Phake::verify($mock, Phake::atLeast(1))->foo();854 }855 /**856 * Tests that never matches857 */858 public function testNeverMatches()859 {860 $mock = Phake::mock('PhakeTest_MockedClass');861 Phake::verify($mock, Phake::never())->foo();862 }863 /**864 * Tests that never catches an invocation865 * @expectedException Phake_Exception_VerificationException866 */867 public function testNeverMismatch()868 {869 $mock = Phake::mock('PhakeTest_MockedClass');870 $mock->foo();871 Phake::verify($mock, Phake::never())->foo();872 }873 /**874 * Tests that atMost passes with exact875 */876 public function testAtMostExactly()877 {878 $mock = Phake::mock('PhakeTest_MockedClass');879 $mock->foo();880 Phake::verify($mock, Phake::atMost(1))->foo();881 }882 /**883 * Tests that atMost passes with under expected calls884 */885 public function testAtMostUnder()886 {887 $mock = Phake::mock('PhakeTest_MockedClass');888 Phake::verify($mock, Phake::atMost(1))->foo();889 }890 /**891 * Tests that atMost fails on over calls892 * @expectedException Phake_Exception_VerificationException893 */894 public function testAtMostOver()895 {896 $mock = Phake::mock('PhakeTest_MockedClass');897 $mock->foo();898 $mock->foo();899 Phake::verify($mock, Phake::atMost(1))->foo();900 }901 /**902 * Tests that the given exception is thrown on thenThrow.903 * @expectedException Phake_Exception_VerificationException904 */905 public function testStubThenThrow()906 {907 $mock = Phake::mock('PhakeTest_MockedClass');908 Phake::when($mock)->foo()->thenThrow(new Phake_Exception_VerificationException());909 $mock->foo();910 }911 /**912 * Tests that Phake::anyParameters() returns an instance of Phake_Matchers_AnyParameters913 */914 public function testAnyParameters()915 {916 $matcher = Phake::anyParameters();917 $this->assertInstanceOf("Phake_Matchers_AnyParameters", $matcher);918 }919 /**920 * Tests that Phake::anyParameters() really matches any invocation921 */922 public function testAnyParametersMatchesEverything()923 {924 $mock = Phake::mock('PhakeTest_MockedClass');925 $mock->fooWithLotsOfParameters(1, 2, 3);926 $mock->fooWithLotsOfParameters(1, 3, 2);927 $mock->fooWithLotsOfParameters(2, 1, 3);928 $mock->fooWithLotsOfParameters(2, 3, 1);929 $mock->fooWithLotsOfParameters(3, 1, 2);930 $mock->fooWithLotsOfParameters(3, 2, 1);931 Phake::verify($mock, Phake::times(6))->fooWithLotsOfParameters(Phake::anyParameters());932 }933 public function testAnyParametersThrowsAnErrorWithTrailingParameters()934 {935 $mock = Phake::mock('PhakeTest_MockedClass');936 $mock->fooWithLotsOfParameters(3, 2, 1);937 $this->setExpectedException('InvalidArgumentException', 'Other matchers cannot be passed with any '938 . 'parameters. It will not work the way you think it works');939 Phake::verify($mock)->fooWithLotsOfParameters(Phake::anyParameters(), 1);940 }941 public function testAnyParametersThrowsAnErrorWithPrecedingParameters()942 {943 $mock = Phake::mock('PhakeTest_MockedClass');944 $mock->fooWithLotsOfParameters(3, 2, 1);945 $this->setExpectedException('InvalidArgumentException', 'Other matchers cannot be passed with any '946 . 'parameters. It will not work the way you think it works');947 Phake::verify($mock)->fooWithLotsOfParameters(3, Phake::anyParameters());948 }949 public function testIgnoreRemainingMatchesEverything()950 {951 $mock = Phake::mock('PhakeTest_MockedClass');952 $mock->fooWithLotsOfParameters(1, 2, 3);953 $mock->fooWithLotsOfParameters(1, 3, 2);954 $mock->fooWithLotsOfParameters(1, 1, 3);955 $mock->fooWithLotsOfParameters(1, 3, 1);956 $mock->fooWithLotsOfParameters(1, 1, 2);957 $mock->fooWithLotsOfParameters(1, 2, 1);958 Phake::verify($mock, Phake::times(6))->fooWithLotsOfParameters(1, Phake::ignoreRemaining());959 }960 public function testIgnoreRemainingThrowsAnErrorWithTrailingParameters()961 {962 $mock = Phake::mock('PhakeTest_MockedClass');963 $mock->fooWithLotsOfParameters(3, 2, 1);964 $this->setExpectedException('InvalidArgumentException', 'Other matchers cannot be checked after you ignore remaining parameters.');965 Phake::verify($mock)->fooWithLotsOfParameters(Phake::ignoreRemaining(), 1);966 }967 /**968 * Tests that when stubs are defined, they're matched in reverse order.969 */970 public function testMatchesInReverseOrder()971 {972 $mock = Phake::mock('PhakeTest_MockedClass');973 Phake::when($mock)->fooWithArgument($this->anything())->thenReturn(false);974 Phake::when($mock)->fooWithArgument('foo')->thenReturn(true);975 $this->assertTrue($mock->fooWithArgument('foo'));976 }977 public function testFailedVerificationWithNoMockInteractions()978 {979 $mock = Phake::mock('PhakeTest_MockedClass');980 $this->setExpectedException(981 'Phake_Exception_VerificationException',982 'Expected PhakeTest_MockedClass->foo() to be called exactly <1> times, actually called <0> times. In fact, there are no interactions with this mock.'983 );984 Phake::verify($mock)->foo();985 }986 public function testFailedVerificationWithNonmatchingMethodCalls()987 {988 $mock = Phake::mock('PhakeTest_MockedClass');989 $mock->foo('test');990 $this->setExpectedException(991 'Phake_Exception_VerificationException',992 'Expected PhakeTest_MockedClass->foo() to be called exactly <1> times, actually called <0> times.' . "\n"993 . "Other Invocations:\n"994 . "===\n"995 . " PhakeTest_MockedClass->foo(<string:test>)\n"996 . " No matchers were given to Phake::when(), but arguments were received by this method.\n"997 . "==="998 );999 Phake::verify($mock)->foo();1000 }1001 public function testStubbingMagicCallMethod()1002 {1003 $mock = Phake::mock('PhakeTest_MagicClass');1004 Phake::when($mock)->magicCall()->thenReturn('magicCalled');1005 $this->assertEquals('magicCalled', $mock->magicCall());1006 }1007 public function testVerifyingMagicCallMethod()1008 {1009 $mock = Phake::mock('PhakeTest_MagicClass');1010 $mock->magicCall();1011 Phake::verify($mock)->magicCall();1012 }1013 public function testStubbingMagicMethodsAlsoResortsToCallIfNoStubsDefined()1014 {1015 $expected = '__call';1016 $mock = Phake::partialMock('PhakeTest_MagicClass');1017 Phake::when($mock)->magicCall()->thenReturn('magicCalled');1018 $this->assertEquals('magicCalled', $mock->magicCall());1019 $this->assertEquals($expected, $mock->unStubbedCall());1020 }1021 public function testStubbingMagicStaticCallMethod()1022 {1023 $mock = Phake::mock('PhakeTest_MagicClass');1024 Phake::whenStatic($mock)->magicCall()->thenReturn('magicCalled');1025 $this->assertEquals('magicCalled', $mock::magicCall());1026 }1027 public function testMockingSoapClient()1028 {1029 // This test requires that E_STRICT be on1030 // It will fail with it on, otherwise it wont' complain1031 $mock = Phake::mock('SoapClient');1032 $this->addToAssertionCount(1);1033 }1034 public function testDefaultClient()1035 {1036 $original_client = Phake::getClient();1037 Phake::setClient(null);1038 $this->assertInstanceOf('Phake_Client_Default', Phake::getClient());1039 Phake::setClient($original_client);1040 }1041 public function testSettingClient()1042 {1043 $original_client = Phake::getClient();1044 $client = Phake::mock('Phake_Client_IClient');1045 Phake::setClient($client);1046 $this->assertSame($client, Phake::getClient());1047 Phake::setClient($original_client);1048 }1049 public function testSettingDefaultClientByString()1050 {1051 $original_client = Phake::getClient();1052 Phake::setClient(Phake::CLIENT_DEFAULT);1053 $this->assertInstanceOf('Phake_Client_Default', Phake::getClient());1054 Phake::setClient($original_client);1055 }1056 public function testSettingPHPUnitClientByString()1057 {1058 $original_client = Phake::getClient();1059 Phake::setClient(Phake::CLIENT_PHPUNIT);1060 $this->assertInstanceOf('Phake_Client_PHPUnit', Phake::getClient());1061 Phake::setClient($original_client);1062 }1063 public function testVerifyNoFurtherInteractionPassesStrict()1064 {1065 Phake::setClient(Phake::CLIENT_PHPUNIT);1066 $mock = Phake::mock('stdClass');1067 $assertionCount = self::getCount();1068 Phake::verifyNoFurtherInteraction($mock);1069 $newAssertionCount = self::getCount();1070 $this->assertGreaterThan($assertionCount, $newAssertionCount);1071 }1072 public function testVerifyNoInteractionPassesStrict()1073 {1074 Phake::setClient(Phake::CLIENT_PHPUNIT);1075 $mock = Phake::mock('stdClass');1076 $assertionCount = self::getCount();1077 Phake::verifyNoInteraction($mock);1078 $newAssertionCount = self::getCount();1079 $this->assertGreaterThan($assertionCount, $newAssertionCount);1080 }1081 public function testMockingStaticClass()1082 {1083 $mock = Phake::mock('PhakeTest_StaticClass');1084 Phake::whenStatic($mock)->staticMethod()->thenReturn('bar');1085 $this->assertEquals('bar', $mock->staticMethod());1086 Phake::verifyStatic($mock)->staticMethod();1087 }1088 public function testMockingStaticInterface()1089 {1090 $mock = Phake::mock('PhakeTest_StaticInterface');1091 $this->assertInstanceOf('Phake_IMock', $mock);1092 }1093 public function testCallingMockStaticMethod()1094 {1095 $mock = Phake::mock('PhakeTest_StaticInterface');1096 $this->assertNull($mock::staticMethod());1097 }1098 public function testVerifyingMockStaticMethod()1099 {1100 $mock = Phake::mock('PhakeTest_StaticInterface');1101 $mock::staticMethod();1102 Phake::verifyStatic($mock)->staticMethod();1103 }1104 public function testMockingAbstractClass()1105 {1106 $mock = Phake::partialMock('PhakeTest_AbstractClass');1107 $this->assertNull($mock->referenceDefault());1108 }1109 public function testStubbingMemcacheSetMethod()1110 {1111 if (!extension_loaded('memcache'))1112 {1113 $this->markTestSkipped('memcache extension not loaded');1114 }1115 $memcache = Phake::mock('Memcache');1116 Phake::when($memcache)->set('key', 'value')->thenReturn(true);1117 $this->assertTrue($memcache->set('key', 'value'));1118 }1119 public function testMockingMethodReturnByReference()1120 {1121 $something = array();1122 $referenceMethodClass = Phake::mock('PhakeTest_ReturnByReferenceMethodClass');1123 Phake::when($referenceMethodClass)->getSomething()->thenReturn($something);1124 $something[] = 'foo';1125 $returnSomething = $referenceMethodClass->getSomething();1126 $this->assertNotContains('foo', $returnSomething);1127 }1128 public function testGetOnMockedClass()1129 {1130 $mock = Phake::mock('PhakeTest_MagicClass');1131 Phake::when($mock)->__get('myId')->thenReturn(500)->thenReturn(501);1132 $this->assertEquals(500, $mock->myId);1133 $this->assertEquals(501, $mock->myId);1134 Phake::verify($mock, Phake::times(2))->__get('myId');1135 }1136 public function testCallOrderInObjectFailsWithPHPUnit()1137 {1138 Phake::setClient(Phake::CLIENT_PHPUNIT);1139 $mock = Phake::mock('PhakeTest_MockedClass');1140 $mock->foo();1141 $mock->callInnerFunc();1142 $mock->fooWithReturnValue();1143 $this->setExpectedException('PHPUnit_Framework_ExpectationFailedException');1144 Phake::inOrder(1145 Phake::verify($mock)->foo(),1146 Phake::verify($mock)->fooWithReturnValue(),1147 Phake::verify($mock)->callInnerFunc()1148 );1149 }1150 public function testGetMockedClassAnythingMatcher()1151 {1152 $mock = Phake::mock('PhakeTest_MagicClass');1153 Phake::when($mock)->__get($this->anything())->thenReturn(500);1154 $this->assertEquals(500, $mock->myId);1155 Phake::verify($mock)->__get($this->anything());1156 }1157 public function testConstructorInterfaceCanBeMocked()1158 {1159 if (defined('HHVM_VERSION')) {1160 $this->markTestSkipped('This test causes a fatal error under HHVM.');...
fooWithReturnValue
Using AI Code Generation
1$mock = Phake::mock('PhakeTest_MockedClass');2Phake::when($mock)->fooWithReturnValue()->thenReturn('bar');3$this->assertEquals('bar', $mock->fooWithReturnValue());4$mock = Phake::mock('PhakeTest_MockedClass');5Phake::when($mock)->fooWithReturnValue()->thenReturn('bar');6$this->assertEquals('bar', $mock->fooWithReturnValue());
fooWithReturnValue
Using AI Code Generation
1$mock = Phake::mock('PhakeTest_MockedClass');2Phake::when($mock)->fooWithReturnValue()->thenReturn(1);3Phake::when($mock)->fooWithReturnValue()->thenReturn(2);4Phake::when($mock)->fooWithReturnValue()->thenReturn(3);5Phake::when($mock)->fooWithReturnValue()->thenReturn(4);6Phake::when($mock)->fooWithReturnValue()->thenReturn(5);7Phake::when($mock)->fooWithReturnValue()->thenReturn(6);8Phake::when($mock)->fooWithReturnValue()->thenReturn(7);9Phake::when($mock)->fooWithReturnValue()->thenReturn(8);10Phake::when($mock)->fooWithReturnValue()->thenReturn(9);11Phake::when($mock)->fooWithReturnValue()->thenReturn(10);12Phake::when($mock)->fooWithReturnValue()->thenReturn(11);13Phake::when($mock)->fooWithReturnValue()->thenReturn(12);14Phake::when($mock)->fooWithReturnValue()->thenReturn(13);15Phake::when($mock)->fooWithReturnValue()->thenReturn(14);16Phake::when($mock)->fooWithReturnValue()->thenReturn(15);17Phake::when($mock)->fooWithReturnValue()->thenReturn(16);18Phake::when($mock)->fooWithReturnValue()->thenReturn(17);19Phake::when($mock)->fooWithReturnValue()->thenReturn(18);20Phake::when($mock)->fooWithReturnValue()->thenReturn(19);21Phake::when($mock)->fooWithReturnValue()->thenReturn(20);22Phake::when($mock)->fooWithReturnValue()->thenReturn(21);23Phake::when($mock)->fooWithReturnValue()->thenReturn(22);24Phake::when($mock)->fooWithReturnValue()->thenReturn(23);25Phake::when($mock)->fooWithReturnValue()->thenReturn(24);26Phake::when($mock)->fooWithReturnValue()->thenReturn(25);27Phake::when($mock)->fooWithReturnValue()->thenReturn(26);28Phake::when($mock)->fooWithReturnValue()->thenReturn(27);29Phake::when($mock)->fooWithReturnValue()->thenReturn(28);30Phake::when($mock)->fooWithReturnValue()->thenReturn(29);31Phake::when($mock)->fooWithReturnValue()->thenReturn(30);32Phake::when($mock)->fooWithReturnValue()->thenReturn(31);33Phake::when($mock)->fooWithReturnValue()->thenReturn(32);
fooWithReturnValue
Using AI Code Generation
1$mockedClass = Phake::mock('PhakeTest_MockedClass');2Phake::when($mockedClass)->fooWithReturnValue()->thenReturn('foo');3$mockedClass = Phake::mock('PhakeTest_MockedClass');4Phake::when($mockedClass)->fooWithReturnValue()->thenReturn('foo');5$mockedClass = Phake::mock('PhakeTest_MockedClass');6Phake::when($mockedClass)->fooWithReturnValue()->thenReturn('foo');7$mockedClass = Phake::mock('PhakeTest_MockedClass');8Phake::when($mockedClass)->fooWithReturnValue()->thenReturn('foo');9$mockedClass = Phake::mock('PhakeTest_MockedClass');10Phake::when($mockedClass)->fooWithReturnValue()->thenReturn('foo');11$mockedClass = Phake::mock('PhakeTest_MockedClass');12Phake::when($mockedClass)->fooWithReturnValue()->thenReturn('foo');13$mockedClass = Phake::mock('PhakeTest_MockedClass');14Phake::when($mockedClass)->fooWithReturnValue()->thenReturn('foo');
fooWithReturnValue
Using AI Code Generation
1$mockedClass->fooWithReturnValue(Phake::anyParameters());2$mockedClass->fooWithReturnValue(Phake::anyParameters());3$mockedClass->fooWithReturnValue(Phake::anyParameters());4$mockedClass->fooWithReturnValue(Phake::anyParameters());5$mockedClass->fooWithReturnValue(Phake::anyParameters());6$mockedClass->fooWithReturnValue(Phake::anyParameters());7$mockedClass->fooWithReturnValue(Phake::anyParameters());8$mockedClass->fooWithReturnValue(Phake::anyParameters());9$mockedClass->fooWithReturnValue(Phake::anyParameters());10$mockedClass->fooWithReturnValue(Phake::anyParameters());11$mockedClass->fooWithReturnValue(Phake::anyParameters());12$mockedClass->fooWithReturnValue(Phake::anyParameters());13$mockedClass->fooWithReturnValue(Phake::anyParameters());14$mockedClass->fooWithReturnValue(Phake::anyParameters());
fooWithReturnValue
Using AI Code Generation
1$mockedClass->fooWithReturnValue(2);2$mockedClass->barWithReturnValue(2);3$mockedClass->fooWithReturnValue(2);4$mockedClass->fooWithReturnValue(3);5$mockedClass->barWithReturnValue(3);6$mockedClass->fooWithReturnValue(3);7$mockedClass->fooWithReturnValue(4);8$mockedClass->barWithReturnValue(4);9$mockedClass->fooWithReturnValue(4);10$mockedClass->fooWithReturnValue(5);11$mockedClass->barWithReturnValue(5);12$mockedClass->fooWithReturnValue(5);13$mockedClass->fooWithReturnValue(6);14$mockedClass->barWithReturnValue(6);15$mockedClass->fooWithReturnValue(6);16$mockedClass->fooWithReturnValue(7);17$mockedClass->barWithReturnValue(7);
fooWithReturnValue
Using AI Code Generation
1$mockedClass = new PhakeTest_MockedClass();2$mockedClass->fooWithReturnValue('argument 1', 'argument 2');3Phake::verify($mockedClass)->fooWithReturnValue('argument 1', 'argument 2');4Phake::verify($mockedClass)->fooWithReturnValue(Phake::anyParameters());5Phake::verify($mockedClass)->fooWithReturnValue(Phake::anyParameters());6Phake::verify($mockedClass)->fooWithReturnValue(Phake::anyParameters());7Phake::verify($mockedClass)->fooWithReturnValue(Phake::anyParameters());8Phake::verify($mockedClass)->fooWithReturnValue(Phake::anyParameters());9Phake::verify($mockedClass)->fooWithReturnValue(Phake::anyParameters());10Phake::verify($mockedClass)->fooWithReturnValue(Phake::anyParameters());11Phake::verify($mockedClass)->fooWithReturnValue(Phake::anyParameters());12Phake::verify($mockedClass)->fooWithReturnValue(Phake::anyParameters());13Phake::verify($mockedClass)->fooWithReturnValue(Phake::anyParameters());14Phake::verify($mockedClass)->fooWithReturnValue(Phake::anyParameters());15Phake::verify($mockedClass)->fooWithReturnValue(Phake::anyParameters());16Phake::verify($mockedClass)->fooWithReturnValue(Phake::anyParameters());
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Execute automation tests with fooWithReturnValue on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.
Test now for FreeGet 100 minutes of automation test minutes FREE!!