Exemplo n.º 1
0
 function testCanCreateBasicMockClassOfBuiltin()
 {
     $mock = Phockito::mock(SoapClient::class);
     $this->assertInstanceOf(SoapClient::class, $mock);
     $this->assertNull($mock->Foo());
     $this->assertNull($mock->Bar());
 }
Exemplo n.º 2
0
 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);
 }
Exemplo n.º 4
0
 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);
 }
Exemplo n.º 6
0
 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();
 }
Exemplo n.º 7
0
 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);
 }
Exemplo n.º 8
0
/**
 * @return VerificationMode
 */
function never()
{
    return Phockito::never();
}
Exemplo n.º 9
0
 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]));
 }
Exemplo n.º 10
0
 function testCanSpyAndOverrideUndefinedToString()
 {
     $mock = Phockito::spy(MockWithoutToString::class);
     Phockito::when($mock)->__toString()->return('NewReturnValue');
     $this->assertEquals('NewReturnValue', '' . $mock);
 }
Exemplo n.º 11
0
 function testCanStubTypeHintedMethodsByPassingOnlyMockIntoWhen()
 {
     $mock = Phockito::mock(MockPassMe::class);
     Phockito::when($mock)->Baz(anything())->return('PassMe');
     $this->assertEquals($mock->Baz(new PassMe()), 'PassMe');
 }
Exemplo n.º 12
0
 /**
  * @expectedException \PHPUnit_Framework_Error
  * @expectedExceptionCode E_USER_ERROR
  */
 function testCannotMockFinalClass()
 {
     Phockito::mock(FinalClass::class);
 }
Exemplo n.º 13
0
 function testConstructorSupressedWhenDesired()
 {
     $spy = Phockito::spy(SpyMe::class, Phockito::DONT_CALL_CONSTRUCTOR);
     $this->assertFalse($spy->constructor_arg);
 }
Exemplo n.º 14
0
 /**
  * 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;
 }
Exemplo n.º 15
0
 /**
  * @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');
 }