FilterTest::testFilterRules()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 226
Code Lines 144

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 144
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 226
rs 8

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace FigTree\Validation\Tests\Support;
4
5
use Closure;
6
use FigTree\Exceptions\UnexpectedTypeException;
7
use FigTree\Validation\{
8
	Contracts\FilterInterface,
9
	Contracts\RuleInterface,
10
	AbstractRuleFactory,
11
	Filter,
12
	FilterFactory,
13
	Rule,
14
	RuleFactory,
15
};
16
use FigTree\Validation\Contracts\RuleFactoryInterface;
17
use FigTree\Validation\Tests\{
18
	AbstractTestCase,
19
	Dummies\DummyFilter,
20
};
21
22
class FilterTest extends AbstractTestCase
23
{
24
	/**
25
	 * @small
26
	 *
27
	 * @return void
28
	 */
29
	public function testFilterFactory()
30
	{
31
		$ruleFactory = new RuleFactory();
32
33
		$filterFactory = new FilterFactory($ruleFactory);
34
35
		$this->assertNotNull($filterFactory->getRuleFactory());
36
		$this->assertInstanceOf(RuleFactoryInterface::class, $filterFactory->getRuleFactory());
37
		$this->assertInstanceOf(AbstractRuleFactory::class, $filterFactory->getRuleFactory());
38
		$this->assertInstanceOf(RuleFactory::class, $filterFactory->getRuleFactory());
39
40
		$filter = $filterFactory
41
			->create(function (RuleFactory $rules) {
42
				$rules = [
43
					'int' => $rules->validInt(0, 10),
44
				];
45
46
				return $rules;
47
			});
48
49
		$this->assertInstanceOf(Filter::class, $filter);
50
	}
51
52
	/**
53
	 * @small
54
	 *
55
	 * @return void
56
	 */
57
	public function testFilterFactoryInvalidReturnType()
58
	{
59
		$ruleFactory = new RuleFactory();
60
61
		$filterFactory = new FilterFactory($ruleFactory);
62
63
		$this->expectException(UnexpectedTypeException::class);
64
		$this->expectExceptionMessage(sprintf('Expected value of type array; NULL given.', RuleInterface::class));
65
66
		$filterFactory
67
			->create(function () {
68
				return null;
69
			});
70
	}
71
72
	/**
73
	 * @small
74
	 *
75
	 * @return void
76
	 */
77
	public function testFilterFactoryInvalidReturnKeys()
78
	{
79
		$ruleFactory = new RuleFactory();
80
81
		$filterFactory = new FilterFactory($ruleFactory);
82
83
		$this->expectException(UnexpectedTypeException::class);
84
		$this->expectExceptionMessage(sprintf('Expected value of type associative array; array given.', RuleInterface::class));
85
86
		$filterFactory
87
			->create(function (RuleFactory $rules) {
88
				return [
89
					$rules->cleanEmail(),
90
				];
91
			});
92
	}
93
94
95
	/**
96
	 * @small
97
	 *
98
	 * @return void
99
	 */
100
	public function testFilterFactoryInvalidReturnValues()
101
	{
102
		$ruleFactory = new RuleFactory();
103
104
		$filterFactory = new FilterFactory($ruleFactory);
105
106
		$this->expectException(UnexpectedTypeException::class);
107
		$this->expectExceptionMessage(sprintf('Expected value of type %s; string given.', RuleInterface::class));
108
109
		$filterFactory
110
			->create(function () {
111
				return [
112
					'foo' => 'bar',
113
				];
114
			});
115
	}
116
117
	/**
118
	 * @small
119
	 *
120
	 * @return void
121
	 */
122
	public function testFilter()
123
	{
124
		$filter = new DummyFilter();
125
126
		$filter->setRuleFactory(new RuleFactory());
127
128
		$this->assertInstanceOf(FilterInterface::class, $filter);
129
130
		$rules = $filter->getRules();
131
132
		$this->assertIsArray($rules);
133
134
		foreach ($rules as $field => $rule) {
135
			$this->assertIsString($field);
136
			$this->assertInstanceOf(RuleInterface::class, $rule);
137
138
			$definition = $rule->toArray();
139
140
			$this->assertIsArray($definition);
141
		}
142
	}
143
144
	/**
145
	 * @medium
146
	 */
147
	public function testFilterRules()
148
	{
149
		$filter = new DummyFilter();
150
151
		$filter->setRuleFactory(new RuleFactory());
152
153
		$rules = $filter->getRules();
154
155
		$expected = [
156
			'filter' => FILTER_VALIDATE_BOOL,
157
			'flags' => FILTER_NULL_ON_FAILURE,
158
		];
159
160
		$this->assertEquals(new Rule(FILTER_VALIDATE_BOOL, FILTER_NULL_ON_FAILURE), $rules['test_valid_bool']);
161
		$this->assertEquals($expected, $rules['test_valid_bool']->toArray());
162
163
		$expected = [
164
			'filter' => FILTER_VALIDATE_DOMAIN,
165
			'flags' => 0,
166
		];
167
168
		$this->assertEquals(new Rule(FILTER_VALIDATE_DOMAIN), $rules['test_valid_domain']);
169
		$this->assertEquals($expected, $rules['test_valid_domain']->toArray());
170
171
		$expected = [
172
			'filter' => FILTER_VALIDATE_EMAIL,
173
			'flags' => 0,
174
		];
175
176
		$this->assertEquals(new Rule(FILTER_VALIDATE_EMAIL), $rules['test_valid_email']);
177
		$this->assertEquals($expected, $rules['test_valid_email']->toArray());
178
179
		$expected = [
180
			'filter' => FILTER_VALIDATE_FLOAT,
181
			'flags' => 0,
182
			'options' => [
183
				'min_range' => -100,
184
				'max_range' => 100,
185
				'decimal' => 2,
186
			]
187
		];
188
189
		$this->assertEquals(
190
			new Rule(FILTER_VALIDATE_FLOAT, 0, ['min_range' => -100, 'max_range' => 100, 'decimal' => 2]),
191
			$rules['test_valid_float']
192
		);
193
		$this->assertEquals($expected, $rules['test_valid_float']->toArray());
194
195
		$expected = [
196
			'filter' => FILTER_VALIDATE_INT,
197
			'flags' => 0,
198
			'options' => [
199
				'min_range' => -100,
200
				'max_range' => 100,
201
			]
202
		];
203
204
		$this->assertEquals(
205
			new Rule(FILTER_VALIDATE_INT, 0, ['min_range' => -100, 'max_range' => 100]),
206
			$rules['test_valid_int']
207
		);
208
		$this->assertEquals($expected, $rules['test_valid_int']->toArray());
209
210
		$expected = [
211
			'filter' => FILTER_VALIDATE_IP,
212
			'flags' => 0,
213
		];
214
215
		$this->assertEquals(new Rule(FILTER_VALIDATE_IP), $rules['test_valid_ip_address']);
216
		$this->assertEquals($expected, $rules['test_valid_ip_address']->toArray());
217
218
		$expected = [
219
			'filter' => FILTER_VALIDATE_MAC,
220
			'flags' => 0,
221
		];
222
223
		$this->assertEquals(new Rule(FILTER_VALIDATE_MAC), $rules['test_valid_mac_address']);
224
		$this->assertEquals($expected, $rules['test_valid_mac_address']->toArray());
225
226
		$expected = [
227
			'filter' => FILTER_VALIDATE_REGEXP,
228
			'flags' => 0,
229
			'options' => [
230
				'regexp' => '/^valid value$/i',
231
			]
232
		];
233
234
		$this->assertEquals(
235
			new Rule(FILTER_VALIDATE_REGEXP, 0, ['regexp' => '/^valid value$/i']),
236
			$rules['test_valid_regexp']
237
		);
238
		$this->assertEquals($expected, $rules['test_valid_regexp']->toArray());
239
240
		$expected = [
241
			'filter' => FILTER_SANITIZE_ADD_SLASHES,
242
			'flags' => 0,
243
		];
244
245
		$this->assertEquals(
246
			new Rule(FILTER_SANITIZE_ADD_SLASHES),
247
			$rules['test_add_slashes']
248
		);
249
		$this->assertEquals($expected, $rules['test_add_slashes']->toArray());
250
251
		$expected = [
252
			'filter' => FILTER_SANITIZE_EMAIL,
253
			'flags' => 0,
254
		];
255
256
		$this->assertEquals(
257
			new Rule(FILTER_SANITIZE_EMAIL),
258
			$rules['test_clean_email']
259
		);
260
		$this->assertEquals($expected, $rules['test_clean_email']->toArray());
261
262
		$expected = [
263
			'filter' => FILTER_SANITIZE_ENCODED,
264
			'flags' => 0,
265
		];
266
267
		$this->assertEquals(
268
			new Rule(FILTER_SANITIZE_ENCODED),
269
			$rules['test_clean_encoded']
270
		);
271
		$this->assertEquals($expected, $rules['test_clean_encoded']->toArray());
272
273
		$expected = [
274
			'filter' => FILTER_SANITIZE_NUMBER_FLOAT,
275
			'flags' => 0,
276
		];
277
278
		$this->assertEquals(
279
			new Rule(FILTER_SANITIZE_NUMBER_FLOAT),
280
			$rules['test_clean_float']
281
		);
282
		$this->assertEquals($expected, $rules['test_clean_float']->toArray());
283
284
		$expected = [
285
			'filter' => FILTER_SANITIZE_FULL_SPECIAL_CHARS,
0 ignored issues
show
Bug introduced by
The constant FigTree\Validation\Tests...TIZE_FULL_SPECIAL_CHARS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
286
			'flags' => 0,
287
		];
288
289
		$this->assertEquals(
290
			new Rule(FILTER_SANITIZE_FULL_SPECIAL_CHARS),
291
			$rules['test_clean_full_special_chars']
292
		);
293
		$this->assertEquals($expected, $rules['test_clean_full_special_chars']->toArray());
294
295
		$expected = [
296
			'filter' => FILTER_SANITIZE_NUMBER_INT,
297
			'flags' => 0,
298
		];
299
300
		$this->assertEquals(
301
			new Rule(FILTER_SANITIZE_NUMBER_INT),
302
			$rules['test_clean_int']
303
		);
304
		$this->assertEquals($expected, $rules['test_clean_int']->toArray());
305
306
		$expected = [
307
			'filter' => FILTER_SANITIZE_SPECIAL_CHARS,
308
			'flags' => 0,
309
		];
310
311
		$this->assertEquals(
312
			new Rule(FILTER_SANITIZE_SPECIAL_CHARS),
313
			$rules['test_clean_special_chars']
314
		);
315
		$this->assertEquals($expected, $rules['test_clean_special_chars']->toArray());
316
317
		$expected = [
318
			'filter' => FILTER_SANITIZE_STRING,
319
			'flags' => 0,
320
		];
321
322
		$this->assertEquals(
323
			new Rule(FILTER_SANITIZE_STRING),
324
			$rules['test_clean_string']
325
		);
326
		$this->assertEquals($expected, $rules['test_clean_string']->toArray());
327
328
		$expected = [
329
			'filter' => FILTER_UNSAFE_RAW,
330
			'flags' => 0,
331
		];
332
333
		$this->assertEquals(
334
			new Rule(FILTER_UNSAFE_RAW),
335
			$rules['test_clean_unsafe']
336
		);
337
		$this->assertEquals($expected, $rules['test_clean_unsafe']->toArray());
338
339
		$expected = [
340
			'filter' => FILTER_SANITIZE_URL,
341
			'flags' => 0,
342
		];
343
344
		$this->assertEquals(
345
			new Rule(FILTER_SANITIZE_URL),
346
			$rules['test_clean_url']
347
		);
348
		$this->assertEquals($expected, $rules['test_clean_url']->toArray());
349
350
		$expected = [
351
			'filter' => FILTER_CALLBACK,
352
			'flags' => 0,
353
			'options' => Closure::fromCallable('trim'),
354
		];
355
356
		$this->assertEquals(
357
			(new Rule(FILTER_CALLBACK))->setCallback(Closure::fromCallable('trim')),
358
			$rules['test_callable']
359
		);
360
		$this->assertEquals($expected, $rules['test_callable']->toArray());
361
362
		$expected = [
363
			'filter' => FILTER_CALLBACK,
364
			'flags' => 0,
365
			'options' => $filter->mult(2),
366
		];
367
368
		$this->assertEquals(
369
			(new Rule(FILTER_CALLBACK))->setCallback($filter->mult(2)),
370
			$rules['test_closure']
371
		);
372
		$this->assertEquals($expected, $rules['test_closure']->toArray());
373
	}
374
}
375