94% of 737OPs |
95% of 113Lines |
78% of 99Branches |
67% of 48Paths |
# | |
---|---|
1 |
<?php |
2 |
|
3 |
namespace mageekguy\atoum\asserters; |
4 |
|
5 |
use |
6 |
mageekguy\atoum, |
7 |
mageekguy\atoum\tools, |
8 |
mageekguy\atoum\asserter, |
9 |
mageekguy\atoum\exceptions, |
10 |
mageekguy\atoum\tools\diffs |
11 |
; |
12 |
|
13 |
class variable extends atoum\asserter |
14 |
{ |
15 |
protected $diff = null; |
16 |
protected $isSet = false; |
17 |
protected $value = null; |
18 |
protected $isSetByReference = false; |
19 |
|
20 |
public function __construct(asserter\generator $generator = null, tools\variable\analyzer $analyzer = null, atoum\locale $locale = null)100% |
21 |
{ |
22 |
parent::__construct($generator, $analyzer, $locale); |
23 |
|
24 |
$this->setDiff(); |
25 |
} |
26 |
|
27 |
public function __toString()100% |
28 |
{ |
29 |
return $this->getTypeOf($this->value); |
30 |
} |
31 |
|
32 |
public function __get($property)100% |
33 |
{ |
34 |
switch (strtolower($property)) |
35 |
{ |
36 |
case 'isnull': |
37 |
case 'isnotnull': |
38 |
case 'isnotfalse': |
39 |
case 'isnottrue': |
40 |
case 'iscallable': |
41 |
case 'isnotcallable': |
42 |
return $this->{$property}(); |
43 |
|
44 |
default: |
45 |
return parent::__get($property); |
46 |
} |
47 |
} |
48 |
|
49 |
public function setDiff(diffs\variable $diff = null)100% |
50 |
{ |
51 |
$this->diff = $diff ?: new diffs\variable(); |
52 |
|
53 |
return $this; |
54 |
} |
55 |
|
56 |
public function getDiff()100% |
57 |
{ |
58 |
return $this->diff; |
59 |
} |
60 |
|
61 |
public function wasSet()100% |
62 |
{ |
63 |
return ($this->isSet === true); |
64 |
} |
65 |
|
66 |
public function setWith($value)100% |
67 |
{ |
68 |
parent::setWith($value); |
69 |
|
70 |
$this->value = $value; |
71 |
$this->isSet = true; |
72 |
$this->isSetByReference = false; |
73 |
|
74 |
return $this; |
75 |
} |
76 |
|
77 |
public function setByReferenceWith(& $value)100% |
78 |
{ |
79 |
$this->reset(); |
80 |
|
81 |
$this->value = & $value; |
82 |
$this->isSet = true; |
83 |
$this->isSetByReference = true; |
84 |
|
85 |
return $this; |
86 |
} |
87 |
|
88 |
public function reset()100% |
89 |
{ |
90 |
$this->value = null; |
91 |
$this->isSet = false; |
92 |
$this->isSetByReference = false; |
93 |
|
94 |
return $this; |
95 |
} |
96 |
|
97 |
public function getValue()100% |
98 |
{ |
99 |
return $this->value; |
100 |
} |
101 |
|
102 |
public function isSetByReference()100% |
103 |
{ |
104 |
return ($this->isSet === true && $this->isSetByReference === true); |
105 |
} |
106 |
|
107 |
public function isEqualTo($value, $failMessage = null)100% |
108 |
{ |
109 |
if ($this->valueIsSet()->value == $value) |
110 |
{ |
111 |
$this->pass(); |
112 |
} |
113 |
else |
114 |
{ |
115 |
$this->fail(($failMessage ?: $this->_('%s is not equal to %s', $this, $this->getTypeOf($value))) . PHP_EOL . $this->diff($value)); |
116 |
} |
117 |
|
118 |
return $this; |
119 |
} |
120 |
|
121 |
public function isNotEqualTo($value, $failMessage = null)100% |
122 |
{ |
123 |
if ($this->valueIsSet()->value != $value) |
124 |
{ |
125 |
$this->pass(); |
126 |
} |
127 |
else |
128 |
{ |
129 |
$this->fail($failMessage ?: $this->_('%s is equal to %s', $this, $this->getTypeOf($value))); |
130 |
} |
131 |
|
132 |
return $this; |
133 |
} |
134 |
|
135 |
public function isIdenticalTo($value, $failMessage = null)100% |
136 |
{ |
137 |
if ($this->valueIsSet()->value === $value) |
138 |
{ |
139 |
$this->pass(); |
140 |
} |
141 |
else |
142 |
{ |
143 |
$this->fail($failMessage ?: $this->_('%s is not identical to %s', $this, $this->getTypeOf($value)) . PHP_EOL . $this->diff($value)); |
144 |
} |
145 |
|
146 |
return $this; |
147 |
} |
148 |
|
149 |
public function isNotIdenticalTo($value, $failMessage = null)100% |
150 |
{ |
151 |
if ($this->valueIsSet()->value !== $value) |
152 |
{ |
153 |
$this->pass(); |
154 |
} |
155 |
else |
156 |
{ |
157 |
$this->fail($failMessage ?: $this->_('%s is identical to %s', $this, $this->getTypeOf($value))); |
158 |
} |
159 |
|
160 |
return $this; |
161 |
} |
162 |
|
163 |
public function isNull($failMessage = null)100% |
164 |
{ |
165 |
if ($this->valueIsSet()->value === null) |
166 |
{ |
167 |
$this->pass(); |
168 |
} |
169 |
else |
170 |
{ |
171 |
$this->fail($failMessage ?: $this->_('%s is not null', $this)); |
172 |
} |
173 |
|
174 |
return $this; |
175 |
} |
176 |
|
177 |
public function isNotNull($failMessage = null)100% |
178 |
{ |
179 |
if ($this->valueIsSet()->value !== null) |
180 |
{ |
181 |
$this->pass(); |
182 |
} |
183 |
else |
184 |
{ |
185 |
$this->fail($failMessage ?: $this->_('%s is null', $this)); |
186 |
} |
187 |
|
188 |
return $this; |
189 |
} |
190 |
|
191 |
public function isReferenceTo(& $reference, $failMessage = null)71% |
192 |
{ |
193 |
if ($this->valueIsSet()->isSetByReference() === false) |
194 |
{ |
195 |
throw new exceptions\logic('Value is not set by reference'); |
196 |
} |
197 |
|
198 |
if (is_object($this->value) === true && is_object($reference) === true) |
199 |
{ |
200 |
if ($this->value === $reference) |
201 |
{ |
202 |
$this->pass(); |
203 |
} |
204 |
else |
205 |
{ |
206 |
$this->fail($failMessage ?: $this->_('%s is not a reference to %s', $this, $this->getTypeOf($reference))); |
207 |
} |
208 |
} |
209 |
else |
210 |
{ |
211 |
$tmp = $reference; |
212 |
$reference = uniqid(mt_rand()); |
213 |
$isReference = ($this->value === $reference); |
214 |
$reference = $tmp; |
215 |
|
216 |
if ($isReference === true) |
217 |
{ |
218 |
$this->pass(); |
219 |
} |
220 |
else |
221 |
{ |
222 |
$this->fail($failMessage ?: $this->_('%s is not a reference to %s', $this, $this->getTypeOf($reference))); |
223 |
} |
224 |
} |
225 |
|
226 |
return $this; |
227 |
} |
228 |
|
229 |
public function isNotFalse($failMessage = null)100% |
230 |
{ |
231 |
return $this->isNotIdenticalTo(false, $failMessage ?: $this->_('%s is false', $this)); |
232 |
} |
233 |
|
234 |
public function isNotTrue($failMessage = null)100% |
235 |
{ |
236 |
return $this->isNotIdenticalTo(true, $failMessage ?: $this->_('%s is true', $this)); |
237 |
} |
238 |
|
239 |
public function isCallable($failMessage = null)100% |
240 |
{ |
241 |
if (is_callable($this->valueIsSet()->value) === true) |
242 |
{ |
243 |
$this->pass(); |
244 |
} |
245 |
else |
246 |
{ |
247 |
$this->fail($failMessage ?: $this->_('%s is not callable', $this)); |
248 |
} |
249 |
|
250 |
return $this; |
251 |
} |
252 |
|
253 |
public function isNotCallable($failMessage = null)100% |
254 |
{ |
255 |
if (is_callable($this->valueIsSet()->value) === false) |
256 |
{ |
257 |
$this->pass(); |
258 |
} |
259 |
else |
260 |
{ |
261 |
$this->fail($failMessage ?: $this->_('%s is callable', $this)); |
262 |
} |
263 |
|
264 |
return $this; |
265 |
} |
266 |
|
267 |
protected function valueIsSet($message = 'Value is undefined')100% |
268 |
{ |
269 |
if ($this->isSet === false) |
270 |
{ |
271 |
throw new exceptions\logic($message); |
272 |
} |
273 |
|
274 |
return $this; |
275 |
} |
276 |
|
277 |
protected function diff($expected)100% |
278 |
{ |
279 |
return $this->diff->setExpected($expected)->setActual($this->value); |
280 |
} |
281 |
} |