PageRenderTime 59ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/Cake/Test/Case/Utility/SetTest.php

https://bitbucket.org/udeshika/fake_twitter
PHP | 3140 lines | 2564 code | 367 blank | 209 comment | 1 complexity | 572f529ed08ee1f1af4bb4f5204bbd12 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. * SetTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
  8. * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice
  12. *
  13. * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
  15. * @package Cake.Test.Case.Utility
  16. * @since CakePHP(tm) v 1.2.0.4206
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('Set', 'Utility');
  20. App::uses('Model', 'Model');
  21. /**
  22. * SetTest class
  23. *
  24. * @package Cake.Test.Case.Utility
  25. */
  26. class SetTest extends CakeTestCase {
  27. /**
  28. * testNumericKeyExtraction method
  29. *
  30. * @return void
  31. */
  32. public function testNumericKeyExtraction() {
  33. $data = array('plugin' => null, 'controller' => '', 'action' => '', 1, 'whatever');
  34. $this->assertEquals(Set::extract($data, '{n}'), array(1, 'whatever'));
  35. $this->assertEquals(Set::diff($data, Set::extract($data, '{n}')), array('plugin' => null, 'controller' => '', 'action' => ''));
  36. }
  37. /**
  38. * testEnum method
  39. *
  40. * @return void
  41. */
  42. public function testEnum() {
  43. $result = Set::enum(1, 'one, two');
  44. $this->assertEquals($result, 'two');
  45. $result = Set::enum(2, 'one, two');
  46. $this->assertNull($result);
  47. $set = array('one', 'two');
  48. $result = Set::enum(0, $set);
  49. $this->assertEquals($result, 'one');
  50. $result = Set::enum(1, $set);
  51. $this->assertEquals($result, 'two');
  52. $result = Set::enum(1, array('one', 'two'));
  53. $this->assertEquals($result, 'two');
  54. $result = Set::enum(2, array('one', 'two'));
  55. $this->assertNull($result);
  56. $result = Set::enum('first', array('first' => 'one', 'second' => 'two'));
  57. $this->assertEquals($result, 'one');
  58. $result = Set::enum('third', array('first' => 'one', 'second' => 'two'));
  59. $this->assertNull($result);
  60. $result = Set::enum('no', array('no' => 0, 'yes' => 1));
  61. $this->assertEquals($result, 0);
  62. $result = Set::enum('not sure', array('no' => 0, 'yes' => 1));
  63. $this->assertNull($result);
  64. $result = Set::enum(0);
  65. $this->assertEquals($result, 'no');
  66. $result = Set::enum(1);
  67. $this->assertEquals($result, 'yes');
  68. $result = Set::enum(2);
  69. $this->assertNull($result);
  70. }
  71. /**
  72. * testFilter method
  73. *
  74. * @return void
  75. */
  76. public function testFilter() {
  77. $result = Set::filter(array('0', false, true, 0, array('one thing', 'I can tell you', 'is you got to be', false)));
  78. $expected = array('0', 2 => true, 3 => 0, 4 => array('one thing', 'I can tell you', 'is you got to be'));
  79. $this->assertSame($expected, $result);
  80. $result = Set::filter(array(1, array(false)));
  81. $expected = array(1);
  82. $this->assertEquals($expected, $result);
  83. $result = Set::filter(array(1, array(false, false)));
  84. $expected = array(1);
  85. $this->assertEquals($expected, $result);
  86. $result = Set::filter(array(1, array('empty', false)));
  87. $expected = array(1, array('empty'));
  88. $this->assertEquals($expected, $result);
  89. $result = Set::filter(array(1, array('2', false, array(3, null))));
  90. $expected = array(1, array('2', 2 => array(3)));
  91. $this->assertEquals($expected, $result);
  92. $this->assertSame(array(), Set::filter(array()));
  93. }
  94. /**
  95. * testNumericArrayCheck method
  96. *
  97. * @return void
  98. */
  99. public function testNumericArrayCheck() {
  100. $data = array('one');
  101. $this->assertTrue(Set::numeric(array_keys($data)));
  102. $data = array(1 => 'one');
  103. $this->assertFalse(Set::numeric($data));
  104. $data = array('one');
  105. $this->assertFalse(Set::numeric($data));
  106. $data = array('one' => 'two');
  107. $this->assertFalse(Set::numeric($data));
  108. $data = array('one' => 1);
  109. $this->assertTrue(Set::numeric($data));
  110. $data = array(0);
  111. $this->assertTrue(Set::numeric($data));
  112. $data = array('one', 'two', 'three', 'four', 'five');
  113. $this->assertTrue(Set::numeric(array_keys($data)));
  114. $data = array(1 => 'one', 2 => 'two', 3 => 'three', 4 => 'four', 5 => 'five');
  115. $this->assertTrue(Set::numeric(array_keys($data)));
  116. $data = array('1' => 'one', 2 => 'two', 3 => 'three', 4 => 'four', 5 => 'five');
  117. $this->assertTrue(Set::numeric(array_keys($data)));
  118. $data = array('one', 2 => 'two', 3 => 'three', 4 => 'four', 'a' => 'five');
  119. $this->assertFalse(Set::numeric(array_keys($data)));
  120. }
  121. /**
  122. * testKeyCheck method
  123. *
  124. * @return void
  125. */
  126. public function testKeyCheck() {
  127. $data = array('Multi' => array('dimensonal' => array('array')));
  128. $this->assertTrue(Set::check($data, 'Multi.dimensonal'));
  129. $this->assertFalse(Set::check($data, 'Multi.dimensonal.array'));
  130. $data = array(
  131. array(
  132. 'Article' => array('id' => '1', 'user_id' => '1', 'title' => 'First Article', 'body' => 'First Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'),
  133. 'User' => array('id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'),
  134. 'Comment' => array(
  135. array('id' => '1', 'article_id' => '1', 'user_id' => '2', 'comment' => 'First Comment for First Article', 'published' => 'Y', 'created' => '2007-03-18 10:45:23', 'updated' => '2007-03-18 10:47:31'),
  136. array('id' => '2', 'article_id' => '1', 'user_id' => '4', 'comment' => 'Second Comment for First Article', 'published' => 'Y', 'created' => '2007-03-18 10:47:23', 'updated' => '2007-03-18 10:49:31'),
  137. ),
  138. 'Tag' => array(
  139. array('id' => '1', 'tag' => 'tag1', 'created' => '2007-03-18 12:22:23', 'updated' => '2007-03-18 12:24:31'),
  140. array('id' => '2', 'tag' => 'tag2', 'created' => '2007-03-18 12:24:23', 'updated' => '2007-03-18 12:26:31')
  141. )
  142. ),
  143. array(
  144. 'Article' => array('id' => '3', 'user_id' => '1', 'title' => 'Third Article', 'body' => 'Third Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:43:23', 'updated' => '2007-03-18 10:45:31'),
  145. 'User' => array('id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'),
  146. 'Comment' => array(),
  147. 'Tag' => array()
  148. )
  149. );
  150. $this->assertTrue(Set::check($data, '0.Article.user_id'));
  151. $this->assertTrue(Set::check($data, '0.Comment.0.id'));
  152. $this->assertFalse(Set::check($data, '0.Comment.0.id.0'));
  153. $this->assertTrue(Set::check($data, '0.Article.user_id'));
  154. $this->assertFalse(Set::check($data, '0.Article.user_id.a'));
  155. }
  156. /**
  157. * testMerge method
  158. *
  159. * @return void
  160. */
  161. public function testMerge() {
  162. $r = Set::merge(array('foo'));
  163. $this->assertEquals($r, array('foo'));
  164. $r = Set::merge('foo');
  165. $this->assertEquals($r, array('foo'));
  166. $r = Set::merge('foo', 'bar');
  167. $this->assertEquals($r, array('foo', 'bar'));
  168. if (substr(PHP_VERSION, 0, 1) >= 5) {
  169. $r = eval('class StaticSetCaller{static function merge($a, $b){return Set::merge($a, $b);}} return StaticSetCaller::merge("foo", "bar");');
  170. $this->assertEquals($r, array('foo', 'bar'));
  171. }
  172. $r = Set::merge('foo', array('user' => 'bob', 'no-bar'), 'bar');
  173. $this->assertEquals($r, array('foo', 'user' => 'bob', 'no-bar', 'bar'));
  174. $a = array('foo', 'foo2');
  175. $b = array('bar', 'bar2');
  176. $this->assertEquals(Set::merge($a, $b), array('foo', 'foo2', 'bar', 'bar2'));
  177. $a = array('foo' => 'bar', 'bar' => 'foo');
  178. $b = array('foo' => 'no-bar', 'bar' => 'no-foo');
  179. $this->assertEquals(Set::merge($a, $b), array('foo' => 'no-bar', 'bar' => 'no-foo'));
  180. $a = array('users' => array('bob', 'jim'));
  181. $b = array('users' => array('lisa', 'tina'));
  182. $this->assertEquals(Set::merge($a, $b), array('users' => array('bob', 'jim', 'lisa', 'tina')));
  183. $a = array('users' => array('jim', 'bob'));
  184. $b = array('users' => 'none');
  185. $this->assertEquals(Set::merge($a, $b), array('users' => 'none'));
  186. $a = array('users' => array('lisa' => array('id' => 5, 'pw' => 'secret')), 'cakephp');
  187. $b = array('users' => array('lisa' => array('pw' => 'new-pass', 'age' => 23)), 'ice-cream');
  188. $this->assertEquals(Set::merge($a, $b), array('users' => array('lisa' => array('id' => 5, 'pw' => 'new-pass', 'age' => 23)), 'cakephp', 'ice-cream'));
  189. $c = array('users' => array('lisa' => array('pw' => 'you-will-never-guess', 'age' => 25, 'pet' => 'dog')), 'chocolate');
  190. $expected = array('users' => array('lisa' => array('id' => 5, 'pw' => 'you-will-never-guess', 'age' => 25, 'pet' => 'dog')), 'cakephp', 'ice-cream', 'chocolate');
  191. $this->assertEquals(Set::merge($a, $b, $c), $expected);
  192. $this->assertEquals(Set::merge($a, $b, array(), $c), $expected);
  193. $r = Set::merge($a, $b, $c);
  194. $this->assertEquals($r, $expected);
  195. $a = array('Tree', 'CounterCache',
  196. 'Upload' => array('folder' => 'products',
  197. 'fields' => array('image_1_id', 'image_2_id', 'image_3_id', 'image_4_id', 'image_5_id')));
  198. $b = array('Cacheable' => array('enabled' => false),
  199. 'Limit',
  200. 'Bindable',
  201. 'Validator',
  202. 'Transactional');
  203. $expected = array('Tree', 'CounterCache',
  204. 'Upload' => array('folder' => 'products',
  205. 'fields' => array('image_1_id', 'image_2_id', 'image_3_id', 'image_4_id', 'image_5_id')),
  206. 'Cacheable' => array('enabled' => false),
  207. 'Limit',
  208. 'Bindable',
  209. 'Validator',
  210. 'Transactional');
  211. $this->assertEquals(Set::merge($a, $b), $expected);
  212. $expected = array('Tree' => null, 'CounterCache' => null,
  213. 'Upload' => array('folder' => 'products',
  214. 'fields' => array('image_1_id', 'image_2_id', 'image_3_id', 'image_4_id', 'image_5_id')),
  215. 'Cacheable' => array('enabled' => false),
  216. 'Limit' => null,
  217. 'Bindable' => null,
  218. 'Validator' => null,
  219. 'Transactional' => null);
  220. $this->assertEquals(Set::normalize(Set::merge($a, $b)), $expected);
  221. }
  222. /**
  223. * testSort method
  224. *
  225. * @return void
  226. */
  227. public function testSort() {
  228. $a = array(
  229. 0 => array('Person' => array('name' => 'Jeff'), 'Friend' => array(array('name' => 'Nate'))),
  230. 1 => array('Person' => array('name' => 'Tracy'),'Friend' => array(array('name' => 'Lindsay')))
  231. );
  232. $b = array(
  233. 0 => array('Person' => array('name' => 'Tracy'),'Friend' => array(array('name' => 'Lindsay'))),
  234. 1 => array('Person' => array('name' => 'Jeff'), 'Friend' => array(array('name' => 'Nate')))
  235. );
  236. $a = Set::sort($a, '{n}.Friend.{n}.name', 'asc');
  237. $this->assertEquals($a, $b);
  238. $b = array(
  239. 0 => array('Person' => array('name' => 'Jeff'), 'Friend' => array(array('name' => 'Nate'))),
  240. 1 => array('Person' => array('name' => 'Tracy'),'Friend' => array(array('name' => 'Lindsay')))
  241. );
  242. $a = array(
  243. 0 => array('Person' => array('name' => 'Tracy'),'Friend' => array(array('name' => 'Lindsay'))),
  244. 1 => array('Person' => array('name' => 'Jeff'), 'Friend' => array(array('name' => 'Nate')))
  245. );
  246. $a = Set::sort($a, '{n}.Friend.{n}.name', 'desc');
  247. $this->assertEquals($a, $b);
  248. $a = array(
  249. 0 => array('Person' => array('name' => 'Jeff'), 'Friend' => array(array('name' => 'Nate'))),
  250. 1 => array('Person' => array('name' => 'Tracy'),'Friend' => array(array('name' => 'Lindsay'))),
  251. 2 => array('Person' => array('name' => 'Adam'),'Friend' => array(array('name' => 'Bob')))
  252. );
  253. $b = array(
  254. 0 => array('Person' => array('name' => 'Adam'),'Friend' => array(array('name' => 'Bob'))),
  255. 1 => array('Person' => array('name' => 'Jeff'), 'Friend' => array(array('name' => 'Nate'))),
  256. 2 => array('Person' => array('name' => 'Tracy'),'Friend' => array(array('name' => 'Lindsay')))
  257. );
  258. $a = Set::sort($a, '{n}.Person.name', 'asc');
  259. $this->assertEquals($a, $b);
  260. $a = array(
  261. array(7,6,4),
  262. array(3,4,5),
  263. array(3,2,1),
  264. );
  265. $b = array(
  266. array(3,2,1),
  267. array(3,4,5),
  268. array(7,6,4),
  269. );
  270. $a = Set::sort($a, '{n}.{n}', 'asc');
  271. $this->assertEquals($a, $b);
  272. $a = array(
  273. array(7,6,4),
  274. array(3,4,5),
  275. array(3,2,array(1,1,1)),
  276. );
  277. $b = array(
  278. array(3,2,array(1,1,1)),
  279. array(3,4,5),
  280. array(7,6,4),
  281. );
  282. $a = Set::sort($a, '{n}', 'asc');
  283. $this->assertEquals($a, $b);
  284. $a = array(
  285. 0 => array('Person' => array('name' => 'Jeff')),
  286. 1 => array('Shirt' => array('color' => 'black'))
  287. );
  288. $b = array(
  289. 0 => array('Shirt' => array('color' => 'black')),
  290. 1 => array('Person' => array('name' => 'Jeff')),
  291. );
  292. $a = Set::sort($a, '{n}.Person.name', 'ASC');
  293. $this->assertEquals($a, $b);
  294. $names = array(
  295. array('employees' => array(array('name' => array('first' => 'John', 'last' => 'Doe')))),
  296. array('employees' => array(array('name' => array('first' => 'Jane', 'last' => 'Doe')))),
  297. array('employees' => array(array('name' => array()))),
  298. array('employees' => array(array('name' => array())))
  299. );
  300. $result = Set::sort($names, '{n}.employees.0.name', 'asc', 1);
  301. $expected = array(
  302. array('employees' => array(array('name' => array('first' => 'John', 'last' => 'Doe')))),
  303. array('employees' => array(array('name' => array('first' => 'Jane', 'last' => 'Doe')))),
  304. array('employees' => array(array('name' => array()))),
  305. array('employees' => array(array('name' => array())))
  306. );
  307. $this->assertEquals($expected, $result);
  308. }
  309. /**
  310. * test sorting with out of order keys.
  311. *
  312. * @return void
  313. */
  314. public function testSortWithOutOfOrderKeys() {
  315. $data = array(
  316. 9 => array('class' => 510, 'test2' => 2),
  317. 1 => array('class' => 500, 'test2' => 1),
  318. 2 => array('class' => 600, 'test2' => 2),
  319. 5 => array('class' => 625, 'test2' => 4),
  320. 0 => array('class' => 605, 'test2' => 3),
  321. );
  322. $expected = array(
  323. array('class' => 500, 'test2' => 1),
  324. array('class' => 510, 'test2' => 2),
  325. array('class' => 600, 'test2' => 2),
  326. array('class' => 605, 'test2' => 3),
  327. array('class' => 625, 'test2' => 4),
  328. );
  329. $result = Set::sort($data, '{n}.class', 'asc');
  330. $this->assertEquals($expected, $result);
  331. $result = Set::sort($data, '{n}.test2', 'asc');
  332. $this->assertEquals($expected, $result);
  333. }
  334. /**
  335. * testExtract method
  336. *
  337. * @return void
  338. */
  339. public function testExtract() {
  340. $a = array(
  341. array(
  342. 'Article' => array('id' => '1', 'user_id' => '1', 'title' => 'First Article', 'body' => 'First Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'),
  343. 'User' => array('id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'),
  344. 'Comment' => array(
  345. array('id' => '1', 'article_id' => '1', 'user_id' => '2', 'comment' => 'First Comment for First Article', 'published' => 'Y', 'created' => '2007-03-18 10:45:23', 'updated' => '2007-03-18 10:47:31'),
  346. array('id' => '2', 'article_id' => '1', 'user_id' => '4', 'comment' => 'Second Comment for First Article', 'published' => 'Y', 'created' => '2007-03-18 10:47:23', 'updated' => '2007-03-18 10:49:31'),
  347. ),
  348. 'Tag' => array(
  349. array('id' => '1', 'tag' => 'tag1', 'created' => '2007-03-18 12:22:23', 'updated' => '2007-03-18 12:24:31'),
  350. array('id' => '2', 'tag' => 'tag2', 'created' => '2007-03-18 12:24:23', 'updated' => '2007-03-18 12:26:31')
  351. ),
  352. 'Deep' => array(
  353. 'Nesting' => array(
  354. 'test' => array(
  355. 1 => 'foo',
  356. 2 => array(
  357. 'and' => array('more' => 'stuff')
  358. )
  359. )
  360. )
  361. )
  362. ),
  363. array(
  364. 'Article' => array('id' => '3', 'user_id' => '1', 'title' => 'Third Article', 'body' => 'Third Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:43:23', 'updated' => '2007-03-18 10:45:31'),
  365. 'User' => array('id' => '2', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'),
  366. 'Comment' => array(),
  367. 'Tag' => array()
  368. ),
  369. array(
  370. 'Article' => array('id' => '3', 'user_id' => '1', 'title' => 'Third Article', 'body' => 'Third Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:43:23', 'updated' => '2007-03-18 10:45:31'),
  371. 'User' => array('id' => '3', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'),
  372. 'Comment' => array(),
  373. 'Tag' => array()
  374. ),
  375. array(
  376. 'Article' => array('id' => '3', 'user_id' => '1', 'title' => 'Third Article', 'body' => 'Third Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:43:23', 'updated' => '2007-03-18 10:45:31'),
  377. 'User' => array('id' => '4', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'),
  378. 'Comment' => array(),
  379. 'Tag' => array()
  380. ),
  381. array(
  382. 'Article' => array('id' => '3', 'user_id' => '1', 'title' => 'Third Article', 'body' => 'Third Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:43:23', 'updated' => '2007-03-18 10:45:31'),
  383. 'User' => array('id' => '5', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'),
  384. 'Comment' => array(),
  385. 'Tag' => array()
  386. )
  387. );
  388. $b = array('Deep' => $a[0]['Deep']);
  389. $c = array(
  390. array('a' => array('I' => array('a' => 1))),
  391. array(
  392. 'a' => array(
  393. 2
  394. )
  395. ),
  396. array('a' => array('II' => array('a' => 3, 'III' => array('a' => array('foo' => 4))))),
  397. );
  398. $expected = array(array('a' => $c[2]['a']));
  399. $r = Set::extract('/a/II[a=3]/..', $c);
  400. $this->assertEquals($r, $expected);
  401. $expected = array(1, 2, 3, 4, 5);
  402. $this->assertEquals(Set::extract('/User/id', $a), $expected);
  403. $expected = array(1, 2, 3, 4, 5);
  404. $this->assertEquals(Set::extract('/User/id', $a), $expected);
  405. $expected = array(
  406. array('id' => 1), array('id' => 2), array('id' => 3), array('id' => 4), array('id' => 5)
  407. );
  408. $r = Set::extract('/User/id', $a, array('flatten' => false));
  409. $this->assertEquals($r, $expected);
  410. $expected = array(array('test' => $a[0]['Deep']['Nesting']['test']));
  411. $this->assertEquals(Set::extract('/Deep/Nesting/test', $a), $expected);
  412. $this->assertEquals(Set::extract('/Deep/Nesting/test', $b), $expected);
  413. $expected = array(array('test' => $a[0]['Deep']['Nesting']['test']));
  414. $r = Set::extract('/Deep/Nesting/test/1/..', $a);
  415. $this->assertEquals($r, $expected);
  416. $expected = array(array('test' => $a[0]['Deep']['Nesting']['test']));
  417. $r = Set::extract('/Deep/Nesting/test/2/and/../..', $a);
  418. $this->assertEquals($r, $expected);
  419. $expected = array(array('test' => $a[0]['Deep']['Nesting']['test']));
  420. $r = Set::extract('/Deep/Nesting/test/2/../../../Nesting/test/2/..', $a);
  421. $this->assertEquals($r, $expected);
  422. $expected = array(2);
  423. $r = Set::extract('/User[2]/id', $a);
  424. $this->assertEquals($r, $expected);
  425. $expected = array(4, 5);
  426. $r = Set::extract('/User[id>3]/id', $a);
  427. $this->assertEquals($r, $expected);
  428. $expected = array(2, 3);
  429. $r = Set::extract('/User[id>1][id<=3]/id', $a);
  430. $this->assertEquals($r, $expected);
  431. $expected = array(array('I'), array('II'));
  432. $r = Set::extract('/a/@*', $c);
  433. $this->assertEquals($r, $expected);
  434. $single = array(
  435. 'User' => array(
  436. 'id' => 4,
  437. 'name' => 'Neo',
  438. )
  439. );
  440. $tricky = array(
  441. 0 => array(
  442. 'User' => array(
  443. 'id' => 1,
  444. 'name' => 'John',
  445. )
  446. ),
  447. 1 => array(
  448. 'User' => array(
  449. 'id' => 2,
  450. 'name' => 'Bob',
  451. )
  452. ),
  453. 2 => array(
  454. 'User' => array(
  455. 'id' => 3,
  456. 'name' => 'Tony',
  457. )
  458. ),
  459. 'User' => array(
  460. 'id' => 4,
  461. 'name' => 'Neo',
  462. )
  463. );
  464. $expected = array(1, 2, 3, 4);
  465. $r = Set::extract('/User/id', $tricky);
  466. $this->assertEquals($r, $expected);
  467. $expected = array(4);
  468. $r = Set::extract('/User/id', $single);
  469. $this->assertEquals($r, $expected);
  470. $expected = array(1, 3);
  471. $r = Set::extract('/User[name=/n/]/id', $tricky);
  472. $this->assertEquals($r, $expected);
  473. $expected = array(4);
  474. $r = Set::extract('/User[name=/N/]/id', $tricky);
  475. $this->assertEquals($r, $expected);
  476. $expected = array(1, 3, 4);
  477. $r = Set::extract('/User[name=/N/i]/id', $tricky);
  478. $this->assertEquals($r, $expected);
  479. $expected = array(array('id', 'name'), array('id', 'name'), array('id', 'name'), array('id', 'name'));
  480. $r = Set::extract('/User/@*', $tricky);
  481. $this->assertEquals($r, $expected);
  482. $common = array(
  483. array(
  484. 'Article' => array(
  485. 'id' => 1,
  486. 'name' => 'Article 1',
  487. ),
  488. 'Comment' => array(
  489. array(
  490. 'id' => 1,
  491. 'user_id' => 5,
  492. 'article_id' => 1,
  493. 'text' => 'Comment 1',
  494. ),
  495. array(
  496. 'id' => 2,
  497. 'user_id' => 23,
  498. 'article_id' => 1,
  499. 'text' => 'Comment 2',
  500. ),
  501. array(
  502. 'id' => 3,
  503. 'user_id' => 17,
  504. 'article_id' => 1,
  505. 'text' => 'Comment 3',
  506. ),
  507. ),
  508. ),
  509. array(
  510. 'Article' => array(
  511. 'id' => 2,
  512. 'name' => 'Article 2',
  513. ),
  514. 'Comment' => array(
  515. array(
  516. 'id' => 4,
  517. 'user_id' => 2,
  518. 'article_id' => 2,
  519. 'text' => 'Comment 4',
  520. 'addition' => '',
  521. ),
  522. array(
  523. 'id' => 5,
  524. 'user_id' => 23,
  525. 'article_id' => 2,
  526. 'text' => 'Comment 5',
  527. 'addition' => 'foo',
  528. ),
  529. ),
  530. ),
  531. array(
  532. 'Article' => array(
  533. 'id' => 3,
  534. 'name' => 'Article 3',
  535. ),
  536. 'Comment' => array(),
  537. )
  538. );
  539. $r = Set::extract('/Comment/id', $common);
  540. $expected = array(1, 2, 3, 4, 5);
  541. $this->assertEquals($r, $expected);
  542. $expected = array(1, 2, 4, 5);
  543. $r = Set::extract('/Comment[id!=3]/id', $common);
  544. $this->assertEquals($r, $expected);
  545. $r = Set::extract('/', $common);
  546. $this->assertEquals($r, $common);
  547. $expected = array(1, 2, 4, 5);
  548. $r = Set::extract($common, '/Comment[id!=3]/id');
  549. $this->assertEquals($r, $expected);
  550. $expected = array($common[0]['Comment'][2]);
  551. $r = Set::extract($common, '/Comment/2');
  552. $this->assertEquals($r, $expected);
  553. $expected = array($common[0]['Comment'][0]);
  554. $r = Set::extract($common, '/Comment[1]/.[id=1]');
  555. $this->assertEquals($r, $expected);
  556. $expected = array($common[1]['Comment'][1]);
  557. $r = Set::extract($common, '/1/Comment/.[2]');
  558. $this->assertEquals($r, $expected);
  559. $expected = array();
  560. $r = Set::extract('/User/id', array());
  561. $this->assertEquals($r, $expected);
  562. $expected = array(5);
  563. $r = Set::extract('/Comment/id[:last]', $common);
  564. $this->assertEquals($r, $expected);
  565. $expected = array(1);
  566. $r = Set::extract('/Comment/id[:first]', $common);
  567. $this->assertEquals($r, $expected);
  568. $expected = array(3);
  569. $r = Set::extract('/Article[:last]/id', $common);
  570. $this->assertEquals($r, $expected);
  571. $expected = array(array('Comment' => $common[1]['Comment'][0]));
  572. $r = Set::extract('/Comment[addition=]', $common);
  573. $this->assertEquals($r, $expected);
  574. $habtm = array(
  575. array(
  576. 'Post' => array(
  577. 'id' => 1,
  578. 'title' => 'great post',
  579. ),
  580. 'Comment' => array(
  581. array(
  582. 'id' => 1,
  583. 'text' => 'foo',
  584. 'User' => array(
  585. 'id' => 1,
  586. 'name' => 'bob'
  587. ),
  588. ),
  589. array(
  590. 'id' => 2,
  591. 'text' => 'bar',
  592. 'User' => array(
  593. 'id' => 2,
  594. 'name' => 'tod'
  595. ),
  596. ),
  597. ),
  598. ),
  599. array(
  600. 'Post' => array(
  601. 'id' => 2,
  602. 'title' => 'fun post',
  603. ),
  604. 'Comment' => array(
  605. array(
  606. 'id' => 3,
  607. 'text' => '123',
  608. 'User' => array(
  609. 'id' => 3,
  610. 'name' => 'dan'
  611. ),
  612. ),
  613. array(
  614. 'id' => 4,
  615. 'text' => '987',
  616. 'User' => array(
  617. 'id' => 4,
  618. 'name' => 'jim'
  619. ),
  620. ),
  621. ),
  622. ),
  623. );
  624. $r = Set::extract('/Comment/User[name=/bob|dan/]/..', $habtm);
  625. $this->assertEquals($r[0]['Comment']['User']['name'], 'bob');
  626. $this->assertEquals($r[1]['Comment']['User']['name'], 'dan');
  627. $this->assertEquals(count($r), 2);
  628. $r = Set::extract('/Comment/User[name=/bob|tod/]/..', $habtm);
  629. $this->assertEquals($r[0]['Comment']['User']['name'], 'bob');
  630. $this->assertEquals($r[1]['Comment']['User']['name'], 'tod');
  631. $this->assertEquals(count($r), 2);
  632. $tree = array(
  633. array(
  634. 'Category' => array('name' => 'Category 1'),
  635. 'children' => array(array('Category' => array('name' => 'Category 1.1')))
  636. ),
  637. array(
  638. 'Category' => array('name' => 'Category 2'),
  639. 'children' => array(
  640. array('Category' => array('name' => 'Category 2.1')),
  641. array('Category' => array('name' => 'Category 2.2'))
  642. )
  643. ),
  644. array(
  645. 'Category' => array('name' => 'Category 3'),
  646. 'children' => array(array('Category' => array('name' => 'Category 3.1')))
  647. )
  648. );
  649. $expected = array(array('Category' => $tree[1]['Category']));
  650. $r = Set::extract('/Category[name=Category 2]', $tree);
  651. $this->assertEquals($r, $expected);
  652. $expected = array(
  653. array('Category' => $tree[1]['Category'], 'children' => $tree[1]['children'])
  654. );
  655. $r = Set::extract('/Category[name=Category 2]/..', $tree);
  656. $this->assertEquals($r, $expected);
  657. $expected = array(
  658. array('children' => $tree[1]['children'][0]),
  659. array('children' => $tree[1]['children'][1])
  660. );
  661. $r = Set::extract('/Category[name=Category 2]/../children', $tree);
  662. $this->assertEquals($r, $expected);
  663. $habtm = array(
  664. array(
  665. 'Post' => array(
  666. 'id' => 1,
  667. 'title' => 'great post',
  668. ),
  669. 'Comment' => array(
  670. array(
  671. 'id' => 1,
  672. 'text' => 'foo',
  673. 'User' => array(
  674. 'id' => 1,
  675. 'name' => 'bob'
  676. ),
  677. ),
  678. array(
  679. 'id' => 2,
  680. 'text' => 'bar',
  681. 'User' => array(
  682. 'id' => 2,
  683. 'name' => 'tod'
  684. ),
  685. ),
  686. ),
  687. ),
  688. array(
  689. 'Post' => array(
  690. 'id' => 2,
  691. 'title' => 'fun post',
  692. ),
  693. 'Comment' => array(
  694. array(
  695. 'id' => 3,
  696. 'text' => '123',
  697. 'User' => array(
  698. 'id' => 3,
  699. 'name' => 'dan'
  700. ),
  701. ),
  702. array(
  703. 'id' => 4,
  704. 'text' => '987',
  705. 'User' => array(
  706. 'id' => 4,
  707. 'name' => 'jim'
  708. ),
  709. ),
  710. ),
  711. ),
  712. );
  713. $r = Set::extract('/Comment/User[name=/\w+/]/..', $habtm);
  714. $this->assertEquals($r[0]['Comment']['User']['name'], 'bob');
  715. $this->assertEquals($r[1]['Comment']['User']['name'], 'tod');
  716. $this->assertEquals($r[2]['Comment']['User']['name'], 'dan');
  717. $this->assertEquals($r[3]['Comment']['User']['name'], 'dan');
  718. $this->assertEquals(count($r), 4);
  719. $r = Set::extract('/Comment/User[name=/[a-z]+/]/..', $habtm);
  720. $this->assertEquals($r[0]['Comment']['User']['name'], 'bob');
  721. $this->assertEquals($r[1]['Comment']['User']['name'], 'tod');
  722. $this->assertEquals($r[2]['Comment']['User']['name'], 'dan');
  723. $this->assertEquals($r[3]['Comment']['User']['name'], 'dan');
  724. $this->assertEquals(count($r), 4);
  725. $r = Set::extract('/Comment/User[name=/bob|dan/]/..', $habtm);
  726. $this->assertEquals($r[0]['Comment']['User']['name'], 'bob');
  727. $this->assertEquals($r[1]['Comment']['User']['name'], 'dan');
  728. $this->assertEquals(count($r), 2);
  729. $r = Set::extract('/Comment/User[name=/bob|tod/]/..', $habtm);
  730. $this->assertEquals($r[0]['Comment']['User']['name'], 'bob');
  731. $this->assertEquals($r[1]['Comment']['User']['name'], 'tod');
  732. $this->assertEquals(count($r), 2);
  733. $mixedKeys = array(
  734. 'User' => array(
  735. 0 => array(
  736. 'id' => 4,
  737. 'name' => 'Neo'
  738. ),
  739. 1 => array(
  740. 'id' => 5,
  741. 'name' => 'Morpheus'
  742. ),
  743. 'stringKey' => array()
  744. )
  745. );
  746. $expected = array('Neo', 'Morpheus');
  747. $r = Set::extract('/User/name', $mixedKeys);
  748. $this->assertEquals($r, $expected);
  749. $f = array(
  750. array(
  751. 'file' => array(
  752. 'name' => 'zipfile.zip',
  753. 'type' => 'application/zip',
  754. 'tmp_name' => '/tmp/php178.tmp',
  755. 'error' => 0,
  756. 'size' => '564647'
  757. )
  758. ),
  759. array(
  760. 'file' => array(
  761. 'name' => 'zipfile2.zip',
  762. 'type' => 'application/x-zip-compressed',
  763. 'tmp_name' => '/tmp/php179.tmp',
  764. 'error' => 0,
  765. 'size' => '354784'
  766. )
  767. ),
  768. array(
  769. 'file' => array(
  770. 'name' => 'picture.jpg',
  771. 'type' => 'image/jpeg',
  772. 'tmp_name' => '/tmp/php180.tmp',
  773. 'error' => 0,
  774. 'size' => '21324'
  775. )
  776. )
  777. );
  778. $expected = array(array('name' => 'zipfile2.zip','type' => 'application/x-zip-compressed','tmp_name' => '/tmp/php179.tmp','error' => 0,'size' => '354784'));
  779. $r = Set::extract('/file/.[type=application/x-zip-compressed]', $f);
  780. $this->assertEquals($r, $expected);
  781. $expected = array(array('name' => 'zipfile.zip','type' => 'application/zip','tmp_name' => '/tmp/php178.tmp','error' => 0,'size' => '564647'));
  782. $r = Set::extract('/file/.[type=application/zip]', $f);
  783. $this->assertEquals($r, $expected);
  784. $f = array(
  785. array(
  786. 'file' => array(
  787. 'name' => 'zipfile.zip',
  788. 'type' => 'application/zip',
  789. 'tmp_name' => '/tmp/php178.tmp',
  790. 'error' => 0,
  791. 'size' => '564647'
  792. )
  793. ),
  794. array(
  795. 'file' => array(
  796. 'name' => 'zipfile2.zip',
  797. 'type' => 'application/x zip compressed',
  798. 'tmp_name' => '/tmp/php179.tmp',
  799. 'error' => 0,
  800. 'size' => '354784'
  801. )
  802. ),
  803. array(
  804. 'file' => array(
  805. 'name' => 'picture.jpg',
  806. 'type' => 'image/jpeg',
  807. 'tmp_name' => '/tmp/php180.tmp',
  808. 'error' => 0,
  809. 'size' => '21324'
  810. )
  811. )
  812. );
  813. $expected = array(array('name' => 'zipfile2.zip','type' => 'application/x zip compressed','tmp_name' => '/tmp/php179.tmp','error' => 0,'size' => '354784'));
  814. $r = Set::extract('/file/.[type=application/x zip compressed]', $f);
  815. $this->assertEquals($r, $expected);
  816. $expected = array(
  817. array('name' => 'zipfile.zip','type' => 'application/zip','tmp_name' => '/tmp/php178.tmp','error' => 0,'size' => '564647'),
  818. array('name' => 'zipfile2.zip','type' => 'application/x zip compressed','tmp_name' => '/tmp/php179.tmp','error' => 0,'size' => '354784')
  819. );
  820. $r = Set::extract('/file/.[tmp_name=/tmp\/php17/]', $f);
  821. $this->assertEquals($r, $expected);
  822. $hasMany = array(
  823. 'Node' => array(
  824. 'id' => 1,
  825. 'name' => 'First',
  826. 'state' => 50
  827. ),
  828. 'ParentNode' => array(
  829. 0 => array(
  830. 'id' => 2,
  831. 'name' => 'Second',
  832. 'state' => 60,
  833. )
  834. )
  835. );
  836. $result = Set::extract('/ParentNode/name', $hasMany);
  837. $expected = array('Second');
  838. $this->assertEquals($expected, $result);
  839. $data = array(
  840. array(
  841. 'Category' => array(
  842. 'id' => 1,
  843. 'name' => 'First'
  844. ),
  845. 0 => array(
  846. 'value' => 50
  847. )
  848. ),
  849. array(
  850. 'Category' => array(
  851. 'id' => 2,
  852. 'name' => 'Second'
  853. ),
  854. 0 => array(
  855. 'value' => 60
  856. )
  857. )
  858. );
  859. $expected = array(
  860. array(
  861. 'Category' => array(
  862. 'id' => 1,
  863. 'name' => 'First'
  864. ),
  865. 0 => array(
  866. 'value' => 50
  867. )
  868. )
  869. );
  870. $result = Set::extract('/Category[id=1]/..', $data);
  871. $this->assertEquals($expected, $result);
  872. $data = array(
  873. array(
  874. 'ChildNode' => array('id' => 1),
  875. array('name' => 'Item 1')
  876. ),
  877. array(
  878. 'ChildNode' => array('id' => 2),
  879. array('name' => 'Item 2')
  880. ),
  881. );
  882. $expected = array(
  883. 'Item 1',
  884. 'Item 2'
  885. );
  886. $result = Set::extract('/0/name', $data);
  887. $this->assertEquals($expected, $result);
  888. $data = array(
  889. array('A1', 'B1'),
  890. array('A2', 'B2')
  891. );
  892. $expected = array('A1', 'A2');
  893. $result = Set::extract('/0', $data);
  894. $this->assertEquals($expected, $result);
  895. }
  896. /**
  897. * test parent selectors with extract
  898. *
  899. * @return void
  900. */
  901. public function testExtractParentSelector() {
  902. $tree = array(
  903. array(
  904. 'Category' => array(
  905. 'name' => 'Category 1'
  906. ),
  907. 'children' => array(
  908. array(
  909. 'Category' => array(
  910. 'name' => 'Category 1.1'
  911. )
  912. )
  913. )
  914. ),
  915. array(
  916. 'Category' => array(
  917. 'name' => 'Category 2'
  918. ),
  919. 'children' => array(
  920. array(
  921. 'Category' => array(
  922. 'name' => 'Category 2.1'
  923. )
  924. ),
  925. array(
  926. 'Category' => array(
  927. 'name' => 'Category 2.2'
  928. )
  929. ),
  930. )
  931. ),
  932. array(
  933. 'Category' => array(
  934. 'name' => 'Category 3'
  935. ),
  936. 'children' => array(
  937. array(
  938. 'Category' => array(
  939. 'name' => 'Category 3.1'
  940. )
  941. )
  942. )
  943. )
  944. );
  945. $expected = array(array('Category' => $tree[1]['Category']));
  946. $r = Set::extract('/Category[name=Category 2]', $tree);
  947. $this->assertEquals($r, $expected);
  948. $expected = array(array('Category' => $tree[1]['Category'], 'children' => $tree[1]['children']));
  949. $r = Set::extract('/Category[name=Category 2]/..', $tree);
  950. $this->assertEquals($r, $expected);
  951. $expected = array(array('children' => $tree[1]['children'][0]), array('children' => $tree[1]['children'][1]));
  952. $r = Set::extract('/Category[name=Category 2]/../children', $tree);
  953. $this->assertEquals($r, $expected);
  954. $single = array(
  955. array(
  956. 'CallType' => array(
  957. 'name' => 'Internal Voice'
  958. ),
  959. 'x' => array(
  960. 'hour' => 7
  961. )
  962. )
  963. );
  964. $expected = array(7);
  965. $r = Set::extract('/CallType[name=Internal Voice]/../x/hour', $single);
  966. $this->assertEquals($r, $expected);
  967. $multiple = array(
  968. array(
  969. 'CallType' => array(
  970. 'name' => 'Internal Voice'
  971. ),
  972. 'x' => array(
  973. 'hour' => 7
  974. )
  975. ),
  976. array(
  977. 'CallType' => array(
  978. 'name' => 'Internal Voice'
  979. ),
  980. 'x' => array(
  981. 'hour' => 2
  982. )
  983. ),
  984. array(
  985. 'CallType' => array(
  986. 'name' => 'Internal Voice'
  987. ),
  988. 'x' => array(
  989. 'hour' => 1
  990. )
  991. )
  992. );
  993. $expected = array(7,2,1);
  994. $r = Set::extract('/CallType[name=Internal Voice]/../x/hour', $multiple);
  995. $this->assertEquals($r, $expected);
  996. $a = array(
  997. 'Model' => array(
  998. '0' => array(
  999. 'id' => 18,
  1000. 'SubModelsModel' => array(
  1001. 'id' => 1,
  1002. 'submodel_id' => 66,
  1003. 'model_id' => 18,
  1004. 'type' => 1
  1005. ),
  1006. ),
  1007. '1' => array(
  1008. 'id' => 0,
  1009. 'SubModelsModel' => array(
  1010. 'id' => 2,
  1011. 'submodel_id' => 66,
  1012. 'model_id' => 0,
  1013. 'type' => 1
  1014. ),
  1015. ),
  1016. '2' => array(
  1017. 'id' => 17,
  1018. 'SubModelsModel' => array(
  1019. 'id' => 3,
  1020. 'submodel_id' => 66,
  1021. 'model_id' => 17,
  1022. 'type' => 2
  1023. ),
  1024. ),
  1025. '3' => array(
  1026. 'id' => 0,
  1027. 'SubModelsModel' => array(
  1028. 'id' => 4,
  1029. 'submodel_id' => 66,
  1030. 'model_id' => 0,
  1031. 'type' => 2
  1032. )
  1033. )
  1034. )
  1035. );
  1036. $expected = array(
  1037. array(
  1038. 'Model' => array(
  1039. 'id' => 17,
  1040. 'SubModelsModel' => array(
  1041. 'id' => 3,
  1042. 'submodel_id' => 66,
  1043. 'model_id' => 17,
  1044. 'type' => 2
  1045. ),
  1046. )
  1047. ),
  1048. array(
  1049. 'Model' => array(
  1050. 'id' => 0,
  1051. 'SubModelsModel' => array(
  1052. 'id' => 4,
  1053. 'submodel_id' => 66,
  1054. 'model_id' => 0,
  1055. 'type' => 2
  1056. )
  1057. )
  1058. )
  1059. );
  1060. $r = Set::extract('/Model/SubModelsModel[type=2]/..', $a);
  1061. $this->assertEquals($r, $expected);
  1062. }
  1063. /**
  1064. * test that extract() still works when arrays don't contain a 0 index.
  1065. *
  1066. * @return void
  1067. */
  1068. public function testExtractWithNonZeroArrays() {
  1069. $nonZero = array(
  1070. 1 => array(
  1071. 'User' => array(
  1072. 'id' => 1,
  1073. 'name' => 'John',
  1074. )
  1075. ),
  1076. 2 => array(
  1077. 'User' => array(
  1078. 'id' => 2,
  1079. 'name' => 'Bob',
  1080. )
  1081. ),
  1082. 3 => array(
  1083. 'User' => array(
  1084. 'id' => 3,
  1085. 'name' => 'Tony',
  1086. )
  1087. )
  1088. );
  1089. $expected = array(1, 2, 3);
  1090. $r = Set::extract('/User/id', $nonZero);
  1091. $this->assertEquals($r, $expected);
  1092. $expected = array(
  1093. array('User' => array('id' => 1, 'name' => 'John')),
  1094. array('User' => array('id' => 2, 'name' => 'Bob')),
  1095. array('User' => array('id' => 3, 'name' => 'Tony')),
  1096. );
  1097. $result = Set::extract('/User', $nonZero);
  1098. $this->assertEquals($expected, $result);
  1099. $nonSequential = array(
  1100. 'User' => array(
  1101. 0 => array('id' => 1),
  1102. 2 => array('id' => 2),
  1103. 6 => array('id' => 3),
  1104. 9 => array('id' => 4),
  1105. 3 => array('id' => 5),
  1106. ),
  1107. );
  1108. $nonZero = array(
  1109. 'User' => array(
  1110. 2 => array('id' => 1),
  1111. 4 => array('id' => 2),
  1112. 6 => array('id' => 3),
  1113. 9 => array('id' => 4),
  1114. 3 => array('id' => 5),
  1115. ),
  1116. );
  1117. $expected = array(1, 2, 3, 4, 5);
  1118. $this->assertEquals(Set::extract('/User/id', $nonSequential), $expected);
  1119. $result = Set::extract('/User/id', $nonZero);
  1120. $this->assertEquals($expected, $result, 'Failed non zero array key extract');
  1121. $expected = array(1, 2, 3, 4, 5);
  1122. $this->assertEquals(Set::extract('/User/id', $nonSequential), $expected);
  1123. $result = Set::extract('/User/id', $nonZero);
  1124. $this->assertEquals($expected, $result, 'Failed non zero array key extract');
  1125. $startingAtOne = array(
  1126. 'Article' => array(
  1127. 1 => array(
  1128. 'id' => 1,
  1129. 'approved' => 1,
  1130. ),
  1131. )
  1132. );
  1133. $expected = array(0 => array('Article' => array('id' => 1, 'approved' => 1)));
  1134. $result = Set::extract('/Article[approved=1]', $startingAtOne);
  1135. $this->assertEquals($expected, $result);
  1136. $items = array(
  1137. 240 => array(
  1138. 'A' => array(
  1139. 'field1' => 'a240',
  1140. 'field2' => 'a240',
  1141. ),
  1142. 'B' => array(
  1143. 'field1' => 'b240',
  1144. 'field2' => 'b240'
  1145. ),
  1146. )
  1147. );
  1148. $expected = array(
  1149. 0 => 'b240'
  1150. );
  1151. $result = Set::extract('/B/field1', $items);
  1152. $this->assertSame($expected, $result);
  1153. $this->assertSame($result, Set::extract('{n}.B.field1', $items));
  1154. }
  1155. /**
  1156. * testExtractWithArrays method
  1157. *
  1158. * @return void
  1159. */
  1160. public function testExtractWithArrays() {
  1161. $data = array(
  1162. 'Level1' => array(
  1163. 'Level2' => array('test1', 'test2'),
  1164. 'Level2bis' => array('test3', 'test4')
  1165. )
  1166. );
  1167. $this->assertEquals(Set::extract('/Level1/Level2', $data), array(array('Level2' => array('test1', 'test2'))));
  1168. $this->assertEquals(Set::extract('/Level1/Level2bis', $data), array(array('Level2bis' => array('test3', 'test4'))));
  1169. }
  1170. /**
  1171. * test extract() with elements that have non-array children.
  1172. *
  1173. * @return void
  1174. */
  1175. public function testExtractWithNonArrayElements() {
  1176. $data = array(
  1177. 'node' => array(
  1178. array('foo'),
  1179. 'bar'
  1180. )
  1181. );
  1182. $result = Set::extract('/node', $data);
  1183. $expected = array(
  1184. array('node' => array('foo')),
  1185. 'bar'
  1186. );
  1187. $this->assertEquals($expected, $result);
  1188. $data = array(
  1189. 'node' => array(
  1190. 'foo' => array('bar'),
  1191. 'bar' => array('foo')
  1192. )
  1193. );
  1194. $result = Set::extract('/node', $data);
  1195. $expected = array(
  1196. array('foo' => array('bar')),
  1197. array('bar' => array('foo')),
  1198. );
  1199. $this->assertEquals($expected, $result);
  1200. $data = array(
  1201. 'node' => array(
  1202. 'foo' => array(
  1203. 'bar'
  1204. ),
  1205. 'bar' => 'foo'
  1206. )
  1207. );
  1208. $result = Set::extract('/node', $data);
  1209. $expected = array(
  1210. array('foo' => array('bar')),
  1211. 'foo'
  1212. );
  1213. $this->assertEquals($expected, $result);
  1214. }
  1215. /**
  1216. * testMatches method
  1217. *
  1218. * @return void
  1219. */
  1220. public function testMatches() {
  1221. $a = array(
  1222. array('Article' => array('id' => 1, 'title' => 'Article 1')),
  1223. array('Article' => array('id' => 2, 'title' => 'Article 2')),
  1224. array('Article' => array('id' => 3, 'title' => 'Article 3'))
  1225. );
  1226. $this->assertTrue(Set::matches(array('id=2'), $a[1]['Article']));
  1227. $this->assertFalse(Set::matches(array('id>2'), $a[1]['Article']));
  1228. $this->assertTrue(Set::matches(array('id>=2'), $a[1]['Article']));
  1229. $this->assertFalse(Set::matches(array('id>=3'), $a[1]['Article']));
  1230. $this->assertTrue(Set::matches(array('id<=2'), $a[1]['Article']));
  1231. $this->assertFalse(Set::matches(array('id<2'), $a[1]['Article']));
  1232. $this->assertTrue(Set::matches(array('id>1'), $a[1]['Article']));
  1233. $this->assertTrue(Set::matches(array('id>1', 'id<3', 'id!=0'), $a[1]['Article']));
  1234. $this->assertTrue(Set::matches(array('3'), null, 3));
  1235. $this->assertTrue(Set::matches(array('5'), null, 5));
  1236. $this->assertTrue(Set::matches(array('id'), $a[1]['Article']));
  1237. $this->assertTrue(Set::matches(array('id', 'title'), $a[1]['Article']));
  1238. $this->assertFalse(Set::matches(array('non-existant'), $a[1]['Article']));
  1239. $this->assertTrue(Set::matches('/Article[id=2]', $a));
  1240. $this->assertFalse(Set::matches('/Article[id=4]', $a));
  1241. $this->assertTrue(Set::matches(array(), $a));
  1242. $r = array(
  1243. 'Attachment' => array(
  1244. 'keep' => array()
  1245. ),
  1246. 'Comment' => array(
  1247. 'keep' => array(
  1248. 'Attachment' => array(
  1249. 'fields' => array(
  1250. 0 => 'attachment',
  1251. ),
  1252. ),
  1253. )
  1254. ),
  1255. 'User' => array(
  1256. 'keep' => array()
  1257. ),
  1258. 'Article' => array(
  1259. 'keep' => array(
  1260. 'Comment' => array(
  1261. 'fields' => array(
  1262. 0 => 'comment',
  1263. 1 => 'published',
  1264. ),
  1265. ),
  1266. 'User' => array(
  1267. 'fields' => array(
  1268. 0 => 'user',
  1269. ),
  1270. ),
  1271. )
  1272. )
  1273. );
  1274. $this->assertTrue(Set::matches('/Article/keep/Comment', $r));
  1275. $this->assertEquals(Set::extract('/Article/keep/Comment/fields', $r), array('comment', 'published'));
  1276. $this->assertEquals(Set::extract('/Article/keep/User/fields', $r), array('user'));
  1277. }
  1278. /**
  1279. * testSetExtractReturnsEmptyArray method
  1280. *
  1281. * @return void
  1282. */
  1283. public function testSetExtractReturnsEmptyArray() {
  1284. $this->assertEquals(Set::extract(array(), '/Post/id'), array());
  1285. $this->assertEquals(Set::extract('/Post/id', array()), array());
  1286. $this->assertEquals(Set::extract('/Post/id', array(
  1287. array('Post' => array('name' => 'bob')),
  1288. array('Post' => array('name' => 'jim'))
  1289. )), array());
  1290. $this->assertEquals(Set::extract(array(), 'Message.flash'), null);
  1291. }
  1292. /**
  1293. * testClassicExtract method
  1294. *
  1295. * @return void
  1296. */
  1297. public function testClassicExtract() {
  1298. $a = array(
  1299. array('Article' => array('id' => 1, 'title' => 'Article 1')),
  1300. array('Article' => array('id' => 2, 'title' => 'Article 2')),
  1301. array('Article' => array('id' => 3, 'title' => 'Article 3'))
  1302. );
  1303. $result = Set::extract($a, '{n}.Article.id');
  1304. $expected = array( 1, 2, 3 );
  1305. $this->assertEquals($expected, $result);
  1306. $result = Set::extract($a, '{n}.Article.title');
  1307. $expected = array('Article 1', 'Article 2', 'Article 3');
  1308. $this->assertEquals($expected, $result);
  1309. $result = Set::extract($a, '1.Article.title');
  1310. $expected = 'Article 2';
  1311. $this->assertEquals($expected, $result);
  1312. $result = Set::extract($a, '3.Article.title');
  1313. $expected = null;
  1314. $this->assertEquals($expected, $result);
  1315. $a = array(
  1316. array(
  1317. 'Article' => array('id' => 1, 'title' => 'Article 1',
  1318. 'User' => array('id' => 1, 'username' => 'mariano.iglesias'))
  1319. ),
  1320. array(
  1321. 'Article' => array('id' => 2, 'title' => 'Article 2',
  1322. 'User' => array('id' => 1, 'username' => 'mariano.iglesias'))
  1323. ),
  1324. array(
  1325. 'Article' => array('id' => 3, 'title' => 'Article 3',
  1326. 'User' => array('id' => 2, 'username' => 'phpnut'))
  1327. )
  1328. );
  1329. $result = Set::extract($a, '{n}.Article.User.username');
  1330. $expected = array('mariano.iglesias', 'mariano.iglesias', 'phpnut');
  1331. $this->assertEquals($expected, $result);
  1332. $a = array(
  1333. array(
  1334. 'Article' => array(
  1335. 'id' => 1, 'title' => 'Article 1',
  1336. 'Comment' => array(
  1337. array('id' => 10, 'title' => 'Comment 10'),
  1338. array('id' => 11, 'title' => 'Comment 11'),
  1339. array('id' => 12, 'title' => 'Comment 12')
  1340. )
  1341. )
  1342. ),
  1343. array(
  1344. 'Article' => array(
  1345. 'id' => 2, 'title' => 'Article 2',
  1346. 'Comment' => array(
  1347. array('id' => 13, 'title' => 'Comment 13'),
  1348. array('id' => 14, 'title' => 'Comment 14')
  1349. )
  1350. )
  1351. ),
  1352. array('Article' => array('id' => 3, 'title' => 'Article 3'))
  1353. );
  1354. $result = Set::extract($a, '{n}.Article.Comment.{n}.id');
  1355. $expected = array(array(10, 11, 12), array(13, 14), null);
  1356. $this->assertEquals($expected, $result);
  1357. $result = Set::extract($a, '{n}.Article.Comment.{n}.title');
  1358. $expected = array(
  1359. array('Comment 10', 'Comment 11', 'Comment 12'),
  1360. array('Comment 13', 'Comment 14'),
  1361. null
  1362. );
  1363. $this->assertEquals($expected, $result);
  1364. $a = array(array('1day' => '20 sales'), array('1day' => '2 sales'));
  1365. $result = Set::extract($a, '{n}.1day');
  1366. $expected = array('20 sales', '2 sales');
  1367. $this->assertEquals($expected, $result);
  1368. $a = array(
  1369. 'pages' => array('name' => 'page'),
  1370. 'fruites' => array('name' => 'fruit'),
  1371. 0 => array('name' => 'zero')
  1372. );
  1373. $result = Set::extract($a, '{s}.name');
  1374. $expected = array('page','fruit');
  1375. $this->assertEquals($expected, $result);
  1376. $a = array(
  1377. 0 => array('pages' => array('name' => 'page')),
  1378. 1 => array('fruites' => array('name' => 'fruit')),
  1379. 'test' => array(array('name' => 'jippi')),
  1380. 'dot.test' => array(array('name' => 'jippi'))
  1381. );
  1382. $result = Set::extract($a, '{n}.{s}.name');
  1383. $expected = array(0 => array('page'), 1 => array('fruit'));
  1384. $this->assertEquals($expected, $result);
  1385. $result = Set::extract($a, '{s}.{n}.name');
  1386. $expected = array(array('jippi'), array('jippi'));
  1387. $this->assertEquals($expected, $result);
  1388. $result = Set::extract($a, '{\w+}.{\w+}.name');
  1389. $expected = array(
  1390. array('pages' => 'page'),
  1391. array('fruites' => 'fruit'),
  1392. 'test' => array('jippi'),
  1393. 'dot.test' => array('jippi')
  1394. );
  1395. $this->assertEquals($expected, $result);
  1396. $result = Set::extract($a, '{\d+}.{\w+}.name');
  1397. $expected = array(array('pages' => 'page'), array('fruites' => 'fruit'));
  1398. $this->assertEquals($expected, $result);
  1399. $result = Set::extract($a, '{n}.{\w+}.name');
  1400. $expected = array(array('pages' => 'page'), array('fruites' => 'fruit'));
  1401. $this->assertEquals($expected, $result);
  1402. $result = Set::extract($a, '{s}.{\d+}.name');
  1403. $expected = array(array('jippi'), array('jippi'));
  1404. $this->assertEquals($expected, $result);
  1405. $result = Set::extract($a, '{s}');
  1406. $expected = array(array(array('name' => 'jippi')), array(array('name' => 'jippi')));
  1407. $this->assertEquals($expected, $result);
  1408. $result = Set::extract($a, '{[a-z]}');
  1409. $expected = array(
  1410. 'test' => array(array('name' => 'jippi')),
  1411. 'dot.test' => array(array('name' => 'jippi'))
  1412. );
  1413. $this->assertEquals($expected, $result);
  1414. $result = Set::extract($a, '{dot\.test}.{n}');
  1415. $expected = array('dot.test' => array(array('name' => 'jippi')));
  1416. $this->assertEquals($expected, $result);
  1417. $a = new stdClass();
  1418. $a->articles = array(
  1419. array('Article' => array('id' => 1, 'title' => 'Article 1')),
  1420. array('Article' => array('id' => 2, 'title' => 'Article 2')),
  1421. array('Article' => array('id' => 3, 'title' => 'Article 3'))
  1422. );
  1423. $result = Set::extract($a, 'articles.{n}.Article.id');
  1424. $expected = array(1, 2, 3);
  1425. $this->assertEquals($expected, $result);
  1426. $result = Set::extract($a, 'articles.{n}.Article.title');
  1427. $expected = array('Article 1', 'Article 2', 'Article 3');
  1428. $this->assertEquals($expected, $result);
  1429. $a = new ArrayObject();
  1430. $a['articles'] = array(
  1431. array('Article' => array('id' => 1, 'title' => 'Article 1')),
  1432. array('Article' => array('id' => 2, 'title' => 'Article 2')),
  1433. array('Article' => array('id' => 3, 'title' => 'Article 3'))
  1434. );
  1435. $result = Set::extract($a, 'articles.{n}.Article.id');
  1436. $expected = array(1, 2, 3);
  1437. $this->assertEquals($expected, $result);
  1438. $result = Set::extract($a, 'articles.{n}.Article.title');
  1439. $expected = array('Article 1', 'Article 2', 'Article 3');
  1440. $this->assertEquals($expected, $result);
  1441. $result = Set::extract($a, 'articles.0.Article.title');
  1442. $expected = 'Article 1';
  1443. $this->assertEquals($expected, $result);
  1444. }
  1445. /**
  1446. * testInsert method
  1447. *
  1448. * @return void
  1449. */
  1450. public function testInsert() {
  1451. $a = array(
  1452. 'pages' => array('name' => 'page')
  1453. );
  1454. $result = Set::insert($a, 'files', array('name' => 'files'));
  1455. $expected = array(
  1456. 'pages' => array('name' => 'page'),
  1457. 'files' => array('name' => 'files')
  1458. );
  1459. $this->assertEquals($expected, $result);
  1460. $a = array(
  1461. 'pages' => array('name' => 'page')
  1462. );
  1463. $result = Set::insert($a, 'pages.name', array());
  1464. $expected = array(
  1465. 'pages' => array('name' => array()),
  1466. );
  1467. $this->assertEquals($expected, $result);
  1468. $a = array(
  1469. 'pages' => array(
  1470. 0 => array('name' => 'main'),
  1471. 1 => array('name' => 'about')
  1472. )
  1473. );
  1474. $result = Set::insert($a, 'pages.1.vars', array('title' => 'page title'));
  1475. $expected = array(
  1476. 'pages' => array(
  1477. 0 => array('name' => 'main'),
  1478. 1 => array('name' => 'about', 'vars' => array('title' => 'page title'))
  1479. )
  1480. );
  1481. $this->assertEquals($expected, $result);
  1482. }
  1483. /**
  1484. * testRemove method
  1485. *
  1486. * @return void
  1487. */
  1488. public function testRemove() {
  1489. $a = array(
  1490. 'pages' => array('name' => 'page'),
  1491. 'files' => array('name' => 'files')
  1492. );
  1493. $result = Set::remove($a, 'files');
  1494. $expected = array(
  1495. 'pages' => array('name' => 'page')
  1496. );
  1497. $this->assertEquals($expected, $result);
  1498. $a = array(
  1499. 'pages' => array(
  1500. 0 => array('name' => 'main'),
  1501. 1 => array('name' => 'about', 'vars' => array('title' => 'page title'))
  1502. )
  1503. );
  1504. $result = Set::remove($a, 'pages.1.vars');
  1505. $expected = array(
  1506. 'pages' => array(
  1507. 0 => array('name' => 'main'),
  1508. 1 => array('name' => 'about')
  1509. )
  1510. );
  1511. $this->assertEquals($expected, $result);
  1512. $result = Set::remove($a, 'pages.2.vars');
  1513. $expected = $a;
  1514. $this->assertEquals($expected, $result);
  1515. }
  1516. /**
  1517. * testCheck method
  1518. *
  1519. * @return void
  1520. */
  1521. public function testCheck() {
  1522. $set = array(
  1523. 'My Index 1' => array('First' => 'The first item')
  1524. );
  1525. $this->assertTrue(Set::check($set, 'My Index 1.First'));
  1526. $this->assertTrue(Set::check($set, 'My Index 1'));
  1527. $this->assertEquals(Set::check($set, array()), $set);
  1528. $set = array(
  1529. 'My Index 1' => array('First' => array('Second' => array('Third' => array('Fourth' => 'Heavy. Nesting.'))))
  1530. );
  1531. $this->assertTrue(Set::check($set, 'My Index 1.First.Second'));
  1532. $this->assertTrue(Set::check($set, 'My Index 1.First.Second.Third'));
  1533. $this->assertTrue(Set::check($set, 'My Index 1.First.Second.Third.Fourth'));
  1534. $this->assertFalse(Set::check($set, 'My Index 1.First.Seconds.Third.Fourth'));
  1535. }
  1536. /**
  1537. * testWritingWithFunkyKeys method
  1538. *
  1539. * @return void
  1540. */
  1541. public function testWritingWithFunkyKeys() {
  1542. $set = Set::insert(array(), 'Session Test', "test");
  1543. $this->assertEquals(Set::extract($set, 'Session Test'), 'test');
  1544. $set = Set::remove($set, 'Session Test');
  1545. $this->assertFalse(Set::check($set, 'Session Test'));
  1546. $expected = array('Session Test' => array('Test Case' => 'test'));
  1547. $this->assertEquals(Set::insert(array(), 'Session Test.Test Case', "test"), $expected);
  1548. $this->assertTrue(Set::check($expected, 'Session Test.Test Case'));
  1549. }
  1550. /**
  1551. * testDiff method
  1552. *
  1553. * @return void
  1554. */
  1555. public function testDiff() {
  1556. $a = array(
  1557. 0 => array(

Large files files are truncated, but you can click here to view the full file