Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix group mapper remove method #8115

Merged
merged 2 commits into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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