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

Breadcrumb

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

class InvocationHandler

@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\InvocationHandler

Expanded class hierarchy of InvocationHandler

1 file declares its use of InvocationHandler
InvocationMocker.php in vendor/phpunit/phpunit/src/Framework/MockObject/Runtime/Builder/InvocationMocker.php

File

vendor/phpunit/phpunit/src/Framework/MockObject/Runtime/InvocationHandler.php, line 23

Namespace

PHPUnit\Framework\MockObject
View source
final class InvocationHandler {
    
    /**
     * @psalm-var list<Matcher>
     */
    private array $matchers = [];
    
    /**
     * @psalm-var array<string,Matcher>
     */
    private array $matcherMap = [];
    
    /**
     * @psalm-var list<ConfigurableMethod>
     */
    private readonly array $configurableMethods;
    private readonly bool $returnValueGeneration;
    
    /**
     * @psalm-param list<ConfigurableMethod> $configurableMethods
     */
    public function __construct(array $configurableMethods, bool $returnValueGeneration) {
        $this->configurableMethods = $configurableMethods;
        $this->returnValueGeneration = $returnValueGeneration;
    }
    public function hasMatchers() : bool {
        foreach ($this->matchers as $matcher) {
            if ($matcher->hasMatchers()) {
                return true;
            }
        }
        return false;
    }
    
    /**
     * Looks up the match builder with identification $id and returns it.
     */
    public function lookupMatcher(string $id) : ?Matcher {
        return $this->matcherMap[$id] ?? null;
    }
    
    /**
     * Registers a matcher with the identification $id. The matcher can later be
     * looked up using lookupMatcher() to figure out if it has been invoked.
     *
     * @throws MatcherAlreadyRegisteredException
     */
    public function registerMatcher(string $id, Matcher $matcher) : void {
        if (isset($this->matcherMap[$id])) {
            throw new MatcherAlreadyRegisteredException($id);
        }
        $this->matcherMap[$id] = $matcher;
    }
    public function expects(InvocationOrder $rule) : InvocationMocker {
        $matcher = new Matcher($rule);
        $this->addMatcher($matcher);
        return new InvocationMocker($this, $matcher, ...$this->configurableMethods);
    }
    
    /**
     * @throws \PHPUnit\Framework\MockObject\Exception
     * @throws Exception
     */
    public function invoke(Invocation $invocation) : mixed {
        $exception = null;
        $hasReturnValue = false;
        $returnValue = null;
        foreach ($this->matchers as $match) {
            try {
                if ($match->matches($invocation)) {
                    $value = $match->invoked($invocation);
                    if (!$hasReturnValue) {
                        $returnValue = $value;
                        $hasReturnValue = true;
                    }
                }
            } catch (Exception $e) {
                $exception = $e;
            }
        }
        if ($exception !== null) {
            throw $exception;
        }
        if ($hasReturnValue) {
            return $returnValue;
        }
        if (!$this->returnValueGeneration) {
            if (strtolower($invocation->methodName()) === '__tostring') {
                return '';
            }
            throw new ReturnValueNotConfiguredException($invocation);
        }
        return $invocation->generateReturnValue();
    }
    
    /**
     * @throws Throwable
     */
    public function verify() : void {
        foreach ($this->matchers as $matcher) {
            $matcher->verify();
        }
    }
    private function addMatcher(Matcher $matcher) : void {
        $this->matchers[] = $matcher;
    }

}

Members

Title Sort descending Modifiers Object type Summary
InvocationHandler::$configurableMethods private property @psalm-var list&lt;ConfigurableMethod&gt;
InvocationHandler::$matcherMap private property @psalm-var array&lt;string,Matcher&gt;
InvocationHandler::$matchers private property @psalm-var list&lt;Matcher&gt;
InvocationHandler::$returnValueGeneration private property
InvocationHandler::addMatcher private function
InvocationHandler::expects public function
InvocationHandler::hasMatchers public function
InvocationHandler::invoke public function
InvocationHandler::lookupMatcher public function Looks up the match builder with identification $id and returns it.
InvocationHandler::registerMatcher public function Registers a matcher with the identification $id. The matcher can later be
looked up using lookupMatcher() to figure out if it has been invoked.
InvocationHandler::verify public function
InvocationHandler::__construct public function @psalm-param list&lt;ConfigurableMethod&gt; $configurableMethods

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal