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;
11
 
12
use function assert;
13
use function implode;
14
use function sprintf;
15
use PHPUnit\Framework\ExpectationFailedException;
16
use PHPUnit\Framework\MockObject\Rule\AnyInvokedCount;
17
use PHPUnit\Framework\MockObject\Rule\AnyParameters;
18
use PHPUnit\Framework\MockObject\Rule\InvocationOrder;
19
use PHPUnit\Framework\MockObject\Rule\InvokedAtMostCount;
20
use PHPUnit\Framework\MockObject\Rule\InvokedCount;
21
use PHPUnit\Framework\MockObject\Rule\MethodName;
22
use PHPUnit\Framework\MockObject\Rule\ParametersRule;
23
use PHPUnit\Framework\MockObject\Stub\Stub;
24
use PHPUnit\Framework\TestFailure;
25
 
26
/**
27
 * @internal This class is not covered by the backward compatibility promise for PHPUnit
28
 */
29
final class Matcher
30
{
31
    /**
32
     * @var InvocationOrder
33
     */
34
    private $invocationRule;
35
 
36
    /**
37
     * @var mixed
38
     */
39
    private $afterMatchBuilderId;
40
 
41
    /**
42
     * @var bool
43
     */
44
    private $afterMatchBuilderIsInvoked = false;
45
 
46
    /**
47
     * @var MethodName
48
     */
49
    private $methodNameRule;
50
 
51
    /**
52
     * @var ParametersRule
53
     */
54
    private $parametersRule;
55
 
56
    /**
57
     * @var Stub
58
     */
59
    private $stub;
60
 
61
    public function __construct(InvocationOrder $rule)
62
    {
63
        $this->invocationRule = $rule;
64
    }
65
 
66
    public function hasMatchers(): bool
67
    {
68
        return !$this->invocationRule instanceof AnyInvokedCount;
69
    }
70
 
71
    public function hasMethodNameRule(): bool
72
    {
73
        return $this->methodNameRule !== null;
74
    }
75
 
76
    public function getMethodNameRule(): MethodName
77
    {
78
        return $this->methodNameRule;
79
    }
80
 
81
    public function setMethodNameRule(MethodName $rule): void
82
    {
83
        $this->methodNameRule = $rule;
84
    }
85
 
86
    public function hasParametersRule(): bool
87
    {
88
        return $this->parametersRule !== null;
89
    }
90
 
91
    public function setParametersRule(ParametersRule $rule): void
92
    {
93
        $this->parametersRule = $rule;
94
    }
95
 
96
    public function setStub(Stub $stub): void
97
    {
98
        $this->stub = $stub;
99
    }
100
 
101
    public function setAfterMatchBuilderId(string $id): void
102
    {
103
        $this->afterMatchBuilderId = $id;
104
    }
105
 
106
    /**
107
     * @throws ExpectationFailedException
108
     * @throws MatchBuilderNotFoundException
109
     * @throws MethodNameNotConfiguredException
110
     * @throws RuntimeException
111
     */
112
    public function invoked(Invocation $invocation)
113
    {
114
        if ($this->methodNameRule === null) {
115
            throw new MethodNameNotConfiguredException;
116
        }
117
 
118
        if ($this->afterMatchBuilderId !== null) {
119
            $matcher = $invocation->getObject()
120
                                  ->__phpunit_getInvocationHandler()
121
                                  ->lookupMatcher($this->afterMatchBuilderId);
122
 
123
            if (!$matcher) {
124
                throw new MatchBuilderNotFoundException($this->afterMatchBuilderId);
125
            }
126
 
127
            assert($matcher instanceof self);
128
 
129
            if ($matcher->invocationRule->hasBeenInvoked()) {
130
                $this->afterMatchBuilderIsInvoked = true;
131
            }
132
        }
133
 
134
        $this->invocationRule->invoked($invocation);
135
 
136
        try {
137
            if ($this->parametersRule !== null) {
138
                $this->parametersRule->apply($invocation);
139
            }
140
        } catch (ExpectationFailedException $e) {
141
            throw new ExpectationFailedException(
142
                sprintf(
143
                    "Expectation failed for %s when %s\n%s",
144
                    $this->methodNameRule->toString(),
145
                    $this->invocationRule->toString(),
146
                    $e->getMessage()
147
                ),
148
                $e->getComparisonFailure()
149
            );
150
        }
151
 
152
        if ($this->stub) {
153
            return $this->stub->invoke($invocation);
154
        }
155
 
156
        return $invocation->generateReturnValue();
157
    }
158
 
159
    /**
160
     * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
161
     * @throws ExpectationFailedException
162
     * @throws MatchBuilderNotFoundException
163
     * @throws MethodNameNotConfiguredException
164
     * @throws RuntimeException
165
     */
166
    public function matches(Invocation $invocation): bool
167
    {
168
        if ($this->afterMatchBuilderId !== null) {
169
            $matcher = $invocation->getObject()
170
                                  ->__phpunit_getInvocationHandler()
171
                                  ->lookupMatcher($this->afterMatchBuilderId);
172
 
173
            if (!$matcher) {
174
                throw new MatchBuilderNotFoundException($this->afterMatchBuilderId);
175
            }
176
 
177
            assert($matcher instanceof self);
178
 
179
            if (!$matcher->invocationRule->hasBeenInvoked()) {
180
                return false;
181
            }
182
        }
183
 
184
        if ($this->methodNameRule === null) {
185
            throw new MethodNameNotConfiguredException;
186
        }
187
 
188
        if (!$this->invocationRule->matches($invocation)) {
189
            return false;
190
        }
191
 
192
        try {
193
            if (!$this->methodNameRule->matches($invocation)) {
194
                return false;
195
            }
196
        } catch (ExpectationFailedException $e) {
197
            throw new ExpectationFailedException(
198
                sprintf(
199
                    "Expectation failed for %s when %s\n%s",
200
                    $this->methodNameRule->toString(),
201
                    $this->invocationRule->toString(),
202
                    $e->getMessage()
203
                ),
204
                $e->getComparisonFailure()
205
            );
206
        }
207
 
208
        return true;
209
    }
210
 
211
    /**
212
     * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
213
     * @throws ExpectationFailedException
214
     * @throws MethodNameNotConfiguredException
215
     */
216
    public function verify(): void
217
    {
218
        if ($this->methodNameRule === null) {
219
            throw new MethodNameNotConfiguredException;
220
        }
221
 
222
        try {
223
            $this->invocationRule->verify();
224
 
225
            if ($this->parametersRule === null) {
226
                $this->parametersRule = new AnyParameters;
227
            }
228
 
229
            $invocationIsAny    = $this->invocationRule instanceof AnyInvokedCount;
230
            $invocationIsNever  = $this->invocationRule instanceof InvokedCount && $this->invocationRule->isNever();
231
            $invocationIsAtMost = $this->invocationRule instanceof InvokedAtMostCount;
232
 
233
            if (!$invocationIsAny && !$invocationIsNever && !$invocationIsAtMost) {
234
                $this->parametersRule->verify();
235
            }
236
        } catch (ExpectationFailedException $e) {
237
            throw new ExpectationFailedException(
238
                sprintf(
239
                    "Expectation failed for %s when %s.\n%s",
240
                    $this->methodNameRule->toString(),
241
                    $this->invocationRule->toString(),
242
                    TestFailure::exceptionToString($e)
243
                )
244
            );
245
        }
246
    }
247
 
248
    public function toString(): string
249
    {
250
        $list = [];
251
 
252
        if ($this->invocationRule !== null) {
253
            $list[] = $this->invocationRule->toString();
254
        }
255
 
256
        if ($this->methodNameRule !== null) {
257
            $list[] = 'where ' . $this->methodNameRule->toString();
258
        }
259
 
260
        if ($this->parametersRule !== null) {
261
            $list[] = 'and ' . $this->parametersRule->toString();
262
        }
263
 
264
        if ($this->afterMatchBuilderId !== null) {
265
            $list[] = 'after ' . $this->afterMatchBuilderId;
266
        }
267
 
268
        if ($this->stub !== null) {
269
            $list[] = 'will ' . $this->stub->toString();
270
        }
271
 
272
        return implode(' ', $list);
273
    }
274
}