3 * This file is part of PHPUnit.
5 * (c) Sebastian Bergmann <sebastian@phpunit.de>
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
12 * @since Class available since Release 3.6.0
14 class PHPUnit_Framework_Constraint_Count extends PHPUnit_Framework_Constraint
19 protected $expectedCount = 0;
22 * @param int $expected
24 public function __construct($expected)
26 parent::__construct();
27 $this->expectedCount = $expected;
31 * Evaluates the constraint for parameter $other. Returns true if the
32 * constraint is met, false otherwise.
38 protected function matches($other)
40 return $this->expectedCount === $this->getCountOf($other);
48 protected function getCountOf($other)
50 if ($other instanceof Countable || is_array($other)) {
52 } elseif ($other instanceof Traversable) {
53 if ($other instanceof IteratorAggregate) {
54 $iterator = $other->getIterator();
59 $key = $iterator->key();
60 $count = iterator_count($iterator);
62 // manually rewind $iterator to previous key, since iterator_count
66 while ($iterator->valid() && $key !== $iterator->key()) {
76 * Returns the description of the failure
78 * The beginning of failure messages is "Failed asserting that" in most
79 * cases. This method should return the second part of that sentence.
81 * @param mixed $other Evaluated value or object.
85 protected function failureDescription($other)
88 'actual size %d matches expected size %d',
89 $this->getCountOf($other),
97 public function toString()