first commit
This commit is contained in:
+32
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
/*
|
||||
* This file is part of the PHPUnit_MockObject package.
|
||||
*
|
||||
* (c) Sebastian Bergmann <sebastian@phpunit.de>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Invocation matcher which checks if a method has been invoked zero or more
|
||||
* times. This matcher will always match.
|
||||
*
|
||||
* @since Class available since Release 1.0.0
|
||||
*/
|
||||
class PHPUnit_Framework_MockObject_Matcher_AnyInvokedCount extends PHPUnit_Framework_MockObject_Matcher_InvokedRecorder
|
||||
{
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function toString()
|
||||
{
|
||||
return 'invoked zero or more times';
|
||||
}
|
||||
|
||||
/**
|
||||
*/
|
||||
public function verify()
|
||||
{
|
||||
}
|
||||
}
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
/*
|
||||
* This file is part of the PHPUnit_MockObject package.
|
||||
*
|
||||
* (c) Sebastian Bergmann <sebastian@phpunit.de>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Invocation matcher which allows any parameters to a method.
|
||||
*
|
||||
* @since Class available since Release 1.0.0
|
||||
*/
|
||||
class PHPUnit_Framework_MockObject_Matcher_AnyParameters extends PHPUnit_Framework_MockObject_Matcher_StatelessInvocation
|
||||
{
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function toString()
|
||||
{
|
||||
return 'with any parameters';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param PHPUnit_Framework_MockObject_Invocation $invocation
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function matches(PHPUnit_Framework_MockObject_Invocation $invocation)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Vendored
+126
@@ -0,0 +1,126 @@
|
||||
<?php
|
||||
/*
|
||||
* This file is part of the PHPUnit_MockObject package.
|
||||
*
|
||||
* (c) Sebastian Bergmann <sebastian@phpunit.de>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Invocation matcher which looks for sets of specific parameters in the invocations.
|
||||
*
|
||||
* Checks the parameters of the incoming invocations, the parameter list is
|
||||
* checked against the defined constraints in $parameters. If the constraint
|
||||
* is met it will return true in matches().
|
||||
*
|
||||
* It takes a list of match groups and and increases a call index after each invocation.
|
||||
* So the first invocation uses the first group of constraints, the second the next and so on.
|
||||
*/
|
||||
class PHPUnit_Framework_MockObject_Matcher_ConsecutiveParameters extends PHPUnit_Framework_MockObject_Matcher_StatelessInvocation
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $parameterGroups = [];
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $invocations = [];
|
||||
|
||||
/**
|
||||
* @param array $parameterGroups
|
||||
*/
|
||||
public function __construct(array $parameterGroups)
|
||||
{
|
||||
foreach ($parameterGroups as $index => $parameters) {
|
||||
foreach ($parameters as $parameter) {
|
||||
if (!$parameter instanceof PHPUnit_Framework_Constraint) {
|
||||
$parameter = new PHPUnit_Framework_Constraint_IsEqual($parameter);
|
||||
}
|
||||
|
||||
$this->parameterGroups[$index][] = $parameter;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function toString()
|
||||
{
|
||||
$text = 'with consecutive parameters';
|
||||
|
||||
return $text;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param PHPUnit_Framework_MockObject_Invocation $invocation
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function matches(PHPUnit_Framework_MockObject_Invocation $invocation)
|
||||
{
|
||||
$this->invocations[] = $invocation;
|
||||
$callIndex = count($this->invocations) - 1;
|
||||
|
||||
$this->verifyInvocation($invocation, $callIndex);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function verify()
|
||||
{
|
||||
foreach ($this->invocations as $callIndex => $invocation) {
|
||||
$this->verifyInvocation($invocation, $callIndex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify a single invocation
|
||||
*
|
||||
* @param PHPUnit_Framework_MockObject_Invocation $invocation
|
||||
* @param int $callIndex
|
||||
*
|
||||
* @throws PHPUnit_Framework_ExpectationFailedException
|
||||
*/
|
||||
private function verifyInvocation(PHPUnit_Framework_MockObject_Invocation $invocation, $callIndex)
|
||||
{
|
||||
if (isset($this->parameterGroups[$callIndex])) {
|
||||
$parameters = $this->parameterGroups[$callIndex];
|
||||
} else {
|
||||
// no parameter assertion for this call index
|
||||
return;
|
||||
}
|
||||
|
||||
if ($invocation === null) {
|
||||
throw new PHPUnit_Framework_ExpectationFailedException(
|
||||
'Mocked method does not exist.'
|
||||
);
|
||||
}
|
||||
|
||||
if (count($invocation->parameters) < count($parameters)) {
|
||||
throw new PHPUnit_Framework_ExpectationFailedException(
|
||||
sprintf(
|
||||
'Parameter count for invocation %s is too low.',
|
||||
$invocation->toString()
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
foreach ($parameters as $i => $parameter) {
|
||||
$parameter->evaluate(
|
||||
$invocation->parameters[$i],
|
||||
sprintf(
|
||||
'Parameter %s for invocation #%d %s does not match expected ' .
|
||||
'value.',
|
||||
$i,
|
||||
$callIndex,
|
||||
$invocation->toString()
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
/*
|
||||
* This file is part of the PHPUnit_MockObject package.
|
||||
*
|
||||
* (c) Sebastian Bergmann <sebastian@phpunit.de>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Interface for classes which matches an invocation based on its
|
||||
* method name, argument, order or call count.
|
||||
*
|
||||
* @since Interface available since Release 1.0.0
|
||||
*/
|
||||
interface PHPUnit_Framework_MockObject_Matcher_Invocation extends PHPUnit_Framework_SelfDescribing, PHPUnit_Framework_MockObject_Verifiable
|
||||
{
|
||||
/**
|
||||
* Registers the invocation $invocation in the object as being invoked.
|
||||
* This will only occur after matches() returns true which means the
|
||||
* current invocation is the correct one.
|
||||
*
|
||||
* The matcher can store information from the invocation which can later
|
||||
* be checked in verify(), or it can check the values directly and throw
|
||||
* and exception if an expectation is not met.
|
||||
*
|
||||
* If the matcher is a stub it will also have a return value.
|
||||
*
|
||||
* @param PHPUnit_Framework_MockObject_Invocation $invocation Object containing information on a mocked or stubbed method which was invoked
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function invoked(PHPUnit_Framework_MockObject_Invocation $invocation);
|
||||
|
||||
/**
|
||||
* Checks if the invocation $invocation matches the current rules. If it does
|
||||
* the matcher will get the invoked() method called which should check if an
|
||||
* expectation is met.
|
||||
*
|
||||
* @param PHPUnit_Framework_MockObject_Invocation $invocation Object containing information on a mocked or stubbed method which was invoked
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function matches(PHPUnit_Framework_MockObject_Invocation $invocation);
|
||||
}
|
||||
+87
@@ -0,0 +1,87 @@
|
||||
<?php
|
||||
/*
|
||||
* This file is part of the PHPUnit_MockObject package.
|
||||
*
|
||||
* (c) Sebastian Bergmann <sebastian@phpunit.de>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Invocation matcher which checks if a method was invoked at a certain index.
|
||||
*
|
||||
* If the expected index number does not match the current invocation index it
|
||||
* will not match which means it skips all method and parameter matching. Only
|
||||
* once the index is reached will the method and parameter start matching and
|
||||
* verifying.
|
||||
*
|
||||
* If the index is never reached it will throw an exception in index.
|
||||
*
|
||||
* @since Class available since Release 1.0.0
|
||||
*/
|
||||
class PHPUnit_Framework_MockObject_Matcher_InvokedAtIndex implements PHPUnit_Framework_MockObject_Matcher_Invocation
|
||||
{
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
protected $sequenceIndex;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
protected $currentIndex = -1;
|
||||
|
||||
/**
|
||||
* @param int $sequenceIndex
|
||||
*/
|
||||
public function __construct($sequenceIndex)
|
||||
{
|
||||
$this->sequenceIndex = $sequenceIndex;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function toString()
|
||||
{
|
||||
return 'invoked at sequence index ' . $this->sequenceIndex;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param PHPUnit_Framework_MockObject_Invocation $invocation
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function matches(PHPUnit_Framework_MockObject_Invocation $invocation)
|
||||
{
|
||||
$this->currentIndex++;
|
||||
|
||||
return $this->currentIndex == $this->sequenceIndex;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param PHPUnit_Framework_MockObject_Invocation $invocation
|
||||
*/
|
||||
public function invoked(PHPUnit_Framework_MockObject_Invocation $invocation)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies that the current expectation is valid. If everything is OK the
|
||||
* code should just return, if not it must throw an exception.
|
||||
*
|
||||
* @throws PHPUnit_Framework_ExpectationFailedException
|
||||
*/
|
||||
public function verify()
|
||||
{
|
||||
if ($this->currentIndex < $this->sequenceIndex) {
|
||||
throw new PHPUnit_Framework_ExpectationFailedException(
|
||||
sprintf(
|
||||
'The expected invocation at index %s was never reached.',
|
||||
$this->sequenceIndex
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
Vendored
+57
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
/*
|
||||
* This file is part of the PHPUnit_MockObject package.
|
||||
*
|
||||
* (c) Sebastian Bergmann <sebastian@phpunit.de>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Invocation matcher which checks if a method has been invoked at least
|
||||
* N times.
|
||||
*
|
||||
* @since Class available since Release 2.2.0
|
||||
*/
|
||||
class PHPUnit_Framework_MockObject_Matcher_InvokedAtLeastCount extends PHPUnit_Framework_MockObject_Matcher_InvokedRecorder
|
||||
{
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
private $requiredInvocations;
|
||||
|
||||
/**
|
||||
* @param int $requiredInvocations
|
||||
*/
|
||||
public function __construct($requiredInvocations)
|
||||
{
|
||||
$this->requiredInvocations = $requiredInvocations;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function toString()
|
||||
{
|
||||
return 'invoked at least ' . $this->requiredInvocations . ' times';
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies that the current expectation is valid. If everything is OK the
|
||||
* code should just return, if not it must throw an exception.
|
||||
*
|
||||
* @throws PHPUnit_Framework_ExpectationFailedException
|
||||
*/
|
||||
public function verify()
|
||||
{
|
||||
$count = $this->getInvocationCount();
|
||||
|
||||
if ($count < $this->requiredInvocations) {
|
||||
throw new PHPUnit_Framework_ExpectationFailedException(
|
||||
'Expected invocation at least ' . $this->requiredInvocations .
|
||||
' times but it occurred ' . $count . ' time(s).'
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
Vendored
+45
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
/*
|
||||
* This file is part of the PHPUnit_MockObject package.
|
||||
*
|
||||
* (c) Sebastian Bergmann <sebastian@phpunit.de>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Invocation matcher which checks if a method has been invoked at least one
|
||||
* time.
|
||||
*
|
||||
* If the number of invocations is 0 it will throw an exception in verify.
|
||||
*
|
||||
* @since Class available since Release 1.0.0
|
||||
*/
|
||||
class PHPUnit_Framework_MockObject_Matcher_InvokedAtLeastOnce extends PHPUnit_Framework_MockObject_Matcher_InvokedRecorder
|
||||
{
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function toString()
|
||||
{
|
||||
return 'invoked at least once';
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies that the current expectation is valid. If everything is OK the
|
||||
* code should just return, if not it must throw an exception.
|
||||
*
|
||||
* @throws PHPUnit_Framework_ExpectationFailedException
|
||||
*/
|
||||
public function verify()
|
||||
{
|
||||
$count = $this->getInvocationCount();
|
||||
|
||||
if ($count < 1) {
|
||||
throw new PHPUnit_Framework_ExpectationFailedException(
|
||||
'Expected invocation at least once but it never occurred.'
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
Vendored
+57
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
/*
|
||||
* This file is part of the PHPUnit_MockObject package.
|
||||
*
|
||||
* (c) Sebastian Bergmann <sebastian@phpunit.de>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Invocation matcher which checks if a method has been invoked at least
|
||||
* N times.
|
||||
*
|
||||
* @since Class available since Release 2.2.0
|
||||
*/
|
||||
class PHPUnit_Framework_MockObject_Matcher_InvokedAtMostCount extends PHPUnit_Framework_MockObject_Matcher_InvokedRecorder
|
||||
{
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
private $allowedInvocations;
|
||||
|
||||
/**
|
||||
* @param int $allowedInvocations
|
||||
*/
|
||||
public function __construct($allowedInvocations)
|
||||
{
|
||||
$this->allowedInvocations = $allowedInvocations;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function toString()
|
||||
{
|
||||
return 'invoked at most ' . $this->allowedInvocations . ' times';
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies that the current expectation is valid. If everything is OK the
|
||||
* code should just return, if not it must throw an exception.
|
||||
*
|
||||
* @throws PHPUnit_Framework_ExpectationFailedException
|
||||
*/
|
||||
public function verify()
|
||||
{
|
||||
$count = $this->getInvocationCount();
|
||||
|
||||
if ($count > $this->allowedInvocations) {
|
||||
throw new PHPUnit_Framework_ExpectationFailedException(
|
||||
'Expected invocation at most ' . $this->allowedInvocations .
|
||||
' times but it occurred ' . $count . ' time(s).'
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
+110
@@ -0,0 +1,110 @@
|
||||
<?php
|
||||
/*
|
||||
* This file is part of the PHPUnit_MockObject package.
|
||||
*
|
||||
* (c) Sebastian Bergmann <sebastian@phpunit.de>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Invocation matcher which checks if a method has been invoked a certain amount
|
||||
* of times.
|
||||
* If the number of invocations exceeds the value it will immediately throw an
|
||||
* exception,
|
||||
* If the number is less it will later be checked in verify() and also throw an
|
||||
* exception.
|
||||
*
|
||||
* @since Class available since Release 1.0.0
|
||||
*/
|
||||
class PHPUnit_Framework_MockObject_Matcher_InvokedCount extends PHPUnit_Framework_MockObject_Matcher_InvokedRecorder
|
||||
{
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
protected $expectedCount;
|
||||
|
||||
/**
|
||||
* @param int $expectedCount
|
||||
*/
|
||||
public function __construct($expectedCount)
|
||||
{
|
||||
$this->expectedCount = $expectedCount;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isNever()
|
||||
{
|
||||
return $this->expectedCount == 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function toString()
|
||||
{
|
||||
return 'invoked ' . $this->expectedCount . ' time(s)';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param PHPUnit_Framework_MockObject_Invocation $invocation
|
||||
*
|
||||
* @throws PHPUnit_Framework_ExpectationFailedException
|
||||
*/
|
||||
public function invoked(PHPUnit_Framework_MockObject_Invocation $invocation)
|
||||
{
|
||||
parent::invoked($invocation);
|
||||
|
||||
$count = $this->getInvocationCount();
|
||||
|
||||
if ($count > $this->expectedCount) {
|
||||
$message = $invocation->toString() . ' ';
|
||||
|
||||
switch ($this->expectedCount) {
|
||||
case 0: {
|
||||
$message .= 'was not expected to be called.';
|
||||
}
|
||||
break;
|
||||
|
||||
case 1: {
|
||||
$message .= 'was not expected to be called more than once.';
|
||||
}
|
||||
break;
|
||||
|
||||
default: {
|
||||
$message .= sprintf(
|
||||
'was not expected to be called more than %d times.',
|
||||
$this->expectedCount
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
throw new PHPUnit_Framework_ExpectationFailedException($message);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies that the current expectation is valid. If everything is OK the
|
||||
* code should just return, if not it must throw an exception.
|
||||
*
|
||||
* @throws PHPUnit_Framework_ExpectationFailedException
|
||||
*/
|
||||
public function verify()
|
||||
{
|
||||
$count = $this->getInvocationCount();
|
||||
|
||||
if ($count !== $this->expectedCount) {
|
||||
throw new PHPUnit_Framework_ExpectationFailedException(
|
||||
sprintf(
|
||||
'Method was expected to be called %d times, ' .
|
||||
'actually called %d times.',
|
||||
$this->expectedCount,
|
||||
$count
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
+68
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
/*
|
||||
* This file is part of the PHPUnit_MockObject package.
|
||||
*
|
||||
* (c) Sebastian Bergmann <sebastian@phpunit.de>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Records invocations and provides convenience methods for checking them later
|
||||
* on.
|
||||
* This abstract class can be implemented by matchers which needs to check the
|
||||
* number of times an invocation has occurred.
|
||||
*
|
||||
* @since Class available since Release 1.0.0
|
||||
* @abstract
|
||||
*/
|
||||
abstract class PHPUnit_Framework_MockObject_Matcher_InvokedRecorder implements PHPUnit_Framework_MockObject_Matcher_Invocation
|
||||
{
|
||||
/**
|
||||
* @var PHPUnit_Framework_MockObject_Invocation[]
|
||||
*/
|
||||
protected $invocations = [];
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getInvocationCount()
|
||||
{
|
||||
return count($this->invocations);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return PHPUnit_Framework_MockObject_Invocation[]
|
||||
*/
|
||||
public function getInvocations()
|
||||
{
|
||||
return $this->invocations;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function hasBeenInvoked()
|
||||
{
|
||||
return count($this->invocations) > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param PHPUnit_Framework_MockObject_Invocation $invocation
|
||||
*/
|
||||
public function invoked(PHPUnit_Framework_MockObject_Invocation $invocation)
|
||||
{
|
||||
$this->invocations[] = $invocation;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param PHPUnit_Framework_MockObject_Invocation $invocation
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function matches(PHPUnit_Framework_MockObject_Invocation $invocation)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
+68
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
/*
|
||||
* This file is part of the PHPUnit_MockObject package.
|
||||
*
|
||||
* (c) Sebastian Bergmann <sebastian@phpunit.de>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Invocation matcher which looks for a specific method name in the invocations.
|
||||
*
|
||||
* Checks the method name all incoming invocations, the name is checked against
|
||||
* the defined constraint $constraint. If the constraint is met it will return
|
||||
* true in matches().
|
||||
*
|
||||
* @since Class available since Release 1.0.0
|
||||
*/
|
||||
class PHPUnit_Framework_MockObject_Matcher_MethodName extends PHPUnit_Framework_MockObject_Matcher_StatelessInvocation
|
||||
{
|
||||
/**
|
||||
* @var PHPUnit_Framework_Constraint
|
||||
*/
|
||||
protected $constraint;
|
||||
|
||||
/**
|
||||
* @param PHPUnit_Framework_Constraint|string
|
||||
*
|
||||
* @throws PHPUnit_Framework_Constraint
|
||||
*/
|
||||
public function __construct($constraint)
|
||||
{
|
||||
if (!$constraint instanceof PHPUnit_Framework_Constraint) {
|
||||
if (!is_string($constraint)) {
|
||||
throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
|
||||
}
|
||||
|
||||
$constraint = new PHPUnit_Framework_Constraint_IsEqual(
|
||||
$constraint,
|
||||
0,
|
||||
10,
|
||||
false,
|
||||
true
|
||||
);
|
||||
}
|
||||
|
||||
$this->constraint = $constraint;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function toString()
|
||||
{
|
||||
return 'method name ' . $this->constraint->toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param PHPUnit_Framework_MockObject_Invocation $invocation
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function matches(PHPUnit_Framework_MockObject_Invocation $invocation)
|
||||
{
|
||||
return $this->constraint->evaluate($invocation->methodName, '', true);
|
||||
}
|
||||
}
|
||||
+158
@@ -0,0 +1,158 @@
|
||||
<?php
|
||||
/*
|
||||
* This file is part of the PHPUnit_MockObject package.
|
||||
*
|
||||
* (c) Sebastian Bergmann <sebastian@phpunit.de>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Invocation matcher which looks for specific parameters in the invocations.
|
||||
*
|
||||
* Checks the parameters of all incoming invocations, the parameter list is
|
||||
* checked against the defined constraints in $parameters. If the constraint
|
||||
* is met it will return true in matches().
|
||||
*
|
||||
* @since Class available since Release 1.0.0
|
||||
*/
|
||||
class PHPUnit_Framework_MockObject_Matcher_Parameters extends PHPUnit_Framework_MockObject_Matcher_StatelessInvocation
|
||||
{
|
||||
/**
|
||||
* @var PHPUnit_Framework_Constraint[]
|
||||
*/
|
||||
protected $parameters = [];
|
||||
|
||||
/**
|
||||
* @var PHPUnit_Framework_MockObject_Invocation
|
||||
*/
|
||||
protected $invocation;
|
||||
|
||||
/**
|
||||
* @var PHPUnit_Framework_ExpectationFailedException
|
||||
*/
|
||||
private $parameterVerificationResult;
|
||||
|
||||
/**
|
||||
* @param array $parameters
|
||||
*/
|
||||
public function __construct(array $parameters)
|
||||
{
|
||||
foreach ($parameters as $parameter) {
|
||||
if (!($parameter instanceof PHPUnit_Framework_Constraint)) {
|
||||
$parameter = new PHPUnit_Framework_Constraint_IsEqual(
|
||||
$parameter
|
||||
);
|
||||
}
|
||||
|
||||
$this->parameters[] = $parameter;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function toString()
|
||||
{
|
||||
$text = 'with parameter';
|
||||
|
||||
foreach ($this->parameters as $index => $parameter) {
|
||||
if ($index > 0) {
|
||||
$text .= ' and';
|
||||
}
|
||||
|
||||
$text .= ' ' . $index . ' ' . $parameter->toString();
|
||||
}
|
||||
|
||||
return $text;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param PHPUnit_Framework_MockObject_Invocation $invocation
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function matches(PHPUnit_Framework_MockObject_Invocation $invocation)
|
||||
{
|
||||
$this->invocation = $invocation;
|
||||
$this->parameterVerificationResult = null;
|
||||
|
||||
try {
|
||||
$this->parameterVerificationResult = $this->verify();
|
||||
|
||||
return $this->parameterVerificationResult;
|
||||
} catch (PHPUnit_Framework_ExpectationFailedException $e) {
|
||||
$this->parameterVerificationResult = $e;
|
||||
|
||||
throw $this->parameterVerificationResult;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the invocation $invocation matches the current rules. If it
|
||||
* does the matcher will get the invoked() method called which should check
|
||||
* if an expectation is met.
|
||||
*
|
||||
* @return bool
|
||||
*
|
||||
* @throws PHPUnit_Framework_ExpectationFailedException
|
||||
*/
|
||||
public function verify()
|
||||
{
|
||||
if (isset($this->parameterVerificationResult)) {
|
||||
return $this->guardAgainstDuplicateEvaluationOfParameterConstraints();
|
||||
}
|
||||
|
||||
if ($this->invocation === null) {
|
||||
throw new PHPUnit_Framework_ExpectationFailedException(
|
||||
'Mocked method does not exist.'
|
||||
);
|
||||
}
|
||||
|
||||
if (count($this->invocation->parameters) < count($this->parameters)) {
|
||||
$message = 'Parameter count for invocation %s is too low.';
|
||||
|
||||
// The user called `->with($this->anything())`, but may have meant
|
||||
// `->withAnyParameters()`.
|
||||
//
|
||||
// @see https://github.com/sebastianbergmann/phpunit-mock-objects/issues/199
|
||||
if (count($this->parameters) === 1 &&
|
||||
get_class($this->parameters[0]) === 'PHPUnit_Framework_Constraint_IsAnything') {
|
||||
$message .= "\nTo allow 0 or more parameters with any value, omit ->with() or use ->withAnyParameters() instead.";
|
||||
}
|
||||
|
||||
throw new PHPUnit_Framework_ExpectationFailedException(
|
||||
sprintf($message, $this->invocation->toString())
|
||||
);
|
||||
}
|
||||
|
||||
foreach ($this->parameters as $i => $parameter) {
|
||||
$parameter->evaluate(
|
||||
$this->invocation->parameters[$i],
|
||||
sprintf(
|
||||
'Parameter %s for invocation %s does not match expected ' .
|
||||
'value.',
|
||||
$i,
|
||||
$this->invocation->toString()
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*
|
||||
* @throws PHPUnit_Framework_ExpectationFailedException
|
||||
*/
|
||||
private function guardAgainstDuplicateEvaluationOfParameterConstraints()
|
||||
{
|
||||
if ($this->parameterVerificationResult instanceof Exception) {
|
||||
throw $this->parameterVerificationResult;
|
||||
}
|
||||
|
||||
return (bool) $this->parameterVerificationResult;
|
||||
}
|
||||
}
|
||||
Vendored
+54
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
/*
|
||||
* This file is part of the PHPUnit_MockObject package.
|
||||
*
|
||||
* (c) Sebastian Bergmann <sebastian@phpunit.de>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Invocation matcher which does not care about previous state from earlier
|
||||
* invocations.
|
||||
*
|
||||
* This abstract class can be implemented by matchers which does not care about
|
||||
* state but only the current run-time value of the invocation itself.
|
||||
*
|
||||
* @since Class available since Release 1.0.0
|
||||
* @abstract
|
||||
*/
|
||||
abstract class PHPUnit_Framework_MockObject_Matcher_StatelessInvocation implements PHPUnit_Framework_MockObject_Matcher_Invocation
|
||||
{
|
||||
/**
|
||||
* Registers the invocation $invocation in the object as being invoked.
|
||||
* This will only occur after matches() returns true which means the
|
||||
* current invocation is the correct one.
|
||||
*
|
||||
* The matcher can store information from the invocation which can later
|
||||
* be checked in verify(), or it can check the values directly and throw
|
||||
* and exception if an expectation is not met.
|
||||
*
|
||||
* If the matcher is a stub it will also have a return value.
|
||||
*
|
||||
* @param PHPUnit_Framework_MockObject_Invocation $invocation Object containing information on a mocked or stubbed method which was invoked
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function invoked(PHPUnit_Framework_MockObject_Invocation $invocation)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the invocation $invocation matches the current rules. If it does
|
||||
* the matcher will get the invoked() method called which should check if an
|
||||
* expectation is met.
|
||||
*
|
||||
* @param PHPUnit_Framework_MockObject_Invocation $invocation Object containing information on a mocked or stubbed method which was invoked
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function verify()
|
||||
{
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user