annotate vendor/phpunit/phpunit-mock-objects/src/Framework/MockObject/Matcher/ConsecutiveParameters.php @ 7:848c88cfe644

More layout
author Chris Cannam
date Fri, 05 Jan 2018 13:59:44 +0000
parents 4c8ae668cc8c
children
rev   line source
Chris@0 1 <?php
Chris@0 2 /*
Chris@0 3 * This file is part of the PHPUnit_MockObject package.
Chris@0 4 *
Chris@0 5 * (c) Sebastian Bergmann <sebastian@phpunit.de>
Chris@0 6 *
Chris@0 7 * For the full copyright and license information, please view the LICENSE
Chris@0 8 * file that was distributed with this source code.
Chris@0 9 */
Chris@0 10
Chris@0 11 /**
Chris@0 12 * Invocation matcher which looks for sets of specific parameters in the invocations.
Chris@0 13 *
Chris@0 14 * Checks the parameters of the incoming invocations, the parameter list is
Chris@0 15 * checked against the defined constraints in $parameters. If the constraint
Chris@0 16 * is met it will return true in matches().
Chris@0 17 *
Chris@0 18 * It takes a list of match groups and and increases a call index after each invocation.
Chris@0 19 * So the first invocation uses the first group of constraints, the second the next and so on.
Chris@0 20 */
Chris@0 21 class PHPUnit_Framework_MockObject_Matcher_ConsecutiveParameters extends PHPUnit_Framework_MockObject_Matcher_StatelessInvocation
Chris@0 22 {
Chris@0 23 /**
Chris@0 24 * @var array
Chris@0 25 */
Chris@0 26 private $_parameterGroups = array();
Chris@0 27
Chris@0 28 /**
Chris@0 29 * @var array
Chris@0 30 */
Chris@0 31 private $_invocations = array();
Chris@0 32
Chris@0 33 /**
Chris@0 34 * @param array $parameterGroups
Chris@0 35 */
Chris@0 36 public function __construct(array $parameterGroups)
Chris@0 37 {
Chris@0 38 foreach ($parameterGroups as $index => $parameters) {
Chris@0 39 foreach ($parameters as $parameter) {
Chris@0 40 if (!($parameter instanceof \PHPUnit_Framework_Constraint)) {
Chris@0 41 $parameter = new \PHPUnit_Framework_Constraint_IsEqual($parameter);
Chris@0 42 }
Chris@0 43 $this->_parameterGroups[$index][] = $parameter;
Chris@0 44 }
Chris@0 45 }
Chris@0 46 }
Chris@0 47
Chris@0 48 /**
Chris@0 49 * @return string
Chris@0 50 */
Chris@0 51 public function toString()
Chris@0 52 {
Chris@0 53 $text = 'with consecutive parameters';
Chris@0 54
Chris@0 55 return $text;
Chris@0 56 }
Chris@0 57
Chris@0 58 /**
Chris@0 59 * @param PHPUnit_Framework_MockObject_Invocation $invocation
Chris@0 60 * @return bool
Chris@0 61 */
Chris@0 62 public function matches(PHPUnit_Framework_MockObject_Invocation $invocation)
Chris@0 63 {
Chris@0 64 $this->_invocations[] = $invocation;
Chris@0 65 $callIndex = count($this->_invocations) - 1;
Chris@0 66 $this->verifyInvocation($invocation, $callIndex);
Chris@0 67
Chris@0 68 return false;
Chris@0 69 }
Chris@0 70
Chris@0 71 public function verify()
Chris@0 72 {
Chris@0 73 foreach ($this->_invocations as $callIndex => $invocation) {
Chris@0 74 $this->verifyInvocation($invocation, $callIndex);
Chris@0 75 }
Chris@0 76 }
Chris@0 77
Chris@0 78 /**
Chris@0 79 * Verify a single invocation
Chris@0 80 *
Chris@0 81 * @param PHPUnit_Framework_MockObject_Invocation $invocation
Chris@0 82 * @param int $callIndex
Chris@0 83 * @throws PHPUnit_Framework_ExpectationFailedException
Chris@0 84 */
Chris@0 85 private function verifyInvocation(PHPUnit_Framework_MockObject_Invocation $invocation, $callIndex)
Chris@0 86 {
Chris@0 87
Chris@0 88 if (isset($this->_parameterGroups[$callIndex])) {
Chris@0 89 $parameters = $this->_parameterGroups[$callIndex];
Chris@0 90 } else {
Chris@0 91 // no parameter assertion for this call index
Chris@0 92 return;
Chris@0 93 }
Chris@0 94
Chris@0 95 if ($invocation === null) {
Chris@0 96 throw new PHPUnit_Framework_ExpectationFailedException(
Chris@0 97 'Mocked method does not exist.'
Chris@0 98 );
Chris@0 99 }
Chris@0 100
Chris@0 101 if (count($invocation->parameters) < count($parameters)) {
Chris@0 102 throw new PHPUnit_Framework_ExpectationFailedException(
Chris@0 103 sprintf(
Chris@0 104 'Parameter count for invocation %s is too low.',
Chris@0 105 $invocation->toString()
Chris@0 106 )
Chris@0 107 );
Chris@0 108 }
Chris@0 109
Chris@0 110 foreach ($parameters as $i => $parameter) {
Chris@0 111 $parameter->evaluate(
Chris@0 112 $invocation->parameters[$i],
Chris@0 113 sprintf(
Chris@0 114 'Parameter %s for invocation #%d %s does not match expected ' .
Chris@0 115 'value.',
Chris@0 116 $i,
Chris@0 117 $callIndex,
Chris@0 118 $invocation->toString()
Chris@0 119 )
Chris@0 120 );
Chris@0 121 }
Chris@0 122 }
Chris@0 123 }