inflector.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. <?php
  2. /**
  3. * Part of the Fuel framework.
  4. *
  5. * @package Fuel
  6. * @version 1.5
  7. * @author Fuel Development Team
  8. * @license MIT License
  9. * @copyright 2010 - 2013 Fuel Development Team
  10. * @link http://fuelphp.com
  11. */
  12. namespace Fuel\Core;
  13. /**
  14. * Inflector class tests
  15. *
  16. * @group Core
  17. * @group Inflector
  18. */
  19. class Test_Inflector extends TestCase
  20. {
  21. public function ordinalize_provider()
  22. {
  23. return array(
  24. array(1, 'st'),
  25. array(21, 'st'),
  26. array(2, 'nd'),
  27. array(22, 'nd'),
  28. array(3, 'rd'),
  29. array(23, 'rd'),
  30. array(4, 'th'),
  31. array(24, 'th'),
  32. array(111, 'th'),
  33. array(112, 'th'),
  34. array(113, 'th'),
  35. );
  36. }
  37. /**
  38. * Test for Inflector::ordinalize()
  39. *
  40. * @test
  41. * @dataProvider ordinalize_provider
  42. */
  43. public function test_ordinalize($number, $ending)
  44. {
  45. $this->assertEquals($number.$ending, Inflector::ordinalize($number));
  46. }
  47. /**
  48. * Test for Inflector::ordinalize()
  49. *
  50. * @test
  51. */
  52. public function test_ordinalize_of_string()
  53. {
  54. $this->assertEquals('Foo', Inflector::ordinalize('Foo'));
  55. }
  56. /**
  57. * Test for Inflector::ascii()
  58. *
  59. * @test
  60. */
  61. public function test_ascii()
  62. {
  63. $output = Inflector::ascii('Inglés');
  64. $expected = "Ingles";
  65. $this->assertEquals($expected, $output);
  66. }
  67. /**
  68. * Test for Inflector::camelize()
  69. *
  70. * @test
  71. */
  72. public function test_camelize()
  73. {
  74. $output = Inflector::camelize('apples_and_oranges');
  75. $expected = 'ApplesAndOranges';
  76. $this->assertEquals($expected, $output);
  77. }
  78. /**
  79. * Test for Inflector::classify()
  80. *
  81. * @test
  82. */
  83. public function test_classify()
  84. {
  85. $output = Inflector::classify('fuel_users');
  86. $expected = 'Fuel_User';
  87. $this->assertEquals($expected, $output);
  88. }
  89. /**
  90. * Test for Inflector::demodulize()
  91. *
  92. * @test
  93. */
  94. public function test_demodulize()
  95. {
  96. $output = Inflector::demodulize('Uri::main()');
  97. $expected = 'main()';
  98. $this->assertEquals($expected, $output);
  99. }
  100. /**
  101. * Test for Inflector::denamespace()
  102. *
  103. * @test
  104. */
  105. public function test_denamespace()
  106. {
  107. $this->assertEquals(Inflector::denamespace('Fuel\\SomeClass'), 'SomeClass');
  108. $this->assertEquals(Inflector::denamespace('\\SomeClass'), 'SomeClass');
  109. $this->assertEquals(Inflector::denamespace('SomeClass'), 'SomeClass');
  110. $this->assertEquals(Inflector::denamespace('SomeClass\\'), 'SomeClass');
  111. }
  112. /**
  113. * Test for Inflector::foreign_key()
  114. *
  115. * @test
  116. */
  117. public function test_foreign_key()
  118. {
  119. $output = Inflector::foreign_key('Inflector');
  120. $expected = 'inflector_id';
  121. $this->assertEquals($expected, $output);
  122. $output = Inflector::foreign_key('Inflector', false);
  123. $expected = 'inflectorid';
  124. $this->assertEquals($expected, $output);
  125. }
  126. /**
  127. * Test for Inflector::foreign_key()
  128. *
  129. * @test
  130. */
  131. public function test_foreign_key_with_model_prefx()
  132. {
  133. $this->assertEquals('inflector_id', Inflector::foreign_key('Model_Inflector'));
  134. }
  135. /**
  136. * Test for Inflector::friendly_title()
  137. *
  138. * @test
  139. */
  140. public function test_friendly_title()
  141. {
  142. $output = Inflector::friendly_title('Fuel is a community driven PHP 5 web framework.');
  143. $expected = 'Fuel-is-a-community-driven-PHP-5-web-framework';
  144. $this->assertEquals($expected, $output);
  145. }
  146. public function test_friendly_title_sep()
  147. {
  148. $output = Inflector::friendly_title('Fuel is a community driven PHP 5 web framework.', '_');
  149. $expected = 'Fuel_is_a_community_driven_PHP_5_web_framework';
  150. $this->assertEquals($expected, $output);
  151. }
  152. public function test_friendly_title_lowercase()
  153. {
  154. $output = Inflector::friendly_title('Fuel is a community driven PHP 5 web framework.', '-', true);
  155. $expected = 'fuel-is-a-community-driven-php-5-web-framework';
  156. $this->assertEquals($expected, $output);
  157. }
  158. public function test_friendly_title_non_ascii()
  159. {
  160. $output = Inflector::friendly_title('وقود هو مجتمع مدفوعة إطار شبكة الإنترنت');
  161. $expected = '';
  162. $this->assertEquals($expected, $output);
  163. }
  164. public function test_friendly_title_allow_non_ascii()
  165. {
  166. $output = Inflector::friendly_title('وقود هو مجتمع مدفوعة إطار شبكة الإنترنت', '-', false, true);
  167. $expected = 'وقود-هو-مجتمع-مدفوعة-إطار-شبكة-الإنترنت';
  168. $this->assertEquals($expected, $output);
  169. }
  170. /**
  171. * Test for Inflector::humanize()
  172. *
  173. * @test
  174. */
  175. public function test_humanize()
  176. {
  177. $output = Inflector::humanize('apples_and_oranges');
  178. $expected = 'Apples and oranges';
  179. $this->assertEquals($expected, $output);
  180. }
  181. /**
  182. * Test for Inflector::is_countable()
  183. *
  184. * @test
  185. */
  186. public function test_is_countable()
  187. {
  188. $output = Inflector::is_countable('fish');
  189. $this->assertFalse($output);
  190. $output = Inflector::is_countable('apple');
  191. $this->assertTrue($output);
  192. }
  193. /**
  194. * Test for Inflector::pluralize()
  195. *
  196. * @test
  197. */
  198. public function test_pluralize()
  199. {
  200. $output = Inflector::pluralize('apple');
  201. $expected = "apples";
  202. $this->assertEquals($expected, $output);
  203. $output = Inflector::pluralize('apple', 1);
  204. $expected = "apple";
  205. $this->assertEquals($expected, $output);
  206. }
  207. /**
  208. * Test for Inflector::pluralize()
  209. *
  210. * @test
  211. */
  212. public function test_pluralize_uncountable()
  213. {
  214. $this->assertEquals('equipment', Inflector::pluralize('equipment'));
  215. }
  216. /**
  217. * Test for Inflector::singularize()
  218. *
  219. * @test
  220. */
  221. public function test_singularize()
  222. {
  223. $output = Inflector::singularize('apples');
  224. $expected = "apple";
  225. $this->assertEquals($expected, $output);
  226. }
  227. /**
  228. * Test for Inflector::singularize()
  229. *
  230. * @test
  231. */
  232. public function test_singularize_uncountable()
  233. {
  234. $this->assertEquals('equipment', Inflector::singularize('equipment'));
  235. }
  236. public function tableize_provider()
  237. {
  238. return array(
  239. array('\\Model\\User', 'users'),
  240. array('\\Model\\Person', 'people'),
  241. array('\\Model\\Mouse', 'mice'),
  242. array('\\Model\\Ox', 'oxen'),
  243. array('\\Model\\Matrix', 'matrices'),
  244. array('Model_User', 'users'),
  245. );
  246. }
  247. /**
  248. * Test for Inflector::tableize()
  249. *
  250. * @test
  251. * @dataProvider tableize_provider
  252. */
  253. public function test_tableize($class, $table)
  254. {
  255. $this->assertEquals(Inflector::tableize($class), $table);
  256. }
  257. public function get_namespace_provider()
  258. {
  259. return array(
  260. array('\\Model\\User', 'Model\\'),
  261. array('\\Fuel\\Core\\Inflector', 'Fuel\\Core\\'),
  262. array('Model_User', ''),
  263. );
  264. }
  265. /**
  266. * Test for Inflector::get_namespace()
  267. *
  268. * @test
  269. * @dataProvider get_namespace_provider
  270. */
  271. public function test_get_namespace($class, $namespace)
  272. {
  273. $this->assertEquals(Inflector::get_namespace($class), $namespace);
  274. }
  275. /**
  276. * Test for Inflector::underscore()
  277. *
  278. * @test
  279. */
  280. public function test_underscore()
  281. {
  282. $output = Inflector::underscore('ApplesAndOranges');
  283. $expected = "apples_and_oranges";
  284. $this->assertEquals($expected, $output);
  285. }
  286. }