1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Povs\ListerBundle\Service; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\QueryBuilder; |
6
|
|
|
use PHPUnit\Framework\TestCase; |
7
|
|
|
use Povs\ListerBundle\Declaration\ListInterface; |
8
|
|
|
use Povs\ListerBundle\Declaration\ListValueInterface; |
9
|
|
|
use Povs\ListerBundle\DependencyInjection\Locator\ListLocator; |
10
|
|
|
use Povs\ListerBundle\Exception\ListException; |
11
|
|
|
use Povs\ListerBundle\Factory\MapperFactory; |
12
|
|
|
use Povs\ListerBundle\Factory\ListValueFactory; |
13
|
|
|
use Povs\ListerBundle\Factory\ViewFactory; |
14
|
|
|
use Povs\ListerBundle\Mapper\FilterMapper; |
15
|
|
|
use Povs\ListerBundle\Mapper\JoinMapper; |
16
|
|
|
use Povs\ListerBundle\Mapper\ListMapper; |
17
|
|
|
use Povs\ListerBundle\View\ListView; |
18
|
|
|
use Symfony\Component\Form\FormInterface; |
19
|
|
|
use Symfony\Component\HttpFoundation\Response; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @author Povilas Margaiatis <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
class ListManagerTest extends TestCase |
25
|
|
|
{ |
26
|
|
|
private $listLocatorMock; |
27
|
|
|
private $listMock; |
28
|
|
|
private $configMock; |
29
|
|
|
private $typeResolverMock; |
30
|
|
|
private $mapperFactoryMock; |
31
|
|
|
private $filterBuilderMock; |
32
|
|
|
private $requestHandlerMock; |
33
|
|
|
private $valueFactoryMock; |
34
|
|
|
private $listQueryBuilderMock; |
35
|
|
|
private $viewFactoryMock; |
36
|
|
|
|
37
|
|
|
private $listMapperMock; |
38
|
|
|
private $filterMapperMock; |
39
|
|
|
private $filterFormMock; |
40
|
|
|
private $valueMock; |
41
|
|
|
private $joinMapperMock; |
42
|
|
|
private $queryBuilderMock; |
43
|
|
|
private $lazyQueryBuilderMock; |
44
|
|
|
private $viewMock; |
45
|
|
|
|
46
|
|
|
public function setUp() |
47
|
|
|
{ |
48
|
|
|
$this->listLocatorMock = $this->createMock(ListLocator::class); |
49
|
|
|
$this->listMock = $this->createMock(ListInterface::class); |
50
|
|
|
$this->configMock = $this->createMock(ConfigurationResolver::class); |
51
|
|
|
$this->typeResolverMock = $this->createMock(ListTypeResolver::class); |
52
|
|
|
$this->mapperFactoryMock = $this->createMock(MapperFactory::class); |
53
|
|
|
$this->filterBuilderMock = $this->createMock(FilterBuilder::class); |
54
|
|
|
$this->requestHandlerMock = $this->createMock(RequestHandler::class); |
55
|
|
|
$this->valueFactoryMock = $this->createMock(ListValueFactory::class); |
56
|
|
|
$this->listQueryBuilderMock = $this->createMock(ListQueryBuilder::class); |
57
|
|
|
$this->viewFactoryMock = $this->createMock(ViewFactory::class); |
58
|
|
|
$this->listMapperMock = $this->createMock(ListMapper::class); |
59
|
|
|
$this->filterMapperMock = $this->createMock(FilterMapper::class); |
60
|
|
|
$this->filterFormMock = $this->createMock(FormInterface::class); |
61
|
|
|
$this->valueMock = $this->createMock(ListValueInterface::class); |
62
|
|
|
$this->joinMapperMock = $this->createMock(JoinMapper::class); |
63
|
|
|
$this->queryBuilderMock = $this->createMock(QueryBuilder::class); |
64
|
|
|
$this->lazyQueryBuilderMock = $this->createMock(QueryBuilder::class); |
65
|
|
|
$this->viewMock = $this->createMock(ListView::class); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
|
69
|
|
|
public function testGetResponse(): void |
70
|
|
|
{ |
71
|
|
|
$this->setBuildCalls(); |
72
|
|
|
$response = new Response(); |
73
|
|
|
$this->typeResolverMock->expects($this->once()) |
74
|
|
|
->method('getResponse') |
75
|
|
|
->with($this->viewMock, ['opt1' => 'foo', 'opt2' => 'bar']) |
76
|
|
|
->willReturn($response); |
77
|
|
|
$listManager = $this->getListManager(); |
78
|
|
|
$listManager->buildList('test_list', 'list_type', ['param1' => 'foo', 'param2' => 'bar']); |
79
|
|
|
$res = $listManager->getResponse(['opt1' => 'foo', 'opt2' => 'bar']); |
80
|
|
|
|
81
|
|
|
$this->assertEquals($response, $res); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function testGetResponseThrowsExceptionIfNotBuilt(): void |
85
|
|
|
{ |
86
|
|
|
$this->expectException(ListException::class); |
87
|
|
|
$this->expectExceptionMessage('List is not built. BuiltList method is required before generating'); |
88
|
|
|
$this->expectExceptionCode(500); |
89
|
|
|
$listManager = $this->getListManager(); |
90
|
|
|
$listManager->getResponse([]); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function testGetData(): void |
94
|
|
|
{ |
95
|
|
|
$this->setBuildCalls(); |
96
|
|
|
$this->typeResolverMock->expects($this->once()) |
97
|
|
|
->method('getData') |
98
|
|
|
->with($this->viewMock, ['opt1' => 'foo', 'opt2' => 'bar']) |
99
|
|
|
->willReturn('test_data'); |
100
|
|
|
$listManager = $this->getListManager(); |
101
|
|
|
$listManager->buildList('test_list', 'list_type', ['param1' => 'foo', 'param2' => 'bar']); |
102
|
|
|
$res = $listManager->getData(['opt1' => 'foo', 'opt2' => 'bar']); |
103
|
|
|
$this->assertEquals('test_data', $res); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function testGetDataThrowsExceptionIfNotBuilt(): void |
107
|
|
|
{ |
108
|
|
|
$this->expectException(ListException::class); |
109
|
|
|
$this->expectExceptionMessage('List is not built. BuiltList method is required before generating'); |
110
|
|
|
$this->expectExceptionCode(500); |
111
|
|
|
$listManager = $this->getListManager(); |
112
|
|
|
$listManager->getData([]); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
private function setBuildCalls(): void |
116
|
|
|
{ |
117
|
|
|
$this->listLocatorMock->expects($this->once()) |
118
|
|
|
->method('get') |
119
|
|
|
->with('test_list') |
120
|
|
|
->willReturn($this->listMock); |
121
|
|
|
$this->configMock->expects($this->once()) |
122
|
|
|
->method('resolve') |
123
|
|
|
->with($this->listMock); |
124
|
|
|
$this->listMock->expects($this->once()) |
125
|
|
|
->method('setParameters') |
126
|
|
|
->with(['param1' => 'foo', 'param2' => 'bar']); |
127
|
|
|
$this->typeResolverMock->expects($this->once()) |
128
|
|
|
->method('resolveType') |
129
|
|
|
->with('list_type') |
130
|
|
|
->willReturnSelf(); |
131
|
|
|
$this->typeResolverMock->expects($this->once()) |
132
|
|
|
->method('getTypeName') |
133
|
|
|
->willReturn('list_type'); |
134
|
|
|
$this->typeResolverMock->expects($this->once()) |
135
|
|
|
->method('getPerPage') |
136
|
|
|
->willReturn(20); |
137
|
|
|
$this->typeResolverMock->expects($this->once()) |
138
|
|
|
->method('getCurrentPage') |
139
|
|
|
->willReturn(5); |
140
|
|
|
$this->mapperFactoryMock->expects($this->once()) |
141
|
|
|
->method('createListMapper') |
142
|
|
|
->with($this->listMock, 'list_type') |
143
|
|
|
->willReturn($this->listMapperMock); |
144
|
|
|
$this->mapperFactoryMock->expects($this->once()) |
145
|
|
|
->method('createFilterMapper') |
146
|
|
|
->with($this->listMock) |
147
|
|
|
->willReturn($this->filterMapperMock); |
148
|
|
|
$this->mapperFactoryMock->expects($this->once()) |
149
|
|
|
->method('createJoinMapper') |
150
|
|
|
->with($this->listMock, $this->listMapperMock, $this->filterMapperMock, $this->valueMock) |
151
|
|
|
->willReturn($this->joinMapperMock); |
152
|
|
|
$this->filterBuilderMock->expects($this->once()) |
153
|
|
|
->method('buildFilterForm') |
154
|
|
|
->with($this->filterMapperMock) |
155
|
|
|
->willReturn($this->filterFormMock); |
156
|
|
|
$this->requestHandlerMock->expects($this->once()) |
157
|
|
|
->method('handleRequest') |
158
|
|
|
->with($this->listMapperMock, $this->filterMapperMock, $this->filterFormMock); |
159
|
|
|
$this->valueFactoryMock->expects($this->once()) |
160
|
|
|
->method('createListValue') |
161
|
|
|
->with($this->listMapperMock, $this->filterMapperMock) |
162
|
|
|
->willReturn($this->valueMock); |
163
|
|
|
$this->listQueryBuilderMock->expects($this->once()) |
164
|
|
|
->method('buildQuery') |
165
|
|
|
->with($this->listMock, $this->joinMapperMock, $this->listMapperMock, $this->filterMapperMock, $this->valueMock) |
166
|
|
|
->willReturn($this->queryBuilderMock); |
167
|
|
|
$this->listQueryBuilderMock->expects($this->once()) |
168
|
|
|
->method('buildLazyQuery') |
169
|
|
|
->with($this->listMock, $this->joinMapperMock, $this->listMapperMock) |
170
|
|
|
->willReturn($this->lazyQueryBuilderMock); |
171
|
|
|
$this->viewFactoryMock->expects($this->once()) |
172
|
|
|
->method('createView') |
173
|
|
|
->with($this->listMapperMock, $this->filterFormMock, $this->queryBuilderMock, $this->lazyQueryBuilderMock, 20, 5) |
174
|
|
|
->willReturn($this->viewMock); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
private function getListManager(): ListManager |
178
|
|
|
{ |
179
|
|
|
return new ListManager( |
180
|
|
|
$this->typeResolverMock, |
181
|
|
|
$this->configMock, |
182
|
|
|
$this->requestHandlerMock, |
183
|
|
|
$this->filterBuilderMock, |
184
|
|
|
$this->listQueryBuilderMock, |
185
|
|
|
$this->viewFactoryMock, |
186
|
|
|
$this->mapperFactoryMock, |
187
|
|
|
$this->valueFactoryMock, |
188
|
|
|
$this->listLocatorMock |
189
|
|
|
); |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|