|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace EasyCorp\Bundle\EasyAdminBundle\Tests\Inspector; |
| 4 | + |
| 5 | +use Doctrine\ORM\EntityRepository; |
| 6 | +use EasyCorp\Bundle\EasyAdminBundle\Config\Action; |
| 7 | +use EasyCorp\Bundle\EasyAdminBundle\Config\Option\EA; |
| 8 | +use EasyCorp\Bundle\EasyAdminBundle\Inspector\DataCollector; |
| 9 | +use EasyCorp\Bundle\EasyAdminBundle\Test\AbstractCrudTestCase; |
| 10 | +use EasyCorp\Bundle\EasyAdminBundle\Tests\TestApplication\Controller\CategoryCrudController; |
| 11 | +use EasyCorp\Bundle\EasyAdminBundle\Tests\TestApplication\Controller\SecureDashboardController; |
| 12 | +use EasyCorp\Bundle\EasyAdminBundle\Tests\TestApplication\Entity\Category; |
| 13 | +use Symfony\Component\VarDumper\Cloner\Data; |
| 14 | + |
| 15 | +class DataCollectorTest extends AbstractCrudTestCase |
| 16 | +{ |
| 17 | + protected EntityRepository $categories; |
| 18 | + |
| 19 | + protected function setUp(): void |
| 20 | + { |
| 21 | + parent::setUp(); |
| 22 | + |
| 23 | + $this->categories = $this->entityManager->getRepository(Category::class); |
| 24 | + |
| 25 | + $this->client->enableProfiler(); |
| 26 | + $this->client->setServerParameters(['PHP_AUTH_USER' => 'admin', 'PHP_AUTH_PW' => '1234']); |
| 27 | + } |
| 28 | + |
| 29 | + protected function getControllerFqcn(): string |
| 30 | + { |
| 31 | + return CategoryCrudController::class; |
| 32 | + } |
| 33 | + |
| 34 | + protected function getDashboardFqcn(): string |
| 35 | + { |
| 36 | + return SecureDashboardController::class; |
| 37 | + } |
| 38 | + |
| 39 | + public function testIndex(): void |
| 40 | + { |
| 41 | + $sort = ['name' => 'DESC', 'slug' => 'ASC']; |
| 42 | + $this->client->request('GET', $this->generateIndexUrl().'&'.http_build_query([EA::SORT => $sort])); |
| 43 | + |
| 44 | + /** @var DataCollector $collector */ |
| 45 | + $collector = $this->client->getProfile()->getCollector('easyadmin'); |
| 46 | + |
| 47 | + $this->assertTrue($collector->isEasyAdminRequest()); |
| 48 | + |
| 49 | + $this->assertSame( |
| 50 | + [ |
| 51 | + 'CRUD Controller FQCN' => CategoryCrudController::class, |
| 52 | + 'CRUD Action' => Action::INDEX, |
| 53 | + 'Entity ID' => null, |
| 54 | + 'Sort' => $sort, |
| 55 | + ], |
| 56 | + array_map(static fn (Data $d) => $d->getValue(true), $collector->getData()), |
| 57 | + ); |
| 58 | + } |
| 59 | + |
| 60 | + public function testEdit(): void |
| 61 | + { |
| 62 | + /** @var Category $category */ |
| 63 | + $category = $this->categories->findOneBy([]); |
| 64 | + |
| 65 | + $this->client->request('GET', $this->generateEditFormUrl($category->getId())); |
| 66 | + |
| 67 | + /** @var DataCollector $collector */ |
| 68 | + $collector = $this->client->getProfile()->getCollector('easyadmin'); |
| 69 | + |
| 70 | + $this->assertTrue($collector->isEasyAdminRequest()); |
| 71 | + |
| 72 | + $this->assertSame( |
| 73 | + [ |
| 74 | + 'CRUD Controller FQCN' => CategoryCrudController::class, |
| 75 | + 'CRUD Action' => Action::EDIT, |
| 76 | + 'Entity ID' => (string) $category->getId(), |
| 77 | + 'Sort' => null, |
| 78 | + ], |
| 79 | + array_map(static fn (Data $d) => $d->getValue(true), $collector->getData()), |
| 80 | + ); |
| 81 | + } |
| 82 | + |
| 83 | + public function testReset(): void |
| 84 | + { |
| 85 | + $this->client->request('GET', $this->generateIndexUrl()); |
| 86 | + |
| 87 | + /** @var DataCollector $collector */ |
| 88 | + $collector = $this->client->getProfile()->getCollector('easyadmin'); |
| 89 | + |
| 90 | + $this->assertSame( |
| 91 | + [ |
| 92 | + 'CRUD Controller FQCN' => CategoryCrudController::class, |
| 93 | + 'CRUD Action' => Action::INDEX, |
| 94 | + 'Entity ID' => null, |
| 95 | + 'Sort' => null, |
| 96 | + ], |
| 97 | + array_map(static fn (Data $d) => $d->getValue(true), $collector->getData()), |
| 98 | + ); |
| 99 | + |
| 100 | + $collector->reset(); |
| 101 | + |
| 102 | + $this->assertSame( |
| 103 | + [], |
| 104 | + array_map(static fn (Data $d) => $d->getValue(true), $collector->getData()), |
| 105 | + ); |
| 106 | + } |
| 107 | +} |
0 commit comments