function testCanPartiallyStub() { $spy = Phockito::spy(SpyMe::class); Phockito::when($spy)->Foo()->return(1); $this->assertEquals($spy->Foo(), 1); $this->assertEquals($spy->Bar(), 1); }
private function _setMatchingInvocationsTo($count) { $matchingInvocations = array(); for ($i = 0; $i < $count; $i++) { $matchingInvocations[] = $this->_mockInvocation; } Phockito::when($this->_mockContext->getMatchingInvocations())->return($matchingInvocations); }
function testSpyingCall() { $spy = Phockito::spy(OverloadedCall::class); $this->assertEquals($spy->Foo(), 'Foo'); Phockito::when($spy)->Foo()->return(1); $this->assertEquals($spy->Foo(), 1); Phockito::verify($spy, 2)->Foo(); }
function testCanMockNamespacedClassWithGloballyResolvedTypedArgument() { $mock = Phockito::mock(HasGloballyResolvedTypedArguments::class); $arg = new Type(); $this->assertNull($mock->Foo($arg)); Phockito::when($mock->Foo($arg))->return('Bar'); $this->assertEquals($mock->Foo($arg), 'Bar'); }
function testCanResetStubbedResultsForSpecificMethod() { $mock = Phockito::mock(MockMe::class); Phockito::when($mock)->Foo()->return(1); Phockito::when($mock)->Bar()->return(2); $this->assertEquals($mock->Foo(), 1); $this->assertEquals($mock->Foo(), 1); $this->assertEquals($mock->Bar(), 2); $this->assertEquals($mock->Bar(), 2); Phockito::reset($mock, 'Foo'); $this->assertNull($mock->Foo()); $this->assertEquals($mock->Bar(), 2); }
/** * When builder. Starts stubbing the method called to build the argument passed to when * * @param MockMarker|object|mixed|null $arg * @return WhenBuilder|Object */ function when($arg) { return Phockito::when($arg); }
function testCanSpyAndOverrideUndefinedToString() { $mock = Phockito::spy(MockWithoutToString::class); Phockito::when($mock)->__toString()->return('NewReturnValue'); $this->assertEquals('NewReturnValue', '' . $mock); }
function testCanStubTypeHintedMethodsByPassingOnlyMockIntoWhen() { $mock = Phockito::mock(MockPassMe::class); Phockito::when($mock)->Baz(anything())->return('PassMe'); $this->assertEquals($mock->Baz(new PassMe()), 'PassMe'); }
function testCanSpecifyReturnObjectForReferenceInterfaceImplemented() { //this call will fatal error if the derived type's method doesn't also return by ref //This is because it's defined like this in the interface (weird..) $mock = Phockito::mock(FooReturnsByReferenceImplements::class); $obj = new stdClass(); Phockito::when($mock->Foo())->return($obj); $res =& $mock->Foo(); $this->assertEquals($obj, $res); }
/** * @expectedException PHPUnit_Framework_Error * @expectedExceptionCode E_USER_ERROR * @expectedExceptionMessage Can't mock non-existent class NotAClass */ function testBridgingInvalidTypeThrowsException() { $mock = Phockito::mock(HamcrestMe::class); Phockito::when($mock->Foo(HamcrestTypeBridge::argOfTypeThat('NotAClass', anInstanceOf('NotAClass'))))->return('PassMe'); }