Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
javiereguiluz committed Oct 20, 2024
1 parent 36163d8 commit aaa51f4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
5 changes: 3 additions & 2 deletions src/Twig/EasyAdminTwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Contracts\Translation\TranslatableInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use Twig\Environment;
use Twig\Error\RuntimeError;
use Twig\Extension\AbstractExtension;
use Twig\Extension\GlobalsInterface;
use Twig\TwigFilter;
Expand Down Expand Up @@ -141,8 +142,8 @@ public function representAsString($value, string|callable|null $toStringMethod =
}

$callable = [$value, $toStringMethod];
if (!\is_callable($callable) || !\method_exists($value, $toStringMethod)) {
throw new \RuntimeException(sprintf('The method "%s()" does not exist or is not callable in the value of type "%s"', $toStringMethod, is_object($value) ? \get_class($value) : \gettype($value)));
if (!\is_callable($callable) || !method_exists($value, $toStringMethod)) {
throw new \RuntimeException(sprintf('The method "%s()" does not exist or is not callable in the value of type "%s"', $toStringMethod, \is_object($value) ? $value::class : \gettype($value)));
}

return \call_user_func($callable);
Expand Down
34 changes: 27 additions & 7 deletions tests/Twig/EasyAdminTwigExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class EasyAdminTwigExtensionTest extends KernelTestCase
public function testRepresentAsString($value, $expectedValue, bool $assertRegex = false, string|callable|null $toStringMethod = null): void
{
$translator = $this->getMockBuilder(TranslatorInterface::class)->disableOriginalConstructor()->getMock();
$translator->method('trans')->willReturnCallback(fn($value) => '*'.$value);
$translator->method('trans')->willReturnCallback(fn ($value) => '*'.$value);

$extension = new EasyAdminTwigExtension(
$this->getMockBuilder(ServiceLocator::class)->disableOriginalConstructor()->getMock(),
Expand Down Expand Up @@ -63,13 +63,33 @@ public function provideValuesForRepresentAsString()
yield [true, 'true'];
yield [false, 'false'];
yield [[1, 2, 3], 'Array (3 items)'];
yield [new class implements TranslatableInterface { public function trans(TranslatorInterface $translator, string $locale = null): string { return $translator->trans('some value'); } }, '*some value'];
yield [new class implements TranslatableInterface {
public function trans(TranslatorInterface $translator, ?string $locale = null): string
{
return $translator->trans('some value');
}
}, '*some value'];
yield [new class {}, '/class@anonymous.*/', true];
yield [new class { public function __toString() { return 'foo bar'; }}, 'foo bar'];
yield [new class { public function getId() { return 1234; }}, '/class@anonymous.* #1234/', true];
yield [new class {
public function __toString()
{
return 'foo bar';
}
}, 'foo bar'];
yield [new class {
public function getId()
{
return 1234;
}
}, '/class@anonymous.* #1234/', true];

yield ['foo', 'foo bar', false, fn($value) => $value.' bar'];
yield ['foo', '*foo bar', false, fn($value, $translator) => $translator->trans($value.' bar')];
yield [new class () { public function someMethod() { return 'foo'; }}, 'foo', false, 'someMethod'];
yield ['foo', 'foo bar', false, fn ($value) => $value.' bar'];
yield [new class {
public function someMethod()
{
return 'foo';
}
}, 'foo', false, 'someMethod'];
yield ['foo', '*foo bar', false, fn ($value, $translator) => $translator->trans($value.' bar')];
}
}

0 comments on commit aaa51f4

Please sign in to comment.