comparison vendor/phpunit/phpunit-mock-objects/src/Framework/MockObject/Matcher/InvokedCount.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2 /*
3 * This file is part of the PHPUnit_MockObject 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
11 /**
12 * Invocation matcher which checks if a method has been invoked a certain amount
13 * of times.
14 * If the number of invocations exceeds the value it will immediately throw an
15 * exception,
16 * If the number is less it will later be checked in verify() and also throw an
17 * exception.
18 *
19 * @since Class available since Release 1.0.0
20 */
21 class PHPUnit_Framework_MockObject_Matcher_InvokedCount extends PHPUnit_Framework_MockObject_Matcher_InvokedRecorder
22 {
23 /**
24 * @var int
25 */
26 protected $expectedCount;
27
28 /**
29 * @param int $expectedCount
30 */
31 public function __construct($expectedCount)
32 {
33 $this->expectedCount = $expectedCount;
34 }
35
36 /**
37 * @return bool
38 */
39 public function isNever()
40 {
41 return $this->expectedCount == 0;
42 }
43
44 /**
45 * @return string
46 */
47 public function toString()
48 {
49 return 'invoked ' . $this->expectedCount . ' time(s)';
50 }
51
52 /**
53 * @param PHPUnit_Framework_MockObject_Invocation $invocation
54 * @throws PHPUnit_Framework_ExpectationFailedException
55 */
56 public function invoked(PHPUnit_Framework_MockObject_Invocation $invocation)
57 {
58 parent::invoked($invocation);
59
60 $count = $this->getInvocationCount();
61
62 if ($count > $this->expectedCount) {
63 $message = $invocation->toString() . ' ';
64
65 switch ($this->expectedCount) {
66 case 0: {
67 $message .= 'was not expected to be called.';
68 }
69 break;
70
71 case 1: {
72 $message .= 'was not expected to be called more than once.';
73 }
74 break;
75
76 default: {
77 $message .= sprintf(
78 'was not expected to be called more than %d times.',
79 $this->expectedCount
80 );
81 }
82 }
83
84 throw new PHPUnit_Framework_ExpectationFailedException($message);
85 }
86 }
87
88 /**
89 * Verifies that the current expectation is valid. If everything is OK the
90 * code should just return, if not it must throw an exception.
91 *
92 * @throws PHPUnit_Framework_ExpectationFailedException
93 */
94 public function verify()
95 {
96 $count = $this->getInvocationCount();
97
98 if ($count !== $this->expectedCount) {
99 throw new PHPUnit_Framework_ExpectationFailedException(
100 sprintf(
101 'Method was expected to be called %d times, ' .
102 'actually called %d times.',
103 $this->expectedCount,
104 $count
105 )
106 );
107 }
108 }
109 }