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)); }
/** * Testing when case add method */ public function testWhenCaseAddMethod() { $mock = ShortifyPunit::mock('Foo'); $whenCase = new WhenCase(get_class($mock), $mock->getShortifyPunitInstanceId()); $whenCase->bar(array())->returns(array()); }
/** * 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); }
public function testLoad() { $containerBuilder = ShortifyPunit::mock('Symfony\\Component\\DependencyInjection\\ContainerBuilder'); $sandboxExtension = new SandboxExtension(); $sandboxExtension->load(array('sandbox' => array('response' => array('force' => true))), $containerBuilder); }
/** * @desc Setting up the mock response in the ShortifyPunit Return Values array * * @param $args * @param $action * @param $returns */ public function setMethod($args, $action, $returns) { ShortifyPunit::setWhenMockResponse($this->className, $this->instanceId, $this->method, $args, $action, $returns); }
/** * 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'); }
/** * Adding chained method responses into ShortifyPunit::ReturnValues * * @param $chainedMethodsBefore * @param $currentMethod * @param $action * @param $lastValue * @param $mockClassInstanceId */ private function addChainedMethodResponse($chainedMethodsBefore, $currentMethod, $action, $lastValue, $mockClassInstanceId) { $response = []; $rResponse =& $response; $currentMethodName = key($currentMethod); foreach ($chainedMethodsBefore as $chainedMethod) { $chainedMethodName = key($chainedMethod); $chainedMethodArgs = $chainedMethod[$chainedMethodName]; $serializedChainedMethodArgs = serialize($chainedMethodArgs); $rResponse[$chainedMethodName][$serializedChainedMethodArgs] = []; $rResponse =& $rResponse[$chainedMethodName][$serializedChainedMethodArgs]; } $rResponse[$currentMethodName][serialize(current($currentMethod))] = ['response' => ['action' => $action, 'value' => $lastValue]]; ShortifyPunit::addChainedResponse(array(get_class($this->mockClass) => [$mockClassInstanceId => $response])); }
/** * Getting the call counter for the specific chained * stubbing methods * * @param $methods * @return int */ private function getChainedMockCounter($methods) { $mockReturnValues = ShortifyPunit::getReturnValues(); $mockResponse = $mockReturnValues[$this->mockedClass][$this->instanceId]; foreach ($methods as $method) { $methodName = key($method); $args = $method[$methodName]; $serializedArgs = serialize($args); if (!isset($mockResponse[$methodName][$serializedArgs])) { if (!isset($mockResponse[$methodName])) { break; } // try to finding matching Hamcrest-API Function (anything(), equalTo()) $serializedArgs = static::checkMatchingArguments($mockResponse[$methodName], $args); if (is_null($serializedArgs)) { break; } } $mockResponse = $mockResponse[$methodName][$serializedArgs]; } return isset($mockResponse['response']['counter']) ? $mockResponse['response']['counter'] : 0; }