Skip to content

Commit 0638fa5

Browse files
committed
Add more tests related to factories
1 parent 68708da commit 0638fa5

File tree

2 files changed

+618
-0
lines changed

2 files changed

+618
-0
lines changed
Lines changed: 355 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,355 @@
1+
<?php
2+
3+
namespace EasyCorp\Bundle\EasyAdminBundle\Tests\Factory;
4+
5+
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
6+
use EasyCorp\Bundle\EasyAdminBundle\Config\Option\EA;
7+
use EasyCorp\Bundle\EasyAdminBundle\Config\Option\IconSet;
8+
use EasyCorp\Bundle\EasyAdminBundle\Factory\AdminContextFactory;
9+
use EasyCorp\Bundle\EasyAdminBundle\Tests\TestApplication\Controller\CategoryCrudController;
10+
use EasyCorp\Bundle\EasyAdminBundle\Tests\TestApplication\Controller\DashboardController;
11+
use EasyCorp\Bundle\EasyAdminBundle\Tests\TestApplication\Entity\Category;
12+
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
13+
use Symfony\Component\HttpFoundation\Request;
14+
15+
class AdminContextFactoryTest extends KernelTestCase
16+
{
17+
private AdminContextFactory $adminContextFactory;
18+
private DashboardController $dashboardController;
19+
private CategoryCrudController $crudController;
20+
21+
protected function setUp(): void
22+
{
23+
/** @var AdminContextFactory $factory */
24+
$factory = static::getContainer()->get(AdminContextFactory::class);
25+
$this->adminContextFactory = $factory;
26+
27+
/** @var DashboardController $dashboardController */
28+
$dashboardController = static::getContainer()->get(DashboardController::class);
29+
$this->dashboardController = $dashboardController;
30+
31+
/** @var CategoryCrudController $crudController */
32+
$crudController = static::getContainer()->get(CategoryCrudController::class);
33+
$this->crudController = $crudController;
34+
}
35+
36+
public function testCreateSetsI18nContext(): void
37+
{
38+
$request = new Request();
39+
$request->setLocale('fr');
40+
41+
$result = $this->adminContextFactory->create($request, $this->dashboardController, null);
42+
43+
$this->assertSame('fr', $result->getI18n()->getLocale());
44+
}
45+
46+
/**
47+
* @dataProvider textDirectionByLocaleProvider
48+
*/
49+
public function testCreateSetsTextDirectionBasedOnLocale(string $locale, string $expectedDirection): void
50+
{
51+
$request = new Request();
52+
$request->setLocale($locale);
53+
54+
$result = $this->adminContextFactory->create($request, $this->dashboardController, null);
55+
56+
$this->assertSame($expectedDirection, $result->getI18n()->getTextDirection());
57+
}
58+
59+
public static function textDirectionByLocaleProvider(): \Generator
60+
{
61+
yield 'english is ltr' => ['en', 'ltr'];
62+
yield 'spanish is ltr' => ['es', 'ltr'];
63+
yield 'french is ltr' => ['fr', 'ltr'];
64+
yield 'arabic is rtl' => ['ar', 'rtl'];
65+
yield 'hebrew is rtl' => ['he', 'rtl'];
66+
yield 'persian is rtl' => ['fa', 'rtl'];
67+
}
68+
69+
public function testCreateSetsRequestContext(): void
70+
{
71+
$request = new Request();
72+
73+
$result = $this->adminContextFactory->create($request, $this->dashboardController, null);
74+
75+
$this->assertSame($request, $result->getRequest());
76+
}
77+
78+
public function testCreateSetsDashboardRouteName(): void
79+
{
80+
$request = new Request();
81+
82+
$result = $this->adminContextFactory->create($request, $this->dashboardController, null);
83+
84+
$this->assertNotEmpty($result->getDashboardRouteName());
85+
}
86+
87+
public function testCreateWithNullCrudControllerReturnsCrudContextNull(): void
88+
{
89+
$request = new Request();
90+
91+
$result = $this->adminContextFactory->create($request, $this->dashboardController, null);
92+
93+
$this->assertNull($result->getCrud());
94+
}
95+
96+
public function testCreateSetsDashboardTitle(): void
97+
{
98+
$request = new Request();
99+
100+
$result = $this->adminContextFactory->create($request, $this->dashboardController, null);
101+
102+
// the title is set by the DashboardController in the TestApplication
103+
$this->assertNotEmpty($result->getDashboardTitle());
104+
}
105+
106+
public function testCreateOverridesActionFromQueryWithExplicitParameter(): void
107+
{
108+
$request = new Request([EA::CRUD_ACTION => 'edit']);
109+
110+
// the explicit 'index' parameter should be used, not the 'edit' from query
111+
$result = $this->adminContextFactory->create($request, $this->dashboardController, null, 'index');
112+
113+
// without a CRUD controller, getCrud() is null, so we just verify the factory accepts the parameter
114+
$this->assertSame($request, $result->getRequest());
115+
}
116+
117+
public function testGetSearchDtoWithNullCrudDtoReturnsNull(): void
118+
{
119+
$request = new Request([EA::QUERY => 'test search']);
120+
121+
$result = $this->adminContextFactory->create($request, $this->dashboardController, null);
122+
123+
$this->assertNull($result->getSearch());
124+
}
125+
126+
public function testCreateSetsUserToNullWhenNoTokenStorage(): void
127+
{
128+
$request = new Request();
129+
130+
$result = $this->adminContextFactory->create($request, $this->dashboardController, null);
131+
132+
// in the test environment, there's no authenticated user by default
133+
$this->assertNull($result->getUser());
134+
}
135+
136+
public function testCreateSetsTranslationDomain(): void
137+
{
138+
$request = new Request();
139+
140+
$result = $this->adminContextFactory->create($request, $this->dashboardController, null);
141+
142+
// default translation domain from the DashboardController
143+
$this->assertNotNull($result->getI18n()->getTranslationDomain());
144+
}
145+
146+
public function testCreateWithCrudControllerReturnsCrudContext(): void
147+
{
148+
$request = new Request();
149+
150+
$result = $this->adminContextFactory->create($request, $this->dashboardController, $this->crudController, Crud::PAGE_INDEX);
151+
152+
$this->assertNotNull($result->getCrud());
153+
}
154+
155+
public function testCreateWithCrudControllerSetsEntityFqcn(): void
156+
{
157+
$request = new Request();
158+
159+
$result = $this->adminContextFactory->create($request, $this->dashboardController, $this->crudController, Crud::PAGE_INDEX);
160+
161+
$this->assertSame(Category::class, $result->getCrud()->getEntityFqcn());
162+
}
163+
164+
/**
165+
* @dataProvider validPageNamesProvider
166+
*/
167+
public function testCreateWithCrudControllerSetsPageName(string $pageName): void
168+
{
169+
$request = new Request();
170+
171+
$result = $this->adminContextFactory->create($request, $this->dashboardController, $this->crudController, $pageName);
172+
173+
$this->assertSame($pageName, $result->getCrud()->getCurrentPage());
174+
}
175+
176+
public static function validPageNamesProvider(): \Generator
177+
{
178+
yield 'index page' => [Crud::PAGE_INDEX];
179+
yield 'detail page' => [Crud::PAGE_DETAIL];
180+
yield 'edit page' => [Crud::PAGE_EDIT];
181+
yield 'new page' => [Crud::PAGE_NEW];
182+
}
183+
184+
public function testCreateWithInvalidPageNameSetsPageNameToNull(): void
185+
{
186+
$request = new Request();
187+
188+
$result = $this->adminContextFactory->create($request, $this->dashboardController, $this->crudController, 'invalidPage');
189+
190+
$this->assertNull($result->getCrud()->getCurrentPage());
191+
}
192+
193+
public function testCreateWithCrudControllerSetsCurrentAction(): void
194+
{
195+
$request = new Request();
196+
197+
$result = $this->adminContextFactory->create($request, $this->dashboardController, $this->crudController, Crud::PAGE_INDEX);
198+
199+
$this->assertSame(Crud::PAGE_INDEX, $result->getCrud()->getCurrentAction());
200+
}
201+
202+
public function testCreateTakesActionFromQueryWhenNotExplicitlyProvided(): void
203+
{
204+
$request = new Request([EA::CRUD_ACTION => Crud::PAGE_DETAIL]);
205+
206+
$result = $this->adminContextFactory->create($request, $this->dashboardController, $this->crudController);
207+
208+
$this->assertSame(Crud::PAGE_DETAIL, $result->getCrud()->getCurrentAction());
209+
}
210+
211+
public function testCreateWithCrudControllerHasSearchDto(): void
212+
{
213+
$request = new Request([EA::QUERY => 'test search']);
214+
215+
$result = $this->adminContextFactory->create($request, $this->dashboardController, $this->crudController, Crud::PAGE_INDEX);
216+
217+
$this->assertNotNull($result->getSearch());
218+
$this->assertSame('test search', $result->getSearch()->getQuery());
219+
}
220+
221+
public function testCreateSetsDashboardFaviconPath(): void
222+
{
223+
$request = new Request();
224+
225+
$result = $this->adminContextFactory->create($request, $this->dashboardController, null);
226+
227+
// favicon can be empty string but should not throw
228+
$this->assertIsString($result->getDashboardFaviconPath());
229+
}
230+
231+
public function testCreateSetsDashboardContentWidth(): void
232+
{
233+
$request = new Request();
234+
235+
$result = $this->adminContextFactory->create($request, $this->dashboardController, null);
236+
237+
$this->assertContains($result->getDashboardContentWidth(), ['normal', 'full']);
238+
}
239+
240+
public function testCreateSetsDashboardControllerFqcn(): void
241+
{
242+
$request = new Request();
243+
244+
$result = $this->adminContextFactory->create($request, $this->dashboardController, null);
245+
246+
$this->assertSame(DashboardController::class, $result->getDashboardControllerFqcn());
247+
}
248+
249+
public function testCreateUsesDefaultLocaleWhenNotSet(): void
250+
{
251+
$request = new Request();
252+
// don't set locale, use default
253+
254+
$result = $this->adminContextFactory->create($request, $this->dashboardController, null);
255+
256+
// should have some locale set (default from framework)
257+
$this->assertNotEmpty($result->getI18n()->getLocale());
258+
}
259+
260+
public function testCreateHandlesLocaleWithRegion(): void
261+
{
262+
$request = new Request();
263+
$request->setLocale('pt_BR');
264+
265+
$result = $this->adminContextFactory->create($request, $this->dashboardController, null);
266+
267+
$this->assertSame('pt_BR', $result->getI18n()->getLocale());
268+
$this->assertSame('ltr', $result->getI18n()->getTextDirection());
269+
}
270+
271+
public function testCreateReturnsAssetsFromDashboardWhenNoCrudController(): void
272+
{
273+
$request = new Request();
274+
275+
$result = $this->adminContextFactory->create($request, $this->dashboardController, null);
276+
277+
$assets = $result->getAssets();
278+
279+
// HTML content from CategoryCrudController should NOT be present
280+
$this->assertNotContains(
281+
'<link data-added-from-controller rel="me" href="https://example.com">',
282+
$assets->getHeadContents()
283+
);
284+
$this->assertNotContains(
285+
'<span data-added-from-controller><!-- foo --></span>',
286+
$assets->getBodyContents()
287+
);
288+
}
289+
290+
public function testCreateReturnsAssetsFromCrudControllerWhenProvided(): void
291+
{
292+
$request = new Request();
293+
294+
$result = $this->adminContextFactory->create($request, $this->dashboardController, $this->crudController, Crud::PAGE_INDEX);
295+
296+
$assets = $result->getAssets();
297+
298+
// HTML content from CategoryCrudController should be present
299+
$this->assertContains(
300+
'<link data-added-from-controller rel="me" href="https://example.com">',
301+
$assets->getHeadContents()
302+
);
303+
$this->assertContains(
304+
'<span data-added-from-controller><!-- foo --></span>',
305+
$assets->getBodyContents()
306+
);
307+
}
308+
309+
public function testCreateReturnsAssetsWithDefaultIconSet(): void
310+
{
311+
$request = new Request();
312+
313+
$result = $this->adminContextFactory->create($request, $this->dashboardController, null);
314+
315+
$this->assertSame(IconSet::FontAwesome, $result->getAssets()->getIconSet());
316+
}
317+
318+
public function testCreateReturnsEmptyAssetArraysForNewDashboard(): void
319+
{
320+
$request = new Request();
321+
322+
$result = $this->adminContextFactory->create($request, $this->dashboardController, null);
323+
324+
$assets = $result->getAssets();
325+
$this->assertIsArray($assets->getCssAssets());
326+
$this->assertIsArray($assets->getJsAssets());
327+
$this->assertIsArray($assets->getWebpackEncoreAssets());
328+
$this->assertIsArray($assets->getAssetMapperAssets());
329+
}
330+
331+
public function testCreateWithCrudControllerPreservesIconSetFromDashboard(): void
332+
{
333+
$request = new Request();
334+
335+
$result = $this->adminContextFactory->create($request, $this->dashboardController, $this->crudController, Crud::PAGE_INDEX);
336+
337+
$this->assertSame(IconSet::FontAwesome, $result->getAssets()->getIconSet());
338+
}
339+
340+
public function testCreateWithInvalidPageNameStillReturnsAssets(): void
341+
{
342+
$request = new Request();
343+
344+
// when pageName is invalid, it gets set to null internally, and loadedOn(null) returns all assets
345+
$result = $this->adminContextFactory->create($request, $this->dashboardController, $this->crudController, 'invalidPage');
346+
347+
$assets = $result->getAssets();
348+
349+
// HTML content should still be present (not filtered by page)
350+
$this->assertContains(
351+
'<link data-added-from-controller rel="me" href="https://example.com">',
352+
$assets->getHeadContents()
353+
);
354+
}
355+
}

0 commit comments

Comments
 (0)