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 sprintf;
13
use PHPUnit\Framework\ExpectationFailedException;
14
use PHPUnit\Framework\MockObject\Invocation as BaseInvocation;
15
 
16
/**
17
 * @internal This class is not covered by the backward compatibility promise for PHPUnit
18
 */
19
final class InvokedCount extends InvocationOrder
20
{
21
    /**
22
     * @var int
23
     */
24
    private $expectedCount;
25
 
26
    /**
27
     * @param int $expectedCount
28
     */
29
    public function __construct($expectedCount)
30
    {
31
        $this->expectedCount = $expectedCount;
32
    }
33
 
34
    public function isNever(): bool
35
    {
36
        return $this->expectedCount === 0;
37
    }
38
 
39
    public function toString(): string
40
    {
41
        return 'invoked ' . $this->expectedCount . ' time(s)';
42
    }
43
 
44
    public function matches(BaseInvocation $invocation): bool
45
    {
46
        return true;
47
    }
48
 
49
    /**
50
     * Verifies that the current expectation is valid. If everything is OK the
51
     * code should just return, if not it must throw an exception.
52
     *
53
     * @throws ExpectationFailedException
54
     */
55
    public function verify(): void
56
    {
57
        $count = $this->getInvocationCount();
58
 
59
        if ($count !== $this->expectedCount) {
60
            throw new ExpectationFailedException(
61
                sprintf(
62
                    'Method was expected to be called %d times, ' .
63
                    'actually called %d times.',
64
                    $this->expectedCount,
65
                    $count
66
                )
67
            );
68
        }
69
    }
70
 
71
    /**
72
     * @throws ExpectationFailedException
73
     */
74
    protected function invokedDo(BaseInvocation $invocation): void
75
    {
76
        $count = $this->getInvocationCount();
77
 
78
        if ($count > $this->expectedCount) {
79
            $message = $invocation->toString() . ' ';
80
 
81
            switch ($this->expectedCount) {
82
                case 0:
83
                    $message .= 'was not expected to be called.';
84
 
85
                    break;
86
 
87
                case 1:
88
                    $message .= 'was not expected to be called more than once.';
89
 
90
                    break;
91
 
92
                default:
93
                    $message .= sprintf(
94
                        'was not expected to be called more than %d times.',
95
                        $this->expectedCount
96
                    );
97
            }
98
 
99
            throw new ExpectationFailedException($message);
100
        }
101
    }
102
}