Skip to content

Commit

Permalink
Merge branch '1.4.x' into 2.0.x
Browse files Browse the repository at this point in the history
* 1.4.x:
  Fix handling of multiple rulesets
  • Loading branch information
alcaeus committed May 29, 2020
2 parents 3fc1712 + 4650c8b commit 9cf661f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 39 deletions.
4 changes: 0 additions & 4 deletions lib/Doctrine/Inflector/RulesetInflector.php
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
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

0 comments on commit 9cf661f

Please sign in to comment.