Search code examples
symfony5easyadmin3

Functional test EasyAdmin 3 backend


I would like to run functional tests on an EasyAdmin 3 backend.

Basically, I want to make sure regular users cannot access pages, view fields or view/run actions they are not allowed to do.

What would be the best way to go ? Is there any useful resource I missed to get started ?


Solution

  • EasyAdmin 3 Crud Controllers are basically regular Symfony controllers, so they can be tested as any other Symfony controller.

    <?php
    // tests/Controller/AdminControllerTest.php
    namespace App\Tests\Controller;
    
    use App\Repository\UserRepository;
    use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
    
    class AdminControllerTest extends WebTestCase
    {
        // ...
    
        public function testVisitingWhileLoggedIn()
        {
            $client = static::createClient();
            $userRepository = static::$container->get(UserRepository::class);
    
            // retrieve the test user
            $testUser = $userRepository->findOneByEmail('john.doe@example.com');
    
            // simulate $testUser being logged in
            $client->loginUser($testUser);
    
            // test e.g. the admin page
            $client->request('GET', '/admin');
            $this->assertResponseStatusCodeSame(403);
        }
    }
    

    EasyAdmin Crud Doc https://symfony.com/doc/current/bundles/EasyAdminBundle/crud.html

    Testing Symfony https://symfony.com/doc/current/testing.html