From 8c8566ee3e7b53baa0cd761676c572c6753f9c1c Mon Sep 17 00:00:00 2001 From: Jesse Rushlow Date: Wed, 25 Sep 2024 15:23:32 -0400 Subject: [PATCH] allow creating class data objects with absolute namespaces --- src/Util/ClassSource/Model/ClassData.php | 5 +++++ tests/Util/ClassSource/ClassDataTest.php | 20 +------------------- 2 files changed, 6 insertions(+), 19 deletions(-) diff --git a/src/Util/ClassSource/Model/ClassData.php b/src/Util/ClassSource/Model/ClassData.php index 53579a95c..4ce0cd605 100644 --- a/src/Util/ClassSource/Model/ClassData.php +++ b/src/Util/ClassSource/Model/ClassData.php @@ -88,6 +88,11 @@ public function getNamespace(): string return $this->rootNamespace; } + // Namespace is already absolute, don't add the rootNamespace. + if (str_starts_with($this->namespace, '\\')) { + return substr_replace($this->namespace, '', 0, 1); + } + return \sprintf('%s\%s', $this->rootNamespace, $this->namespace); } diff --git a/tests/Util/ClassSource/ClassDataTest.php b/tests/Util/ClassSource/ClassDataTest.php index c0990041d..27d345028 100644 --- a/tests/Util/ClassSource/ClassDataTest.php +++ b/tests/Util/ClassSource/ClassDataTest.php @@ -14,7 +14,6 @@ use PHPUnit\Framework\TestCase; use Symfony\Bundle\MakerBundle\MakerBundle; use Symfony\Bundle\MakerBundle\Test\MakerTestKernel; -use Symfony\Bundle\MakerBundle\Util\ClassNameDetails; use Symfony\Bundle\MakerBundle\Util\ClassSource\Model\ClassData; class ClassDataTest extends TestCase @@ -115,12 +114,9 @@ public function testGetClassNameRelativeNamespace(): void public function testGetClassNameWithAbsoluteNamespace(): void { - $this->markTestSkipped(); $class = ClassData::create(class: '\\Foo\\Bar\\Admin\\Baz', suffix: 'Controller'); self::assertSame('BazController', $class->getClassName()); - self::assertSame('Baz', $class->getClassName(relative: false, withoutSuffix: true)); - // self::assertSame('Admin\FooController', $class->getClassName(relative: true, withoutSuffix: false)); - // self::assertSame('Admin\Baz', $class->getClassName(relative: true, withoutSuffix: true)); + self::assertSame('Foo\Bar\Admin', $class->getNamespace()); self::assertSame('Foo\Bar\Admin\BazController', $class->getFullClassName()); } @@ -147,18 +143,4 @@ public function fullClassNameProvider(): \Generator yield ['Controller\MyController', 'Custom', false, true, 'Custom\Controller\My']; yield ['Controller\MyController', 'Custom', true, true, 'Controller\My']; } - - // public function testClassNameDetails(): void - // { - // $class = new ClassNameDetails( - // fullClassName: 'Foo', - // namespacePrefix: 'Controller\\', - // suffix: 'Controller', - // ); - // - // self::assertSame('FooController', $class->getFullName()); - // self::assertSame('MyController', $class->getShortName()); - // self::assertSame('My', $class->getRelativeNameWithoutSuffix()); - // self::assertSame('MyController', $class->getRelativeName()); - // } }