|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace EasyCorp\Bundle\EasyAdminBundle\Tests\Router; |
| 4 | + |
| 5 | +use EasyCorp\Bundle\EasyAdminBundle\Router\AdminRouteGenerator; |
| 6 | +use EasyCorp\Bundle\EasyAdminBundle\Tests\PrettyUrlsTestApplication\Controller\BlogPostCrudController; |
| 7 | +use EasyCorp\Bundle\EasyAdminBundle\Tests\PrettyUrlsTestApplication\Controller\CategoryCrudController; |
| 8 | +use EasyCorp\Bundle\EasyAdminBundle\Tests\PrettyUrlsTestApplication\Controller\DashboardController; |
| 9 | +use EasyCorp\Bundle\EasyAdminBundle\Tests\PrettyUrlsTestApplication\Controller\SecondDashboardController; |
| 10 | +use Psr\Cache\CacheItemPoolInterface; |
| 11 | +use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait; |
| 12 | +use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; |
| 13 | +use Symfony\Component\Cache\CacheItem; |
| 14 | +use Symfony\Component\DependencyInjection\Argument\RewindableGenerator; |
| 15 | +use Symfony\Component\Filesystem\Filesystem; |
| 16 | + |
| 17 | +class AdminRouteGeneratorTest extends WebTestCase |
| 18 | +{ |
| 19 | + use ExpectDeprecationTrait; |
| 20 | + |
| 21 | + /** |
| 22 | + * @dataProvider provideFindRouteData |
| 23 | + */ |
| 24 | + public function testFindRoute(?string $dashboardControllerFqcn, ?string $crudControllerFqcn, ?string $action, ?string $expectedRouteName) |
| 25 | + { |
| 26 | + $client = static::createClient(); |
| 27 | + $cacheMock = $this->getMockBuilder(CacheItemPoolInterface::class)->getMock(); |
| 28 | + $cacheMock->method('getItem')->willReturnCallback(function ($key) { |
| 29 | + $item = new CacheItem(); |
| 30 | + $item->expiresAfter(3600); |
| 31 | + |
| 32 | + if (AdminRouteGenerator::CACHE_KEY_FQCN_TO_ROUTE !== $key) { |
| 33 | + return $item; |
| 34 | + } |
| 35 | + |
| 36 | + $item->set([ |
| 37 | + DashboardController::class => [ |
| 38 | + '' => [ |
| 39 | + '' => 'admin', |
| 40 | + ], |
| 41 | + BlogPostCrudController::class => [ |
| 42 | + 'index' => 'admin_post_index', |
| 43 | + 'new' => 'admin_post_new', |
| 44 | + 'edit' => 'admin_post_edit', |
| 45 | + 'detail' => 'admin_post_detail', |
| 46 | + ], |
| 47 | + ], |
| 48 | + SecondDashboardController::class => [ |
| 49 | + '' => [ |
| 50 | + '' => 'second_admin', |
| 51 | + ], |
| 52 | + ], |
| 53 | + ]); |
| 54 | + |
| 55 | + return $item; |
| 56 | + }); |
| 57 | + |
| 58 | + $dashboardControllers = new RewindableGenerator(function () { |
| 59 | + yield DashboardController::class => new DashboardController(); |
| 60 | + yield SecondDashboardController::class => new SecondDashboardController(); |
| 61 | + }, 2); |
| 62 | + |
| 63 | + $adminRouteGenerator = new AdminRouteGenerator( |
| 64 | + $dashboardControllers, |
| 65 | + [], |
| 66 | + $cacheMock, |
| 67 | + new Filesystem(), |
| 68 | + $client->getKernel()->getBuildDir(), |
| 69 | + ); |
| 70 | + |
| 71 | + $routeName = $adminRouteGenerator->findRouteName($dashboardControllerFqcn, $crudControllerFqcn, $action); |
| 72 | + $this->assertSame($expectedRouteName, $routeName); |
| 73 | + } |
| 74 | + |
| 75 | + public function provideFindRouteData(): iterable |
| 76 | + { |
| 77 | + yield [null, null, null, 'admin']; |
| 78 | + yield [DashboardController::class, null, null, 'admin']; |
| 79 | + yield [DashboardController::class, BlogPostCrudController::class, null, null]; |
| 80 | + yield [DashboardController::class, BlogPostCrudController::class, 'index', 'admin_post_index']; |
| 81 | + yield [DashboardController::class, BlogPostCrudController::class, 'detail', 'admin_post_detail']; |
| 82 | + yield [DashboardController::class, CategoryCrudController::class, null, null]; |
| 83 | + yield [DashboardController::class, CategoryCrudController::class, 'index', null]; |
| 84 | + yield [DashboardController::class, CategoryCrudController::class, 'detail', null]; |
| 85 | + yield [SecondDashboardController::class, null, null, 'second_admin']; |
| 86 | + yield [SecondDashboardController::class, BlogPostCrudController::class, null, null]; |
| 87 | + yield [SecondDashboardController::class, BlogPostCrudController::class, 'index', null]; |
| 88 | + yield [SecondDashboardController::class, BlogPostCrudController::class, 'detail', null]; |
| 89 | + } |
| 90 | +} |
0 commit comments