|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace App\Tests\Controller\Admin; |
| 6 | + |
| 7 | +use App\Controller\Admin\ServerCrudController; |
| 8 | +use App\Entity\User; |
| 9 | +use EasyCorp\Bundle\EasyAdminBundle\Config\Crud; |
| 10 | +use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator; |
| 11 | +use Hautelook\AliceBundle\PhpUnit\RefreshDatabaseTrait; |
| 12 | +use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; |
| 13 | + |
| 14 | +/** |
| 15 | + * The dashboard exists only to redirect to the Server index. |
| 16 | + * |
| 17 | + * It builds that URL on an injected AdminUrlGenerator, and the chain starts |
| 18 | + * with unsetAll() because the generator instance is held for as long as this |
| 19 | + * controller is — which, in a worker, is longer than one request. Order matters |
| 20 | + * there: unsetAll() after setController() rather than before would wipe the |
| 21 | + * controller back out and produce a URL pointing nowhere in particular, without |
| 22 | + * anything throwing. Hence asserting where the redirect actually lands. |
| 23 | + */ |
| 24 | +class DashboardControllerTest extends WebTestCase |
| 25 | +{ |
| 26 | + use RefreshDatabaseTrait; |
| 27 | + |
| 28 | + public function testItRedirectsToTheServerIndex(): void |
| 29 | + { |
| 30 | + $client = static::createClient(); |
| 31 | + |
| 32 | + $user = static::getContainer()->get('doctrine')->getManager() |
| 33 | + ->getRepository(User::class)->findOneBy([]); |
| 34 | + $client->loginUser($user); |
| 35 | + |
| 36 | + $client->request('GET', '/admin'); |
| 37 | + |
| 38 | + $this->assertResponseRedirects(); |
| 39 | + |
| 40 | + // Ask EasyAdmin what that URL should be rather than hard-coding the |
| 41 | + // slug, so this keeps working if the route path changes. |
| 42 | + $expected = static::getContainer()->get(AdminUrlGenerator::class) |
| 43 | + ->setController(ServerCrudController::class) |
| 44 | + ->setAction(Crud::PAGE_INDEX) |
| 45 | + ->generateUrl(); |
| 46 | + |
| 47 | + $location = (string) $client->getResponse()->headers->get('Location'); |
| 48 | + $this->assertSame( |
| 49 | + parse_url($expected, \PHP_URL_PATH), |
| 50 | + parse_url($location, \PHP_URL_PATH), |
| 51 | + 'the redirect must still land on the Server index' |
| 52 | + ); |
| 53 | + } |
| 54 | + |
| 55 | + public function testTheRedirectTargetLoads(): void |
| 56 | + { |
| 57 | + $client = static::createClient(); |
| 58 | + |
| 59 | + $user = static::getContainer()->get('doctrine')->getManager() |
| 60 | + ->getRepository(User::class)->findOneBy([]); |
| 61 | + $client->loginUser($user); |
| 62 | + |
| 63 | + $client->request('GET', '/admin'); |
| 64 | + $client->followRedirect(); |
| 65 | + |
| 66 | + $this->assertResponseIsSuccessful(); |
| 67 | + } |
| 68 | +} |
0 commit comments