Subversion-Projekte lars-tiefland.laravel_shop

Revision

Zur aktuellen 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 gettype;
14
use function is_iterable;
15
use function sprintf;
16
use PHPUnit\Framework\Constraint\Constraint;
17
use PHPUnit\Framework\Constraint\IsEqual;
18
use PHPUnit\Framework\ExpectationFailedException;
19
use PHPUnit\Framework\InvalidParameterGroupException;
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 ConsecutiveParameters implements ParametersRule
26
{
27
    /**
28
     * @var array
29
     */
30
    private $parameterGroups = [];
31
 
32
    /**
33
     * @var array
34
     */
35
    private $invocations = [];
36
 
37
    /**
38
     * @throws \PHPUnit\Framework\Exception
39
     */
40
    public function __construct(array $parameterGroups)
41
    {
42
        foreach ($parameterGroups as $index => $parameters) {
43
            if (!is_iterable($parameters)) {
44
                throw new InvalidParameterGroupException(
45
                    sprintf(
46
                        'Parameter group #%d must be an array or Traversable, got %s',
47
                        $index,
48
                        gettype($parameters)
49
                    )
50
                );
51
            }
52
 
53
            foreach ($parameters as $parameter) {
54
                if (!$parameter instanceof Constraint) {
55
                    $parameter = new IsEqual($parameter);
56
                }
57
 
58
                $this->parameterGroups[$index][] = $parameter;
59
            }
60
        }
61
    }
62
 
63
    public function toString(): string
64
    {
65
        return 'with consecutive parameters';
66
    }
67
 
68
    /**
69
     * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
70
     * @throws ExpectationFailedException
71
     */
72
    public function apply(BaseInvocation $invocation): void
73
    {
74
        $this->invocations[] = $invocation;
75
        $callIndex           = count($this->invocations) - 1;
76
 
77
        $this->verifyInvocation($invocation, $callIndex);
78
    }
79
 
80
    /**
81
     * @throws \PHPUnit\Framework\ExpectationFailedException
82
     * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
83
     */
84
    public function verify(): void
85
    {
86
        foreach ($this->invocations as $callIndex => $invocation) {
87
            $this->verifyInvocation($invocation, $callIndex);
88
        }
89
    }
90
 
91
    /**
92
     * Verify a single invocation.
93
     *
94
     * @param int $callIndex
95
     *
96
     * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
97
     * @throws ExpectationFailedException
98
     */
99
    private function verifyInvocation(BaseInvocation $invocation, $callIndex): void
100
    {
101
        if (!isset($this->parameterGroups[$callIndex])) {
102
            // no parameter assertion for this call index
103
            return;
104
        }
105
 
106
        $parameters = $this->parameterGroups[$callIndex];
107
 
108
        if (count($invocation->getParameters()) < count($parameters)) {
109
            throw new ExpectationFailedException(
110
                sprintf(
111
                    'Parameter count for invocation %s is too low.',
112
                    $invocation->toString()
113
                )
114
            );
115
        }
116
 
117
        foreach ($parameters as $i => $parameter) {
118
            $parameter->evaluate(
119
                $invocation->getParameters()[$i],
120
                sprintf(
121
                    'Parameter %s for invocation #%d %s does not match expected ' .
122
                    'value.',
123
                    $i,
124
                    $callIndex,
125
                    $invocation->toString()
126
                )
127
            );
128
        }
129
    }
130
}