PageRenderTime 53ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/lithium-0.6/libraries/lithium/tests/cases/util/StringTest.php

https://github.com/fabpot/framework-benchs
PHP | 433 lines | 328 code | 54 blank | 51 comment | 8 complexity | 9380becd7093d9f7368ff1522b969c64 MD5 | raw file
  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2010, Union of RAD (http://union-of-rad.org)
  6. * @license http://opensource.org/licenses/bsd-license.php The BSD License
  7. */
  8. namespace lithium\tests\cases\util;
  9. use \lithium\util\String;
  10. use \lithium\net\http\Request;
  11. use \lithium\tests\mocks\util\MockStringObject;
  12. class StringTest extends \lithium\test\Unit {
  13. /**
  14. * testUuidGeneration method
  15. *
  16. * @return void
  17. */
  18. public function testUuidGeneration() {
  19. $result = String::uuid(new Request());
  20. $pattern = "/^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/";
  21. $this->assertPattern($pattern, $result);
  22. $result = String::uuid($_SERVER);
  23. $this->assertPattern($pattern, $result);
  24. }
  25. /**
  26. * testMultipleUuidGeneration method
  27. *
  28. * @return void
  29. */
  30. public function testMultipleUuidGeneration() {
  31. $check = array();
  32. $count = 500;
  33. $pattern = "/^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/";
  34. for ($i = 0; $i < $count; $i++) {
  35. $result = String::uuid($_SERVER);
  36. $match = preg_match($pattern, $result);
  37. $this->assertTrue($match);
  38. $this->assertFalse(in_array($result, $check));
  39. $check[] = $result;
  40. }
  41. }
  42. /**
  43. * Tests generating a UUID with seed data provided by an anonymous function.
  44. *
  45. * @return void
  46. */
  47. public function testGeneratingUuidWithCallback() {
  48. $pattern = "/^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/";
  49. $result = String::uuid(function($value) {
  50. if ($value == 'SERVER_ADDR') {
  51. return '::1';
  52. }
  53. });
  54. $this->assertPattern($pattern, $result);
  55. $result = String::uuid(function($value) {
  56. if ($value == 'HOST') {
  57. return '127.0.0.1';
  58. }
  59. });
  60. $this->assertPattern($pattern, $result);
  61. $result = String::uuid(function($value) {
  62. if ($value == 'SERVER_ADDR') {
  63. return '127.0.0.2';
  64. }
  65. });
  66. $this->assertPattern($pattern, $result);
  67. }
  68. /**
  69. * testHash method - Tests hash generation using `util\String::hash()`
  70. *
  71. * @return void
  72. */
  73. public function testHash() {
  74. $salt = 'Salt and pepper';
  75. $value = 'Lithium rocks!';
  76. $expected = sha1($value);
  77. $result = String::hash($value, 'sha1');
  78. $this->assertEqual($expected, $result);
  79. $result = String::hash($value);
  80. $this->assertEqual($expected, $result);
  81. $expected = sha1($salt . $value);
  82. $result = String::hash($value, 'sha1', $salt);
  83. $this->assertEqual($expected, $result);
  84. $expected = md5($value);
  85. $result = String::hash($value, 'md5');
  86. $this->assertEqual($expected, $result);
  87. $expected = md5($salt . $value);
  88. $result = String::hash($value, 'md5', $salt);
  89. $this->assertEqual($expected, $result);
  90. $sha256 = function($value) {
  91. if (function_exists('mhash')) {
  92. return bin2hex(mhash(MHASH_SHA256, $value));
  93. } elseif (function_exists('hash')) {
  94. return hash('sha256', $value);
  95. }
  96. throw new Exception();
  97. };
  98. try {
  99. $expected = $sha256($value);
  100. $result = String::hash($value, 'sha256');
  101. $this->assertEqual($expected, $result);
  102. $expected = $sha256($salt . $value);
  103. $result = String::hash($value, 'sha256', $salt);
  104. $this->assertEqual($expected, $result);
  105. } catch (Exception $e) {
  106. }
  107. }
  108. /**
  109. * testInsert method
  110. *
  111. * @return void
  112. */
  113. public function testInsert() {
  114. $string = '2 + 2 = {:sum}. Lithium is {:adjective}.';
  115. $expected = '2 + 2 = 4. Lithium is yummy.';
  116. $result = String::insert($string, array('sum' => '4', 'adjective' => 'yummy'));
  117. $this->assertEqual($expected, $result);
  118. $string = '2 + 2 = %sum. Lithium is %adjective.';
  119. $result = String::insert($string, array('sum' => '4', 'adjective' => 'yummy'), array(
  120. 'before' => '%', 'after' => ''
  121. ));
  122. $this->assertEqual($expected, $result);
  123. $string = '2 + 2 = 2sum2. Lithium is 9adjective9.';
  124. $result = String::insert($string, array('sum' => '4', 'adjective' => 'yummy'), array(
  125. 'format' => '/([\d])%s\\1/'
  126. ));
  127. $this->assertEqual($expected, $result);
  128. $string = '2 + 2 = 12sum21. Lithium is 23adjective45.';
  129. $expected = '2 + 2 = 4. Lithium is 23adjective45.';
  130. $result = String::insert($string, array('sum' => '4', 'adjective' => 'yummy'), array(
  131. 'format' => '/([\d])([\d])%s\\2\\1/'
  132. ));
  133. $this->assertEqual($expected, $result);
  134. $string = '{:web} {:web_site}';
  135. $expected = 'www http';
  136. $result = String::insert($string, array('web' => 'www', 'web_site' => 'http'));
  137. $this->assertEqual($expected, $result);
  138. $string = '2 + 2 = <sum. Lithium is <adjective>.';
  139. $expected = '2 + 2 = <sum. Lithium is yummy.';
  140. $result = String::insert($string, array('sum' => '4', 'adjective' => 'yummy'), array(
  141. 'before' => '<', 'after' => '>'
  142. ));
  143. $this->assertEqual($expected, $result);
  144. $string = '2 + 2 = \:sum. Lithium is :adjective.';
  145. $expected = '2 + 2 = :sum. Lithium is yummy.';
  146. $result = String::insert(
  147. $string,
  148. array('sum' => '4', 'adjective' => 'yummy'),
  149. array('before' => ':', 'after' => null, 'escape' => '\\')
  150. );
  151. $this->assertEqual($expected, $result);
  152. $string = '2 + 2 = !:sum. Lithium is :adjective.';
  153. $result = String::insert($string, array('sum' => '4', 'adjective' => 'yummy'), array(
  154. 'escape' => '!', 'before' => ':', 'after' => ''
  155. ));
  156. $this->assertEqual($expected, $result);
  157. $string = '2 + 2 = \%sum. Lithium is %adjective.';
  158. $expected = '2 + 2 = %sum. Lithium is yummy.';
  159. $result = String::insert($string, array('sum' => '4', 'adjective' => 'yummy'), array(
  160. 'before' => '%', 'after' => '', 'escape' => '\\'
  161. ));
  162. $this->assertEqual($expected, $result);
  163. $string = ':a :b \:a :a';
  164. $expected = '1 2 :a 1';
  165. $result = String::insert($string, array('a' => 1, 'b' => 2), array(
  166. 'before' => ':', 'after' => '', 'escape' => '\\'
  167. ));
  168. $this->assertEqual($expected, $result);
  169. $string = '{:a} {:b} {:c}';
  170. $expected = '2 3';
  171. $result = String::insert($string, array('b' => 2, 'c' => 3), array('clean' => true));
  172. $this->assertEqual($expected, $result);
  173. $string = '{:a} {:b} {:c}';
  174. $expected = '1 3';
  175. $result = String::insert($string, array('a' => 1, 'c' => 3), array('clean' => true));
  176. $this->assertEqual($expected, $result);
  177. $string = '{:a} {:b} {:c}';
  178. $expected = '2 3';
  179. $result = String::insert($string, array('b' => 2, 'c' => 3), array('clean' => true));
  180. $this->assertEqual($expected, $result);
  181. $string = ':a, :b and :c';
  182. $expected = '2 and 3';
  183. $result = String::insert($string, array('b' => 2, 'c' => 3), array(
  184. 'clean' => true, 'before' => ':', 'after' => ''
  185. ));
  186. $this->assertEqual($expected, $result);
  187. $string = '{:a}, {:b} and {:c}';
  188. $expected = '2 and 3';
  189. $result = String::insert($string, array('b' => 2, 'c' => 3), array('clean' => true));
  190. $this->assertEqual($expected, $result);
  191. $string = '"{:a}, {:b} and {:c}"';
  192. $expected = '"1, 2"';
  193. $result = String::insert($string, array('a' => 1, 'b' => 2), array('clean' => true));
  194. $this->assertEqual($expected, $result);
  195. $string = '"${a}, ${b} and ${c}"';
  196. $expected = '"1, 2"';
  197. $result = String::insert($string, array('a' => 1, 'b' => 2), array(
  198. 'before' => '${', 'after' => '}', 'clean' => true
  199. ));
  200. $this->assertEqual($expected, $result);
  201. $string = '<img src="{:src}" alt="{:alt}" class="foo {:extra} bar"/>';
  202. $expected = '<img src="foo" class="foo bar"/>';
  203. $result = String::insert($string, array('src' => 'foo'), array('clean' => 'html'));
  204. $this->assertEqual($expected, $result);
  205. $string = '<img src=":src" class=":no :extra"/>';
  206. $expected = '<img src="foo"/>';
  207. $result = String::insert($string, array('src' => 'foo'), array(
  208. 'clean' => 'html', 'before' => ':', 'after' => ''
  209. ));
  210. $this->assertEqual($expected, $result);
  211. $string = '<img src="{:src}" class="{:no} {:extra}"/>';
  212. $expected = '<img src="foo" class="bar"/>';
  213. $result = String::insert($string, array('src' => 'foo', 'extra' => 'bar'), array(
  214. 'clean' => 'html'
  215. ));
  216. $this->assertEqual($expected, $result);
  217. $result = String::insert("this is a ? string", "test");
  218. $expected = "this is a test string";
  219. $this->assertEqual($expected, $result);
  220. $result = String::insert("this is a ? string with a ? ? ?", array(
  221. 'long', 'few?', 'params', 'you know'
  222. ));
  223. $expected = "this is a long string with a few? params you know";
  224. $this->assertEqual($expected, $result);
  225. $result = String::insert(
  226. 'update saved_urls set url = :url where id = :id',
  227. array('url' => 'http://testurl.com/param1:url/param2:id', 'id' => 1),
  228. array('before' => ':', 'after' => '')
  229. );
  230. $expected = "update saved_urls set url = http://testurl.com/param1:url/param2:id ";
  231. $expected .= "where id = 1";
  232. $this->assertEqual($expected, $result);
  233. $result = String::insert(
  234. 'update saved_urls set url = :url where id = :id',
  235. array('id' => 1, 'url' => 'http://www.testurl.com/param1:url/param2:id'),
  236. array('before' => ':', 'after' => '')
  237. );
  238. $expected = "update saved_urls set url = http://www.testurl.com/param1:url/param2:id ";
  239. $expected .= "where id = 1";
  240. $this->assertEqual($expected, $result);
  241. $result = String::insert('{:me} lithium. {:subject} {:verb} fantastic.', array(
  242. 'me' => 'I :verb', 'subject' => 'lithium', 'verb' => 'is'
  243. ));
  244. $expected = "I :verb lithium. lithium is fantastic.";
  245. $this->assertEqual($expected, $result);
  246. $result = String::insert(':I.am: :not.yet: passing.', array('I.am' => 'We are'), array(
  247. 'before' => ':', 'after' => ':', 'clean' => array(
  248. 'replacement' => ' of course', 'method' => 'text'
  249. )
  250. ));
  251. $expected = "We are of course passing.";
  252. $this->assertEqual($expected, $result);
  253. $result = String::insert(
  254. ':I.am: :not.yet: passing.',
  255. array('I.am' => 'We are'),
  256. array('before' => ':', 'after' => ':', 'clean' => true)
  257. );
  258. $expected = "We are passing.";
  259. $this->assertEqual($expected, $result);
  260. $result = String::insert('?-pended result', array('Pre'));
  261. $expected = "Pre-pended result";
  262. $this->assertEqual($expected, $result);
  263. }
  264. /**
  265. * Tests casting/inserting of custom objects with `String::insert()`.
  266. *
  267. * @return void
  268. */
  269. public function testInsertWithObject() {
  270. $foo = new MockStringObject();
  271. $result = String::insert('This is a {:foo}', compact('foo'));
  272. $expected = 'This is a custom object';
  273. $this->assertEqual($expected, $result);
  274. }
  275. /**
  276. * test Clean Insert
  277. *
  278. * @return void
  279. */
  280. public function testCleanInsert() {
  281. $result = String::clean(':incomplete', array(
  282. 'clean' => true, 'before' => ':', 'after' => ''
  283. ));
  284. $this->assertEqual('', $result);
  285. $result = String::clean(':incomplete', array(
  286. 'clean' => array('method' => 'text', 'replacement' => 'complete'),
  287. 'before' => ':', 'after' => '')
  288. );
  289. $this->assertEqual('complete', $result);
  290. $result = String::clean(':in.complete', array(
  291. 'clean' => true, 'before' => ':', 'after' => ''
  292. ));
  293. $this->assertEqual('', $result);
  294. $result = String::clean(':in.complete and', array(
  295. 'clean' => true, 'before' => ':', 'after' => ''
  296. ));
  297. $this->assertEqual('', $result);
  298. $result = String::clean(':in.complete or stuff', array(
  299. 'clean' => true, 'before' => ':', 'after' => ''
  300. ));
  301. $this->assertEqual('stuff', $result);
  302. $result = String::clean(
  303. '<p class=":missing" id=":missing">Text here</p>',
  304. array('clean' => 'html', 'before' => ':', 'after' => '')
  305. );
  306. $this->assertEqual('<p>Text here</p>', $result);
  307. $string = ':a 2 3';
  308. $result = String::clean($string, array('clean' => true, 'before' => ':', 'after' => ''));
  309. $this->assertEqual('2 3', $result);
  310. $result = String::clean($string, array('clean' => false, 'before' => ':', 'after' => ''));
  311. $this->assertEqual($string, $result);
  312. }
  313. /**
  314. * testTokenize method
  315. *
  316. * @return void
  317. */
  318. public function testTokenize() {
  319. $result = String::tokenize('A,(short,boring test)');
  320. $expected = array('A', '(short,boring test)');
  321. $this->assertEqual($expected, $result);
  322. $result = String::tokenize('A,(short,more interesting( test)');
  323. $expected = array('A', '(short,more interesting( test)');
  324. $this->assertEqual($expected, $result);
  325. $result = String::tokenize('A,(short,very interesting( test))');
  326. $expected = array('A', '(short,very interesting( test))');
  327. $this->assertEqual($expected, $result);
  328. $result = String::tokenize('"single tag"', array(
  329. 'separator' => ' ', 'leftBound' => '"', 'rightBound' => '"'
  330. ));
  331. $expected = array('"single tag"');
  332. $this->assertEqual($expected, $result);
  333. $result = String::tokenize('tagA "single tag" tagB', array(
  334. 'separator' => ' ', 'leftBound' => '"', 'rightBound' => '"'
  335. ));
  336. $expected = array('tagA', '"single tag"', 'tagB');
  337. $this->assertEqual($expected, $result);
  338. $result = String::tokenize(array());
  339. $expected = array();
  340. $this->assertEqual($expected, $result);
  341. $result = String::tokenize(null);
  342. $expected = null;
  343. $this->assertEqual($expected, $result);
  344. }
  345. /**
  346. * Tests the `String::extract()` regex helper method.
  347. *
  348. * @return void
  349. */
  350. public function testStringExtraction() {
  351. $result = String::extract('/string/', 'whole string');
  352. $this->assertEqual('string', $result);
  353. $this->assertFalse(String::extract('/not/', 'whole string'));
  354. $this->assertEqual('part', String::extract('/\w+\s*(\w+)/', 'second part', 1));
  355. $this->assertNull(String::extract('/\w+\s*(\w+)/', 'second part', 2));
  356. }
  357. public function testStringInsertWithQuestionMark() {
  358. $result = String::insert('some string with a ?', array());
  359. $this->assertEqual('some string with a ?', $result);
  360. $result = String::insert('some {:param}string with a ?', array('param' => null));
  361. $this->assertEqual('some string with a ?', $result);
  362. }
  363. }
  364. ?>