Skip to content

Commit

Permalink
Fix group mapper remove method (#8115)
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet authored Oct 4, 2023
1 parent 99777c8 commit eed8f63
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 6 deletions.
8 changes: 4 additions & 4 deletions src/Mapper/BaseGroupedMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down Expand Up @@ -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);
}
}

Expand Down
7 changes: 7 additions & 0 deletions tests/Fixtures/Mapper/AbstractDummyGroupedMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ public function __construct(
) {
}

public function add(string $fieldName, ?string $name = null): self
{
$this->addFieldToCurrentGroup($fieldName, $name);

return $this;
}

/**
* @return AdminInterface<object>
*/
Expand Down
47 changes: 45 additions & 2 deletions tests/Mapper/BaseGroupedMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,7 +27,7 @@
final class BaseGroupedMapperTest extends TestCase
{
/**
* @var BaseGroupedMapper<object>&MockObject
* @var AbstractDummyGroupedMapper&MockObject
*/
protected $baseGroupedMapper;

Expand Down Expand Up @@ -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());
Expand Down

0 comments on commit eed8f63

Please sign in to comment.