Subversion-Projekte lars-tiefland.laravel_shop

Revision

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