Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: /** Chris@0: * Invocation matcher which looks for sets of specific parameters in the invocations. Chris@0: * Chris@0: * Checks the parameters of the incoming invocations, the parameter list is Chris@0: * checked against the defined constraints in $parameters. If the constraint Chris@0: * is met it will return true in matches(). Chris@0: * Chris@0: * It takes a list of match groups and and increases a call index after each invocation. Chris@0: * So the first invocation uses the first group of constraints, the second the next and so on. Chris@0: */ Chris@0: class PHPUnit_Framework_MockObject_Matcher_ConsecutiveParameters extends PHPUnit_Framework_MockObject_Matcher_StatelessInvocation Chris@0: { Chris@0: /** Chris@0: * @var array Chris@0: */ Chris@0: private $_parameterGroups = array(); Chris@0: Chris@0: /** Chris@0: * @var array Chris@0: */ Chris@0: private $_invocations = array(); Chris@0: Chris@0: /** Chris@0: * @param array $parameterGroups Chris@0: */ Chris@0: public function __construct(array $parameterGroups) Chris@0: { Chris@0: foreach ($parameterGroups as $index => $parameters) { Chris@0: foreach ($parameters as $parameter) { Chris@0: if (!($parameter instanceof \PHPUnit_Framework_Constraint)) { Chris@0: $parameter = new \PHPUnit_Framework_Constraint_IsEqual($parameter); Chris@0: } Chris@0: $this->_parameterGroups[$index][] = $parameter; Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * @return string Chris@0: */ Chris@0: public function toString() Chris@0: { Chris@0: $text = 'with consecutive parameters'; Chris@0: Chris@0: return $text; Chris@0: } Chris@0: Chris@0: /** Chris@0: * @param PHPUnit_Framework_MockObject_Invocation $invocation Chris@0: * @return bool Chris@0: */ Chris@0: public function matches(PHPUnit_Framework_MockObject_Invocation $invocation) Chris@0: { Chris@0: $this->_invocations[] = $invocation; Chris@0: $callIndex = count($this->_invocations) - 1; Chris@0: $this->verifyInvocation($invocation, $callIndex); Chris@0: Chris@0: return false; Chris@0: } Chris@0: Chris@0: public function verify() Chris@0: { Chris@0: foreach ($this->_invocations as $callIndex => $invocation) { Chris@0: $this->verifyInvocation($invocation, $callIndex); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Verify a single invocation Chris@0: * Chris@0: * @param PHPUnit_Framework_MockObject_Invocation $invocation Chris@0: * @param int $callIndex Chris@0: * @throws PHPUnit_Framework_ExpectationFailedException Chris@0: */ Chris@0: private function verifyInvocation(PHPUnit_Framework_MockObject_Invocation $invocation, $callIndex) Chris@0: { Chris@0: Chris@0: if (isset($this->_parameterGroups[$callIndex])) { Chris@0: $parameters = $this->_parameterGroups[$callIndex]; Chris@0: } else { Chris@0: // no parameter assertion for this call index Chris@0: return; Chris@0: } Chris@0: Chris@0: if ($invocation === null) { Chris@0: throw new PHPUnit_Framework_ExpectationFailedException( Chris@0: 'Mocked method does not exist.' Chris@0: ); Chris@0: } Chris@0: Chris@0: if (count($invocation->parameters) < count($parameters)) { Chris@0: throw new PHPUnit_Framework_ExpectationFailedException( Chris@0: sprintf( Chris@0: 'Parameter count for invocation %s is too low.', Chris@0: $invocation->toString() Chris@0: ) Chris@0: ); Chris@0: } Chris@0: Chris@0: foreach ($parameters as $i => $parameter) { Chris@0: $parameter->evaluate( Chris@0: $invocation->parameters[$i], Chris@0: sprintf( Chris@0: 'Parameter %s for invocation #%d %s does not match expected ' . Chris@0: 'value.', Chris@0: $i, Chris@0: $callIndex, Chris@0: $invocation->toString() Chris@0: ) Chris@0: ); Chris@0: } Chris@0: } Chris@0: }