diff --git a/src/Str.php b/src/Str.php index 3954d75d8..7e1d52557 100644 --- a/src/Str.php +++ b/src/Str.php @@ -102,7 +102,9 @@ public static function asRoutePath(string $value): string public static function asRouteName(string $value): string { - return self::asTwigVariable($value); + $routeName = self::asTwigVariable($value); + + return str_starts_with($routeName, 'app_') ? $routeName : 'app_'.$routeName; } public static function asSnakeCase(string $value): string diff --git a/src/Test/MakerTestCase.php b/src/Test/MakerTestCase.php index b7e9dfebb..3b550a735 100644 --- a/src/Test/MakerTestCase.php +++ b/src/Test/MakerTestCase.php @@ -106,7 +106,7 @@ private function getMakerInstance(string $makerClass): MakerInterface } // a cheap way to guess the service id - $serviceId = $serviceId ?? sprintf('maker.maker.%s', Str::asRouteName((new \ReflectionClass($makerClass))->getShortName())); + $serviceId = $serviceId ?? sprintf('maker.maker.%s', Str::asSnakeCase((new \ReflectionClass($makerClass))->getShortName())); return $this->kernel->getContainer()->get($serviceId); } diff --git a/tests/StrTest.php b/tests/StrTest.php index 99022b4e0..5c639624e 100644 --- a/tests/StrTest.php +++ b/tests/StrTest.php @@ -200,4 +200,19 @@ public function asHumanWordsTests() yield [' FooBar', 'Foo Bar']; yield [' Foo Bar ', 'Foo Bar']; } + + /** + * @dataProvider provideAsRouteName + */ + public function testAsRouteName(string $value, string $expectedRouteName) + { + $this->assertSame($expectedRouteName, Str::asRouteName($value)); + } + + public function provideAsRouteName() + { + yield ['Example', 'app_example']; + yield ['AppExample', 'app_example']; + yield ['Apple', 'app_apple']; + } }