1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Graze\ArrayFilter\Test\Unit; |
4
|
|
|
|
5
|
|
|
use Graze\ArrayFilter\AllOfFilter; |
6
|
|
|
use Graze\ArrayFilter\ClosureFilter; |
7
|
|
|
use Graze\ArrayFilter\Test\TestCase; |
8
|
|
|
|
9
|
|
|
class AllOfFilterTest extends TestCase |
10
|
|
|
{ |
11
|
|
|
public function testInstanceOf() |
12
|
|
|
{ |
13
|
|
|
$filter = new AllOfFilter(); |
14
|
|
|
static::assertInstanceOf('Graze\ArrayFilter\ArrayFilterInterface', $filter); |
15
|
|
|
} |
16
|
|
|
|
17
|
|
View Code Duplication |
public function testSingleChildConstructorEquals() |
|
|
|
|
18
|
|
|
{ |
19
|
|
|
$filter = new AllOfFilter([ |
20
|
|
|
new ClosureFilter('test', function ($actual) { |
21
|
|
|
return $actual == 'value'; |
22
|
|
|
}), |
23
|
|
|
]); |
24
|
|
|
static::assertTrue($filter->matches(['test' => 'value'])); |
25
|
|
|
static::assertFalse($filter->matches(['test' => 'values'])); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
View Code Duplication |
public function testSingleChildAddedLaterEquals() |
|
|
|
|
29
|
|
|
{ |
30
|
|
|
$filter = new AllOfFilter(); |
31
|
|
|
$filter->addFilter(new ClosureFilter('test', function ($actual) { |
32
|
|
|
return $actual == 'value'; |
33
|
|
|
})); |
34
|
|
|
static::assertTrue($filter->matches(['test' => 'value'])); |
35
|
|
|
static::assertFalse($filter->matches(['test' => 'values'])); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function testMultipleChildrenWithDifferentProperties() |
39
|
|
|
{ |
40
|
|
|
$filter = new AllOfFilter([ |
41
|
|
|
new ClosureFilter('test', function ($actual) { |
42
|
|
|
return $actual == 'value'; |
43
|
|
|
}), |
44
|
|
|
new ClosureFilter('test2', function ($actual) { |
45
|
|
|
return $actual == 'value2'; |
46
|
|
|
}), |
47
|
|
|
]); |
48
|
|
|
static::assertTrue($filter->matches(['test' => 'value', 'test2' => 'value2'])); |
49
|
|
|
static::assertFalse($filter->matches(['test' => 'values', 'test2' => 'value2'])); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
View Code Duplication |
public function testMultipleChildrenWithTheSameProperty() |
|
|
|
|
53
|
|
|
{ |
54
|
|
|
$filter = new AllOfFilter([ |
55
|
|
|
new ClosureFilter('test', function ($actual) { |
56
|
|
|
return $actual != 'foo'; |
57
|
|
|
}), |
58
|
|
|
new ClosureFilter('test', function ($actual) { |
59
|
|
|
return $actual != 'bar'; |
60
|
|
|
}), |
61
|
|
|
]); |
62
|
|
|
static::assertTrue($filter->matches(['test' => 'value'])); |
63
|
|
|
static::assertFalse($filter->matches(['test' => 'foo'])); |
64
|
|
|
static::assertFalse($filter->matches(['test' => 'bar'])); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
View Code Duplication |
public function testFilterCanBeInvoked() |
|
|
|
|
68
|
|
|
{ |
69
|
|
|
$filter = new AllOfFilter([ |
70
|
|
|
new ClosureFilter('test', function ($actual) { |
71
|
|
|
return $actual == 'value'; |
72
|
|
|
}), |
73
|
|
|
]); |
74
|
|
|
static::assertTrue(call_user_func($filter, ['test' => 'value'])); |
75
|
|
|
static::assertFalse(call_user_func($filter, ['test' => 'values'])); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
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.