InflectorTest.php 3.88 KB
Newer Older
Antonio Ramirez committed
1 2 3 4 5 6 7 8
<?php

namespace yiiunit\framework\helpers;

use Yii;
use yii\helpers\Inflector;
use yiiunit\TestCase;

9 10 11
/**
 * @group helpers
 */
Antonio Ramirez committed
12 13
class InflectorTest extends TestCase
{
Alexander Makarov committed
14 15
	public function testPluralize()
	{
Alexander Makarov committed
16
		$testData = [
Alexander Makarov committed
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
			'move' => 'moves',
			'foot' => 'feet',
			'child' => 'children',
			'human' => 'humans',
			'man' => 'men',
			'staff' => 'staff',
			'tooth' => 'teeth',
			'person' => 'people',
			'mouse' => 'mice',
			'touch' => 'touches',
			'hash' => 'hashes',
			'shelf' => 'shelves',
			'potato' => 'potatoes',
			'bus' => 'buses',
			'test' => 'tests',
			'car' => 'cars',
Alexander Makarov committed
33
		];
Alexander Makarov committed
34 35 36 37 38 39 40 41 42

		foreach ($testData as $testIn => $testOut) {
			$this->assertEquals($testOut, Inflector::pluralize($testIn));
			$this->assertEquals(ucfirst($testOut), ucfirst(Inflector::pluralize($testIn)));
		}
	}

	public function testSingularize()
	{
Alexander Makarov committed
43
		$testData = [
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
			'moves' => 'move',
			'feet' => 'foot',
			'children' => 'child',
			'humans' => 'human',
			'men' => 'man',
			'staff' => 'staff',
			'teeth' => 'tooth',
			'people' => 'person',
			'mice' => 'mouse',
			'touches' => 'touch',
			'hashes' => 'hash',
			'shelves' => 'shelf',
			'potatoes' => 'potato',
			'buses' => 'bus',
			'tests' => 'test',
			'cars' => 'car',
Alexander Makarov committed
60
		];
61
		foreach ($testData as $testIn => $testOut) {
Antonio Ramirez committed
62 63
			$this->assertEquals($testOut, Inflector::singularize($testIn));
			$this->assertEquals(ucfirst($testOut), ucfirst(Inflector::singularize($testIn)));
64
		}
Alexander Makarov committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
	}

	public function testTitleize()
	{
		$this->assertEquals("Me my self and i", Inflector::titleize('MeMySelfAndI'));
		$this->assertEquals("Me My Self And I", Inflector::titleize('MeMySelfAndI', true));
	}

	public function testCamelize()
	{
		$this->assertEquals("MeMySelfAndI", Inflector::camelize('me my_self-andI'));
	}

	public function testUnderscore()
	{
		$this->assertEquals("me_my_self_and_i", Inflector::underscore('MeMySelfAndI'));
	}

	public function testCamel2words()
	{
		$this->assertEquals('Camel Case', Inflector::camel2words('camelCase'));
		$this->assertEquals('Lower Case', Inflector::camel2words('lower_case'));
		$this->assertEquals('Tricky Stuff It Is Testing', Inflector::camel2words(' tricky_stuff.it-is testing... '));
	}

	public function testCamel2id()
	{
		$this->assertEquals('post-tag', Inflector::camel2id('PostTag'));
		$this->assertEquals('post_tag', Inflector::camel2id('PostTag', '_'));

		$this->assertEquals('post-tag', Inflector::camel2id('postTag'));
		$this->assertEquals('post_tag', Inflector::camel2id('postTag', '_'));
	}

	public function testId2camel()
	{
		$this->assertEquals('PostTag', Inflector::id2camel('post-tag'));
		$this->assertEquals('PostTag', Inflector::id2camel('post_tag', '_'));

		$this->assertEquals('PostTag', Inflector::id2camel('post-tag'));
		$this->assertEquals('PostTag', Inflector::id2camel('post_tag', '_'));
	}

	public function testHumanize()
	{
		$this->assertEquals("Me my self and i", Inflector::humanize('me_my_self_and_i'));
		$this->assertEquals("Me My Self And I", Inflector::humanize('me_my_self_and_i', true));
	}

	public function testVariablize()
	{
		$this->assertEquals("customerTable", Inflector::variablize('customer_table'));
	}

	public function testTableize()
	{
		$this->assertEquals("customer_tables", Inflector::tableize('customerTable'));
	}

	public function testSlug()
	{
		$this->assertEquals("this-is-a-title", Inflector::slug('this is a title'));
	}

	public function testClassify()
	{
		$this->assertEquals("CustomerTable", Inflector::classify('customer_tables'));
	}

	public function testOrdinalize()
	{
		$this->assertEquals('21st', Inflector::ordinalize('21'));
137 138 139 140 141 142
		$this->assertEquals('22nd', Inflector::ordinalize('22'));
		$this->assertEquals('23rd', Inflector::ordinalize('23'));
		$this->assertEquals('24th', Inflector::ordinalize('24'));
		$this->assertEquals('25th', Inflector::ordinalize('25'));
		$this->assertEquals('111th', Inflector::ordinalize('111'));
		$this->assertEquals('113th', Inflector::ordinalize('113'));
Alexander Makarov committed
143
	}
Antonio Ramirez committed
144
}