Total Complexity | 49 |
Total Lines | 293 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like ComparissonFunctionTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ComparissonFunctionTest, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class ComparissonFunctionTest extends TestCase |
||
17 | { |
||
18 | public function setup(): void |
||
19 | { |
||
20 | FunctionsLoader::include(); |
||
21 | } |
||
22 | |||
23 | public function testCanFindEqualToString(): void |
||
24 | { |
||
25 | foreach (ComparissonCases::stringComparisson('pass') as $condition) { |
||
26 | $this->assertTrue( |
||
27 | Comp\isEqualTo($condition['expected'])($condition['test']), |
||
28 | "Failed on {$condition['expected']} = {$condition['test']}" |
||
29 | ); |
||
30 | } |
||
31 | foreach (ComparissonCases::stringComparisson('fail') as $condition) { |
||
32 | $this->assertFalse( |
||
33 | Comp\isEqualTo($condition['expected'])($condition['test']), |
||
34 | "Failed on {$condition['expected']} != {$condition['test']}" |
||
35 | ); |
||
36 | } |
||
37 | } |
||
38 | |||
39 | public function testCanFindEqualToInteger(): void |
||
40 | { |
||
41 | foreach (ComparissonCases::integerComparisons('pass') as $condition) { |
||
42 | $this->assertTrue( |
||
43 | Comp\isEqualTo($condition['expected'])($condition['test']), |
||
44 | "Failed on {$condition['expected']} = {$condition['test']}" |
||
45 | ); |
||
46 | } |
||
47 | foreach (ComparissonCases::integerComparisons('fail') as $condition) { |
||
48 | $this->assertFalse( |
||
49 | Comp\isEqualTo($condition['expected'])($condition['test']), |
||
50 | "Failed on {$condition['expected']} != {$condition['test']}" |
||
51 | ); |
||
52 | } |
||
53 | } |
||
54 | |||
55 | public function testCanFindEqualToFloat(): void |
||
56 | { |
||
57 | foreach (ComparissonCases::floatComparisons('pass') as $condition) { |
||
58 | $this->assertTrue( |
||
59 | Comp\isEqualTo($condition['expected'])($condition['test']), |
||
60 | "Failed on {$condition['expected']} = {$condition['test']}" |
||
61 | ); |
||
62 | } |
||
63 | foreach (ComparissonCases::floatComparisons('fail') as $condition) { |
||
64 | $this->assertFalse( |
||
65 | Comp\isEqualTo($condition['expected'])($condition['test']), |
||
66 | "Failed on {$condition['expected']} != {$condition['test']}" |
||
67 | ); |
||
68 | } |
||
69 | } |
||
70 | |||
71 | public function testCanFindEqualToArray(): void |
||
72 | { |
||
73 | foreach (ComparissonCases::arrayComparisons('pass') as $condition) { |
||
74 | $this->assertTrue( |
||
75 | Comp\isEqualTo($condition['expected'])($condition['test']) |
||
76 | ); |
||
77 | } |
||
78 | foreach (ComparissonCases::arrayComparisons('fail') as $condition) { |
||
79 | $this->assertFalse( |
||
80 | Comp\isEqualTo($condition['expected'])($condition['test']) |
||
81 | ); |
||
82 | } |
||
83 | } |
||
84 | |||
85 | public function testCanFindEqualToObject(): void |
||
86 | { |
||
87 | foreach (ComparissonCases::objectComparisons('pass') as $condition) { |
||
88 | $this->assertTrue( |
||
89 | Comp\isEqualTo($condition['expected'])($condition['test']) |
||
90 | ); |
||
91 | } |
||
92 | foreach (ComparissonCases::objectComparisons('fail') as $condition) { |
||
93 | $this->assertFalse( |
||
94 | Comp\isEqualTo($condition['expected'])($condition['test']) |
||
95 | ); |
||
96 | } |
||
97 | } |
||
98 | |||
99 | // Test notEqual with just 1 set, its just !isEqualTo()() |
||
100 | public function testCanNotFindEqualToObject(): void |
||
101 | { |
||
102 | foreach (ComparissonCases::objectComparisons('fail') as $condition) { |
||
103 | $this->assertTrue( |
||
104 | Comp\isNotEqualTo($condition['expected'])($condition['test']) |
||
105 | ); |
||
106 | } |
||
107 | foreach (ComparissonCases::objectComparisons('pass') as $condition) { |
||
108 | $this->assertFalse( |
||
109 | Comp\isNotEqualTo($condition['expected'])($condition['test']) |
||
110 | ); |
||
111 | } |
||
112 | } |
||
113 | |||
114 | public function testCanDoGreaterThan(): void |
||
115 | { |
||
116 | $this->assertFalse(Comp\isGreaterThan(12)(10)); |
||
117 | $this->assertFalse(Comp\isGreaterThan(99.99)(98)); |
||
118 | $this->assertTrue(Comp\isGreaterThan(99.99)(100)); |
||
119 | $this->assertTrue(Comp\isGreaterThan(1)(1.0000001)); |
||
120 | } |
||
121 | |||
122 | public function testCanDoLessThan(): void |
||
123 | { |
||
124 | $this->assertTrue(Comp\isLessThan(12)(10)); |
||
125 | $this->assertTrue(Comp\isLessThan(99.99)(98)); |
||
126 | $this->assertFalse(Comp\isLessThan(99.99)(100)); |
||
127 | $this->assertFalse(Comp\isLessThan(1)(1.0000001)); |
||
128 | } |
||
129 | |||
130 | /** @testdox It should be possible to create a closure that is created with a comparisson value and then be used to check if the passed vaue is equal to or less than. */ |
||
131 | public function testCanDoLessThanOrEqualTo(): void |
||
132 | { |
||
133 | $this->assertTrue(Comp\isLessThanOrEqualTo(12)(12)); |
||
134 | $this->assertTrue(Comp\isLessThanOrEqualTo(12)(10)); |
||
135 | $this->assertTrue(Comp\isLessThanOrEqualTo(99.99)(99.99)); |
||
136 | $this->assertTrue(Comp\isLessThanOrEqualTo(99.99)(98)); |
||
137 | |||
138 | $this->assertFalse(Comp\isLessThanOrEqualTo(99.99)(100)); |
||
139 | $this->assertFalse(Comp\isLessThanOrEqualTo(1)(1.0000001)); |
||
140 | } |
||
141 | |||
142 | /** @testdox It should be possible to create a closure that is created with a comparisson value and then be used to check if the passed vaue is equal to or greater than. */ |
||
143 | public function testCanDoGreaterThanOrEqualTo(): void |
||
144 | { |
||
145 | $this->assertFalse(Comp\isGreaterThanOrEqualTo(12)(10)); |
||
146 | $this->assertFalse(Comp\isGreaterThanOrEqualTo(99.99)(98)); |
||
147 | |||
148 | $this->assertTrue(Comp\isGreaterThanOrEqualTo(99.99)(99.99)); |
||
149 | $this->assertTrue(Comp\isGreaterThanOrEqualTo(99.99)(100)); |
||
150 | $this->assertTrue(Comp\isGreaterThanOrEqualTo(1)(1.0000001)); |
||
151 | $this->assertTrue(Comp\isGreaterThanOrEqualTo(1)(1)); |
||
152 | } |
||
153 | |||
154 | public function testCanCompareScalarTypeGroup(): void |
||
155 | { |
||
156 | foreach (ComparissonCases::scalarComparisons('pass') as $condition) { |
||
157 | $this->assertTrue( |
||
158 | call_user_func_array( |
||
159 | 'PinkCrab\FunctionConstructors\Comparisons\sameScalar', |
||
160 | $condition |
||
161 | ) |
||
162 | ); |
||
163 | } |
||
164 | foreach (ComparissonCases::scalarComparisons('fail') as $condition) { |
||
165 | $this->assertFalse( |
||
166 | call_user_func_array( |
||
167 | 'PinkCrab\FunctionConstructors\Comparisons\sameScalar', |
||
168 | $condition |
||
169 | ) |
||
170 | ); |
||
171 | } |
||
172 | } |
||
173 | |||
174 | |||
175 | public function testIsScala(): void |
||
176 | { |
||
177 | foreach ( |
||
178 | array( |
||
179 | 'integer' => 12, |
||
180 | 'double' => 12.5, |
||
181 | 'boolean' => false, |
||
182 | 'string' => 'string', |
||
183 | 'array' => array(), |
||
184 | 'object' => (object) array(), |
||
185 | ) as $type => $expression |
||
186 | ) { |
||
187 | $callback = Comp\isScalar($type); |
||
188 | |||
189 | $this->assertTrue( |
||
190 | $callback($expression) |
||
191 | ); |
||
192 | } |
||
193 | } |
||
194 | |||
195 | /** OR */ |
||
196 | |||
197 | public function testCanFindEqualToOrString(): void |
||
198 | { |
||
199 | foreach (ComparissonCases::equalToOrComparisson('pass') as $condition) { |
||
200 | $this->assertTrue( |
||
201 | Comp\isEqualIn($condition['needles'])($condition['haystack']) |
||
202 | ); |
||
203 | } |
||
204 | |||
205 | foreach (ComparissonCases::equalToOrComparisson('fail') as $condition) { |
||
206 | $this->assertFalse( |
||
207 | Comp\isEqualIn($condition['needles'])($condition['haystack']) |
||
208 | ); |
||
209 | } |
||
210 | } |
||
211 | |||
212 | /** AND */ |
||
213 | |||
214 | public function testCanGroupAndConditionalsWithArrays(): void |
||
215 | { |
||
216 | foreach (ComparissonCases::groupSingleAndComparisonsArrays('pass') as $condition) { |
||
217 | $this->assertEquals( |
||
218 | $condition['expected'], |
||
219 | array_values(call_user_func($condition['function'], $condition['array'])) |
||
220 | ); |
||
221 | } |
||
222 | |||
223 | foreach (ComparissonCases::groupSingleAndComparisonsArrays('fail') as $condition) { |
||
224 | $this->assertNotEquals( |
||
225 | $condition['expected'], |
||
226 | array_values(call_user_func($condition['function'], $condition['array'])) |
||
227 | ); |
||
228 | } |
||
229 | } |
||
230 | |||
231 | public function testCanGroupAndConditionalsWithString(): void |
||
232 | { |
||
233 | foreach (ComparissonCases::groupSingleAndComparisonsStrings('pass') as $condition) { |
||
234 | foreach ($condition['value'] as $value) { |
||
235 | $this->assertTrue($condition['function']($value)); |
||
236 | } |
||
237 | } |
||
238 | |||
239 | foreach (ComparissonCases::groupSingleAndComparisonsStrings('fail') as $condition) { |
||
240 | foreach ($condition['value'] as $value) { |
||
241 | $this->assertFalse($condition['function']($value)); |
||
242 | } |
||
243 | } |
||
244 | } |
||
245 | |||
246 | public function testCanGroupGroupsOfConditionalsComparisonsMixed() |
||
247 | { |
||
248 | foreach (ComparissonCases::groupedAndOrComparissonMixed('pass') as $condition) { |
||
249 | foreach ($condition['value'] as $value) { |
||
250 | $this->assertTrue($condition['function']($value), $value); |
||
251 | } |
||
252 | } |
||
253 | |||
254 | foreach (ComparissonCases::groupedAndOrComparissonMixed('fail') as $condition) { |
||
255 | foreach ($condition['value'] as $value) { |
||
256 | $this->assertFalse($condition['function']($value)); |
||
257 | } |
||
258 | } |
||
259 | } |
||
260 | |||
261 | public function testCanMatchBooleans(): void |
||
262 | { |
||
263 | $this->assertTrue(Comp\allTrue(true, true, 1 == 1, 4 === (3 + 1))); // t |
||
264 | $this->assertFalse(Comp\allTrue(true, true, 1 == 3, 4 === (3 + 1))); // f |
||
265 | $this->assertTrue(Comp\anyTrue(true, true, 1 == 3, 4 === (3 + 1))); //t |
||
266 | $this->assertTrue(! Comp\allTrue(false, false, 1 == 3, 4 === (3 * 1))); //t |
||
267 | $this->assertFalse(Comp\allTrue(false, false, 1 == 3, 4 === (3 * 1))); //t |
||
268 | $this->assertFalse(Comp\anyTrue(false, false, 1 == 3, 4 === (3 * 1))); //f |
||
269 | } |
||
270 | |||
271 | public function testCanCheckIsTrue(): void |
||
272 | { |
||
273 | $this->assertTrue(Comp\isTrue(true)); |
||
274 | $this->assertTrue(Comp\isTrue((bool) 1)); |
||
275 | $this->assertFalse(Comp\isTrue(false)); |
||
276 | $this->assertFalse(Comp\isTrue(0)); |
||
277 | $this->assertFalse(Comp\isTrue(array( 0 ))); |
||
278 | } |
||
279 | |||
280 | public function testCanCheckIsFalse(): void |
||
281 | { |
||
282 | $this->assertTrue(Comp\isFalse(false)); |
||
283 | $this->assertTrue(Comp\isFalse((bool) 0)); |
||
284 | $this->assertFalse(Comp\isFalse(true)); |
||
285 | $this->assertFalse(Comp\isFalse(0)); |
||
286 | $this->assertFalse(Comp\isFalse(array( 0 ))); |
||
287 | } |
||
288 | |||
289 | public function testNotEmpty() |
||
290 | { |
||
291 | $this->assertTrue(Comp\notEmpty(array( false, 1, 2 ))); |
||
292 | $this->assertTrue(Comp\notEmpty('Hello')); |
||
293 | $this->assertFalse(Comp\notEmpty('')); |
||
294 | $this->assertFalse(Comp\notEmpty(array())); |
||
295 | } |
||
296 | |||
297 | public function testCanUseNot() |
||
298 | { |
||
299 | $function = function ($a) { |
||
300 | return $a === 1; |
||
301 | }; |
||
302 | |||
303 | $this->assertTrue(Comp\not($function)(2)); |
||
304 | $this->assertFalse(Comp\not($function)(1)); |
||
305 | |||
306 | $notEquals1 = Comp\not($function); |
||
307 | $this->assertTrue($notEquals1(2)); |
||
308 | $this->assertFalse($notEquals1(1)); |
||
309 | } |
||
311 |