Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. InvokedCount.php

class InvokedCount

@no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit

@internal This class is not covered by the backward compatibility promise for PHPUnit

Hierarchy

  • class \PHPUnit\Framework\MockObject\Rule\InvocationOrder implements \PHPUnit\Framework\SelfDescribing
    • class \PHPUnit\Framework\MockObject\Rule\InvokedCount extends \PHPUnit\Framework\MockObject\Rule\InvocationOrder

Expanded class hierarchy of InvokedCount

3 files declare their use of InvokedCount
Functions.php in vendor/phpunit/phpunit/src/Framework/Assert/Functions.php
Matcher.php in vendor/phpunit/phpunit/src/Framework/MockObject/Runtime/Matcher.php
TestCase.php in vendor/phpunit/phpunit/src/Framework/TestCase.php

File

vendor/phpunit/phpunit/src/Framework/MockObject/Runtime/Rule/InvokedCount.php, line 21

Namespace

PHPUnit\Framework\MockObject\Rule
View source
final class InvokedCount extends InvocationOrder {
    private readonly int $expectedCount;
    public function __construct(int $expectedCount) {
        $this->expectedCount = $expectedCount;
    }
    public function isNever() : bool {
        return $this->expectedCount === 0;
    }
    public function toString() : string {
        return sprintf('invoked %d time%s', $this->expectedCount, $this->expectedCount !== 1 ? 's' : '');
    }
    public function matches(BaseInvocation $invocation) : bool {
        return true;
    }
    
    /**
     * Verifies that the current expectation is valid. If everything is OK the
     * code should just return, if not it must throw an exception.
     *
     * @throws ExpectationFailedException
     */
    public function verify() : void {
        $actualCount = $this->numberOfInvocations();
        if ($actualCount !== $this->expectedCount) {
            throw new ExpectationFailedException(sprintf('Method was expected to be called %d time%s, actually called %d time%s.', $this->expectedCount, $this->expectedCount !== 1 ? 's' : '', $actualCount, $actualCount !== 1 ? 's' : ''));
        }
    }
    
    /**
     * @throws ExpectationFailedException
     */
    protected function invokedDo(BaseInvocation $invocation) : void {
        $count = $this->numberOfInvocations();
        if ($count > $this->expectedCount) {
            $message = $invocation->toString() . ' ';
            $message .= match ($this->expectedCount) {    0 => 'was not expected to be called.',
                1 => 'was not expected to be called more than once.',
                default => sprintf('was not expected to be called more than %d times.', $this->expectedCount),
            
            };
            throw new ExpectationFailedException($message);
        }
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
InvocationOrder::$invocations private property @psalm-var list<BaseInvocation>
InvocationOrder::hasBeenInvoked public function
InvocationOrder::invoked final public function
InvocationOrder::numberOfInvocations public function
InvokedCount::$expectedCount private property
InvokedCount::invokedDo protected function Overrides InvocationOrder::invokedDo
InvokedCount::isNever public function
InvokedCount::matches public function Overrides InvocationOrder::matches
InvokedCount::toString public function Returns a string representation of the object. Overrides SelfDescribing::toString
InvokedCount::verify public function Verifies that the current expectation is valid. If everything is OK the
code should just return, if not it must throw an exception.
Overrides InvocationOrder::verify
InvokedCount::__construct public function
RSS feed
Powered by Drupal