1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of graze/array-filter |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
* |
10
|
|
|
* @license https://github.com/graze/array-filter/blob/master/LICENSE.md |
11
|
|
|
* @link https://github.com/graze/array-filter |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Graze\ArrayFilter\Test\Unit; |
15
|
|
|
|
16
|
|
|
use Graze\ArrayFilter\AllOfFilter; |
17
|
|
|
use Graze\ArrayFilter\ClosureFilter; |
18
|
|
|
use Graze\ArrayFilter\Test\TestCase; |
19
|
|
|
|
20
|
|
|
class AllOfFilterTest extends TestCase |
21
|
|
|
{ |
22
|
|
|
public function testInstanceOf() |
23
|
|
|
{ |
24
|
|
|
$filter = new AllOfFilter(); |
25
|
|
|
static::assertInstanceOf('Graze\ArrayFilter\ArrayFilterInterface', $filter); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
View Code Duplication |
public function testSingleChildConstructorEqualsMatchesAll() |
|
|
|
|
29
|
|
|
{ |
30
|
|
|
$filter = new AllOfFilter([ |
31
|
|
|
new ClosureFilter('test', function ($actual) { |
32
|
|
|
return $actual == 'value'; |
33
|
|
|
}), |
34
|
|
|
]); |
35
|
|
|
static::assertTrue($filter->matches(['test' => 'value'])); |
36
|
|
|
static::assertFalse($filter->matches(['test' => 'values'])); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
View Code Duplication |
public function testSingleChildAddedLaterEqualsMatchesAll() |
|
|
|
|
40
|
|
|
{ |
41
|
|
|
$filter = new AllOfFilter(); |
42
|
|
|
$filter->addFilter(new ClosureFilter('test', function ($actual) { |
43
|
|
|
return $actual == 'value'; |
44
|
|
|
})); |
45
|
|
|
static::assertTrue($filter->matches(['test' => 'value'])); |
46
|
|
|
static::assertFalse($filter->matches(['test' => 'values'])); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function testMultipleChildrenWithDifferentPropertiesMatchesAll() |
50
|
|
|
{ |
51
|
|
|
$filter = new AllOfFilter([ |
52
|
|
|
new ClosureFilter('test', function ($actual) { |
53
|
|
|
return $actual == 'value'; |
54
|
|
|
}), |
55
|
|
|
new ClosureFilter('test2', function ($actual) { |
56
|
|
|
return $actual == 'value2'; |
57
|
|
|
}), |
58
|
|
|
]); |
59
|
|
|
static::assertTrue($filter->matches(['test' => 'value', 'test2' => 'value2'])); |
60
|
|
|
static::assertFalse($filter->matches(['test' => 'values', 'test2' => 'value2'])); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
View Code Duplication |
public function testMultipleChildrenWithTheSamePropertyMatchesAll() |
|
|
|
|
64
|
|
|
{ |
65
|
|
|
$filter = new AllOfFilter([ |
66
|
|
|
new ClosureFilter('test', function ($actual) { |
67
|
|
|
return $actual != 'foo'; |
68
|
|
|
}), |
69
|
|
|
new ClosureFilter('test', function ($actual) { |
70
|
|
|
return $actual != 'bar'; |
71
|
|
|
}), |
72
|
|
|
]); |
73
|
|
|
static::assertTrue($filter->matches(['test' => 'value'])); |
74
|
|
|
static::assertFalse($filter->matches(['test' => 'foo'])); |
75
|
|
|
static::assertFalse($filter->matches(['test' => 'bar'])); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
View Code Duplication |
public function testCallableFiltersMatchesAll() |
|
|
|
|
79
|
|
|
{ |
80
|
|
|
$filter = new AllOfFilter([ |
81
|
|
|
function (array $data) { |
82
|
|
|
return isset($data['test']) && $data['test'] == 'value'; |
83
|
|
|
} |
84
|
|
|
]); |
85
|
|
|
static::assertTrue($filter(['test' => 'value'])); |
86
|
|
|
static::assertFalse($filter(['test' => 'values'])); |
87
|
|
|
static::assertFalse($filter(['tests' => 'values'])); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
View Code Duplication |
public function testAllOfFilterCanBeInvoked() |
|
|
|
|
91
|
|
|
{ |
92
|
|
|
$filter = new AllOfFilter([ |
93
|
|
|
new ClosureFilter('test', function ($actual) { |
94
|
|
|
return $actual == 'value'; |
95
|
|
|
}), |
96
|
|
|
]); |
97
|
|
|
static::assertTrue(call_user_func($filter, ['test' => 'value'])); |
98
|
|
|
static::assertFalse(call_user_func($filter, ['test' => 'values'])); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.