|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace TraderInteractive; |
|
4
|
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
|
6
|
|
|
use TraderInteractive\Exceptions\FilterException; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* @coversDefaultClass \TraderInteractive\InvokableFilterer |
|
10
|
|
|
* @covers ::__construct |
|
11
|
|
|
* @covers ::<private> |
|
12
|
|
|
*/ |
|
13
|
|
|
final class InvokableFiltererTest extends TestCase |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @test |
|
17
|
|
|
* @covers ::execute |
|
18
|
|
|
*/ |
|
19
|
|
|
public function execute() |
|
20
|
|
|
{ |
|
21
|
|
|
$expected = new FilterResponse(['some' => 'response']); |
|
22
|
|
|
$mock = $this->getMockFilterer(); |
|
23
|
|
|
$mock->method('execute')->willReturn($expected); |
|
24
|
|
|
|
|
25
|
|
|
$filterer = new InvokableFilterer($mock); |
|
26
|
|
|
$actual = $filterer->execute([]); |
|
27
|
|
|
|
|
28
|
|
|
$this->assertSame($expected, $actual); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @test |
|
33
|
|
|
* @covers ::getAliases |
|
34
|
|
|
*/ |
|
35
|
|
|
public function getAliases() |
|
36
|
|
|
{ |
|
37
|
|
|
$expected = ['some' => 'alias']; |
|
38
|
|
|
$mock = $this->getMockFilterer(); |
|
39
|
|
|
$mock->method('getAliases')->willReturn($expected); |
|
40
|
|
|
|
|
41
|
|
|
$filterer = new InvokableFilterer($mock); |
|
42
|
|
|
$actual = $filterer->getAliases(); |
|
43
|
|
|
|
|
44
|
|
|
$this->assertSame($expected, $actual); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @test |
|
49
|
|
|
* @covers ::getSpecification |
|
50
|
|
|
*/ |
|
51
|
|
|
public function getSpecification() |
|
52
|
|
|
{ |
|
53
|
|
|
$expected = ['some' => 'specification']; |
|
54
|
|
|
$mock = $this->getMockFilterer(); |
|
55
|
|
|
$mock->method('getSpecification')->willReturn($expected); |
|
56
|
|
|
|
|
57
|
|
|
$filterer = new InvokableFilterer($mock); |
|
58
|
|
|
$actual = $filterer->getSpecification(); |
|
59
|
|
|
|
|
60
|
|
|
$this->assertSame($expected, $actual); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @test |
|
65
|
|
|
* @covers ::withAliases |
|
66
|
|
|
*/ |
|
67
|
|
|
public function withAliases() |
|
68
|
|
|
{ |
|
69
|
|
|
$mock = $this->getMockFilterer(); |
|
70
|
|
|
$mock->method('withAliases')->willReturnSelf(); |
|
71
|
|
|
|
|
72
|
|
|
$filterer = new InvokableFilterer($mock); |
|
73
|
|
|
$actual = $filterer->withAliases([]); |
|
74
|
|
|
|
|
75
|
|
|
$this->assertSame($mock, $actual); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @test |
|
80
|
|
|
* @covers ::withSpecification |
|
81
|
|
|
*/ |
|
82
|
|
|
public function withSpecification() |
|
83
|
|
|
{ |
|
84
|
|
|
$mock = $this->getMockFilterer(); |
|
85
|
|
|
$mock->method('withSpecification')->willReturnSelf(); |
|
86
|
|
|
|
|
87
|
|
|
$filterer = new InvokableFilterer($mock); |
|
88
|
|
|
$actual = $filterer->withSpecification([]); |
|
89
|
|
|
|
|
90
|
|
|
$this->assertSame($mock, $actual); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @test |
|
95
|
|
|
* @covers ::__invoke |
|
96
|
|
|
* @dataProvider provideInvoke |
|
97
|
|
|
* |
|
98
|
|
|
* @param array $filter |
|
99
|
|
|
* @param array $options |
|
100
|
|
|
* @param array|null $filterAliases |
|
101
|
|
|
* @param mixed $value |
|
102
|
|
|
* @param array $expected |
|
103
|
|
|
*/ |
|
104
|
|
|
public function invoke(array $filter, array $options, $filterAliases, $value, array $expected) |
|
105
|
|
|
{ |
|
106
|
|
|
$filterer = new InvokableFilterer(new Filterer($filter, $options, $filterAliases)); |
|
107
|
|
|
$response = $filterer($value); |
|
108
|
|
|
|
|
109
|
|
|
$this->assertSame($expected, $response); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @returns array |
|
114
|
|
|
*/ |
|
115
|
|
|
public function provideInvoke() : array |
|
116
|
|
|
{ |
|
117
|
|
|
return [ |
|
118
|
|
|
'empty' => [ |
|
119
|
|
|
'filter' => [], |
|
120
|
|
|
'options' => [], |
|
121
|
|
|
'filterAliases' => null, |
|
122
|
|
|
'value' => [], |
|
123
|
|
|
'expected' => [], |
|
124
|
|
|
], |
|
125
|
|
|
'basic use' => [ |
|
126
|
|
|
'filter' => ['id' => [['uint']]], |
|
127
|
|
|
'options' => ['defaultRequired' => true], |
|
128
|
|
|
'filterAliases' => null, |
|
129
|
|
|
'value' => ['id' => '1'], |
|
130
|
|
|
'expected' => ['id' => 1], |
|
131
|
|
|
], |
|
132
|
|
|
'with custom alias' => [ |
|
133
|
|
|
'filter' => ['id' => [['hocuspocus']]], |
|
134
|
|
|
'options' => ['defaultRequired' => true], |
|
135
|
|
|
'filterAliases' => ['hocuspocus' => 'intval'], |
|
136
|
|
|
'value' => ['id' => '1'], |
|
137
|
|
|
'expected' => ['id' => 1], |
|
138
|
|
|
], |
|
139
|
|
|
]; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @test |
|
144
|
|
|
* @covers ::__invoke |
|
145
|
|
|
*/ |
|
146
|
|
|
public function invokeThrowsFilterException() |
|
147
|
|
|
{ |
|
148
|
|
|
$filter = ['id' => ['required' => true]]; |
|
149
|
|
|
$options = []; |
|
150
|
|
|
$value = []; |
|
151
|
|
|
|
|
152
|
|
|
$this->expectException(FilterException::class); |
|
153
|
|
|
$this->expectExceptionMessage("Field 'id' was required and not present"); |
|
154
|
|
|
|
|
155
|
|
|
$filterer = new InvokableFilterer(new Filterer($filter, $options)); |
|
156
|
|
|
$filterer($value); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* @return FiltererInterface|\PHPUnit\Framework\MockObject\MockObject |
|
161
|
|
|
*/ |
|
162
|
|
|
private function getMockFilterer() |
|
163
|
|
|
{ |
|
164
|
|
|
return $this->getMockBuilder(FiltererInterface::class)->getMock(); |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
|