Tests
As EasyAdmin is based on Symfony, you can add functional tests for admin pages by
extending the WebTestCase
class and using the Symfony functional testing workflow .
However, as EasyAdmin uses specific ways of displaying the data in its
CRUD pages, a custom test class is provided: AbstractCrudTestCase
. The
class is based on traits that define custom asserts and helpers:
Functional Test Case Example
Suppose you have a Dashboard named App\Controller\Admin\AppDashboardController
and a Category
Crud Controller named App\Controller\Admin\CategoryCrudController
.
Here's an example of a functional test class for that controller.
First, your test class needs to extend the AbstractCrudTestCase
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
# tests/Admin/Controller/CategoryCrudControllerTest.php
namespace App\Tests\Admin\Controller;
use App\Controller\Admin\AppDashboardController;
use App\Controller\Admin\CategoryCrudController;
use EasyCorp\Bundle\EasyAdminBundle\Test\AbstractCrudTestCase;
final class CategoryCrudControllerTest extends AbstractCrudTestCase
{
protected function getControllerFqcn(): string
{
return CategoryCrudController::class;
}
protected function getDashboardFqcn(): string
{
return AppDashboardController::class;
}
public function testIndexPage(): void
{
// this examples doesn't use security; in your application you may
// need to ensure that the user is logged before the test
$this->client->request("GET", $this->generateIndexUrl());
static::assertResponseIsSuccessful();
}
}
URL Generation
Used by the AbstractCrudTestCase
, CrudTestUrlGeneration
is a
URL generation trait that helps generate EasyAdmin-specific URLs.
Note
The trait can be used on its own but, in that case, the class that is using it needs either of the following:
- to define two functions
getControllerFqcn()
andgetDashboardFqcn()
; - to pass the DashboardFqcn (class name) and ControllerFqcn (class name) as input to the URL generation functions.
Here is the list of URL generation functions. All of them build URLs based on the provided Dashboard and Controller class names:
getCrudUrl()
: is the main function that allows for a complete generation with all possible options;generateIndexUrl()
: generates the URL for the index page (based on the Dashboard and Controller defined);generateNewFormUrl()
: generates the URL for the New form page (based on the Dashboard and Controller defined);generateEditFormUrl()
: generates the URL for the Edit form page of a specific entity (based on the Dashboard and Controller defined and the entity ID);generateDetailUrl()
: generates the URL for the Detail page of a specific entity (based on the Dashboard and Controller defined and the entity ID);generateFilterRenderUrl()
: generates the URL to get the rendering of the filters (based on the Dashboard and Controller defined).
Asserts
Used by the AbstractCrudTestCase
, are two traits filled with specific
asserts for EasyAdmin web testing:
CrudTestIndexAsserts
: providing asserts for the index page of EasyAdmin;CrudTestFormAsserts
: providing asserts for the form page of EasyAdmin.
Note
The trait can be used on its own but, in that case, the class that is using it needs both:
- a class property
client
: instance ofSymfony\Bundle\FrameworkBundle\KernelBrowser
- a class property
entityManager
: instance ofDoctrine\ORM\EntityManagerInterface
CrudTestIndexAsserts
As EasyAdmin uses specific layout, the goal of these asserts is to ease the way you're testing your EasyAdmin backend by providing specific asserts linked to the index page.
The following asserts are provided:
assertIndexFullEntityCount()
assertIndexPageEntityCount()
assertIndexPagesCount()
assertIndexEntityActionExists()
assertIndexEntityActionNotExists()
assertIndexEntityActionTextSame()
assertIndexEntityActionNotTextSame()
assertGlobalActionExists()
assertGlobalActionNotExists()
assertGlobalActionDisplays()
assertGlobalActionNotDisplays()
assertIndexColumnExists()
assertIndexColumnNotExists()
assertIndexColumnHeaderContains()
assertIndexColumnHeaderNotContains()
CrudTestFormAsserts
As EasyAdmin uses specific layout, the goal of these asserts is to ease the way you're testing your EasyAdmin backend by providing specific asserts linked to the form (new or edit) page.
The following asserts are provided:
assertFormFieldExists()
assertFormFieldNotExists()
assertFormFieldHasLabel()
assertFormFieldNotHasLabel()
Selector Helpers
Used by the Asserts to locate elements, the CrudTestSelectors
trait defines
a set of selector helpers tailored to EasyAdmin layout.
Note
The trait can be used on its own. It only defines selector strings.
The following helpers are provided:
getActionSelector()
getGlobalActionSelector()
getIndexEntityActionSelector()
getIndexEntityRowSelector()
getIndexColumnSelector()
getIndexHeaderColumnSelector()
getIndexHeaderRowSelector()
getFormEntity()
getEntityFormSelector()
getFormFieldIdValue()
getFormFieldSelector()
getFormFieldLabelSelector()