/** * Creating a chain array of return values * * @param $action * @param $response */ private function createChainArrayOfReturnValues($action, $response) { // Pop first method $methods = $this->methods; $firstMethod = array_pop($methods); $lastValue = $response; $mockClassType = get_class($this->mockClass); if (!$this->mockClass instanceof MockInterface) { throw self::generateException('Class is not implementing MockInterface.'); } $mockClassInstanceId = $this->mockClass->getShortifyPunitInstanceId(); foreach ($methods as $currentMethod) { $fakeClass = new MockClassOnTheFly(); // extracting methods before the current method into an array $chainedMethodsBefore = $this->extractChainedMethodsBefore(array_reverse($this->methods), $currentMethod); // adding to the ShortifyPunit chained method response $this->addChainedMethodResponse($chainedMethodsBefore, $currentMethod, $action, $lastValue, $mockClassInstanceId); $currentMethodName = key($currentMethod); // closure for MockOnTheFly chained methods $fakeClass->{$currentMethodName} = function () use($mockClassInstanceId, $mockClassType, $chainedMethodsBefore, $currentMethod) { return ShortifyPunit::createChainResponse($mockClassInstanceId, $mockClassType, $chainedMethodsBefore, $currentMethod, func_get_args()); }; $lastValue = $fakeClass; // except from the last method all other chained method `returns` a calls so set the action for the next loop $action = MockAction::RETURNS; } $whenCase = new WhenCase($mockClassType, $this->mockClass->getShortifyPunitInstanceId(), key($firstMethod)); $whenCase->setMethod(current($firstMethod), $action, $lastValue); }
/** * 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()); }