str.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. * Str class tests
  15. *
  16. * @group Core
  17. * @group Str
  18. */
  19. class Test_Str extends TestCase
  20. {
  21. public function truncate_provider()
  22. {
  23. return array(
  24. array(15, 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.'),
  25. );
  26. }
  27. /**
  28. * Test for Str::truncate()
  29. *
  30. * @test
  31. * @dataProvider truncate_provider
  32. */
  33. public function test_truncate_plain($limit, $string)
  34. {
  35. $output = Str::truncate($string, $limit);
  36. $expected = 'Lorem ipsum dol...';
  37. $this->assertEquals($expected, $output);
  38. }
  39. /**
  40. * Test for Str::truncate()
  41. *
  42. * @test
  43. * @dataProvider truncate_provider
  44. */
  45. public function test_truncate_custom_continuation($limit, $string)
  46. {
  47. $output = Str::truncate($string, $limit, '..');
  48. $expected = 'Lorem ipsum dol..';
  49. $this->assertEquals($expected, $output);
  50. }
  51. /**
  52. * Test for Str::truncate()
  53. *
  54. * @test
  55. * @dataProvider truncate_provider
  56. */
  57. public function test_truncate_not_html($limit, $string)
  58. {
  59. $string = '<h1>'.$string.'</h1>';
  60. $output = Str::truncate($string, $limit, '...', false);
  61. $expected = '<h1>Lorem ipsum...';
  62. $this->assertEquals($expected, $output);
  63. $output = Str::truncate($string, $limit, '...', true);
  64. $expected = '<h1>Lorem ipsum dol...</h1>';
  65. $this->assertEquals($expected, $output);
  66. }
  67. /**
  68. * Test for Str::truncate()
  69. *
  70. * @test
  71. * @dataProvider truncate_provider
  72. */
  73. public function test_truncate_is_html($limit, $string)
  74. {
  75. $string = '<h1>'.$string.'</h1>';
  76. $output = Str::truncate($string, $limit, '...', true);
  77. $expected = '<h1>Lorem ipsum dol...</h1>';
  78. $this->assertEquals($expected, $output);
  79. }
  80. /**
  81. * Test for Str::truncate()
  82. *
  83. * @test
  84. * @dataProvider truncate_provider
  85. */
  86. public function test_truncate_multiple_tags($limit, $string)
  87. {
  88. $limit = 400;
  89. $string = '<p><strong>'.$string.'</strong></p>';
  90. $output = Str::truncate($string, $limit, '...', true);
  91. $this->assertEquals($string, $output);
  92. }
  93. /**
  94. * Test for Str::increment()
  95. *
  96. * @test
  97. */
  98. public function test_increment()
  99. {
  100. $values = array('valueA', 'valueB', 'valueC');
  101. for ($i = 0; $i < count($values); $i ++)
  102. {
  103. $output = Str::increment($values[$i], $i);
  104. $expected = $values[$i].'_'.$i;
  105. $this->assertEquals($expected, $output);
  106. }
  107. }
  108. /**
  109. * Test for Str::lower()
  110. *
  111. * @test
  112. */
  113. public function test_lower()
  114. {
  115. $output = Str::lower('HELLO WORLD');
  116. $expected = "hello world";
  117. $this->assertEquals($expected, $output);
  118. }
  119. /**
  120. * Test for Str::upper()
  121. *
  122. * @test
  123. */
  124. public function test_upper()
  125. {
  126. $output = Str::upper('hello world');
  127. $expected = "HELLO WORLD";
  128. $this->assertEquals($expected, $output);
  129. }
  130. /**
  131. * Test for Str::lcfirst()
  132. *
  133. * @test
  134. */
  135. public function test_lcfirst()
  136. {
  137. $output = Str::lcfirst('Hello World');
  138. $expected = "hello World";
  139. $this->assertEquals($expected, $output);
  140. }
  141. /**
  142. * Test for Str::ucfirst()
  143. *
  144. * @test
  145. */
  146. public function test_ucfirst()
  147. {
  148. $output = Str::ucfirst('hello world');
  149. $expected = "Hello world";
  150. $this->assertEquals($expected, $output);
  151. }
  152. /**
  153. * Test for Str::ucwords()
  154. *
  155. * @test
  156. */
  157. public function test_ucwords()
  158. {
  159. $output = Str::ucwords('hello world');
  160. $expected = "Hello World";
  161. $this->assertEquals($expected, $output);
  162. }
  163. /**
  164. * Test for Str::random()
  165. *
  166. * @test
  167. */
  168. public function test_random()
  169. {
  170. // testing length
  171. $output = Str::random('alnum', 34);
  172. $this->assertEquals(34, strlen($output));
  173. // testing alnum
  174. $output = Str::random('alnum', 15);
  175. $this->assertTrue(ctype_alnum($output));
  176. // testing numeric
  177. $output = Str::random('numeric', 20);
  178. $this->assertTrue(ctype_digit($output));
  179. // testing alpha
  180. $output = Str::random('alpha', 35);
  181. $this->assertTrue(ctype_alpha($output));
  182. // testing nozero
  183. $output = Str::random('nozero', 22);
  184. $this->assertFalse(strpos($output, '0'));
  185. }
  186. /**
  187. * Test for Str::is_json()
  188. *
  189. * @test
  190. */
  191. public function test_is_json()
  192. {
  193. $values = array('fuelphp','is' => array('awesome' => true));
  194. $string = json_encode($values);
  195. $this->assertTrue(Str::is_json($string));
  196. $string = serialize($values);
  197. $this->assertFalse(Str::is_json($string));
  198. }
  199. /**
  200. * Test for Str::is_xml()
  201. *
  202. * @test
  203. * @requires extension libxml
  204. */
  205. public function test_is_xml()
  206. {
  207. $valid_xml = '<?xml version="1.0" encoding="UTF-8"?>
  208. <phpunit colors="true" stopOnFailure="false" bootstrap="bootstrap_phpunit.php">
  209. <php>
  210. <server name="doc_root" value="../../"/>
  211. <server name="app_path" value="fuel/app"/>
  212. <server name="core_path" value="fuel/core"/>
  213. <server name="package_path" value="fuel/packages"/>
  214. </php>
  215. </phpunit>';
  216. $invalid_xml = '<?xml version="1.0" encoding="UTF-8"?>
  217. <phpunit colors="true" stopOnFailure="false" bootstrap="bootstrap_phpunit.php">
  218. <php>
  219. <server name="doc_root" value="../../"/>
  220. <server name="app_path" value="fuel/app"/>
  221. <server name="core_path" value="fuel/core"/>
  222. <server name="package_path" value="fuel/packages"/>
  223. </
  224. </phpunit>';
  225. $this->assertTrue(Str::is_xml($valid_xml));
  226. $this->assertFalse(Str::is_xml($invalid_xml));
  227. }
  228. /**
  229. * Test for Str::is_serialized()
  230. *
  231. * @test
  232. */
  233. public function test_is_serialized()
  234. {
  235. $values = array('fuelphp','is' => array('awesome' => true));
  236. $string = json_encode($values);
  237. $this->assertFalse(Str::is_serialized($string));
  238. $string = serialize($values);
  239. $this->assertTrue(Str::is_serialized($string));
  240. }
  241. /**
  242. * Test for Str::is_html()
  243. *
  244. * @test
  245. */
  246. public function test_is_html()
  247. {
  248. $html = '<div class="row"><div class="span12"><strong>FuelPHP</strong> is a simple, flexible, <i>community<i> driven PHP 5.3 web framework based on the best ideas of other frameworks with a fresh start.</p>';
  249. $simple_string = strip_tags($html);
  250. $this->assertTrue(Str::is_html($html));
  251. $this->assertFalse(Str::is_html($simple_string));
  252. }
  253. /**
  254. * Test for Str::starts_with()
  255. *
  256. * @test
  257. */
  258. public function test_starts_with()
  259. {
  260. $string = 'HELLO WORLD';
  261. $output = Str::starts_with($string, 'HELLO');
  262. $this->assertTrue($output);
  263. $output = Str::starts_with($string, 'hello');
  264. $this->assertFalse($output);
  265. $output = Str::starts_with($string, 'hello', true);
  266. $this->assertTrue($output);
  267. }
  268. /**
  269. * Test for Str::ends_with()
  270. *
  271. * @test
  272. */
  273. public function test_ends_with()
  274. {
  275. $string = 'HELLO WORLD';
  276. $output = Str::ends_with($string, 'WORLD');
  277. $this->assertTrue($output);
  278. $output = Str::ends_with($string, 'world');
  279. $this->assertFalse($output);
  280. $output = Str::ends_with($string, 'world', true);
  281. $this->assertTrue($output);
  282. }
  283. /**
  284. * Test for Str::alternator()
  285. *
  286. * @test
  287. */
  288. public function test_alternator()
  289. {
  290. $alt = Str::alternator('one', 'two', 'three');
  291. $output = $alt();
  292. $expected = 'one';
  293. $this->assertEquals($output, $expected);
  294. $output = $alt(false);
  295. $expected = 'two';
  296. $this->assertEquals($output, $expected);
  297. $output = $alt();
  298. $expected = 'two';
  299. $this->assertEquals($output, $expected);
  300. $output = $alt();
  301. $expected = 'three';
  302. $this->assertEquals($output, $expected);
  303. $output = $alt();
  304. $expected = 'one';
  305. $this->assertEquals($output, $expected);
  306. }
  307. }