Skip to content

Commit eed8f63

Browse files
Fix group mapper remove method (#8115)
1 parent 99777c8 commit eed8f63

File tree

3 files changed

+56
-6
lines changed

3 files changed

+56
-6
lines changed

src/Mapper/BaseGroupedMapper.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -268,8 +268,8 @@ final public function removeGroup(string $group, string $tab = 'default', bool $
268268
}
269269

270270
if (isset($groups[$group])) {
271-
foreach ($groups[$group]['fields'] as $field) {
272-
$this->remove($field);
271+
foreach ($groups[$group]['fields'] as $fieldKey => $field) {
272+
$this->remove((string) $fieldKey);
273273
}
274274
}
275275
unset($groups[$group]);
@@ -302,8 +302,8 @@ final public function removeTab(string $tab): self
302302

303303
foreach ($tabs[$tab]['groups'] as $group) {
304304
if (isset($groups[$group])) {
305-
foreach ($groups[$group]['fields'] as $field) {
306-
$this->remove($field);
305+
foreach ($groups[$group]['fields'] as $fieldKey => $field) {
306+
$this->remove((string) $fieldKey);
307307
}
308308
}
309309

tests/Fixtures/Mapper/AbstractDummyGroupedMapper.php

+7
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ public function __construct(
2929
) {
3030
}
3131

32+
public function add(string $fieldName, ?string $name = null): self
33+
{
34+
$this->addFieldToCurrentGroup($fieldName, $name);
35+
36+
return $this;
37+
}
38+
3239
/**
3340
* @return AdminInterface<object>
3441
*/

tests/Mapper/BaseGroupedMapperTest.php

+45-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
use PHPUnit\Framework\TestCase;
1818
use Sonata\AdminBundle\Admin\AbstractAdmin;
1919
use Sonata\AdminBundle\Admin\Pool;
20-
use Sonata\AdminBundle\Mapper\BaseGroupedMapper;
2120
use Sonata\AdminBundle\Tests\Fixtures\Mapper\AbstractDummyGroupedMapper;
2221
use Sonata\AdminBundle\Translator\LabelTranslatorStrategyInterface;
2322
use Symfony\Component\DependencyInjection\Container;
@@ -28,7 +27,7 @@
2827
final class BaseGroupedMapperTest extends TestCase
2928
{
3029
/**
31-
* @var BaseGroupedMapper<object>&MockObject
30+
* @var AbstractDummyGroupedMapper&MockObject
3231
*/
3332
protected $baseGroupedMapper;
3433

@@ -120,6 +119,50 @@ public function testTab2(): void
120119
static::assertCount(0, $this->groups);
121120
}
122121

122+
public function testRemoveGroup(): void
123+
{
124+
static::assertCount(0, $this->tabs);
125+
static::assertCount(0, $this->groups);
126+
127+
$this->baseGroupedMapper
128+
->tab('fooTab1')
129+
->with('fooGroup1')
130+
->add('field1', 'name1')
131+
->end()
132+
->end();
133+
134+
static::assertCount(1, $this->tabs);
135+
static::assertCount(1, $this->groups);
136+
137+
$this->baseGroupedMapper->expects(static::once())->method('remove')->with('field1');
138+
$this->baseGroupedMapper->removeGroup('fooGroup1', 'fooTab1');
139+
140+
static::assertCount(1, $this->tabs);
141+
static::assertCount(0, $this->groups);
142+
}
143+
144+
public function testRemoveTab(): void
145+
{
146+
static::assertCount(0, $this->tabs);
147+
static::assertCount(0, $this->groups);
148+
149+
$this->baseGroupedMapper
150+
->tab('fooTab1')
151+
->with('fooGroup1')
152+
->add('field1', 'name1')
153+
->end()
154+
->end();
155+
156+
static::assertCount(1, $this->tabs);
157+
static::assertCount(1, $this->groups);
158+
159+
$this->baseGroupedMapper->expects(static::once())->method('remove')->with('field1');
160+
$this->baseGroupedMapper->removeTab('fooTab1');
161+
162+
static::assertCount(0, $this->tabs);
163+
static::assertCount(0, $this->groups);
164+
}
165+
123166
public function testFluidInterface(): void
124167
{
125168
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());

0 commit comments

Comments
 (0)