Fixing #138
[akelos.git] / test / unit / lib / AkInflector.php
blobf63fb908e6546938cae20ad7b121a1b8a93a1fd3
1 <?php
3 defined('AK_TEST_DATABASE_ON') ? null : define('AK_TEST_DATABASE_ON', true);
4 require_once(dirname(__FILE__).'/../../fixtures/config/config.php');
6 require_once(AK_LIB_DIR.DS.'AkInflector.php');
9 class Test_of_AkInflector extends UnitTestCase
11 var $SingularToPlural = array(
12 "search" => "searches",
13 "switch" => "switches",
14 "fix" => "fixes",
15 "box" => "boxes",
16 "process" => "processes",
17 "address" => "addresses",
18 "case" => "cases",
19 "stack" => "stacks",
20 "wish" => "wishes",
21 "fish" => "fish",
23 "category" => "categories",
24 "query" => "queries",
25 "ability" => "abilities",
26 "agency" => "agencies",
27 "movie" => "movies",
29 "archive" => "archives",
31 "index" => "indices",
33 "wife" => "wives",
34 "safe" => "saves",
35 "half" => "halves",
37 "move" => "moves",
39 "salesperson" => "salespeople",
40 "person" => "people",
42 "spokesman" => "spokesmen",
43 "man" => "men",
44 "woman" => "women",
46 "basis" => "bases",
47 "diagnosis" => "diagnoses",
49 "datum" => "data",
50 "medium" => "media",
51 "analysis" => "analyses",
53 "node_child" => "node_children",
54 "child" => "children",
56 "database" => "databases",
58 "experience" => "experiences",
59 "day" => "days",
61 "comment" => "comments",
62 "foobar" => "foobars",
63 "newsletter" => "newsletters",
65 "old_news" => "old_news",
66 "news" => "news",
68 "series" => "series",
69 "species" => "species",
71 "quiz" => "quizzes",
73 "perspective" => "perspectives",
75 "ox" => "oxen",
76 "photo" => "photos",
77 "buffalo" => "buffaloes",
78 "tomato" => "tomatoes",
79 "dwarf" => "dwarves",
80 "elf" => "elves",
81 "information" => "information",
82 "equipment" => "equipment",
83 "bus" => "buses",
84 "status" => "statuses",
85 "wax" => "waxes",
86 "mouse" => "mice",
88 "louse" => "lice",
89 "house" => "houses",
90 "octopus" => "octopi",
91 "virus" => "viri",
92 "alias" => "aliases",
93 "portfolio" => "portfolios",
95 "vertex" => "vertices",
96 "matrix" => "matrices",
98 "axis" => "axes",
99 "testis" => "testes",
100 "crisis" => "crises",
102 "rice" => "rice",
103 "shoe" => "shoes",
105 "horse" => "horses",
106 "prize" => "prizes",
107 "edge" => "edges"
110 var $CamelToUnderscore = array(
111 "Product" => "product",
112 "SpecialGuest" => "special_guest",
113 "ApplicationController" => "application_controller",
114 "Area51Controller" => "area51_controller",
117 var $CamelToUnderscoreWithoutReverse = array(
118 "HTMLTidy" => "html_tidy",
119 "HTMLTidyGenerator" => "html_tidy_generator",
120 "FreeBSD" => "free_bsd",
121 "HTML" => "html",
124 var $ClassNameToForeignKeyWithUnderscore = array(
125 "Person" => "person_id",
128 var $ClassNameToForeignKeyWithoutUnderscore = array(
129 "Person" => "personid",
132 var $ClassNameToTableName = array(
133 "PrimarySpokesman" => "primary_spokesmen",
134 "NodeChild" => "node_children"
137 var $UnderscoreToHuman = array(
138 "employee_salary" => "Employee salary",
139 "employee_id" => "Employee",
140 "underground" => "Underground"
143 var $MixtureToTitleCase = array(
144 'active_record' => 'Active Record',
145 'ActiveRecord' => 'Active Record',
146 'action web service' => 'Action Web Service',
147 'Action Web Service' => 'Action Web Service',
148 'Action web service' => 'Action Web Service',
149 'actionwebservice' => 'Actionwebservice',
150 'Actionwebservice' => 'Actionwebservice'
153 var $OrdinalNumbers = array(
154 "0" => "0th",
155 "1" => "1st",
156 "2" => "2nd",
157 "3" => "3rd",
158 "4" => "4th",
159 "5" => "5th",
160 "6" => "6th",
161 "7" => "7th",
162 "8" => "8th",
163 "9" => "9th",
164 "10" => "10th",
165 "11" => "11th",
166 "12" => "12th",
167 "13" => "13th",
168 "14" => "14th",
169 "20" => "20th",
170 "21" => "21st",
171 "22" => "22nd",
172 "23" => "23rd",
173 "24" => "24th",
174 "100" => "100th",
175 "101" => "101st",
176 "102" => "102nd",
177 "103" => "103rd",
178 "104" => "104th",
179 "110" => "110th",
180 "1000" => "1000th",
181 "1001" => "1001st"
184 function Test_of_pluralize_plurals()
186 $this->assertEqual('plurals', AkInflector::pluralize("plurals"));
187 $this->assertEqual('Plurals', AkInflector::pluralize("Plurals"));
190 function Test_of_pluralize_singular()
192 foreach ($this->SingularToPlural as $singular=>$plural){
193 $this->assertEqual($plural, AkInflector::pluralize($singular));
194 $this->assertEqual(ucfirst($plural), AkInflector::pluralize(ucfirst($singular)));
198 function Test_of_singularize_plural()
200 foreach ($this->SingularToPlural as $singular=>$plural){
201 $this->assertEqual($singular, AkInflector::singularize($plural));
202 $this->assertEqual(ucfirst($singular), AkInflector::singularize(ucfirst($plural)));
206 function Test_of_titleize()
208 foreach ($this->MixtureToTitleCase as $source=>$expected){
209 $this->assertEqual($expected, AkInflector::titleize($source));
213 function Test_of_camelize()
215 foreach ($this->CamelToUnderscore as $camel=>$underscore){
216 $this->assertEqual($camel, AkInflector::camelize($underscore));
220 function Test_of_underscore()
222 foreach ($this->CamelToUnderscore as $camel=>$underscore){
223 $this->assertEqual($underscore, AkInflector::underscore($camel));
226 foreach ($this->CamelToUnderscoreWithoutReverse as $camel=>$underscore){
227 $this->assertEqual($underscore, AkInflector::underscore($camel));
231 function Test_of_foreignKey()
233 foreach ($this->ClassNameToForeignKeyWithUnderscore as $class=>$foreign_key){
234 $this->assertEqual($foreign_key, AkInflector::foreignKey($class));
236 foreach ($this->ClassNameToForeignKeyWithoutUnderscore as $class=>$foreign_key){
237 $this->assertEqual($foreign_key, AkInflector::foreignKey($class, false));
242 function Test_of_tableize()
244 foreach ($this->ClassNameToTableName as $class_name=>$table_name){
245 $this->assertEqual($table_name, AkInflector::tableize($class_name));
249 function Test_of_classify()
251 foreach ($this->ClassNameToTableName as $class_name=>$table_name){
252 $this->assertEqual($class_name, AkInflector::classify($table_name));
256 function Test_of_humanize()
258 foreach ($this->UnderscoreToHuman as $underscore=>$human){
259 $this->assertEqual($human, AkInflector::humanize($underscore));
263 function Test_of_ordinalize()
265 foreach ($this->OrdinalNumbers as $number=>$ordinalized){
266 $this->assertEqual($ordinalized, AkInflector::ordinalize($number));
270 function Test_of_unnaccent()
272 $this->assertEqual( 'AAAAAAACEEEEIIIIDNOOOOOOUUUUYTsaaaaaaaceeeeiiiienoooooouuuuyty',
273 AkInflector::unaccent('ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ'));
276 function Test_for_setting_custom_plurals()
278 AkInflector::pluralize('camión', 'camiones');
279 $this->assertEqual(AkInflector::pluralize('camión'), 'camiones');
282 function Test_for_setting_custom_singulars()
284 AkInflector::singularize('camiones', 'camión');
285 $this->assertEqual(AkInflector::singularize('camiones'), 'camión');
288 function test_should_detect_singulars()
290 foreach (array_keys($this->SingularToPlural) as $singular){
291 $this->assertTrue(AkInflector::is_singular($singular), $singular.' is not detected as singular');
295 function test_should_detect_plurals()
297 foreach (array_values($this->SingularToPlural) as $plural){
298 $this->assertTrue(AkInflector::is_plural($plural), $plural.' is not detected as plural');
302 function test_should_demodulize()
304 $this->assertEqual(AkInflector::demodulize('admin/dashboard_controller'), 'dashboard_controller');
305 $this->assertEqual(AkInflector::demodulize('Admin_DashboardController'), 'DashboardController');
306 $this->assertEqual(AkInflector::demodulize('Admin::Dashboard'), 'Dashboard');
307 $this->assertEqual(AkInflector::demodulize('User'), 'User');
310 function test_should_get_controller_file_name()
312 $this->assertEqual(AkInflector::toControllerFilename('admin'), AK_CONTROLLERS_DIR.DS.'admin_controller.php');
313 $this->assertEqual(AkInflector::toControllerFilename('user_authentication'), AK_CONTROLLERS_DIR.DS.'user_authentication_controller.php');
314 $this->assertEqual(AkInflector::toControllerFilename('admin/users'), AK_CONTROLLERS_DIR.DS.'admin'.DS.'users_controller.php');
318 ak_test('Test_of_AkInflector');