1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Povs\ListerBundle\Service; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
6
|
|
|
use PHPUnit\Framework\TestCase; |
7
|
|
|
use Povs\ListerBundle\Mapper\FilterField; |
8
|
|
|
use Povs\ListerBundle\Mapper\FilterMapper; |
9
|
|
|
use Povs\ListerBundle\Mapper\ListField; |
10
|
|
|
use Povs\ListerBundle\Mapper\ListMapper; |
11
|
|
|
use Symfony\Component\Form\FormInterface; |
12
|
|
|
use Symfony\Component\HttpFoundation\ParameterBag; |
13
|
|
|
use Symfony\Component\HttpFoundation\Request; |
14
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @author Povilas Margaiatis <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
class RequestHandlerTest extends TestCase |
20
|
|
|
{ |
21
|
|
|
public function testHandlerRequest(): void |
22
|
|
|
{ |
23
|
|
|
$listMapperMock = $this->createMock(ListMapper::class); |
24
|
|
|
$filterMapperMock = $this->createMock(FilterMapper::class); |
25
|
|
|
$formMock = $this->createMock(FormInterface::class); |
26
|
|
|
$listFieldMock = $this->createMock(ListField::class); |
27
|
|
|
$listFieldMock2 = $this->createMock(ListField::class); |
28
|
|
|
$listFieldMock->expects($this->exactly(2)) |
29
|
|
|
->method('setOption') |
30
|
|
|
->withConsecutive( |
31
|
|
|
['sort_value', 'DESC'], |
32
|
|
|
['lazy', false] |
33
|
|
|
); |
34
|
|
|
$listFieldMock->expects($this->once()) |
35
|
|
|
->method('getId') |
36
|
|
|
->willReturn('test_id'); |
37
|
|
|
$listMapperMock->expects($this->once()) |
38
|
|
|
->method('getFields') |
39
|
|
|
->willReturn(new ArrayCollection([$listFieldMock2])); |
40
|
|
|
$listFieldMock2->expects($this->once()) |
41
|
|
|
->method('getId') |
42
|
|
|
->willReturn('another_id'); |
43
|
|
|
$listFieldMock2->expects($this->once()) |
44
|
|
|
->method('setOption') |
45
|
|
|
->with('sort_value', null); |
46
|
|
|
$listMapperMock->expects($this->once()) |
47
|
|
|
->method('get') |
48
|
|
|
->willReturn($listFieldMock); |
49
|
|
|
$formMock->expects($this->once()) |
50
|
|
|
->method('handleRequest'); |
51
|
|
|
$formMock->expects($this->once()) |
52
|
|
|
->method('isSubmitted') |
53
|
|
|
->willReturn(true); |
54
|
|
|
$formMock->expects($this->once()) |
55
|
|
|
->method('isValid') |
56
|
|
|
->willReturn(true); |
57
|
|
|
$formMock->expects($this->once()) |
58
|
|
|
->method('getData') |
59
|
|
|
->willReturn(['field1' => 'val1', 'field2' => 'val2']); |
60
|
|
|
$filterFieldMock1 = $this->createMock(FilterField::class); |
61
|
|
|
$filterFieldMock2 = $this->createMock(FilterField::class); |
62
|
|
|
$filterFieldMock1->expects($this->once()) |
63
|
|
|
->method('setValue') |
64
|
|
|
->with('val1'); |
65
|
|
|
$filterFieldMock2->expects($this->once()) |
66
|
|
|
->method('setValue') |
67
|
|
|
->with('val2'); |
68
|
|
|
$filterMapperMock->expects($this->exactly(2)) |
69
|
|
|
->method('get') |
70
|
|
|
->willReturnMap([ |
71
|
|
|
['field1', $filterFieldMock1], |
72
|
|
|
['field2', $filterFieldMock2] |
73
|
|
|
]); |
74
|
|
|
|
75
|
|
|
$requestHandler = $this->getRequestHandler([['sort', null, ['field2' => 'invalid_sort', 'field1' => 'desc']]], [], [['sort', 'sort']]); |
76
|
|
|
$requestHandler->handleRequest($listMapperMock, $filterMapperMock, $formMock); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function testGetCurrentPage(): void |
80
|
|
|
{ |
81
|
|
|
$handler = $this->getRequestHandler([['page', null, 20]], [], [['page', 'page']]); |
82
|
|
|
|
83
|
|
|
$this->assertEquals(20, $handler->getCurrentPage()); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function testGetLength(): void |
87
|
|
|
{ |
88
|
|
|
$handler = $this->getRequestHandler([['length', null, 20]], [], [['length', 'length']]); |
89
|
|
|
|
90
|
|
|
$this->assertEquals(20, $handler->getLength()); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function testGetValue(): void |
94
|
|
|
{ |
95
|
|
|
$handler = $this->getRequestHandler([['foo', null, 'custom_value']], [], [['custom_name', 'foo']]); |
96
|
|
|
|
97
|
|
|
$this->assertEquals('custom_value', $handler->getValue('custom_name')); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function testGetName(): void |
101
|
|
|
{ |
102
|
|
|
$handler = $this->getRequestHandler([], [], [['custom_name', 'foo']]); |
103
|
|
|
|
104
|
|
|
$this->assertEquals('foo', $handler->getName('custom_name')); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function testGetRequest(): void |
108
|
|
|
{ |
109
|
|
|
$handler = $this->getRequestHandler([], [], []); |
110
|
|
|
$this->assertNotNull($handler->getRequest()); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
private function getRequestHandler(array $query, array $attributes, array $configs): RequestHandler |
114
|
|
|
{ |
115
|
|
|
$requestMock = $this->createMock(Request::class); |
116
|
|
|
$queryMock = $this->createMock(ParameterBag::class); |
117
|
|
|
$attributesMock = $this->createMock(ParameterBag::class); |
118
|
|
|
$configMock = $this->createMock(ConfigurationResolver::class); |
119
|
|
|
$requestStackMock = $this->createMock(RequestStack::class); |
120
|
|
|
|
121
|
|
|
$queryMock->expects($this->exactly(count($query))) |
122
|
|
|
->method('get') |
123
|
|
|
->willReturnMap($query); |
124
|
|
|
$attributesMock->expects($this->exactly(count($attributes))) |
125
|
|
|
->method('get') |
126
|
|
|
->willReturnMap($attributes); |
127
|
|
|
$configMock->expects($this->exactly(count($configs))) |
128
|
|
|
->method('getRequestConfiguration') |
129
|
|
|
->willReturnMap($configs); |
130
|
|
|
$configMock->method('isMultiColumnSortable') |
131
|
|
|
->willReturn(false); |
132
|
|
|
$requestMock->query = $queryMock; |
|
|
|
|
133
|
|
|
$requestMock->attributes = $attributesMock; |
|
|
|
|
134
|
|
|
$requestStackMock->expects($this->once()) |
135
|
|
|
->method('getCurrentRequest') |
136
|
|
|
->willReturn($requestMock); |
137
|
|
|
|
138
|
|
|
return new RequestHandler($requestStackMock, $configMock); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|