1 | <?php |
||
2 | |||
3 | /** |
||
4 | * Copyright © 2016-present Spryker Systems GmbH. All rights reserved. |
||
5 | * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file. |
||
6 | */ |
||
7 | |||
8 | namespace SprykerTest\Zed\ZedNavigation\Business; |
||
9 | |||
10 | use Codeception\Test\Unit; |
||
11 | use Generated\Shared\Transfer\NavigationItemCollectionTransfer; |
||
12 | use Generated\Shared\Transfer\NavigationItemTransfer; |
||
13 | use Spryker\Zed\Router\Business\Route\RouteCollection; |
||
14 | use Spryker\Zed\Router\Business\Router\Router; |
||
15 | use Spryker\Zed\ZedNavigation\Business\Model\Cache\ZedNavigationCacheInterface; |
||
16 | use Spryker\Zed\ZedNavigation\Business\Model\Collector\ZedNavigationCollectorInterface; |
||
17 | use Spryker\Zed\ZedNavigation\Business\Model\Formatter\MenuFormatter; |
||
18 | use Spryker\Zed\ZedNavigation\Business\ZedNavigationBusinessFactory; |
||
19 | use Spryker\Zed\ZedNavigation\Business\ZedNavigationFacade; |
||
20 | use Spryker\Zed\ZedNavigation\Business\ZedNavigationFacadeInterface; |
||
21 | use Spryker\Zed\ZedNavigation\Dependency\Facade\ZedNavigationToRouterFacadeBridge; |
||
22 | use Spryker\Zed\ZedNavigation\Dependency\Facade\ZedNavigationToRouterFacadeInterface; |
||
23 | use Spryker\Zed\ZedNavigation\ZedNavigationConfig; |
||
24 | use Spryker\Zed\ZedNavigation\ZedNavigationDependencyProvider; |
||
25 | use Symfony\Component\Routing\Route; |
||
26 | |||
27 | class ZedNavigationBusinessTester extends Unit |
||
28 | { |
||
29 | /** |
||
30 | * @var \SprykerTest\Zed\ZedNavigation\ZedNavigationBusinessTester |
||
31 | */ |
||
32 | protected $tester; |
||
33 | |||
34 | /** |
||
35 | * @return \Spryker\Zed\ZedNavigation\Business\ZedNavigationFacadeInterface |
||
36 | */ |
||
37 | protected function getFacade(): ZedNavigationFacadeInterface |
||
38 | { |
||
39 | return new ZedNavigationFacade(); |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * @return \Spryker\Zed\ZedNavigation\Business\ZedNavigationBusinessFactory |
||
44 | */ |
||
45 | protected function getFactory(): ZedNavigationBusinessFactory |
||
46 | { |
||
47 | return new ZedNavigationBusinessFactory(); |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * @return \Spryker\Zed\ZedNavigation\Business\Model\Collector\ZedNavigationCollectorInterface|\PHPUnit\Framework\MockObject\MockObject |
||
52 | */ |
||
53 | protected function getZedNavigationCollectorMock(): ZedNavigationCollectorInterface |
||
54 | { |
||
55 | return $this |
||
56 | ->getMockBuilder(ZedNavigationCollectorInterface::class) |
||
57 | ->setMethods(['getNavigation']) |
||
58 | ->getMock(); |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * @return \Spryker\Zed\ZedNavigation\Business\Model\Cache\ZedNavigationCacheInterface|\PHPUnit\Framework\MockObject\MockObject |
||
63 | */ |
||
64 | protected function getZedNavigationCacheMock(): ZedNavigationCacheInterface |
||
65 | { |
||
66 | $cacheMock = $this |
||
67 | ->getMockBuilder(ZedNavigationCacheInterface::class) |
||
68 | ->setMethods(['setNavigation', 'getNavigation', 'hasContent', 'isEnabled', 'removeCache']) |
||
69 | ->getMock(); |
||
70 | |||
71 | $cacheMock->expects($this->never()) |
||
72 | ->method('isEnabled'); |
||
73 | $cacheMock->expects($this->never()) |
||
74 | ->method('hasContent'); |
||
75 | |||
76 | return $cacheMock; |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * @param array $navigationData |
||
81 | * @param bool $hasContent |
||
82 | * |
||
83 | * @return \Spryker\Zed\ZedNavigation\Business\Model\Cache\ZedNavigationCacheInterface|\PHPUnit\Framework\MockObject\MockObject |
||
84 | */ |
||
85 | protected function getZedNavigationCacheMockWithReturn( |
||
86 | array $navigationData, |
||
87 | bool $hasContent = true |
||
88 | ): ZedNavigationCacheInterface { |
||
89 | $cacheMock = $this |
||
90 | ->getMockBuilder(ZedNavigationCacheInterface::class) |
||
91 | ->setMethods(['setNavigation', 'getNavigation', 'hasContent', 'isEnabled', 'removeCache']) |
||
92 | ->getMock(); |
||
93 | |||
94 | $cacheMock->expects($this->never()) |
||
95 | ->method('isEnabled'); |
||
96 | $cacheMock->expects($this->once()) |
||
97 | ->method('hasContent') |
||
98 | ->willReturn($hasContent); |
||
99 | $cacheMock->expects($this->once()) |
||
100 | ->method('getNavigation') |
||
101 | ->will($this->returnValue($navigationData)); |
||
102 | |||
103 | return $cacheMock; |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * @return \Spryker\Zed\ZedNavigation\ZedNavigationConfig|\PHPUnit\Framework\MockObject\MockObject |
||
108 | */ |
||
109 | protected function getZedNavigationConfigMock(): ZedNavigationConfig |
||
110 | { |
||
111 | return $this |
||
112 | ->getMockBuilder(ZedNavigationConfig::class) |
||
113 | ->setMethods(['isNavigationCacheEnabled']) |
||
114 | ->getMock(); |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * @param array $navigationItems |
||
119 | * @param \Generated\Shared\Transfer\NavigationItemCollectionTransfer $navigationItemCollectionTransfer |
||
120 | * |
||
121 | * @return \Generated\Shared\Transfer\NavigationItemCollectionTransfer |
||
122 | */ |
||
123 | protected function mapNavigationItemsToNavigationItemCollectionTransfer( |
||
124 | array $navigationItems, |
||
125 | NavigationItemCollectionTransfer $navigationItemCollectionTransfer |
||
126 | ): NavigationItemCollectionTransfer { |
||
127 | foreach ($navigationItems as $navigationItem) { |
||
128 | if ($this->hasNestedNavigationItems($navigationItem)) { |
||
129 | $navigationItemCollectionTransfer = $this->mapNavigationItemsToNavigationItemCollectionTransfer( |
||
130 | $navigationItem[MenuFormatter::PAGES], |
||
131 | $navigationItemCollectionTransfer, |
||
132 | ); |
||
133 | |||
134 | continue; |
||
135 | } |
||
136 | |||
137 | $navigationItemTransfer = (new NavigationItemTransfer()) |
||
138 | ->fromArray($navigationItem, true) |
||
139 | ->setModule($navigationItem[MenuFormatter::BUNDLE] ?? null); |
||
140 | |||
141 | $navigationItemName = $this->getNavigationItemKey($navigationItem); |
||
142 | |||
143 | if ($navigationItemName !== '') { |
||
144 | $navigationItemCollectionTransfer->addNavigationItem( |
||
145 | $navigationItemName, |
||
146 | $navigationItemTransfer, |
||
147 | ); |
||
148 | } |
||
149 | } |
||
150 | |||
151 | return $navigationItemCollectionTransfer; |
||
152 | } |
||
153 | |||
154 | /** |
||
155 | * @param array<string, mixed> $navigationItem |
||
156 | * |
||
157 | * @return bool |
||
158 | */ |
||
159 | protected function hasNestedNavigationItems(array $navigationItem): bool |
||
160 | { |
||
161 | return isset($navigationItem[MenuFormatter::PAGES]); |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * @param array<string> $navigationItem |
||
166 | * |
||
167 | * @return string |
||
168 | */ |
||
169 | protected function getNavigationItemKey(array $navigationItem): string |
||
170 | { |
||
171 | if ( |
||
172 | isset($navigationItem[MenuFormatter::BUNDLE]) |
||
173 | && isset($navigationItem[MenuFormatter::CONTROLLER]) |
||
174 | && isset($navigationItem[MenuFormatter::ACTION]) |
||
175 | ) { |
||
176 | return sprintf( |
||
177 | '%s:%s:%s', |
||
178 | $navigationItem[MenuFormatter::BUNDLE], |
||
179 | $navigationItem[MenuFormatter::CONTROLLER], |
||
180 | $navigationItem[MenuFormatter::ACTION], |
||
181 | ); |
||
182 | } |
||
183 | |||
184 | return $navigationItem[MenuFormatter::URI] ?? ''; |
||
185 | } |
||
186 | |||
187 | /** |
||
188 | * @param array<\Spryker\Zed\ZedNavigationExtension\Dependency\Plugin\NavigationItemCollectionFilterPluginInterface> $plugins |
||
189 | * |
||
190 | * @return void |
||
191 | */ |
||
192 | protected function provideNavigationItemCollectionFilterPlugins(array $plugins = []): void |
||
193 | { |
||
194 | $this->tester->setDependency(ZedNavigationDependencyProvider::PLUGINS_NAVIGATION_ITEM_COLLECTION_FILTER, $plugins); |
||
195 | } |
||
196 | |||
197 | /** |
||
198 | * @return void |
||
199 | */ |
||
200 | protected function provideRouterFacade() |
||
201 | { |
||
202 | $this->tester->setDependency(ZedNavigationDependencyProvider::FACADE_ROUTER, $this->buildZedNavigationToRouterFacadeBridge()); |
||
203 | } |
||
204 | |||
205 | /** |
||
206 | * @return \Spryker\Zed\ZedNavigation\Dependency\Facade\ZedNavigationToRouterFacadeInterface |
||
207 | */ |
||
208 | protected function buildZedNavigationToRouterFacadeBridge(): ZedNavigationToRouterFacadeInterface |
||
209 | { |
||
210 | $collection = new RouteCollection(); |
||
211 | $collection->add('test:index:index', $this->createMock(Route::class)); |
||
212 | |||
213 | $zedNavigationToRouterFacadeBridgeMock = $this->createPartialMock(ZedNavigationToRouterFacadeBridge::class, ['getBackofficeRouter']); |
||
214 | $routerMock = $this->createPartialMock(Router::class, ['getRouteCollection']); |
||
215 | |||
216 | $routerMock->method('getRouteCollection')->willReturn($collection); |
||
217 | |||
218 | $zedNavigationToRouterFacadeBridgeMock->method('getBackofficeRouter')->willReturn($routerMock); |
||
219 | |||
220 | return $zedNavigationToRouterFacadeBridgeMock; |
||
221 | } |
||
222 | |||
223 | /** |
||
224 | * @return \Spryker\Zed\ZedNavigation\Business\ZedNavigationFacadeInterface |
||
225 | */ |
||
226 | protected function getFacadeWithCustomNavigationFile(): ZedNavigationFacadeInterface |
||
227 | { |
||
228 | $zedNavigationConfigMock = $this->buildZedNavigationConfigMock(); |
||
229 | $zedNavigationBusinessFactoryMock = $this->buildZedNavigationBusinessFactoryMock($zedNavigationConfigMock); |
||
230 | |||
231 | $zedNavigationFacade = new ZedNavigationFacade(); |
||
232 | |||
233 | return $zedNavigationFacade->setFactory($zedNavigationBusinessFactoryMock); |
||
234 | } |
||
235 | |||
236 | /** |
||
237 | * @return \Spryker\Zed\ZedNavigation\ZedNavigationConfig |
||
238 | */ |
||
239 | protected function buildZedNavigationConfigMock(): ZedNavigationConfig |
||
240 | { |
||
241 | $zedNavigationConfig = $this->createPartialMock(ZedNavigationConfig::class, ['getNavigationSchemaPathPattern', 'getRootNavigationSchemaPaths']); |
||
242 | $zedNavigationConfig->method('getNavigationSchemaPathPattern')->willReturn([codecept_data_dir()]); |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
243 | $zedNavigationConfig->method('getRootNavigationSchemaPaths')->willReturn([]); |
||
244 | |||
245 | return $zedNavigationConfig; |
||
246 | } |
||
247 | |||
248 | /** |
||
249 | * @param \Spryker\Zed\ZedNavigation\ZedNavigationConfig $zedNavigationConfig |
||
250 | * |
||
251 | * @return \Spryker\Zed\ZedNavigation\Business\ZedNavigationBusinessFactory |
||
252 | */ |
||
253 | protected function buildZedNavigationBusinessFactoryMock(ZedNavigationConfig $zedNavigationConfig): ZedNavigationBusinessFactory |
||
254 | { |
||
255 | $factory = new ZedNavigationBusinessFactory(); |
||
256 | |||
257 | return $factory->setConfig($zedNavigationConfig); |
||
258 | } |
||
259 | } |
||
260 |