From 4eed6b0f11dc553aeea12b76b6e07a4b54f945fe Mon Sep 17 00:00:00 2001 From: Jaapio Date: Mon, 20 Jan 2025 20:53:56 +0100 Subject: [PATCH] FEAT: make textroles case insentive fixes: 1195 --- .../TextRoles/DefaultTextRoleFactory.php | 8 +++++--- .../tests/unit/TextRoles/DefaultTextRoleFactoryTest.php | 9 ++++++++- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/packages/guides-restructured-text/src/RestructuredText/TextRoles/DefaultTextRoleFactory.php b/packages/guides-restructured-text/src/RestructuredText/TextRoles/DefaultTextRoleFactory.php index ba1a3769d..65810fc2e 100644 --- a/packages/guides-restructured-text/src/RestructuredText/TextRoles/DefaultTextRoleFactory.php +++ b/packages/guides-restructured-text/src/RestructuredText/TextRoles/DefaultTextRoleFactory.php @@ -14,6 +14,7 @@ namespace phpDocumentor\Guides\RestructuredText\TextRoles; use function in_array; +use function strtolower; final class DefaultTextRoleFactory implements TextRoleFactory { @@ -53,16 +54,17 @@ public function replaceTextRole(TextRole $newTextRole): void public function getTextRole(string $name, string|null $domain = null): TextRole { - if ($name === 'default') { + $normalizedName = strtolower($name); + if ($normalizedName === 'default') { return $this->defaultTextRole; } if ($domain === null) { - return $this->findTextRole($this->textRoles, $name); + return $this->findTextRole($this->textRoles, $normalizedName); } if (isset($this->domains[$domain])) { - return $this->findTextRole($this->domains[$domain], $name); + return $this->findTextRole($this->domains[$domain], $normalizedName); } return $this->genericTextRole; diff --git a/packages/guides-restructured-text/tests/unit/TextRoles/DefaultTextRoleFactoryTest.php b/packages/guides-restructured-text/tests/unit/TextRoles/DefaultTextRoleFactoryTest.php index 7e05219ba..d88404e95 100644 --- a/packages/guides-restructured-text/tests/unit/TextRoles/DefaultTextRoleFactoryTest.php +++ b/packages/guides-restructured-text/tests/unit/TextRoles/DefaultTextRoleFactoryTest.php @@ -26,7 +26,7 @@ public function setUp(): void { $this->logger = new Logger('test'); $this->defaultTextRoleFactory = new DefaultTextRoleFactory( - new GenericTextRole(self::createMock(SettingsManager::class)), + new GenericTextRole($this->createMock(SettingsManager::class)), new LiteralTextRole(), [], [], @@ -45,4 +45,11 @@ public function testRegisteredTextRoleIsReturned(): void $textRole = $this->defaultTextRoleFactory->getTextRole('abbreviation'); self::assertInstanceOf(AbbreviationTextRole::class, $textRole); } + + public function testRegisteredTextRoleIsCaseInSensitive(): void + { + $this->defaultTextRoleFactory->registerTextRole(new AbbreviationTextRole($this->logger)); + $textRole = $this->defaultTextRoleFactory->getTextRole('ABbreviation'); + self::assertInstanceOf(AbbreviationTextRole::class, $textRole); + } }