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());