function testCanCreateBasicMockClassOfBuiltin() { $mock = Phockito::mock(SoapClient::class); $this->assertInstanceOf(SoapClient::class, $mock); $this->assertNull($mock->Foo()); $this->assertNull($mock->Bar()); }
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'); }
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 testTwoCallsVerifiedWithAtMostPassesVerificationAgainstNoMoreInteractions() { $mock = Phockito::mock(self::MOCK_CLASS); $mock->Foo(); $mock->Foo(); Phockito::verify($mock, Phockito::atMost(2))->Foo(); Phockito::verifyNoMoreInteractions($mock); }
function testCanResetCallRecordForSpecificMethod() { $mock = Phockito::mock(MockMe::class); $mock->Foo(); $mock->Bar(); Phockito::verify($mock)->Foo(); Phockito::verify($mock)->Bar(); Phockito::reset($mock, 'Foo'); Phockito::verify($mock, 0)->Foo(); Phockito::verify($mock)->Bar(); }
function __call($called, $args) { if ($this->mode instanceof VerificationMode) { $verificationMode = $this->mode; } else { if (preg_match('/([0-9]+)\\+/', $this->mode, $match)) { $verificationMode = Phockito::atLeast((int) $match[1]); } else { $verificationMode = Phockito::times($this->mode); } } $verificationContext = new VerificationContext($this->instance, $called, $args); $verificationResult = $verificationMode->verify($verificationContext); if ($verificationResult instanceof SuccessfulVerificationResult) { $verificationContext->markMatchingInvocationsAsVerified(); return; } (new UnsuccessfulVerificationReporter())->reportUnsuccessfulVerification($verificationResult); }
/** * @return VerificationMode */ function never() { return Phockito::never(); }
function testImplementsInjectionOfAnInterfaceDouble() { global $_PSST_ALL_CLASSES; $class = Phockito::mock_class(PhockitoSilverStripeTest_Interface::class); $this->assertTrue(array_key_exists($class, $_PSST_ALL_CLASSES['implementors'][PhockitoSilverStripeTest_Interface::class])); }
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'); }
/** * @expectedException \PHPUnit_Framework_Error * @expectedExceptionCode E_USER_ERROR */ function testCannotMockFinalClass() { Phockito::mock(FinalClass::class); }
function testConstructorSupressedWhenDesired() { $spy = Phockito::spy(SpyMe::class, Phockito::DONT_CALL_CONSTRUCTOR); $this->assertFalse($spy->constructor_arg); }
/** * Creates a special mock of $type which wraps the given $matcher. * * @param string $type Name of the class to subtype * @param Matcher $matcher The matcher to proxy * @return Object A special mock of type $type that wraps $matcher, circumventing type issues. */ public static function argOfTypeThat($type, Matcher $matcher) { $mockOfType = Phockito::mock($type); $mockOfType->__phockito_matcher = $matcher; return $mockOfType; }
/** * @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'); }