Version 1
[yaffs-website] / vendor / phpunit / phpunit / src / Framework / Constraint / Not.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 NOT.
13  *
14  * @since Class available since Release 3.0.0
15  */
16 class PHPUnit_Framework_Constraint_Not extends PHPUnit_Framework_Constraint
17 {
18     /**
19      * @var PHPUnit_Framework_Constraint
20      */
21     protected $constraint;
22
23     /**
24      * @param PHPUnit_Framework_Constraint $constraint
25      */
26     public function __construct($constraint)
27     {
28         parent::__construct();
29
30         if (!($constraint instanceof PHPUnit_Framework_Constraint)) {
31             $constraint = new PHPUnit_Framework_Constraint_IsEqual($constraint);
32         }
33
34         $this->constraint = $constraint;
35     }
36
37     /**
38      * @param string $string
39      *
40      * @return string
41      */
42     public static function negate($string)
43     {
44         return str_replace(
45             array(
46             'contains ',
47             'exists',
48             'has ',
49             'is ',
50             'are ',
51             'matches ',
52             'starts with ',
53             'ends with ',
54             'reference ',
55             'not not '
56             ),
57             array(
58             'does not contain ',
59             'does not exist',
60             'does not have ',
61             'is not ',
62             'are not ',
63             'does not match ',
64             'starts not with ',
65             'ends not with ',
66             'don\'t reference ',
67             'not '
68             ),
69             $string
70         );
71     }
72
73     /**
74      * Evaluates the constraint for parameter $other
75      *
76      * If $returnResult is set to false (the default), an exception is thrown
77      * in case of a failure. null is returned otherwise.
78      *
79      * If $returnResult is true, the result of the evaluation is returned as
80      * a boolean value instead: true in case of success, false in case of a
81      * failure.
82      *
83      * @param mixed  $other        Value or object to evaluate.
84      * @param string $description  Additional information about the test
85      * @param bool   $returnResult Whether to return a result or throw an exception
86      *
87      * @return mixed
88      *
89      * @throws PHPUnit_Framework_ExpectationFailedException
90      */
91     public function evaluate($other, $description = '', $returnResult = false)
92     {
93         $success = !$this->constraint->evaluate($other, $description, true);
94
95         if ($returnResult) {
96             return $success;
97         }
98
99         if (!$success) {
100             $this->fail($other, $description);
101         }
102     }
103
104     /**
105      * Returns the description of the failure
106      *
107      * The beginning of failure messages is "Failed asserting that" in most
108      * cases. This method should return the second part of that sentence.
109      *
110      * @param mixed $other Evaluated value or object.
111      *
112      * @return string
113      */
114     protected function failureDescription($other)
115     {
116         switch (get_class($this->constraint)) {
117             case 'PHPUnit_Framework_Constraint_And':
118             case 'PHPUnit_Framework_Constraint_Not':
119             case 'PHPUnit_Framework_Constraint_Or':
120                 return 'not( ' . $this->constraint->failureDescription($other) . ' )';
121
122             default:
123                 return self::negate(
124                     $this->constraint->failureDescription($other)
125                 );
126         }
127     }
128
129     /**
130      * Returns a string representation of the constraint.
131      *
132      * @return string
133      */
134     public function toString()
135     {
136         switch (get_class($this->constraint)) {
137             case 'PHPUnit_Framework_Constraint_And':
138             case 'PHPUnit_Framework_Constraint_Not':
139             case 'PHPUnit_Framework_Constraint_Or':
140                 return 'not( ' . $this->constraint->toString() . ' )';
141
142             default:
143                 return self::negate(
144                     $this->constraint->toString()
145                 );
146         }
147     }
148
149     /**
150      * Counts the number of constraint elements.
151      *
152      * @return int
153      *
154      * @since  Method available since Release 3.4.0
155      */
156     public function count()
157     {
158         return count($this->constraint);
159     }
160 }