b261456441d996c519ea43b8c66d8142d41389ef
[yaffs-website] / vendor / phpunit / phpunit / src / Framework / Constraint / Or.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  * Logical OR.
13  *
14  * @since Class available since Release 3.0.0
15  */
16 class PHPUnit_Framework_Constraint_Or extends PHPUnit_Framework_Constraint
17 {
18     /**
19      * @var PHPUnit_Framework_Constraint[]
20      */
21     protected $constraints = array();
22
23     /**
24      * @param PHPUnit_Framework_Constraint[] $constraints
25      */
26     public function setConstraints(array $constraints)
27     {
28         $this->constraints = array();
29
30         foreach ($constraints as $constraint) {
31             if (!($constraint instanceof PHPUnit_Framework_Constraint)) {
32                 $constraint = new PHPUnit_Framework_Constraint_IsEqual(
33                     $constraint
34                 );
35             }
36
37             $this->constraints[] = $constraint;
38         }
39     }
40
41     /**
42      * Evaluates the constraint for parameter $other
43      *
44      * If $returnResult is set to false (the default), an exception is thrown
45      * in case of a failure. null is returned otherwise.
46      *
47      * If $returnResult is true, the result of the evaluation is returned as
48      * a boolean value instead: true in case of success, false in case of a
49      * failure.
50      *
51      * @param mixed  $other        Value or object to evaluate.
52      * @param string $description  Additional information about the test
53      * @param bool   $returnResult Whether to return a result or throw an exception
54      *
55      * @return mixed
56      *
57      * @throws PHPUnit_Framework_ExpectationFailedException
58      */
59     public function evaluate($other, $description = '', $returnResult = false)
60     {
61         $success    = false;
62         $constraint = null;
63
64         foreach ($this->constraints as $constraint) {
65             if ($constraint->evaluate($other, $description, true)) {
66                 $success = true;
67                 break;
68             }
69         }
70
71         if ($returnResult) {
72             return $success;
73         }
74
75         if (!$success) {
76             $this->fail($other, $description);
77         }
78     }
79
80     /**
81      * Returns a string representation of the constraint.
82      *
83      * @return string
84      */
85     public function toString()
86     {
87         $text = '';
88
89         foreach ($this->constraints as $key => $constraint) {
90             if ($key > 0) {
91                 $text .= ' or ';
92             }
93
94             $text .= $constraint->toString();
95         }
96
97         return $text;
98     }
99
100     /**
101      * Counts the number of constraint elements.
102      *
103      * @return int
104      *
105      * @since  Method available since Release 3.4.0
106      */
107     public function count()
108     {
109         $count = 0;
110
111         foreach ($this->constraints as $constraint) {
112             $count += count($constraint);
113         }
114
115         return $count;
116     }
117 }