Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of PHPUnit.
4
 *
5
 * (c) Sebastian Bergmann <sebastian@phpunit.de>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace PHPUnit\Framework\MockObject\Rule;
11
 
12
use function count;
13
use function get_class;
14
use function sprintf;
15
use Exception;
16
use PHPUnit\Framework\Constraint\Constraint;
17
use PHPUnit\Framework\Constraint\IsAnything;
18
use PHPUnit\Framework\Constraint\IsEqual;
19
use PHPUnit\Framework\ExpectationFailedException;
20
use PHPUnit\Framework\MockObject\Invocation as BaseInvocation;
21
 
22
/**
23
 * @internal This class is not covered by the backward compatibility promise for PHPUnit
24
 */
25
final class Parameters implements ParametersRule
26
{
27
    /**
28
     * @var Constraint[]
29
     */
30
    private $parameters = [];
31
 
32
    /**
33
     * @var BaseInvocation
34
     */
35
    private $invocation;
36
 
37
    /**
38
     * @var bool|ExpectationFailedException
39
     */
40
    private $parameterVerificationResult;
41
 
42
    /**
43
     * @throws \PHPUnit\Framework\Exception
44
     */
45
    public function __construct(array $parameters)
46
    {
47
        foreach ($parameters as $parameter) {
48
            if (!($parameter instanceof Constraint)) {
49
                $parameter = new IsEqual(
50
                    $parameter
51
                );
52
            }
53
 
54
            $this->parameters[] = $parameter;
55
        }
56
    }
57
 
58
    public function toString(): string
59
    {
60
        $text = 'with parameter';
61
 
62
        foreach ($this->parameters as $index => $parameter) {
63
            if ($index > 0) {
64
                $text .= ' and';
65
            }
66
 
67
            $text .= ' ' . $index . ' ' . $parameter->toString();
68
        }
69
 
70
        return $text;
71
    }
72
 
73
    /**
74
     * @throws Exception
75
     */
76
    public function apply(BaseInvocation $invocation): void
77
    {
78
        $this->invocation                  = $invocation;
79
        $this->parameterVerificationResult = null;
80
 
81
        try {
82
            $this->parameterVerificationResult = $this->doVerify();
83
        } catch (ExpectationFailedException $e) {
84
            $this->parameterVerificationResult = $e;
85
 
86
            throw $this->parameterVerificationResult;
87
        }
88
    }
89
 
90
    /**
91
     * Checks if the invocation $invocation matches the current rules. If it
92
     * does the rule will get the invoked() method called which should check
93
     * if an expectation is met.
94
     *
95
     * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
96
     * @throws ExpectationFailedException
97
     */
98
    public function verify(): void
99
    {
100
        $this->doVerify();
101
    }
102
 
103
    /**
104
     * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
105
     * @throws ExpectationFailedException
106
     */
107
    private function doVerify(): bool
108
    {
109
        if (isset($this->parameterVerificationResult)) {
110
            return $this->guardAgainstDuplicateEvaluationOfParameterConstraints();
111
        }
112
 
113
        if ($this->invocation === null) {
114
            throw new ExpectationFailedException('Mocked method does not exist.');
115
        }
116
 
117
        if (count($this->invocation->getParameters()) < count($this->parameters)) {
118
            $message = 'Parameter count for invocation %s is too low.';
119
 
120
            // The user called `->with($this->anything())`, but may have meant
121
            // `->withAnyParameters()`.
122
            //
123
            // @see https://github.com/sebastianbergmann/phpunit-mock-objects/issues/199
124
            if (count($this->parameters) === 1 &&
125
                get_class($this->parameters[0]) === IsAnything::class) {
126
                $message .= "\nTo allow 0 or more parameters with any value, omit ->with() or use ->withAnyParameters() instead.";
127
            }
128
 
129
            throw new ExpectationFailedException(
130
                sprintf($message, $this->invocation->toString())
131
            );
132
        }
133
 
134
        foreach ($this->parameters as $i => $parameter) {
135
            $parameter->evaluate(
136
                $this->invocation->getParameters()[$i],
137
                sprintf(
138
                    'Parameter %s for invocation %s does not match expected ' .
139
                    'value.',
140
                    $i,
141
                    $this->invocation->toString()
142
                )
143
            );
144
        }
145
 
146
        return true;
147
    }
148
 
149
    /**
150
     * @throws ExpectationFailedException
151
     */
152
    private function guardAgainstDuplicateEvaluationOfParameterConstraints(): bool
153
    {
154
        if ($this->parameterVerificationResult instanceof ExpectationFailedException) {
155
            throw $this->parameterVerificationResult;
156
        }
157
 
158
        return (bool) $this->parameterVerificationResult;
159
    }
160
}