-
-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix grid sorting with sorting key not defined and refactor grid defin…
…ition creation
- Loading branch information
1 parent
0f305fe
commit 42ea1d8
Showing
22 changed files
with
620 additions
and
228 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Sylius Sp. z o.o. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\Bundle\GridBundle\Factory; | ||
|
||
use Sylius\Bundle\GridBundle\Registry\GridRegistryInterface; | ||
use Sylius\Component\Grid\Configuration\GridConfigurationExtenderInterface; | ||
use Sylius\Component\Grid\Factory\GridConfigurationProcessEvent; | ||
use Webmozart\Assert\Assert; | ||
|
||
final class GridConfigurationExtenderListener | ||
{ | ||
public function __construct( | ||
private GridRegistryInterface $gridRegistry, | ||
private array $gridConfigurations, | ||
private GridConfigurationExtenderInterface $gridConfigurationExtender, | ||
) { | ||
} | ||
|
||
public function __invoke(GridConfigurationProcessEvent $event): void | ||
{ | ||
$gridConfiguration = $event->getGridConfiguration(); | ||
$parentGridCode = $gridConfiguration['extends'] ?? null; | ||
|
||
if (null === $parentGridCode) { | ||
return; | ||
} | ||
|
||
$parentGridConfiguration = $this->gridRegistry->getGrid($parentGridCode)?->toArray() ?? $this->gridConfigurations[$parentGridCode] ?? null; | ||
Assert::notNull($parentGridConfiguration, sprintf('Parent grid with code "%s" does not exists.', $gridConfiguration['extends'])); | ||
|
||
$gridConfiguration = $this->gridConfigurationExtender->extends($gridConfiguration, $parentGridConfiguration); | ||
|
||
$event->setGridConfiguration($gridConfiguration); | ||
} | ||
} |
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
108 changes: 108 additions & 0 deletions
108
src/Bundle/spec/Factory/GridConfigurationExtenderListenerSpec.php
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,108 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Sylius Sp. z o.o. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace spec\Sylius\Bundle\GridBundle\Factory; | ||
|
||
use PhpSpec\ObjectBehavior; | ||
use PHPUnit\Framework\Assert; | ||
use Sylius\Bundle\GridBundle\Grid\GridInterface; | ||
use Sylius\Bundle\GridBundle\Registry\GridRegistryInterface; | ||
use Sylius\Component\Grid\Configuration\GridConfigurationExtenderInterface; | ||
use Sylius\Component\Grid\Factory\GridConfigurationProcessEvent; | ||
|
||
final class GridConfigurationExtenderListenerSpec extends ObjectBehavior | ||
{ | ||
function let( | ||
GridRegistryInterface $gridRegistry, | ||
GridConfigurationExtenderInterface $gridConfigurationExtender, | ||
): void { | ||
$this->beConstructedWith( | ||
$gridRegistry, | ||
[ | ||
'author' => ['foo' => 'foobar'], | ||
], | ||
$gridConfigurationExtender, | ||
); | ||
} | ||
|
||
function it_does_not_update_grid( | ||
GridRegistryInterface $gridRegistry, | ||
GridConfigurationExtenderInterface $gridConfigurationExtender, | ||
): void { | ||
$event = new GridConfigurationProcessEvent('author', ['bar' => 'baz']); | ||
|
||
$this->__invoke($event); | ||
|
||
Assert::assertEquals($event->getGridConfiguration(), ['bar' => 'baz']); | ||
} | ||
|
||
function it_updates_grid_with_parent_from_grid_registry( | ||
GridRegistryInterface $gridRegistry, | ||
GridInterface $grid, | ||
GridConfigurationExtenderInterface $gridConfigurationExtender, | ||
): void { | ||
$gridRegistry->getGrid('author')->willReturn($grid); | ||
$grid->toArray()->willReturn([ | ||
'bar' => 'baz', | ||
]); | ||
|
||
$gridConfigurationExtender | ||
->extends(['extends' => 'author'], ['bar' => 'baz']) | ||
->willReturn([ | ||
'extends' => 'author', | ||
'bar' => 'baz', | ||
]); | ||
|
||
$event = new GridConfigurationProcessEvent('author_with_books', [ | ||
'extends' => 'author', | ||
]); | ||
|
||
$this->__invoke($event); | ||
|
||
Assert::assertEquals($event->getGridConfiguration(), [ | ||
'extends' => 'author', | ||
'bar' => 'baz', | ||
]); | ||
} | ||
|
||
function it_updates_grid_with_parent_from_grid_configurations_array( | ||
GridConfigurationExtenderInterface $gridConfigurationExtender, | ||
): void { | ||
$gridConfigurationExtender | ||
->extends(['extends' => 'author'], ['foo' => 'foobar']) | ||
->willReturn([ | ||
'extends' => 'author', | ||
'foo' => 'foobar', | ||
]); | ||
|
||
$event = new GridConfigurationProcessEvent('author_with_books', [ | ||
'extends' => 'author', | ||
]); | ||
|
||
$this->__invoke($event); | ||
|
||
Assert::assertEquals($event->getGridConfiguration(), [ | ||
'extends' => 'author', | ||
'foo' => 'foobar', | ||
]); | ||
} | ||
|
||
function it_throws_an_invalid_argument_exception_when_parent_grid_is_not_found(): void | ||
{ | ||
$event = new GridConfigurationProcessEvent('sylius_admin_book', [ | ||
'extends' => '404', | ||
]); | ||
|
||
$this->shouldThrow(\InvalidArgumentException::class)->during('__invoke', [$event]); | ||
} | ||
} |
Oops, something went wrong.