Fix bug in style changes for the Use cases on the live site.
[yaffs-website] / vendor / phpunit / phpunit / src / Framework / Constraint / Count.php
1 <?php
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
11 /**
12  * @since Class available since Release 3.6.0
13  */
14 class PHPUnit_Framework_Constraint_Count extends PHPUnit_Framework_Constraint
15 {
16     /**
17      * @var int
18      */
19     protected $expectedCount = 0;
20
21     /**
22      * @param int $expected
23      */
24     public function __construct($expected)
25     {
26         parent::__construct();
27         $this->expectedCount = $expected;
28     }
29
30     /**
31      * Evaluates the constraint for parameter $other. Returns true if the
32      * constraint is met, false otherwise.
33      *
34      * @param mixed $other
35      *
36      * @return bool
37      */
38     protected function matches($other)
39     {
40         return $this->expectedCount === $this->getCountOf($other);
41     }
42
43     /**
44      * @param mixed $other
45      *
46      * @return bool
47      */
48     protected function getCountOf($other)
49     {
50         if ($other instanceof Countable || is_array($other)) {
51             return count($other);
52         } elseif ($other instanceof Traversable) {
53             if ($other instanceof IteratorAggregate) {
54                 $iterator = $other->getIterator();
55             } else {
56                 $iterator = $other;
57             }
58
59             $key   = $iterator->key();
60             $count = iterator_count($iterator);
61
62             // manually rewind $iterator to previous key, since iterator_count
63             // moves pointer
64             if ($key !== null) {
65                 $iterator->rewind();
66                 while ($iterator->valid() && $key !== $iterator->key()) {
67                     $iterator->next();
68                 }
69             }
70
71             return $count;
72         }
73     }
74
75     /**
76      * Returns the description of the failure
77      *
78      * The beginning of failure messages is "Failed asserting that" in most
79      * cases. This method should return the second part of that sentence.
80      *
81      * @param mixed $other Evaluated value or object.
82      *
83      * @return string
84      */
85     protected function failureDescription($other)
86     {
87         return sprintf(
88             'actual size %d matches expected size %d',
89             $this->getCountOf($other),
90             $this->expectedCount
91         );
92     }
93
94     /**
95      * @return string
96      */
97     public function toString()
98     {
99         return sprintf(
100             'count matches %d',
101             $this->expectedCount
102         );
103     }
104 }