mirror of
https://github.com/doctrine/inflector.git
synced 2026-03-23 22:42:11 +01:00
154 lines
4.4 KiB
PHP
154 lines
4.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Doctrine\Tests\Inflector;
|
|
|
|
use Doctrine\Inflector\Inflector;
|
|
use Doctrine\Inflector\WordInflector;
|
|
use PHPUnit\Framework\Attributes\DataProvider;
|
|
use PHPUnit\Framework\MockObject\Stub;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
class InflectorTest extends TestCase
|
|
{
|
|
/** @var WordInflector&Stub */
|
|
private $singularInflector;
|
|
|
|
/** @var WordInflector&Stub */
|
|
private $pluralInflector;
|
|
|
|
/** @var Inflector */
|
|
private $inflector;
|
|
|
|
public function testTableize(): void
|
|
{
|
|
self::assertSame('model_name', $this->inflector->tableize('ModelName'));
|
|
self::assertSame('model_name', $this->inflector->tableize('modelName'));
|
|
self::assertSame('model_name', $this->inflector->tableize('model_name'));
|
|
}
|
|
|
|
public function testClassify(): void
|
|
{
|
|
self::assertSame('ModelName', $this->inflector->classify('model_name'));
|
|
self::assertSame('ModelName', $this->inflector->classify('modelName'));
|
|
self::assertSame('ModelName', $this->inflector->classify('ModelName'));
|
|
}
|
|
|
|
public function testCamelize(): void
|
|
{
|
|
self::assertSame('modelName', $this->inflector->camelize('ModelName'));
|
|
self::assertSame('modelName', $this->inflector->camelize('model_name'));
|
|
self::assertSame('modelName', $this->inflector->camelize('modelName'));
|
|
}
|
|
|
|
public function testCapitalize(): void
|
|
{
|
|
self::assertSame(
|
|
'Top-O-The-Morning To All_of_you!',
|
|
$this->inflector->capitalize('top-o-the-morning to all_of_you!')
|
|
);
|
|
}
|
|
|
|
public function testSeemsUtf8(): void
|
|
{
|
|
self::assertTrue($this->inflector->seemsUtf8('teléfono'));
|
|
self::assertTrue($this->inflector->seemsUtf8('král'));
|
|
self::assertTrue($this->inflector->seemsUtf8('telephone'));
|
|
}
|
|
|
|
public function testUnaccent(): void
|
|
{
|
|
self::assertSame('telefono', $this->inflector->unaccent('teléfono'));
|
|
self::assertSame('telephone', $this->inflector->unaccent('telephone'));
|
|
}
|
|
|
|
/** @dataProvider dataStringsUrlize */
|
|
#[DataProvider('dataStringsUrlize')]
|
|
public function testUrlize(string $expected, string $string): void
|
|
{
|
|
self::assertSame(
|
|
$expected,
|
|
$this->inflector->urlize($string)
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Strings which are used for testUrlize.
|
|
*
|
|
* @return string[][]
|
|
*/
|
|
public static function dataStringsUrlize(): array
|
|
{
|
|
return [
|
|
[
|
|
'testing-creating-a-slug-from-a-random-string',
|
|
'Testing_Creating a -Slug from a random-string!@#',
|
|
],
|
|
[
|
|
'contesta-el-telefono',
|
|
'Contesta el teléfono',
|
|
],
|
|
[
|
|
'den-hund-fuettern',
|
|
'den hund füttern',
|
|
],
|
|
[
|
|
'jsem-kral-na-severu',
|
|
'Jsem král na severu',
|
|
],
|
|
[
|
|
'test1-test2',
|
|
'test1::test2',
|
|
],
|
|
[
|
|
'test1-test2',
|
|
'test1$test2',
|
|
],
|
|
[
|
|
'testab-test2',
|
|
'TESTAb-test2',
|
|
],
|
|
[
|
|
'ano',
|
|
'año',
|
|
],
|
|
];
|
|
}
|
|
|
|
public function testPluralize(): void
|
|
{
|
|
$pluralInflector = $this->createMock(WordInflector::class);
|
|
|
|
$pluralInflector->expects(self::once())
|
|
->method('inflect')
|
|
->with('in')
|
|
->willReturn('out');
|
|
|
|
$inflector = new Inflector($this->singularInflector, $pluralInflector);
|
|
|
|
self::assertSame('out', $inflector->pluralize('in'));
|
|
}
|
|
|
|
public function testSingularize(): void
|
|
{
|
|
$singularInflector = $this->createMock(WordInflector::class);
|
|
|
|
$inflector = new Inflector($singularInflector, $this->pluralInflector);
|
|
$singularInflector->expects(self::once())
|
|
->method('inflect')
|
|
->with('in')
|
|
->willReturn('out');
|
|
|
|
self::assertSame('out', $inflector->singularize('in'));
|
|
}
|
|
|
|
protected function setUp(): void
|
|
{
|
|
$this->singularInflector = self::createStub(WordInflector::class);
|
|
$this->pluralInflector = self::createStub(WordInflector::class);
|
|
|
|
$this->inflector = new Inflector($this->singularInflector, $this->pluralInflector);
|
|
}
|
|
}
|