Вход Регистрация
Файл: vendor/phpunit/phpunit/src/Framework/MockObject/Builder/InvocationMocker.php
Строк: 360
<?php declare(strict_types=1);
/*
 * This file is part of PHPUnit.
 *
 * (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.
 */
namespace PHPUnitFrameworkMockObjectBuilder;

use function 
array_map;
use function 
array_merge;
use function 
count;
use function 
in_array;
use function 
is_string;
use function 
strtolower;
use 
PHPUnitFrameworkConstraintConstraint;
use 
PHPUnitFrameworkMockObjectConfigurableMethod;
use 
PHPUnitFrameworkMockObjectIncompatibleReturnValueException;
use 
PHPUnitFrameworkMockObjectInvocationHandler;
use 
PHPUnitFrameworkMockObjectMatcher;
use 
PHPUnitFrameworkMockObjectMatcherAlreadyRegisteredException;
use 
PHPUnitFrameworkMockObjectMethodCannotBeConfiguredException;
use 
PHPUnitFrameworkMockObjectMethodNameAlreadyConfiguredException;
use 
PHPUnitFrameworkMockObjectMethodNameNotConfiguredException;
use 
PHPUnitFrameworkMockObjectMethodParametersAlreadyConfiguredException;
use 
PHPUnitFrameworkMockObjectRule;
use 
PHPUnitFrameworkMockObjectStubConsecutiveCalls;
use 
PHPUnitFrameworkMockObjectStubException;
use 
PHPUnitFrameworkMockObjectStubReturnArgument;
use 
PHPUnitFrameworkMockObjectStubReturnCallback;
use 
PHPUnitFrameworkMockObjectStubReturnReference;
use 
PHPUnitFrameworkMockObjectStubReturnSelf;
use 
PHPUnitFrameworkMockObjectStubReturnStub;
use 
PHPUnitFrameworkMockObjectStubReturnValueMap;
use 
PHPUnitFrameworkMockObjectStubStub;
use 
Throwable;

/**
 * @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
 */
final class InvocationMocker implements InvocationStubberMethodNameMatch
{
    
/**
     * @var InvocationHandler
     */
    
private $invocationHandler;

    
/**
     * @var Matcher
     */
    
private $matcher;

    
/**
     * @var ConfigurableMethod[]
     */
    
private $configurableMethods;

    public function 
__construct(InvocationHandler $handlerMatcher $matcherConfigurableMethod ...$configurableMethods)
    {
        
$this->invocationHandler   $handler;
        
$this->matcher             $matcher;
        
$this->configurableMethods $configurableMethods;
    }

    
/**
     * @throws MatcherAlreadyRegisteredException
     *
     * @return $this
     */
    
public function id($id): self
    
{
        
$this->invocationHandler->registerMatcher($id$this->matcher);

        return 
$this;
    }

    
/**
     * @return $this
     */
    
public function will(Stub $stub): Identity
    
{
        
$this->matcher->setStub($stub);

        return 
$this;
    }

    
/**
     * @param mixed   $value
     * @param mixed[] $nextValues
     *
     * @throws IncompatibleReturnValueException
     */
    
public function willReturn($value, ...$nextValues): self
    
{
        if (
count($nextValues) === 0) {
            
$this->ensureTypeOfReturnValues([$value]);

            
$stub $value instanceof Stub $value : new ReturnStub($value);
        } else {
            
$values array_merge([$value], $nextValues);

            
$this->ensureTypeOfReturnValues($values);

            
$stub = new ConsecutiveCalls($values);
        }

        return 
$this->will($stub);
    }

    public function 
willReturnReference(&$reference): self
    
{
        
$stub = new ReturnReference($reference);

        return 
$this->will($stub);
    }

    public function 
willReturnMap(array $valueMap): self
    
{
        
$stub = new ReturnValueMap($valueMap);

        return 
$this->will($stub);
    }

    public function 
willReturnArgument($argumentIndex): self
    
{
        
$stub = new ReturnArgument($argumentIndex);

        return 
$this->will($stub);
    }

    public function 
willReturnCallback($callback): self
    
{
        
$stub = new ReturnCallback($callback);

        return 
$this->will($stub);
    }

    public function 
willReturnSelf(): self
    
{
        
$stub = new ReturnSelf;

        return 
$this->will($stub);
    }

    public function 
willReturnOnConsecutiveCalls(...$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($id): self
    
{
        
$this->matcher->setAfterMatchBuilderId($id);

        return 
$this;
    }

    
/**
     * @param mixed[] $arguments
     *
     * @throws PHPUnitFrameworkException
     * @throws MethodNameNotConfiguredException
     * @throws MethodParametersAlreadyConfiguredException
     *
     * @return $this
     */
    
public function with(...$arguments): self
    
{
        
$this->ensureParametersCanBeConfigured();

        
$this->matcher->setParametersRule(new RuleParameters($arguments));

        return 
$this;
    }

    
/**
     * @param array ...$arguments
     *
     * @throws PHPUnitFrameworkException
     * @throws MethodNameNotConfiguredException
     * @throws MethodParametersAlreadyConfiguredException
     *
     * @return $this
     */
    
public function withConsecutive(...$arguments): self
    
{
        
$this->ensureParametersCanBeConfigured();

        
$this->matcher->setParametersRule(new RuleConsecutiveParameters($arguments));

        return 
$this;
    }

    
/**
     * @throws MethodNameNotConfiguredException
     * @throws MethodParametersAlreadyConfiguredException
     *
     * @return $this
     */
    
public function withAnyParameters(): self
    
{
        
$this->ensureParametersCanBeConfigured();

        
$this->matcher->setParametersRule(new RuleAnyParameters);

        return 
$this;
    }

    
/**
     * @param Constraint|string $constraint
     *
     * @throws PHPUnitFrameworkInvalidArgumentException
     * @throws MethodCannotBeConfiguredException
     * @throws MethodNameAlreadyConfiguredException
     *
     * @return $this
     */
    
public function method($constraint): self
    
{
        if (
$this->matcher->hasMethodNameRule()) {
            throw new 
MethodNameAlreadyConfiguredException;
        }

        
$configurableMethodNames array_map(
            static function (
ConfigurableMethod $configurable)
            {
                return 
strtolower($configurable->getName());
            },
            
$this->configurableMethods
        
);

        if (
is_string($constraint) && !in_array(strtolower($constraint), $configurableMethodNamestrue)) {
            throw new 
MethodCannotBeConfiguredException($constraint);
        }

        
$this->matcher->setMethodNameRule(new RuleMethodName($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 
getConfiguredMethod(): ?ConfigurableMethod
    
{
        
$configuredMethod null;

        foreach (
$this->configurableMethods as $configurableMethod) {
            if (
$this->matcher->getMethodNameRule()->matchesName($configurableMethod->getName())) {
                if (
$configuredMethod !== null) {
                    return 
null;
                }

                
$configuredMethod $configurableMethod;
            }
        }

        return 
$configuredMethod;
    }

    
/**
     * @throws IncompatibleReturnValueException
     */
    
private function ensureTypeOfReturnValues(array $values): void
    
{
        
$configuredMethod $this->getConfiguredMethod();

        if (
$configuredMethod === null) {
            return;
        }

        foreach (
$values as $value) {
            if (!
$configuredMethod->mayReturn($value)) {
                throw new 
IncompatibleReturnValueException(
                    
$configuredMethod,
                    
$value
                
);
            }
        }
    }
}
Онлайн: 1
Реклама