Mercurial > hg > isophonics-drupal-site
comparison vendor/phpunit/phpunit-mock-objects/src/Matcher/InvokedCount.php @ 14:1fec387a4317
Update Drupal core to 8.5.2 via Composer
author | Chris Cannam |
---|---|
date | Mon, 23 Apr 2018 09:46:53 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
13:5fb285c0d0e3 | 14:1fec387a4317 |
---|---|
1 <?php | |
2 /* | |
3 * This file is part of the phpunit-mock-objects package. | |
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\Matcher; | |
11 | |
12 use PHPUnit\Framework\ExpectationFailedException; | |
13 use PHPUnit\Framework\MockObject\Invocation as BaseInvocation; | |
14 | |
15 /** | |
16 * Invocation matcher which checks if a method has been invoked a certain amount | |
17 * of times. | |
18 * If the number of invocations exceeds the value it will immediately throw an | |
19 * exception, | |
20 * If the number is less it will later be checked in verify() and also throw an | |
21 * exception. | |
22 */ | |
23 class InvokedCount extends InvokedRecorder | |
24 { | |
25 /** | |
26 * @var int | |
27 */ | |
28 private $expectedCount; | |
29 | |
30 /** | |
31 * @param int $expectedCount | |
32 */ | |
33 public function __construct($expectedCount) | |
34 { | |
35 $this->expectedCount = $expectedCount; | |
36 } | |
37 | |
38 /** | |
39 * @return bool | |
40 */ | |
41 public function isNever() | |
42 { | |
43 return $this->expectedCount === 0; | |
44 } | |
45 | |
46 /** | |
47 * @return string | |
48 */ | |
49 public function toString() | |
50 { | |
51 return 'invoked ' . $this->expectedCount . ' time(s)'; | |
52 } | |
53 | |
54 /** | |
55 * @param BaseInvocation $invocation | |
56 * | |
57 * @throws ExpectationFailedException | |
58 */ | |
59 public function invoked(BaseInvocation $invocation) | |
60 { | |
61 parent::invoked($invocation); | |
62 | |
63 $count = $this->getInvocationCount(); | |
64 | |
65 if ($count > $this->expectedCount) { | |
66 $message = $invocation->toString() . ' '; | |
67 | |
68 switch ($this->expectedCount) { | |
69 case 0: | |
70 $message .= 'was not expected to be called.'; | |
71 | |
72 break; | |
73 | |
74 case 1: | |
75 $message .= 'was not expected to be called more than once.'; | |
76 | |
77 break; | |
78 | |
79 default: | |
80 $message .= \sprintf( | |
81 'was not expected to be called more than %d times.', | |
82 $this->expectedCount | |
83 ); | |
84 } | |
85 | |
86 throw new ExpectationFailedException($message); | |
87 } | |
88 } | |
89 | |
90 /** | |
91 * Verifies that the current expectation is valid. If everything is OK the | |
92 * code should just return, if not it must throw an exception. | |
93 * | |
94 * @throws ExpectationFailedException | |
95 */ | |
96 public function verify() | |
97 { | |
98 $count = $this->getInvocationCount(); | |
99 | |
100 if ($count !== $this->expectedCount) { | |
101 throw new ExpectationFailedException( | |
102 \sprintf( | |
103 'Method was expected to be called %d times, ' . | |
104 'actually called %d times.', | |
105 $this->expectedCount, | |
106 $count | |
107 ) | |
108 ); | |
109 } | |
110 } | |
111 } |