Skip to content

Commit

Permalink
feature #228 Add an ability to define your own Inflector for Metadata…
Browse files Browse the repository at this point in the history
… class (pamil)

This PR was merged into the 1.7-dev branch.

Discussion
----------

See Sylius/Sylius#11440 for more details.

Commits
-------

ece9536 Add an ability to define your own Inflector for Metadata class
  • Loading branch information
pamil authored Nov 24, 2020
2 parents 83a8032 + ece9536 commit 8b9520f
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 19 deletions.
1 change: 1 addition & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<testsuite name="SyliusResourceBundle Test Suite">
<directory>./src/Bundle/test/</directory>
<directory>./src/Bundle/Tests/</directory>
<directory>./src/Component/Tests/</directory>
</testsuite>
</testsuites>
</phpunit>
2 changes: 2 additions & 0 deletions src/Component/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@

/composer.phar
/composer.lock

/.phpunit.result.cache
33 changes: 16 additions & 17 deletions src/Component/Metadata/Metadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

use Doctrine\Inflector\Inflector as InflectorObject;
use Doctrine\Inflector\InflectorFactory;
use Doctrine\Inflector\LanguageInflectorFactory;

final class Metadata implements MetadataInterface
{
Expand All @@ -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;
Expand All @@ -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;
Expand Down
61 changes: 61 additions & 0 deletions src/Component/Tests/Metadata/MetadataTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\Component\Resource\Tests\Metadata;

use Doctrine\Inflector\InflectorFactory;
use Doctrine\Inflector\Rules\Patterns;
use Doctrine\Inflector\Rules\Ruleset;
use Doctrine\Inflector\Rules\Substitution;
use Doctrine\Inflector\Rules\Substitutions;
use Doctrine\Inflector\Rules\Transformations;
use Doctrine\Inflector\Rules\Word;
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\TestCase;
use Sylius\Component\Resource\Metadata\Metadata;

/**
* @psalm-suppress PropertyNotSetInConstructor Having some issues with custom PHPUnit annotations
*/
final class MetadataTest extends TestCase
{
/**
* @test
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function it_uses_a_custom_inflector(): void
{
$factory = InflectorFactory::create();
$factory->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());
}
}
6 changes: 4 additions & 2 deletions src/Component/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -51,7 +52,8 @@
},
"autoload-dev": {
"psr-4": {
"Sylius\\Component\\Resource\\spec\\": "spec/"
"Sylius\\Component\\Resource\\spec\\": "spec/",
"Sylius\\Component\\Resource\\Tests\\": "Tests"
}
}
}
12 changes: 12 additions & 0 deletions src/Component/phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.4/phpunit.xsd"
colors="true"
>
<testsuites>
<testsuite name="SyliusResource Test Suite">
<directory>./Tests/</directory>
</testsuite>
</testsuites>
</phpunit>

0 comments on commit 8b9520f

Please sign in to comment.