123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- <?php
- /*
- * This file is part of the phpunit-mock-objects 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.
- */
- use PHPUnit\Framework\ExpectationFailedException;
- use PHPUnit\Framework\TestFailure;
- /**
- * Main matcher which defines a full expectation using method, parameter and
- * invocation matchers.
- * This matcher encapsulates all the other matchers and allows the builder to
- * set the specific matchers when the appropriate methods are called (once(),
- * where() etc.).
- *
- * All properties are public so that they can easily be accessed by the builder.
- */
- class PHPUnit_Framework_MockObject_Matcher implements PHPUnit_Framework_MockObject_Matcher_Invocation
- {
- /**
- * @var PHPUnit_Framework_MockObject_Matcher_Invocation
- */
- public $invocationMatcher;
- /**
- * @var mixed
- */
- public $afterMatchBuilderId = null;
- /**
- * @var bool
- */
- public $afterMatchBuilderIsInvoked = false;
- /**
- * @var PHPUnit_Framework_MockObject_Matcher_MethodName
- */
- public $methodNameMatcher = null;
- /**
- * @var PHPUnit_Framework_MockObject_Matcher_Parameters
- */
- public $parametersMatcher = null;
- /**
- * @var PHPUnit_Framework_MockObject_Stub
- */
- public $stub = null;
- /**
- * @param PHPUnit_Framework_MockObject_Matcher_Invocation $invocationMatcher
- */
- public function __construct(PHPUnit_Framework_MockObject_Matcher_Invocation $invocationMatcher)
- {
- $this->invocationMatcher = $invocationMatcher;
- }
- /**
- * @return string
- */
- public function toString()
- {
- $list = [];
- if ($this->invocationMatcher !== null) {
- $list[] = $this->invocationMatcher->toString();
- }
- if ($this->methodNameMatcher !== null) {
- $list[] = 'where ' . $this->methodNameMatcher->toString();
- }
- if ($this->parametersMatcher !== null) {
- $list[] = 'and ' . $this->parametersMatcher->toString();
- }
- if ($this->afterMatchBuilderId !== null) {
- $list[] = 'after ' . $this->afterMatchBuilderId;
- }
- if ($this->stub !== null) {
- $list[] = 'will ' . $this->stub->toString();
- }
- return implode(' ', $list);
- }
- /**
- * @param PHPUnit_Framework_MockObject_Invocation $invocation
- *
- * @return mixed
- */
- public function invoked(PHPUnit_Framework_MockObject_Invocation $invocation)
- {
- if ($this->invocationMatcher === null) {
- throw new PHPUnit_Framework_MockObject_RuntimeException(
- 'No invocation matcher is set'
- );
- }
- if ($this->methodNameMatcher === null) {
- throw new PHPUnit_Framework_MockObject_RuntimeException('No method matcher is set');
- }
- if ($this->afterMatchBuilderId !== null) {
- $builder = $invocation->object
- ->__phpunit_getInvocationMocker()
- ->lookupId($this->afterMatchBuilderId);
- if (!$builder) {
- throw new PHPUnit_Framework_MockObject_RuntimeException(
- sprintf(
- 'No builder found for match builder identification <%s>',
- $this->afterMatchBuilderId
- )
- );
- }
- $matcher = $builder->getMatcher();
- if ($matcher && $matcher->invocationMatcher->hasBeenInvoked()) {
- $this->afterMatchBuilderIsInvoked = true;
- }
- }
- $this->invocationMatcher->invoked($invocation);
- try {
- if ($this->parametersMatcher !== null &&
- !$this->parametersMatcher->matches($invocation)) {
- $this->parametersMatcher->verify();
- }
- } catch (ExpectationFailedException $e) {
- throw new ExpectationFailedException(
- sprintf(
- "Expectation failed for %s when %s\n%s",
- $this->methodNameMatcher->toString(),
- $this->invocationMatcher->toString(),
- $e->getMessage()
- ),
- $e->getComparisonFailure()
- );
- }
- if ($this->stub) {
- return $this->stub->invoke($invocation);
- }
- return $invocation->generateReturnValue();
- }
- /**
- * @param PHPUnit_Framework_MockObject_Invocation $invocation
- *
- * @return bool
- */
- public function matches(PHPUnit_Framework_MockObject_Invocation $invocation)
- {
- if ($this->afterMatchBuilderId !== null) {
- $builder = $invocation->object
- ->__phpunit_getInvocationMocker()
- ->lookupId($this->afterMatchBuilderId);
- if (!$builder) {
- throw new PHPUnit_Framework_MockObject_RuntimeException(
- sprintf(
- 'No builder found for match builder identification <%s>',
- $this->afterMatchBuilderId
- )
- );
- }
- $matcher = $builder->getMatcher();
- if (!$matcher) {
- return false;
- }
- if (!$matcher->invocationMatcher->hasBeenInvoked()) {
- return false;
- }
- }
- if ($this->invocationMatcher === null) {
- throw new PHPUnit_Framework_MockObject_RuntimeException(
- 'No invocation matcher is set'
- );
- }
- if ($this->methodNameMatcher === null) {
- throw new PHPUnit_Framework_MockObject_RuntimeException('No method matcher is set');
- }
- if (!$this->invocationMatcher->matches($invocation)) {
- return false;
- }
- try {
- if (!$this->methodNameMatcher->matches($invocation)) {
- return false;
- }
- } catch (ExpectationFailedException $e) {
- throw new ExpectationFailedException(
- sprintf(
- "Expectation failed for %s when %s\n%s",
- $this->methodNameMatcher->toString(),
- $this->invocationMatcher->toString(),
- $e->getMessage()
- ),
- $e->getComparisonFailure()
- );
- }
- return true;
- }
- /**
- * @throws PHPUnit_Framework_MockObject_RuntimeException
- * @throws ExpectationFailedException
- */
- public function verify()
- {
- if ($this->invocationMatcher === null) {
- throw new PHPUnit_Framework_MockObject_RuntimeException(
- 'No invocation matcher is set'
- );
- }
- if ($this->methodNameMatcher === null) {
- throw new PHPUnit_Framework_MockObject_RuntimeException('No method matcher is set');
- }
- try {
- $this->invocationMatcher->verify();
- if ($this->parametersMatcher === null) {
- $this->parametersMatcher = new PHPUnit_Framework_MockObject_Matcher_AnyParameters;
- }
- $invocationIsAny = $this->invocationMatcher instanceof PHPUnit_Framework_MockObject_Matcher_AnyInvokedCount;
- $invocationIsNever = $this->invocationMatcher instanceof PHPUnit_Framework_MockObject_Matcher_InvokedCount && $this->invocationMatcher->isNever();
- if (!$invocationIsAny && !$invocationIsNever) {
- $this->parametersMatcher->verify();
- }
- } catch (ExpectationFailedException $e) {
- throw new ExpectationFailedException(
- sprintf(
- "Expectation failed for %s when %s.\n%s",
- $this->methodNameMatcher->toString(),
- $this->invocationMatcher->toString(),
- TestFailure::exceptionToString($e)
- )
- );
- }
- }
- public function hasMatchers()
- {
- if ($this->invocationMatcher !== null &&
- !$this->invocationMatcher instanceof PHPUnit_Framework_MockObject_Matcher_AnyInvokedCount) {
- return true;
- }
- return false;
- }
- }
|