Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix handling of multiple rulesets #162

Merged
merged 1 commit into from
May 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions lib/Doctrine/Inflector/RulesetInflector.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,13 @@ public function inflect(string $word) : string
if ($ruleset->getUninflected()->matches($word)) {
return $word;
}
}

foreach ($this->rulesets as $ruleset) {
$inflected = $ruleset->getIrregular()->inflect($word);

if ($inflected !== $word) {
return $inflected;
}
}

foreach ($this->rulesets as $ruleset) {
$inflected = $ruleset->getRegular()->inflect($word);

if ($inflected !== $word) {
Expand Down
56 changes: 21 additions & 35 deletions tests/Doctrine/Tests/Inflector/RulesetInflectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@

namespace Doctrine\Tests\Inflector;

use Doctrine\Inflector\Rules\Pattern;
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 Doctrine\Inflector\RulesetInflector;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -49,28 +52,21 @@ public function testInflectIrregularUsesFirstMatch() : void

public function testInflectIrregularContinuesIfFirstRulesetReturnsOriginalValue() : void
{
$firstIrregular = $this->createMock(Substitutions::class);
$secondIrregular = $this->createMock(Substitutions::class);
$firstRuleset = new Ruleset(
new Transformations(),
new Patterns(),
new Substitutions()
);

$this->firstRuleset
->method('getIrregular')
->willReturn($firstIrregular);

$this->secondRuleset
->method('getIrregular')
->willReturn($secondIrregular);
$secondRuleset = new Ruleset(
new Transformations(),
new Patterns(),
new Substitutions(new Substitution(new Word('in'), new Word('second')))
);

$firstIrregular->expects(self::once())
->method('inflect')
->with('in')
->willReturn('in');

$secondIrregular->expects(self::once())
->method('inflect')
->with('in')
->willReturn('second');
$inflector = new RulesetInflector($firstRuleset, $secondRuleset);

self::assertSame('second', $this->rulesetInflector->inflect('in'));
self::assertSame('second', $inflector->inflect('in'));
}

public function testInflectUninflectedSkipsOnFirstMatch() : void
Expand All @@ -97,30 +93,20 @@ public function testInflectUninflectedSkipsOnFirstMatch() : void
self::assertSame('in', $this->rulesetInflector->inflect('in'));
}

public function testInflectUninflectedContinuesIfFirstRulesetDoesNotIgnore() : void
public function testIrregularIsInflectedEvenIfLaterRulesetIgnores() : void
{
$firstUninflected = $this->createMock(Patterns::class);
$secondUninflected = $this->createMock(Patterns::class);
$firstIrregular = new Substitutions(new Substitution(new Word('travel'), new Word('travels')));
$secondUninflected = new Patterns(new Pattern('travel'));

$this->firstRuleset
->method('getUninflected')
->willReturn($firstUninflected);
->method('getIrregular')
->willReturn($firstIrregular);

$this->secondRuleset
->method('getUninflected')
->willReturn($secondUninflected);

$firstUninflected->expects(self::once())
->method('matches')
->with('in')
->willReturn(false);

$secondUninflected->expects(self::once())
->method('matches')
->with('in')
->willReturn(true);

self::assertSame('in', $this->rulesetInflector->inflect('in'));
self::assertSame('travels', $this->rulesetInflector->inflect('travel'));
}

public function testInflectRegularUsesFirstMatch() : void
Expand Down