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