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