From eed8f63a1ca73d9440dddfce2cceb81cf71575de Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Wed, 4 Oct 2023 22:38:24 +0200 Subject: [PATCH] Fix group mapper remove method (#8115) --- src/Mapper/BaseGroupedMapper.php | 8 ++-- .../Mapper/AbstractDummyGroupedMapper.php | 7 +++ tests/Mapper/BaseGroupedMapperTest.php | 47 ++++++++++++++++++- 3 files changed, 56 insertions(+), 6 deletions(-) diff --git a/src/Mapper/BaseGroupedMapper.php b/src/Mapper/BaseGroupedMapper.php index 6c3b269446..22d416c9c5 100644 --- a/src/Mapper/BaseGroupedMapper.php +++ b/src/Mapper/BaseGroupedMapper.php @@ -268,8 +268,8 @@ final public function removeGroup(string $group, string $tab = 'default', bool $ } if (isset($groups[$group])) { - foreach ($groups[$group]['fields'] as $field) { - $this->remove($field); + foreach ($groups[$group]['fields'] as $fieldKey => $field) { + $this->remove((string) $fieldKey); } } unset($groups[$group]); @@ -302,8 +302,8 @@ final public function removeTab(string $tab): self foreach ($tabs[$tab]['groups'] as $group) { if (isset($groups[$group])) { - foreach ($groups[$group]['fields'] as $field) { - $this->remove($field); + foreach ($groups[$group]['fields'] as $fieldKey => $field) { + $this->remove((string) $fieldKey); } } diff --git a/tests/Fixtures/Mapper/AbstractDummyGroupedMapper.php b/tests/Fixtures/Mapper/AbstractDummyGroupedMapper.php index ceec9e8e82..36bc47f7f5 100644 --- a/tests/Fixtures/Mapper/AbstractDummyGroupedMapper.php +++ b/tests/Fixtures/Mapper/AbstractDummyGroupedMapper.php @@ -29,6 +29,13 @@ public function __construct( ) { } + public function add(string $fieldName, ?string $name = null): self + { + $this->addFieldToCurrentGroup($fieldName, $name); + + return $this; + } + /** * @return AdminInterface */ diff --git a/tests/Mapper/BaseGroupedMapperTest.php b/tests/Mapper/BaseGroupedMapperTest.php index 478c1e2649..8835662c00 100644 --- a/tests/Mapper/BaseGroupedMapperTest.php +++ b/tests/Mapper/BaseGroupedMapperTest.php @@ -17,7 +17,6 @@ use PHPUnit\Framework\TestCase; use Sonata\AdminBundle\Admin\AbstractAdmin; use Sonata\AdminBundle\Admin\Pool; -use Sonata\AdminBundle\Mapper\BaseGroupedMapper; use Sonata\AdminBundle\Tests\Fixtures\Mapper\AbstractDummyGroupedMapper; use Sonata\AdminBundle\Translator\LabelTranslatorStrategyInterface; use Symfony\Component\DependencyInjection\Container; @@ -28,7 +27,7 @@ final class BaseGroupedMapperTest extends TestCase { /** - * @var BaseGroupedMapper&MockObject + * @var AbstractDummyGroupedMapper&MockObject */ protected $baseGroupedMapper; @@ -120,6 +119,50 @@ public function testTab2(): void static::assertCount(0, $this->groups); } + public function testRemoveGroup(): void + { + static::assertCount(0, $this->tabs); + static::assertCount(0, $this->groups); + + $this->baseGroupedMapper + ->tab('fooTab1') + ->with('fooGroup1') + ->add('field1', 'name1') + ->end() + ->end(); + + static::assertCount(1, $this->tabs); + static::assertCount(1, $this->groups); + + $this->baseGroupedMapper->expects(static::once())->method('remove')->with('field1'); + $this->baseGroupedMapper->removeGroup('fooGroup1', 'fooTab1'); + + static::assertCount(1, $this->tabs); + static::assertCount(0, $this->groups); + } + + public function testRemoveTab(): void + { + static::assertCount(0, $this->tabs); + static::assertCount(0, $this->groups); + + $this->baseGroupedMapper + ->tab('fooTab1') + ->with('fooGroup1') + ->add('field1', 'name1') + ->end() + ->end(); + + static::assertCount(1, $this->tabs); + static::assertCount(1, $this->groups); + + $this->baseGroupedMapper->expects(static::once())->method('remove')->with('field1'); + $this->baseGroupedMapper->removeTab('fooTab1'); + + static::assertCount(0, $this->tabs); + static::assertCount(0, $this->groups); + } + public function testFluidInterface(): void { static::assertSame($this->baseGroupedMapper, $this->baseGroupedMapper->tab('fooTab')->with('fooGroup1')->end()->with('fooGroup2')->end()->with('fooGroup3')->end()->end()->tab('barTab')->with('barGroup1')->end()->with('barGroup2')->end()->with('barGroup3')->end()->end());