Skip to content

Test consolidation #37

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

Open
wants to merge 10 commits into
base: 2.0-dev
Choose a base branch
from
147 changes: 73 additions & 74 deletions Tests/InflectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
/**
* @copyright Copyright (C) 2005 - 2021 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*
* @noinspection PhpDeprecationInspection
* @noinspection SpellCheckingInspection
*/

namespace Joomla\String\Tests;
Expand All @@ -25,7 +28,7 @@ class InflectorTest extends TestCase
protected $inflector;

/**
* Method to seed data to testIsCountable.
* Seed data to testIsCountable.
*
* @return \Generator
*/
Expand All @@ -36,7 +39,7 @@ public function seedIsCountable(): \Generator
}

/**
* Method to seed data to testToPlural.
* Seed data to testToPlural.
*
* @return \Generator
*
Expand Down Expand Up @@ -103,48 +106,56 @@ protected function tearDown(): void
}

/**
* @testdox A rule cannot be added to the inflector if it is of an unsupported type
* @testdox A single word can be added to the inflector countable rules
*/
public function testAddRuleException()
public function testAddCountableWord(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->assertFalse(
$this->inflector->isCountable('foo'),
'"foo" should not be known to the inflector by default.'
);

$this->inflector->addCountableRule('foo');

/** @noinspection PhpParamsInspection */
TestHelper::invoke($this->inflector, 'addRule', new \stdClass, 'singular');
$this->assertTrue(
$this->inflector->isCountable('foo'),
'"foo" should be known to the inflector after being set explicitely.'
);
}

/**
* @testdox A countable rule can be added to the inflector
* @testdox An array of words can be added to the inflector countable rules
*/
public function testAddCountableRule()
public function testAddCountableArray(): void
{
// Add string.
$this->inflector->addCountableRule('foo');

$countable = TestHelper::getValue($this->inflector, 'countable');
$this->assertFalse(
$this->inflector->isCountable('goo'),
'"goo" should not be known to the inflector by default.'
);

$this->assertContains(
'foo',
$countable['rules'],
'Checks a countable rule was added.'
$this->assertFalse(
$this->inflector->isCountable('car'),
'"car" should not be known to the inflector by default.'
);

// Add array.
$this->inflector->addCountableRule(array('goo', 'car'));
$this->inflector->addCountableRule(['goo', 'car']);

$countable = TestHelper::getValue($this->inflector, 'countable');
$this->assertTrue(
$this->inflector->isCountable('goo'),
'"goo" should be known to the inflector after being set explicitely.'
);

$this->assertContains(
'car',
$countable['rules'],
'Checks a countable rule was added by array.'
$this->assertTrue(
$this->inflector->isCountable('car'),
'"car" should be known to the inflector after being set explicitely.'
);
}

/**
* @testdox A word can be added to the inflector without a plural form
* @throws \ReflectionException
*/
public function testAddWordWithoutPlural()
public function testAddWordWithoutPlural(): void
{
if (!$this->checkInflectorImplementation($this->inflector))
{
Expand All @@ -158,21 +169,24 @@ public function testAddWordWithoutPlural()

$plural = TestHelper::getValue(DoctrineInflector::class, 'plural');

$this->assertTrue(
in_array('foo', $plural['uninflected'])
$this->assertContains(
'foo',
$plural['uninflected']
);

$singular = TestHelper::getValue(DoctrineInflector::class, 'singular');

$this->assertTrue(
in_array('foo', $singular['uninflected'])
$this->assertContains(
'foo',
$singular['uninflected']
);
}

/**
* @testdox A word can be added to the inflector with a plural form
* @throws \ReflectionException
*/
public function testAddWordWithPlural()
public function testAddWordWithPlural(): void
{
if (!$this->checkInflectorImplementation($this->inflector))
{
Expand Down Expand Up @@ -202,57 +216,41 @@ public function testAddWordWithPlural()
/**
* @testdox A pluralisation rule can be added to the inflector
*/
public function testAddPluraliseRule()
public function testAddPluraliseRule(): void
{
if (!$this->checkInflectorImplementation($this->inflector))
{
$this->markTestSkipped('This test depends on the library\'s implementation');
}

$this->assertSame(
$this->inflector->addPluraliseRule(['/^(custom)$/i' => '\1izables']),
$this->inflector,
'Checks chaining.'
);
$this->inflector::rules('plural', ['/^(custom)$/i' => '\1izables']);

$plural = TestHelper::getValue(DoctrineInflector::class, 'plural');

$this->assertArrayHasKey(
'/^(custom)$/i',
$plural['rules'],
'Checks a pluralisation rule was added.'
$this->assertEquals(
'customizables',
$this->inflector::pluralize('custom'),
'"custom" should become "customizables" after being set explicitely.'
);
}

/**
* @testdox A singularisation rule can be added to the inflector
*/
public function testAddSingulariseRule()
public function testAddSingulariseRule(): void
{
if (!$this->checkInflectorImplementation($this->inflector))
{
$this->markTestSkipped('This test depends on the library\'s implementation');
}
$this->inflector::rules('singular', ['/^(inflec|contribu)tors$/i' => '\1ta']);

$this->assertSame(
$this->inflector->addSingulariseRule(['/^(inflec|contribu)tors$/i' => '\1ta']),
$this->inflector,
'Checks chaining.'
$this->assertEquals(
'inflecta',
$this->inflector::singularize('inflectors'),
'"inflectors" should become "inflecta" after being set explicitely.'
);

$singular = TestHelper::getValue(DoctrineInflector::class, 'singular');

$this->assertArrayHasKey(
'/^(inflec|contribu)tors$/i',
$singular['rules'],
'Checks a singularisation rule was added.'
$this->assertEquals(
'contributa',
$this->inflector::singularize('contributors'),
'"contributors" should become "contributa" after being set explicitely.'
);
}

/**
* @testdox The singleton instance of the inflector can be retrieved
*/
public function testGetInstance()
public function testGetInstance(): void
{
$this->assertInstanceOf(
Inflector::class,
Expand All @@ -268,14 +266,14 @@ public function testGetInstance()
}

/**
* @testdox A string is checked to determine if it a countable word
* @testdox A string is checked to determine if it is a countable word
*
* @param string $input A string.
* @param boolean $expected The expected result of the function call.
*
* @dataProvider seedIsCountable
*/
public function testIsCountable(string $input, bool $expected)
public function testIsCountable(string $input, bool $expected): void
{
$this->assertEquals(
$expected,
Expand All @@ -291,9 +289,10 @@ public function testIsCountable(string $input, bool $expected)
*
* @dataProvider seedSinglePlural
*/
public function testIsPlural(string $singular, string $plural)
public function testIsPlural(string $singular, string $plural): void
{
if ($singular === 'bus' && !$this->checkInflectorImplementation($this->inflector)) {
if ($singular === 'bus' && !$this->checkInflectorImplementation($this->inflector))
{
$this->markTestSkipped('"bus/buses" is not known to the new implementation');
}

Expand All @@ -319,7 +318,7 @@ public function testIsPlural(string $singular, string $plural)
*
* @dataProvider seedSinglePlural
*/
public function testIsSingular(string $singular, string $plural)
public function testIsSingular(string $singular, string $plural): void
{
if ($singular === 'bus' && !$this->checkInflectorImplementation($this->inflector))
{
Expand Down Expand Up @@ -348,7 +347,7 @@ public function testIsSingular(string $singular, string $plural)
*
* @dataProvider seedSinglePlural
*/
public function testToPlural(string $singular, string $plural)
public function testToPlural(string $singular, string $plural): void
{
$this->assertSame(
$plural,
Expand All @@ -358,9 +357,9 @@ public function testToPlural(string $singular, string $plural)
}

/**
* @testdox A string that is already plural is returned in the same form
* @testdox A string that is already plural is returned unchanged
*/
public function testToPluralAlreadyPlural()
public function testToPluralAlreadyPlural(): void
{
$this->assertSame(
'buses',
Expand All @@ -377,7 +376,7 @@ public function testToPluralAlreadyPlural()
*
* @dataProvider seedSinglePlural
*/
public function testToSingular(string $singular, string $plural)
public function testToSingular(string $singular, string $plural): void
{
$this->assertSame(
$singular,
Expand All @@ -387,9 +386,9 @@ public function testToSingular(string $singular, string $plural)
}

/**
* @testdox A string that is already singular is returned in the same form
* @testdox A string that is already singular is returned unchanged
*/
public function testToSingularAlreadySingular()
public function testToSingularAlreadySingular(): void
{
if (!$this->checkInflectorImplementation($this->inflector))
{
Expand Down
Loading