1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
<?php
/**
* PHP Unit test suite for Magento
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
*
* @category EcomDev
* @package EcomDev_PHPUnit
* @copyright Copyright (c) 2012 EcomDev BV (http://www.ecomdev.org)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @author Ivan Chepurnyi <ivan.chepurnyi@ecomdev.org>
*/
/**
* Abstract constraint for EcomDev_PHPUnit constraints
* Contains flexible constaint types implementation
*
* @todo refactor failures for being 100% compatible with PHPUnit 3.6
*/
abstract class EcomDev_PHPUnit_Constraint_Abstract
extends PHPUnit_Framework_Constraint
{
/**
* List of valiadation rules for expected value
* It is an associative array with key as type and value
* as an array of rules.
*
* First item of the rule array is mandatory indicator,
* second is function name for checking the type,
* third one is the type that will be displayed in invalid argument expception
* each of them can be ommited or if it between other ones just by specifying null value
*
* @var array
*/
protected $_expectedValueValidation = array();
/**
* List of types that will use diff for displaying fail result
*
* @var array
*/
protected $_typesWithDiff = array();
/**
* Comparisment type defined in the constructor
*
* @var string
*/
protected $_type = null;
/**
* Expected value defined in the constructor
*
* @var mixed
*/
protected $_expectedValue = null;
/**
* Custom actual value
*
* @var mixed
*/
protected $_actualValue = null;
/**
* Flag for using of actual value in failure description
*
* @var boolean
*/
protected $_useActualValue = false;
/**
* Abstract cnstraint constructor,
* provides unified interface for working with multiple types of evalation
*
* @param string $type
* @param mixed $expectedValue
*/
public function __construct($type, $expectedValue = null)
{
$reflection = EcomDev_Utils_Reflection::getRelflection(get_class($this));
$types = array();
foreach ($reflection->getConstants() as $name => $constant) {
if (strpos($name, 'TYPE_') === 0) {
$types[] = $constant;
}
}
if (empty($type) || !is_string($type) || !in_array($type, $types)) {
throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string', $type);
}
if (isset($this->_expectedValueValidation[$type])) {
$expectedValueType = (isset($this->_expectedValueValidation[$type][2]) ?
isset($this->_expectedValueValidation[$type][2]) :
'');
// Mandatory check
if (isset($this->_expectedValueValidation[$type][0])
&& $this->_expectedValueValidation[$type][0]
&& $expectedValue === null) {
throw PHPUnit_Util_InvalidArgumentHelper::factory(2, $expectedValueType, $expectedValue);
}
// Type check
if (isset($this->_expectedValueValidation[$type][1])
&& $expectedValue !== null
&& !$this->_expectedValueValidation[$type][1]($expectedValue)) {
throw PHPUnit_Util_InvalidArgumentHelper::factory(2, $expectedValueType, $expectedValue);
}
}
$this->_type = $type;
$this->_expectedValue = $expectedValue;
}
/**
* Set actual value that will be used in the fail message
*
* @param mixed $actual
* @return EcomDev_PHPUnit_Constraint_Abstract
*/
protected function setActualValue($actual)
{
$this->_useActualValue = true;
$this->_actualValue = $actual;
return $this;
}
/**
* Calls internal protected method by defined constraint type
* Also can be passed a single argument
*
* @param string $prefix
* @return mixed
*/
protected function callProtectedByType($prefix, $argument = null)
{
$camelizedType = uc_words($this->_type, '');
$methodName = $prefix . $camelizedType;
return $this->$methodName($argument);
}
/**
* Evaluates value by type.
*
* @see PHPUnit_Framework_Constraint::evaluate()
*
* @param mixed $other Value or object to evaluate.
* @param string $description Additional information about the test
* @param bool $returnResult Whether to return a result or throw an exception
* @return mixed
*/
public function evaluate($other, $description = '', $returnResult = false)
{
$success = false;
if ($this->callProtectedByType('evaluate', $other)) {
$success = true;
}
if ($returnResult) {
return $success;
}
if (!$success) {
$this->fail($other, $description);
}
}
/**
* Generates a failure exception based on exception type
*
* (non-PHPdoc)
* @see PHPUnit_Framework_Constraint::fail()
*/
public function fail($other, $description, $not = FALSE)
{
$failureDescription = $this->failureDescription($other, $description, $not);
if (in_array($this->_type, $this->_typesWithDiff)) {
throw new EcomDev_PHPUnit_Constraint_Exception(
$failureDescription,
new PHPUnit_Framework_ComparisonFailure(
$this->getExpectedValue(),
$this->getActualValue($other),
$this->getExpectedValue(),
$this->getActualValue($other)
),
$description
);
} else {
throw new EcomDev_PHPUnit_Constraint_Exception(
$failureDescription, $this->getActualValue($other), $description
);
}
}
/**
* Adds compatibility to PHPUnit 3.6
*
* @param mixed $other
* @param mixed $description (custom description)
* @param boolean $not
* @return string
*/
protected function failureDescription($other, $description, $not)
{
if (method_exists($this, 'customFailureDescription')) {
return $this->customFailureDescription($other, $description, $not);
}
return parent::failureDescription($other, $description, $not);
}
/**
* Returns a scalar representation of actual value,
* Returns $other if internal acutal value is not set
*
* @param Varien_Simplexml_Element $other
* @return scalar
*/
protected function getActualValue($other = null)
{
if ($this->_useActualValue) {
return $this->_actualValue;
}
return $other;
}
/**
* Returns a scalar representation of expected value
*
* @return scalar
*/
protected function getExpectedValue()
{
return $this->_expectedValue;
}
/**
* Text reperesentation of constraint
* (non-PHPdoc)
* @see PHPUnit_Framework_SelfDescribing::toString()
*/
public function toString()
{
return $this->callProtectedByType('text');
}
}