-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #5852 Make *-To-Many relations sortable (chapterjason)
This PR was squashed before being merged into the 4.x branch. Discussion ---------- Make *-To-Many relations sortable In a project I've a several *-To-Many relations which I need to be sortable, to have better insight into the data. I tested One-To-Many and Many-To-Many and also the `setDefaultSort` in `configureCrud`, works as expected. ~~Haven't tested ManyToMany, but I think they should work, too.~~ Didn't worked, but I found a solution. As it wasn't working before, I made it Opt-In. - [x] Add tests **Can Someone help me out here? Not sure about the testing structure.** - [x] Update docs? (Removed the limits so far, I don't think there is something we could add) - [x] Test ManyToMany Cheers ## Many-To-Many ### Foo <img width="1553" alt="Screenshot 2023-07-23 at 8 16 37 PM" src="https://github.com/EasyCorp/EasyAdminBundle/assets/1337562/28fcebc6-c4d2-4a96-943e-f41f9df38601"> <img width="1553" alt="Screenshot 2023-07-23 at 8 16 48 PM" src="https://github.com/EasyCorp/EasyAdminBundle/assets/1337562/b26eed84-4737-4faa-aab7-2e8492cb08ac"> ### Bar <img width="1553" alt="Screenshot 2023-07-23 at 8 17 27 PM" src="https://github.com/EasyCorp/EasyAdminBundle/assets/1337562/ad71e64f-b379-4a0d-beea-296f8f9add8a"> <img width="1553" alt="Screenshot 2023-07-23 at 8 17 15 PM" src="https://github.com/EasyCorp/EasyAdminBundle/assets/1337562/afe5b625-6ed8-4fce-9e9a-14d752267481"> ## One-To-Many <img width="1553" alt="Screenshot 2023-07-23 at 8 18 25 PM" src="https://github.com/EasyCorp/EasyAdminBundle/assets/1337562/1991e589-4a4f-4455-84c4-28b2346b4807"> <img width="1553" alt="Screenshot 2023-07-23 at 8 18 41 PM" src="https://github.com/EasyCorp/EasyAdminBundle/assets/1337562/b2d62fb7-fc84-4bba-88e8-c00cdbed0dd5"> Commits ------- 3167f9e Make *-To-Many relations sortable
- Loading branch information
Showing
16 changed files
with
860 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<?php | ||
|
||
namespace EasyCorp\Bundle\EasyAdminBundle\Tests\Orm; | ||
|
||
use EasyCorp\Bundle\EasyAdminBundle\Test\AbstractCrudTestCase; | ||
use EasyCorp\Bundle\EasyAdminBundle\Tests\TestApplication\Controller\DashboardController; | ||
use EasyCorp\Bundle\EasyAdminBundle\Tests\TestApplication\Controller\Sort\BillCrudController; | ||
use EasyCorp\Bundle\EasyAdminBundle\Tests\TestApplication\Entity\Bill; | ||
|
||
class BillSortTest extends AbstractCrudTestCase | ||
{ | ||
private $repository; | ||
|
||
protected function getControllerFqcn(): string | ||
{ | ||
return BillCrudController::class; | ||
} | ||
|
||
protected function getDashboardFqcn(): string | ||
{ | ||
return DashboardController::class; | ||
} | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
$this->client->followRedirects(); | ||
$this->repository = $this->entityManager->getRepository(Bill::class); | ||
} | ||
|
||
/** | ||
* @dataProvider sorting | ||
*/ | ||
public function testSorting(array $query, ?string $sortFunction, string $expectedSortIcon) | ||
{ | ||
// Arrange | ||
$expectedAmountMapping = []; | ||
|
||
/** | ||
* @var Bill $entity | ||
*/ | ||
foreach ($this->repository->findAll() as $entity) { | ||
$expectedAmountMapping[$entity->getName()] = $entity->getCustomers()->count(); | ||
} | ||
|
||
if (null !== $sortFunction) { | ||
$sortFunction($expectedAmountMapping); | ||
} | ||
|
||
// Act | ||
$crawler = $this->client->request('GET', $this->generateIndexUrl().'&'.http_build_query($query)); | ||
|
||
// Assert | ||
$this->assertResponseIsSuccessful(); | ||
$this->assertSelectorTextSame('th.header-for-field-association > a', 'Customers'); | ||
$this->assertSelectorExists('th.header-for-field-association i.'.$expectedSortIcon); | ||
|
||
$index = 1; | ||
|
||
foreach ($expectedAmountMapping as $expectedName => $expectedValue) { | ||
$expectedRow = $index++; | ||
|
||
$this->assertSelectorTextSame('tbody tr:nth-child('.$expectedRow.') td:nth-child(2)', $expectedName, sprintf('Expected "%s" in row %d', $expectedName, $expectedRow)); | ||
$this->assertSelectorTextSame('tbody tr:nth-child('.$expectedRow.') td:nth-child(3)', $expectedValue, sprintf('Expected "%s" in row %d', $expectedValue, $expectedRow)); | ||
} | ||
} | ||
|
||
public function sorting(): \Generator | ||
{ | ||
yield [ | ||
[], | ||
null, | ||
'fa-sort', | ||
]; | ||
|
||
yield [ | ||
['sort' => ['customers' => 'ASC']], | ||
'asort', | ||
'fa-arrow-up', | ||
]; | ||
|
||
yield [ | ||
['sort' => ['customers' => 'DESC']], | ||
'arsort', | ||
'fa-arrow-down', | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<?php | ||
|
||
namespace EasyCorp\Bundle\EasyAdminBundle\Tests\Orm; | ||
|
||
use EasyCorp\Bundle\EasyAdminBundle\Test\AbstractCrudTestCase; | ||
use EasyCorp\Bundle\EasyAdminBundle\Tests\TestApplication\Controller\DashboardController; | ||
use EasyCorp\Bundle\EasyAdminBundle\Tests\TestApplication\Controller\Sort\CustomerCrudController; | ||
use EasyCorp\Bundle\EasyAdminBundle\Tests\TestApplication\Entity\Customer; | ||
|
||
class CustomerSortTest extends AbstractCrudTestCase | ||
{ | ||
private $repository; | ||
|
||
protected function getControllerFqcn(): string | ||
{ | ||
return CustomerCrudController::class; | ||
} | ||
|
||
protected function getDashboardFqcn(): string | ||
{ | ||
return DashboardController::class; | ||
} | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
$this->client->followRedirects(); | ||
$this->repository = $this->entityManager->getRepository(Customer::class); | ||
} | ||
|
||
/** | ||
* @dataProvider sorting | ||
*/ | ||
public function testSorting(array $query, ?string $sortFunction, string $expectedSortIcon) | ||
{ | ||
// Arrange | ||
$expectedAmountMapping = []; | ||
|
||
/** | ||
* @var Customer $entity | ||
*/ | ||
foreach ($this->repository->findAll() as $entity) { | ||
$expectedAmountMapping[$entity->getName()] = $entity->getBills()->count(); | ||
} | ||
|
||
if (null !== $sortFunction) { | ||
$sortFunction($expectedAmountMapping); | ||
} | ||
|
||
// Act | ||
$crawler = $this->client->request('GET', $this->generateIndexUrl().'&'.http_build_query($query)); | ||
|
||
// Assert | ||
$this->assertResponseIsSuccessful(); | ||
$this->assertSelectorTextSame('th.header-for-field-association > a', 'Bills'); | ||
$this->assertSelectorExists('th.header-for-field-association i.'.$expectedSortIcon); | ||
|
||
$index = 1; | ||
|
||
foreach ($expectedAmountMapping as $expectedName => $expectedValue) { | ||
$expectedRow = $index++; | ||
|
||
$this->assertSelectorTextSame('tbody tr:nth-child('.$expectedRow.') td:nth-child(2)', $expectedName, sprintf('Expected "%s" in row %d', $expectedName, $expectedRow)); | ||
$this->assertSelectorTextSame('tbody tr:nth-child('.$expectedRow.') td:nth-child(3)', $expectedValue, sprintf('Expected "%s" in row %d', $expectedValue, $expectedRow)); | ||
} | ||
} | ||
|
||
public function sorting(): \Generator | ||
{ | ||
yield [ | ||
[], | ||
null, | ||
'fa-sort', | ||
]; | ||
|
||
yield [ | ||
['sort' => ['bills' => 'ASC']], | ||
'asort', | ||
'fa-arrow-up', | ||
]; | ||
|
||
yield [ | ||
['sort' => ['bills' => 'DESC']], | ||
'arsort', | ||
'fa-arrow-down', | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<?php | ||
|
||
namespace EasyCorp\Bundle\EasyAdminBundle\Tests\Orm; | ||
|
||
use EasyCorp\Bundle\EasyAdminBundle\Test\AbstractCrudTestCase; | ||
use EasyCorp\Bundle\EasyAdminBundle\Tests\TestApplication\Controller\DashboardController; | ||
use EasyCorp\Bundle\EasyAdminBundle\Tests\TestApplication\Controller\Sort\PageCrudController; | ||
use EasyCorp\Bundle\EasyAdminBundle\Tests\TestApplication\Entity\Page; | ||
|
||
class PageSortTest extends AbstractCrudTestCase | ||
{ | ||
private $repository; | ||
|
||
protected function getControllerFqcn(): string | ||
{ | ||
return PageCrudController::class; | ||
} | ||
|
||
protected function getDashboardFqcn(): string | ||
{ | ||
return DashboardController::class; | ||
} | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
$this->client->followRedirects(); | ||
$this->repository = $this->entityManager->getRepository(Page::class); | ||
} | ||
|
||
/** | ||
* @dataProvider sorting | ||
*/ | ||
public function testSorting(array $query, ?string $sortFunction, string $expectedSortIcon) | ||
{ | ||
// Arrange | ||
$expectedAmountMapping = []; | ||
|
||
/** | ||
* @var Page $entity | ||
*/ | ||
foreach ($this->repository->findAll() as $entity) { | ||
$expectedAmountMapping[$entity->getName()] = $entity->getWebsite()->getName(); | ||
} | ||
|
||
if (null !== $sortFunction) { | ||
$sortFunction($expectedAmountMapping); | ||
} | ||
|
||
// Act | ||
$crawler = $this->client->request('GET', $this->generateIndexUrl().'&'.http_build_query($query)); | ||
|
||
// Assert | ||
$this->assertResponseIsSuccessful(); | ||
$this->assertSelectorTextSame('th.header-for-field-association > a', 'Website'); | ||
$this->assertSelectorExists('th.header-for-field-association i.'.$expectedSortIcon); | ||
|
||
$index = 1; | ||
|
||
foreach ($expectedAmountMapping as $expectedName => $expectedValue) { | ||
$expectedRow = $index++; | ||
|
||
$this->assertSelectorTextSame('tbody tr:nth-child('.$expectedRow.') td:nth-child(2)', $expectedName, sprintf('Expected "%s" in row %d', $expectedName, $expectedRow)); | ||
$this->assertSelectorTextSame('tbody tr:nth-child('.$expectedRow.') td:nth-child(3)', $expectedValue, sprintf('Expected "%s" in row %d', $expectedValue, $expectedRow)); | ||
} | ||
} | ||
|
||
public function sorting(): \Generator | ||
{ | ||
yield [ | ||
[], | ||
null, | ||
'fa-sort', | ||
]; | ||
|
||
yield [ | ||
['sort' => ['website' => 'ASC']], | ||
'asort', | ||
'fa-arrow-up', | ||
]; | ||
|
||
yield [ | ||
['sort' => ['website' => 'DESC']], | ||
'arsort', | ||
'fa-arrow-down', | ||
]; | ||
} | ||
} |
Oops, something went wrong.