Skip to content

Instantly share code, notes, and snippets.

@damienalexandre
Created August 4, 2025 08:54
Show Gist options
  • Save damienalexandre/4a7e40ac196055b68105fff899a93d61 to your computer and use it in GitHub Desktop.
Save damienalexandre/4a7e40ac196055b68105fff899a93d61 to your computer and use it in GitHub Desktop.
Test all EasyAdmin CRUD's
<?php
declare(strict_types=1);
namespace App\Tests\Controller\Admin;
use App\Controller\Admin\DashboardController;
use App\Entity\Admin;
use Doctrine\ORM\EntityManagerInterface;
use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGeneratorInterface;
use EasyCorp\Bundle\EasyAdminBundle\Test\Trait\CrudTestActions;
use EasyCorp\Bundle\EasyAdminBundle\Test\Trait\CrudTestIndexAsserts;
use EasyCorp\Bundle\EasyAdminBundle\Test\Trait\CrudTestUrlGeneration;
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
/**
* Inspired by \EasyCorp\Bundle\EasyAdminBundle\Test\AbstractCrudTestCase but dynamic.
*/
class CRUDTest extends WebTestCase
{
use CrudTestActions;
use CrudTestIndexAsserts;
use CrudTestUrlGeneration;
protected KernelBrowser $client;
protected AdminUrlGeneratorInterface $adminUrlGenerator;
protected EntityManagerInterface $entityManager;
private string $controllerFqcn;
protected function setUp(): void
{
$this->client = static::createClient();
$container = static::getContainer();
$this->entityManager = $container->get(EntityManagerInterface::class);
$this->adminUrlGenerator = $container->get(AdminUrlGenerator::class);
}
protected function getControllerFqcn(): string
{
return $this->controllerFqcn;
}
protected function getDashboardFqcn(): string
{
return DashboardController::class;
}
public function testAllEasyAdminCruds(): void
{
$doctrine = static::getContainer()->get('doctrine');
$admin = $doctrine->getRepository(Admin::class)->findOneBy(['email' => 'xxx']);
$this->client->loginUser($admin, 'admin');
$container = static::getContainer();
foreach ($container->getServiceIds() as $serviceId) {
if (!str_ends_with($serviceId, 'CrudController')) {
continue;
}
/** @var AbstractCrudController $crud */
$crud = $container->get($serviceId);
$this->controllerFqcn = $crud::class;
$this->client->request('GET', $this->generateIndexUrl());
static::assertResponseIsSuccessful();
$listCrawler = $this->client->getCrawler();
// If there is entities we try EDIT and DETAIL actions
$entityRow = $listCrawler->filter('tbody tr[data-id]')->first();
if (count($entityRow) > 0) {
$editAction = $entityRow->filter($this->getActionSelector(Action::EDIT));
if (count($editAction) > 0) {
$entityId = $entityRow->attr('data-id');
$this->client->request('GET', $this->generateEditFormUrl($entityId));
static::assertResponseIsSuccessful();
}
$detailAction = $entityRow->filter($this->getActionSelector(Action::DETAIL));
if (count($detailAction) > 0) {
$entityId = $entityRow->attr('data-id');
$this->client->request('GET', $this->generateDetailUrl($entityId));
static::assertResponseIsSuccessful();
}
}
// We try NEW action
$newAction = $listCrawler->filter($this->getGlobalActionSelector(Action::NEW));
if (count($newAction) > 0) {
$this->client->request('GET', $this->generateNewFormUrl());
static::assertResponseIsSuccessful();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment