From ece9536fed6e10eeefee319c75f243196447c2bb Mon Sep 17 00:00:00 2001 From: Kamil Kokot Date: Tue, 24 Nov 2020 19:11:50 +0100 Subject: [PATCH] Add an ability to define your own Inflector for Metadata class --- phpunit.xml.dist | 1 + src/Component/.gitignore | 2 + src/Component/Metadata/Metadata.php | 33 +++++----- src/Component/Tests/Metadata/MetadataTest.php | 61 +++++++++++++++++++ src/Component/composer.json | 6 +- src/Component/phpunit.xml.dist | 12 ++++ 6 files changed, 96 insertions(+), 19 deletions(-) create mode 100644 src/Component/Tests/Metadata/MetadataTest.php create mode 100644 src/Component/phpunit.xml.dist diff --git a/phpunit.xml.dist b/phpunit.xml.dist index d1d0f43a2..a8d26018b 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -16,6 +16,7 @@ ./src/Bundle/test/ ./src/Bundle/Tests/ + ./src/Component/Tests/ diff --git a/src/Component/.gitignore b/src/Component/.gitignore index ff2fefa58..9fbf3fcd0 100644 --- a/src/Component/.gitignore +++ b/src/Component/.gitignore @@ -5,3 +5,5 @@ /composer.phar /composer.lock + +/.phpunit.result.cache diff --git a/src/Component/Metadata/Metadata.php b/src/Component/Metadata/Metadata.php index d1f6025a4..701077cea 100644 --- a/src/Component/Metadata/Metadata.php +++ b/src/Component/Metadata/Metadata.php @@ -15,7 +15,6 @@ use Doctrine\Inflector\Inflector as InflectorObject; use Doctrine\Inflector\InflectorFactory; -use Doctrine\Inflector\LanguageInflectorFactory; final class Metadata implements MetadataInterface { @@ -34,25 +33,9 @@ final class Metadata implements MetadataInterface /** @var array */ private $parameters; - /** @var LanguageInflectorFactory|null */ - private static $inflectorFactory; - /** @var InflectorObject|null */ private static $inflectorInstance; - private static function getInflector(): InflectorObject - { - if (self::$inflectorFactory === null) { - self::$inflectorFactory = InflectorFactory::create(); - } - - if (self::$inflectorInstance === null) { - self::$inflectorInstance = self::$inflectorFactory->build(); - } - - return self::$inflectorInstance; - } - private function __construct(string $name, string $applicationName, array $parameters) { $this->name = $name; @@ -71,6 +54,22 @@ public static function fromAliasAndConfiguration(string $alias, array $parameter return new self($name, $applicationName, $parameters); } + public static function setInflector(InflectorObject $inflector): void + { + self::$inflectorInstance = $inflector; + } + + private static function getInflector(): InflectorObject + { + if (self::$inflectorInstance === null) { + $inflectorFactory = InflectorFactory::create(); + + self::$inflectorInstance = $inflectorFactory->build(); + } + + return self::$inflectorInstance; + } + public function getAlias(): string { return $this->applicationName . '.' . $this->name; diff --git a/src/Component/Tests/Metadata/MetadataTest.php b/src/Component/Tests/Metadata/MetadataTest.php new file mode 100644 index 000000000..d9d24c7fa --- /dev/null +++ b/src/Component/Tests/Metadata/MetadataTest.php @@ -0,0 +1,61 @@ +withPluralRules(new Ruleset( + new Transformations(), + new Patterns(), + new Substitutions(new Substitution(new Word('taxon'), new Word('taxons'))) + )); + $inflector = $factory->build(); + + Metadata::setInflector($inflector); + + $metadata = Metadata::fromAliasAndConfiguration('app.taxon', ['driver' => 'whatever']); + + Assert::assertSame('taxons', $metadata->getPluralName()); + } + + /** @test */ + public function it_uses_a_default_inflector(): void + { + $metadata = Metadata::fromAliasAndConfiguration('app.taxon', ['driver' => 'whatever']); + + Assert::assertSame('taxa', $metadata->getPluralName()); + } +} diff --git a/src/Component/composer.json b/src/Component/composer.json index 04e2c2571..511eb6669 100644 --- a/src/Component/composer.json +++ b/src/Component/composer.json @@ -37,7 +37,8 @@ }, "require-dev": { "behat/transliterator": "^1.3", - "phpspec/phpspec": "^7.0" + "phpspec/phpspec": "^7.0", + "phpunit/phpunit": "^9.4" }, "extra": { "branch-alias": { @@ -51,7 +52,8 @@ }, "autoload-dev": { "psr-4": { - "Sylius\\Component\\Resource\\spec\\": "spec/" + "Sylius\\Component\\Resource\\spec\\": "spec/", + "Sylius\\Component\\Resource\\Tests\\": "Tests" } } } diff --git a/src/Component/phpunit.xml.dist b/src/Component/phpunit.xml.dist new file mode 100644 index 000000000..28b503841 --- /dev/null +++ b/src/Component/phpunit.xml.dist @@ -0,0 +1,12 @@ + + + + + + ./Tests/ + + +