1397b033068786503a2001867d8f9c65e2a48902
[yaffs-website] / vendor / phpunit / phpunit / src / Framework / Constraint / TraversableContainsOnly.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  * Constraint that asserts that the Traversable it is applied to contains
13  * only values of a given type.
14  *
15  * @since Class available since Release 3.1.4
16  */
17 class PHPUnit_Framework_Constraint_TraversableContainsOnly extends PHPUnit_Framework_Constraint
18 {
19     /**
20      * @var PHPUnit_Framework_Constraint
21      */
22     protected $constraint;
23
24     /**
25      * @var string
26      */
27     protected $type;
28
29     /**
30      * @param string $type
31      * @param bool   $isNativeType
32      */
33     public function __construct($type, $isNativeType = true)
34     {
35         parent::__construct();
36
37         if ($isNativeType) {
38             $this->constraint = new PHPUnit_Framework_Constraint_IsType($type);
39         } else {
40             $this->constraint = new PHPUnit_Framework_Constraint_IsInstanceOf(
41                 $type
42             );
43         }
44
45         $this->type = $type;
46     }
47
48     /**
49      * Evaluates the constraint for parameter $other
50      *
51      * If $returnResult is set to false (the default), an exception is thrown
52      * in case of a failure. null is returned otherwise.
53      *
54      * If $returnResult is true, the result of the evaluation is returned as
55      * a boolean value instead: true in case of success, false in case of a
56      * failure.
57      *
58      * @param mixed  $other        Value or object to evaluate.
59      * @param string $description  Additional information about the test
60      * @param bool   $returnResult Whether to return a result or throw an exception
61      *
62      * @return mixed
63      *
64      * @throws PHPUnit_Framework_ExpectationFailedException
65      */
66     public function evaluate($other, $description = '', $returnResult = false)
67     {
68         $success = true;
69
70         foreach ($other as $item) {
71             if (!$this->constraint->evaluate($item, '', true)) {
72                 $success = false;
73                 break;
74             }
75         }
76
77         if ($returnResult) {
78             return $success;
79         }
80
81         if (!$success) {
82             $this->fail($other, $description);
83         }
84     }
85
86     /**
87      * Returns a string representation of the constraint.
88      *
89      * @return string
90      */
91     public function toString()
92     {
93         return 'contains only values of type "' . $this->type . '"';
94     }
95 }