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

Breadcrumb

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

class InvocationMocker

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

Hierarchy

  • class \PHPUnit\Framework\MockObject\Builder\InvocationMocker implements \PHPUnit\Framework\MockObject\Builder\InvocationStubber, \PHPUnit\Framework\MockObject\Builder\MethodNameMatch

Expanded class hierarchy of InvocationMocker

7 files declare their use of InvocationMocker
InvocationHandler.php in vendor/phpunit/phpunit/src/Framework/MockObject/Runtime/InvocationHandler.php
InvocationMockerDynamicReturnTypeExtension.php in vendor/phpstan/phpstan-phpunit/src/Type/PHPUnit/InvocationMockerDynamicReturnTypeExtension.php
Method.php in vendor/phpunit/phpunit/src/Framework/MockObject/Runtime/Api/Method.php
MockMethodCallRule.php in vendor/phpstan/phpstan-phpunit/src/Rules/PHPUnit/MockMethodCallRule.php
MockObject.php in vendor/phpunit/phpunit/src/Framework/MockObject/Runtime/Interface/MockObject.php

... See full list

File

vendor/phpunit/phpunit/src/Framework/MockObject/Runtime/Builder/InvocationMocker.php, line 48

Namespace

PHPUnit\Framework\MockObject\Builder
View source
final class InvocationMocker implements InvocationStubber, MethodNameMatch {
    private readonly InvocationHandler $invocationHandler;
    private readonly Matcher $matcher;
    
    /**
     * @psalm-var list<ConfigurableMethod>
     */
    private readonly array $configurableMethods;
    
    /**
     * @psalm-var ?array<string, int>
     */
    private ?array $configurableMethodNames = null;
    public function __construct(InvocationHandler $handler, Matcher $matcher, ConfigurableMethod ...$configurableMethods) {
        $this->invocationHandler = $handler;
        $this->matcher = $matcher;
        $this->configurableMethods = $configurableMethods;
    }
    
    /**
     * @throws MatcherAlreadyRegisteredException
     *
     * @return $this
     */
    public function id(string $id) : self {
        $this->invocationHandler
            ->registerMatcher($id, $this->matcher);
        return $this;
    }
    
    /**
     * @return $this
     */
    public function will(Stub $stub) : Identity {
        $this->matcher
            ->setStub($stub);
        return $this;
    }
    
    /**
     * @throws IncompatibleReturnValueException
     */
    public function willReturn(mixed $value, mixed ...$nextValues) : self {
        if (count($nextValues) === 0) {
            $this->ensureTypeOfReturnValues([
                $value,
            ]);
            $stub = $value instanceof Stub ? $value : new ReturnStub($value);
            return $this->will($stub);
        }
        $values = array_merge([
            $value,
        ], $nextValues);
        $this->ensureTypeOfReturnValues($values);
        $stub = new ConsecutiveCalls($values);
        return $this->will($stub);
    }
    public function willReturnReference(mixed &$reference) : self {
        $stub = new ReturnReference($reference);
        return $this->will($stub);
    }
    public function willReturnMap(array $valueMap) : self {
        $method = $this->configuredMethod();
        assert($method instanceof ConfigurableMethod);
        $numberOfParameters = $method->numberOfParameters();
        $defaultValues = $method->defaultParameterValues();
        $hasDefaultValues = !empty($defaultValues);
        $_valueMap = [];
        foreach ($valueMap as $mapping) {
            $numberOfConfiguredParameters = count($mapping) - 1;
            if ($numberOfConfiguredParameters === $numberOfParameters || !$hasDefaultValues) {
                $_valueMap[] = $mapping;
                continue;
            }
            $_mapping = [];
            $returnValue = array_pop($mapping);
            foreach (range(0, $numberOfParameters - 1) as $i) {
                if (isset($mapping[$i])) {
                    $_mapping[] = $mapping[$i];
                    continue;
                }
                if (isset($defaultValues[$i])) {
                    $_mapping[] = $defaultValues[$i];
                }
            }
            $_mapping[] = $returnValue;
            $_valueMap[] = $_mapping;
        }
        $stub = new ReturnValueMap($_valueMap);
        return $this->will($stub);
    }
    public function willReturnArgument(int $argumentIndex) : self {
        $stub = new ReturnArgument($argumentIndex);
        return $this->will($stub);
    }
    public function willReturnCallback(callable $callback) : self {
        $stub = new ReturnCallback($callback);
        return $this->will($stub);
    }
    public function willReturnSelf() : self {
        $stub = new ReturnSelf();
        return $this->will($stub);
    }
    public function willReturnOnConsecutiveCalls(mixed ...$values) : self {
        $stub = new ConsecutiveCalls($values);
        return $this->will($stub);
    }
    public function willThrowException(Throwable $exception) : self {
        $stub = new Exception($exception);
        return $this->will($stub);
    }
    
    /**
     * @return $this
     */
    public function after(string $id) : self {
        $this->matcher
            ->setAfterMatchBuilderId($id);
        return $this;
    }
    
    /**
     * @throws \PHPUnit\Framework\Exception
     * @throws MethodNameNotConfiguredException
     * @throws MethodParametersAlreadyConfiguredException
     *
     * @return $this
     */
    public function with(mixed ...$arguments) : self {
        $this->ensureParametersCanBeConfigured();
        $this->matcher
            ->setParametersRule(new Rule\Parameters($arguments));
        return $this;
    }
    
    /**
     * @throws MethodNameNotConfiguredException
     * @throws MethodParametersAlreadyConfiguredException
     *
     * @return $this
     */
    public function withAnyParameters() : self {
        $this->ensureParametersCanBeConfigured();
        $this->matcher
            ->setParametersRule(new Rule\AnyParameters());
        return $this;
    }
    
    /**
     * @throws InvalidArgumentException
     * @throws MethodCannotBeConfiguredException
     * @throws MethodNameAlreadyConfiguredException
     *
     * @return $this
     */
    public function method(Constraint|string $constraint) : self {
        if ($this->matcher
            ->hasMethodNameRule()) {
            throw new MethodNameAlreadyConfiguredException();
        }
        if (is_string($constraint)) {
            $this->configurableMethodNames ??= array_flip(array_map(static fn(ConfigurableMethod $configurable) => strtolower($configurable->name()), $this->configurableMethods));
            if (!array_key_exists(strtolower($constraint), $this->configurableMethodNames)) {
                throw new MethodCannotBeConfiguredException($constraint);
            }
        }
        $this->matcher
            ->setMethodNameRule(new Rule\MethodName($constraint));
        return $this;
    }
    
    /**
     * @throws MethodNameNotConfiguredException
     * @throws MethodParametersAlreadyConfiguredException
     */
    private function ensureParametersCanBeConfigured() : void {
        if (!$this->matcher
            ->hasMethodNameRule()) {
            throw new MethodNameNotConfiguredException();
        }
        if ($this->matcher
            ->hasParametersRule()) {
            throw new MethodParametersAlreadyConfiguredException();
        }
    }
    private function configuredMethod() : ?ConfigurableMethod {
        $configuredMethod = null;
        foreach ($this->configurableMethods as $configurableMethod) {
            if ($this->matcher
                ->methodNameRule()
                ->matchesName($configurableMethod->name())) {
                if ($configuredMethod !== null) {
                    return null;
                }
                $configuredMethod = $configurableMethod;
            }
        }
        return $configuredMethod;
    }
    
    /**
     * @throws IncompatibleReturnValueException
     */
    private function ensureTypeOfReturnValues(array $values) : void {
        $configuredMethod = $this->configuredMethod();
        if ($configuredMethod === null) {
            return;
        }
        foreach ($values as $value) {
            if (!$configuredMethod->mayReturn($value)) {
                throw new IncompatibleReturnValueException($configuredMethod, $value);
            }
        }
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
InvocationMocker::$configurableMethodNames private property @psalm-var ?array&lt;string, int&gt;
InvocationMocker::$configurableMethods private property @psalm-var list&lt;ConfigurableMethod&gt;
InvocationMocker::$invocationHandler private property
InvocationMocker::$matcher private property
InvocationMocker::after public function Overrides ParametersMatch::after
InvocationMocker::configuredMethod private function
InvocationMocker::ensureParametersCanBeConfigured private function
InvocationMocker::ensureTypeOfReturnValues private function
InvocationMocker::id public function Overrides Identity::id
InvocationMocker::method public function Overrides MethodNameMatch::method
InvocationMocker::will public function Overrides InvocationStubber::will
InvocationMocker::willReturn public function Overrides InvocationStubber::willReturn
InvocationMocker::willReturnArgument public function Overrides InvocationStubber::willReturnArgument
InvocationMocker::willReturnCallback public function Overrides InvocationStubber::willReturnCallback
InvocationMocker::willReturnMap public function @psalm-param array&lt;int, array&lt;int, mixed&gt;&gt; $valueMap Overrides InvocationStubber::willReturnMap
InvocationMocker::willReturnOnConsecutiveCalls public function Overrides InvocationStubber::willReturnOnConsecutiveCalls
InvocationMocker::willReturnReference public function Overrides InvocationStubber::willReturnReference
InvocationMocker::willReturnSelf public function Overrides InvocationStubber::willReturnSelf
InvocationMocker::willThrowException public function Overrides InvocationStubber::willThrowException
InvocationMocker::with public function Overrides ParametersMatch::with
InvocationMocker::withAnyParameters public function Overrides ParametersMatch::withAnyParameters
InvocationMocker::__construct public function

API Navigation

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