Skip to content

Commit

Permalink
bug #4196 Fixed the handling of label callables (javiereguiluz)
Browse files Browse the repository at this point in the history
This PR was squashed before being merged into the 3.0.x-dev branch.

Discussion
----------

Fixed the handling of label callables

Fixes #4176.

Commits
-------

b66498b Fixed the handling of label callables
  • Loading branch information
javiereguiluz committed Feb 11, 2021
2 parents 423c7f3 + b66498b commit 4dd5f70
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/Dto/CrudDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,15 @@ public function setEntityFqcn(string $entityFqcn): void

public function getEntityLabelInSingular($entityInstance = null): ?string
{
if (\is_callable($this->entityLabelInSingular)) {
return ($this->entityLabelInSingular)($entityInstance);
if (null === $this->entityLabelInSingular) {
return null;
}

return $this->entityLabelInSingular;
if (\is_string($this->entityLabelInSingular)) {
return $this->entityLabelInSingular;
}

return ($this->entityLabelInSingular)($entityInstance);
}

/**
Expand All @@ -113,11 +117,15 @@ public function setEntityLabelInSingular($label): void

public function getEntityLabelInPlural($entityInstance = null): ?string
{
if (\is_callable($this->entityLabelInPlural)) {
return ($this->entityLabelInPlural)($entityInstance);
if (null === $this->entityLabelInPlural) {
return null;
}

if (\is_string($this->entityLabelInPlural)) {
return $this->entityLabelInPlural;
}

return $this->entityLabelInPlural;
return ($this->entityLabelInPlural)($entityInstance);
}

/**
Expand Down
49 changes: 49 additions & 0 deletions tests/Dto/CrudDtoTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace EasyCorp\Bundle\EasyAdminBundle\Tests\Dto;

use EasyCorp\Bundle\EasyAdminBundle\Dto\CrudDto;
use PHPUnit\Framework\TestCase;

class CrudDtoTest extends TestCase
{
/**
* @dataProvider provideLabels
*
* @param string|closure|null $setLabel
*/
public function testGetEntityLabelInSingular($setLabel, ?string $expectedGetLabel)
{
$crudDto = new CrudDto();

if (null !== $setLabel) {
$crudDto->setEntityLabelInSingular($setLabel);
$crudDto->setEntityLabelInPlural($setLabel);
}

$entityInstance = new class() {
public function getPrimaryKeyValue()
{
return '42';
}
};
$this->assertSame($expectedGetLabel, $crudDto->getEntityLabelInSingular($entityInstance));
$this->assertSame($expectedGetLabel, $crudDto->getEntityLabelInPlural($entityInstance));
}

public function provideLabels()
{
yield [null, null];
yield ['', ''];
yield ['foo', 'foo'];
yield ['Foo Bar', 'Foo Bar'];
// see https://github.com/EasyCorp/EasyAdminBundle/issues/4176
yield ['link', 'link'];
yield [fn () => null, null];
yield [fn () => '', ''];
yield [fn () => 'foo', 'foo'];
yield [fn () => 'Foo Bar', 'Foo Bar'];
yield [fn () => 'link', 'link'];
yield [fn ($entityInstance) => 'Entity #'.$entityInstance->getPrimaryKeyValue(), 'Entity #42'];
}
}

0 comments on commit 4dd5f70

Please sign in to comment.