/** * Sets up expectations on the members of the CompositeExpectation and * builds up any demeter chain that was passed to shouldReceive * * @param \Mockery\MockInterface $mock * @param string $arg * @param callable $add * @throws Mockery\Exception * @return \Mockery\ExpectationDirector */ protected static function _buildDemeterChain(\Mockery\MockInterface $mock, $arg, $add) { /** @var Mockery\Container $container */ $container = $mock->mockery_getContainer(); $methodNames = explode('->', $arg); reset($methodNames); if (!\Mockery::getConfiguration()->mockingNonExistentMethodsAllowed() && !$mock->mockery_isAnonymous() && !in_array(current($methodNames), $mock->mockery_getMockableMethods())) { throw new \Mockery\Exception('Mockery\'s configuration currently forbids mocking the method ' . current($methodNames) . ' as it does not exist on the class or object ' . 'being mocked'); } /** @var ExpectationInterface|null $exp */ $exp = null; /** @var Callable $nextExp */ $nextExp = function ($method) use($add) { return $add($method); }; while (true) { $method = array_shift($methodNames); $exp = $mock->mockery_getExpectationsFor($method); if (is_null($exp) || self::noMoreElementsInChain($methodNames)) { $exp = $nextExp($method); if (self::noMoreElementsInChain($methodNames)) { break; } $mock = self::getNewDemeterMock($container, $method, $exp); } else { $demeterMockKey = $container->getKeyOfDemeterMockFor($method); if ($demeterMockKey) { $mock = self::getExistingDemeterMock($container, $demeterMockKey); } } $nextExp = function ($n) use($mock) { return $mock->shouldReceive($n); }; } return $exp; }
/** * Sets up expectations on the members of the CompositeExpectation and * builds up any demeter chain that was passed to shouldReceive * * @param \Mockery\MockInterface $mock * @param string $arg * @param Closure $add * @return \Mockery\ExpectationDirector */ protected static function _buildDemeterChain(\Mockery\MockInterface $mock, $arg, $add) { $container = $mock->mockery_getContainer(); $names = explode('->', $arg); reset($names); if (!\Mockery::getConfiguration()->mockingNonExistentMethodsAllowed() && method_exists($mock, "mockery_getMockableMethods") && !in_array(current($names), $mock->mockery_getMockableMethods())) { throw new \Mockery\Exception('Mockery\'s configuration currently forbids mocking the method ' . current($names) . ' as it does not exist on the class or object ' . 'being mocked'); } $exp = null; $nextExp = function ($n) use($add) { return $add($n); }; while (true) { $method = array_shift($names); $exp = $mock->mockery_getExpectationsFor($method); $needNew = false; if (is_null($exp) || empty($names)) { $needNew = true; } if ($needNew) { $exp = $nextExp($method); } if (empty($names)) { break; } if ($needNew) { $mock = $container->mock('demeter_' . $method); $exp->andReturn($mock); } $nextExp = function ($n) use($mock) { return $mock->shouldReceive($n); }; } return $exp; }