Examples:
Chain Stubbing
ShortifyPunit::when($mock)->first_method()->second_method(1)->returns(1);
ShortifyPunit::when($mock)->first_method()->second_method(2)->returns(2);
ShortifyPunit::when($mock)->first_method(1)->second_method(1)->returns(3);
ShortifyPunit::when($mock)->first_method(2)->second_method(2)->third_method()->returns(4);
echo $mock->first_method()->second_method(1); // prints '1'
echo $mock->first_method()->second_method(2); // prints '2'
echo $mock->first_method(1)->second_method(1); // prints '3'
echo $mock->first_method(2)->second_method(2)->third_method(); // prints '4'
public static when ( ShortifyPunit\Mock\MockInterface $mock ) : ShortifyPunit\Stub\WhenChainCase | ||
$mock | ShortifyPunit\Mock\MockInterface | |
리턴 | ShortifyPunit\Stub\WhenChainCase |
public function testOnKernelController() { $request = ShortifyPunit::mock('Symfony\\Component\\HttpFoundation\\Request'); $requestStack = ShortifyPunit::mock('Symfony\\Component\\HttpFoundation\\RequestStack'); $sandboxResponseManager = ShortifyPunit::mock('danrevah\\SandboxBundle\\Managers\\SandboxResponseManager'); $event = ShortifyPunit::mock('Symfony\\Component\\HttpKernel\\Event\\FilterControllerEvent'); $parameterBag = ShortifyPunit::mock('Symfony\\Component\\HttpFoundation\\ParameterBag'); ShortifyPunit::when($request)->getContent()->returns(''); $request->query = $parameterBag; $request->request = $parameterBag; ShortifyPunit::when($requestStack)->getCurrentRequest()->returns($request); ShortifyPunit::when($event)->getController()->returns([0, 1]); ShortifyPunit::when($event)->setController(anything())->returns(1); $sandboxListener = new SandboxListener($requestStack, $sandboxResponseManager); $sandboxListener->onKernelController($event); $this->assertTrue(ShortifyPunit::verify($event)->setController(anything())->atLeastOnce()); $response = [false, 0, 0, 0]; ShortifyPunit::when($sandboxResponseManager)->getResponseController(anything(), anything(), anything(), anything(), anything())->returns($response); $event2 = ShortifyPunit::mock('Symfony\\Component\\HttpKernel\\Event\\FilterControllerEvent'); ShortifyPunit::when($event2)->setController(anything())->returns(''); ShortifyPunit::when($event2)->getController(anything())->returns([0, 1]); $sandboxListener = new SandboxListener($requestStack, $sandboxResponseManager); $sandboxListener->onKernelController($event2); $this->assertTrue(ShortifyPunit::verify($event2)->setController(anything())->neverCalled()); }
/** * Testing with Hamcrest matching functions */ public function testWithHamcrestMatcher() { $mock = ShortifyPunit::mock('Foo'); ShortifyPunit::when($mock)->bar(equalTo(1))->foo(anything())->returns(10); $this->assertTrue(ShortifyPunit::verify($mock)->bar(1)->foo(2)->neverCalled()); $this->assertTrue(ShortifyPunit::verify($mock)->bar(1)->foo(2)->atLeast(0)); $this->assertTrue(ShortifyPunit::verify($mock)->bar(1)->foo(2)->calledTimes(0)); $this->assertTrue(ShortifyPunit::verify($mock)->bar(1)->foo(2)->lessThan(1)); $this->assertFalse(ShortifyPunit::verify($mock)->bar(1)->foo(2)->atLeastOnce()); $mock->bar(1)->foo(2); $this->assertFalse(ShortifyPunit::verify($mock)->bar(1)->foo(2)->neverCalled()); $this->assertTrue(ShortifyPunit::verify($mock)->bar(1)->foo(2)->atLeast(1)); $this->assertTrue(ShortifyPunit::verify($mock)->bar(1)->foo(2)->atLeastOnce()); $this->assertTrue(ShortifyPunit::verify($mock)->bar(1)->foo(2)->calledTimes(1)); $this->assertTrue(ShortifyPunit::verify($mock)->bar(1)->foo(2)->lessThan(2)); $this->assertFalse(ShortifyPunit::verify($mock)->bar(1)->foo(2)->atLeast(2)); $this->assertFalse(ShortifyPunit::verify($mock)->bar(1)->foo(2)->calledTimes(2)); $this->assertFalse(ShortifyPunit::verify($mock)->bar(1)->foo(2)->lessThan(1)); }
/** * Creating sandboxResponseManager with dependencies * * @param $force * @param bool $annotationsReader * @return SandboxResponseManager */ private function createManager($force, $annotationsReader = false) { if (!$annotationsReader instanceof AnnotationReader) { $annotationsReader = new AnnotationReader(); } // Mocking the sandbox response manager dependencies $kernel = ShortifyPunit::mock('SandboxBundle\\Tests\\Managers\\AppKernel'); ShortifyPunit::when($kernel)->locateResource('@SandboxBundle/Resources/responses/token.xml')->returns(self::$XML_PATH); ShortifyPunit::when($kernel)->locateResource('@SandboxBundle/Resources/responses/token.json')->returns(self::$JSON_PATH); // Create manager return new SandboxResponseManager($kernel, $force, $annotationsReader); }
/** * Stubbing magic methods */ public function testStubbingMagicMethods() { $mock = ShortifyPunit::mock('MagicClass'); ShortifyPunit::when($mock)->__toString()->returns('mockString'); $this->assertEquals($mock->__toString(), 'mockString'); $this->assertEquals($mock, 'mockString'); }