PageRenderTime 35ms CodeModel.GetById 22ms 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
  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('name' => 'main'),
  1558. 1 => array('name' => 'about')
  1559. );
  1560. $b = array(
  1561. 0 => array('name' => 'main'),
  1562. 1 => array('name' => 'about'),
  1563. 2 => array('name' => 'contact')
  1564. );
  1565. $result = Set::diff($a, $b);
  1566. $expected = array(
  1567. 2 => array('name' => 'contact')
  1568. );
  1569. $this->assertEquals($expected, $result);
  1570. $result = Set::diff($a, array());
  1571. $expected = $a;
  1572. $this->assertEquals($expected, $result);
  1573. $result = Set::diff(array(), $b);
  1574. $expected = $b;
  1575. $this->assertEquals($expected, $result);
  1576. $b = array(
  1577. 0 => array('name' => 'me'),
  1578. 1 => array('name' => 'about')
  1579. );
  1580. $result = Set::diff($a, $b);
  1581. $expected = array(
  1582. 0 => array('name' => 'main')
  1583. );
  1584. $this->assertEquals($expected, $result);
  1585. $a = array();
  1586. $b = array('name' => 'bob', 'address' => 'home');
  1587. $result = Set::diff($a, $b);
  1588. $this->assertEquals($result, $b);
  1589. $a = array('name' => 'bob', 'address' => 'home');
  1590. $b = array();
  1591. $result = Set::diff($a, $b);
  1592. $this->assertEquals($result, $a);
  1593. $a = array('key' => true, 'another' => false, 'name' => 'me');
  1594. $b = array('key' => 1, 'another' => 0);
  1595. $expected = array('name' => 'me');
  1596. $result = Set::diff($a, $b);
  1597. $this->assertEquals($expected, $result);
  1598. $a = array('key' => 'value', 'another' => null, 'name' => 'me');
  1599. $b = array('key' => 'differentValue', 'another' => null);
  1600. $expected = array('key' => 'value', 'name' => 'me');
  1601. $result = Set::diff($a, $b);
  1602. $this->assertEquals($expected, $result);
  1603. $a = array('key' => 'value', 'another' => null, 'name' => 'me');
  1604. $b = array('key' => 'differentValue', 'another' => 'value');
  1605. $expected = array('key' => 'value', 'another' => null, 'name' => 'me');
  1606. $result = Set::diff($a, $b);
  1607. $this->assertEquals($expected, $result);
  1608. $a = array('key' => 'value', 'another' => null, 'name' => 'me');
  1609. $b = array('key' => 'differentValue', 'another' => 'value');
  1610. $expected = array('key' => 'differentValue', 'another' => 'value', 'name' => 'me');
  1611. $result = Set::diff($b, $a);
  1612. $this->assertEquals($expected, $result);
  1613. $a = array('key' => 'value', 'another' => null, 'name' => 'me');
  1614. $b = array(0 => 'differentValue', 1 => 'value');
  1615. $expected = $a + $b;
  1616. $result = Set::diff($a, $b);
  1617. $this->assertEquals($expected, $result);
  1618. }
  1619. /**
  1620. * testContains method
  1621. *
  1622. * @return void
  1623. */
  1624. public function testContains() {
  1625. $a = array(
  1626. 0 => array('name' => 'main'),
  1627. 1 => array('name' => 'about')
  1628. );
  1629. $b = array(
  1630. 0 => array('name' => 'main'),
  1631. 1 => array('name' => 'about'),
  1632. 2 => array('name' => 'contact'),
  1633. 'a' => 'b'
  1634. );
  1635. $this->assertTrue(Set::contains($a, $a));
  1636. $this->assertFalse(Set::contains($a, $b));
  1637. $this->assertTrue(Set::contains($b, $a));
  1638. }
  1639. /**
  1640. * testCombine method
  1641. *
  1642. * @return void
  1643. */
  1644. public function testCombine() {
  1645. $result = Set::combine(array(), '{n}.User.id', '{n}.User.Data');
  1646. $this->assertTrue(empty($result));
  1647. $result = Set::combine('', '{n}.User.id', '{n}.User.Data');
  1648. $this->assertTrue(empty($result));
  1649. $a = array(
  1650. array('User' => array('id' => 2, 'group_id' => 1,
  1651. 'Data' => array('user' => 'mariano.iglesias','name' => 'Mariano Iglesias'))),
  1652. array('User' => array('id' => 14, 'group_id' => 2,
  1653. 'Data' => array('user' => 'phpnut', 'name' => 'Larry E. Masters'))),
  1654. array('User' => array('id' => 25, 'group_id' => 1,
  1655. 'Data' => array('user' => 'gwoo','name' => 'The Gwoo'))));
  1656. $result = Set::combine($a, '{n}.User.id');
  1657. $expected = array(2 => null, 14 => null, 25 => null);
  1658. $this->assertEquals($expected, $result);
  1659. $result = Set::combine($a, '{n}.User.id', '{n}.User.non-existant');
  1660. $expected = array(2 => null, 14 => null, 25 => null);
  1661. $this->assertEquals($expected, $result);
  1662. $result = Set::combine($a, '{n}.User.id', '{n}.User.Data');
  1663. $expected = array(
  1664. 2 => array('user' => 'mariano.iglesias', 'name' => 'Mariano Iglesias'),
  1665. 14 => array('user' => 'phpnut', 'name' => 'Larry E. Masters'),
  1666. 25 => array('user' => 'gwoo', 'name' => 'The Gwoo'));
  1667. $this->assertEquals($expected, $result);
  1668. $result = Set::combine($a, '{n}.User.id', '{n}.User.Data.name');
  1669. $expected = array(
  1670. 2 => 'Mariano Iglesias',
  1671. 14 => 'Larry E. Masters',
  1672. 25 => 'The Gwoo');
  1673. $this->assertEquals($expected, $result);
  1674. $result = Set::combine($a, '{n}.User.id', '{n}.User.Data', '{n}.User.group_id');
  1675. $expected = array(
  1676. 1 => array(
  1677. 2 => array('user' => 'mariano.iglesias', 'name' => 'Mariano Iglesias'),
  1678. 25 => array('user' => 'gwoo', 'name' => 'The Gwoo')),
  1679. 2 => array(
  1680. 14 => array('user' => 'phpnut', 'name' => 'Larry E. Masters')));
  1681. $this->assertEquals($expected, $result);
  1682. $result = Set::combine($a, '{n}.User.id', '{n}.User.Data.name', '{n}.User.group_id');
  1683. $expected = array(
  1684. 1 => array(
  1685. 2 => 'Mariano Iglesias',
  1686. 25 => 'The Gwoo'),
  1687. 2 => array(
  1688. 14 => 'Larry E. Masters'));
  1689. $this->assertEquals($expected, $result);
  1690. $result = Set::combine($a, '{n}.User.id');
  1691. $expected = array(2 => null, 14 => null, 25 => null);
  1692. $this->assertEquals($expected, $result);
  1693. $result = Set::combine($a, '{n}.User.id', '{n}.User.Data');
  1694. $expected = array(
  1695. 2 => array('user' => 'mariano.iglesias', 'name' => 'Mariano Iglesias'),
  1696. 14 => array('user' => 'phpnut', 'name' => 'Larry E. Masters'),
  1697. 25 => array('user' => 'gwoo', 'name' => 'The Gwoo'));
  1698. $this->assertEquals($expected, $result);
  1699. $result = Set::combine($a, '{n}.User.id', '{n}.User.Data.name');
  1700. $expected = array(2 => 'Mariano Iglesias', 14 => 'Larry E. Masters', 25 => 'The Gwoo');
  1701. $this->assertEquals($expected, $result);
  1702. $result = Set::combine($a, '{n}.User.id', '{n}.User.Data', '{n}.User.group_id');
  1703. $expected = array(
  1704. 1 => array(
  1705. 2 => array('user' => 'mariano.iglesias', 'name' => 'Mariano Iglesias'),
  1706. 25 => array('user' => 'gwoo', 'name' => 'The Gwoo')),
  1707. 2 => array(
  1708. 14 => array('user' => 'phpnut', 'name' => 'Larry E. Masters')));
  1709. $this->assertEquals($expected, $result);
  1710. $result = Set::combine($a, '{n}.User.id', '{n}.User.Data.name', '{n}.User.group_id');
  1711. $expected = array(
  1712. 1 => array(
  1713. 2 => 'Mariano Iglesias',
  1714. 25 => 'The Gwoo'),
  1715. 2 => array(
  1716. 14 => 'Larry E. Masters'));
  1717. $this->assertEquals($expected, $result);
  1718. $result = Set::combine($a, '{n}.User.id', array('{0}: {1}', '{n}.User.Data.user', '{n}.User.Data.name'), '{n}.User.group_id');
  1719. $expected = array(
  1720. 1 => array(
  1721. 2 => 'mariano.iglesias: Mariano Iglesias',
  1722. 25 => 'gwoo: The Gwoo'),
  1723. 2 => array(14 => 'phpnut: Larry E. Masters'));
  1724. $this->assertEquals($expected, $result);
  1725. $result = Set::combine($a, array('{0}: {1}', '{n}.User.Data.user', '{n}.User.Data.name'), '{n}.User.id');
  1726. $expected = array('mariano.iglesias: Mariano Iglesias' => 2, 'phpnut: Larry E. Masters' => 14, 'gwoo: The Gwoo' => 25);
  1727. $this->assertEquals($expected, $result);
  1728. $result = Set::combine($a, array('{1}: {0}', '{n}.User.Data.user', '{n}.User.Data.name'), '{n}.User.id');
  1729. $expected = array('Mariano Iglesias: mariano.iglesias' => 2, 'Larry E. Masters: phpnut' => 14, 'The Gwoo: gwoo' => 25);
  1730. $this->assertEquals($expected, $result);
  1731. $result = Set::combine($a, array('%1$s: %2$d', '{n}.User.Data.user', '{n}.User.id'), '{n}.User.Data.name');
  1732. $expected = array('mariano.iglesias: 2' => 'Mariano Iglesias', 'phpnut: 14' => 'Larry E. Masters', 'gwoo: 25' => 'The Gwoo');
  1733. $this->assertEquals($expected, $result);
  1734. $result = Set::combine($a, array('%2$d: %1$s', '{n}.User.Data.user', '{n}.User.id'), '{n}.User.Data.name');
  1735. $expected = array('2: mariano.iglesias' => 'Mariano Iglesias', '14: phpnut' => 'Larry E. Masters', '25: gwoo' => 'The Gwoo');
  1736. $this->assertEquals($expected, $result);
  1737. $b = new stdClass();
  1738. $b->users = array(
  1739. array('User' => array('id' => 2, 'group_id' => 1,
  1740. 'Data' => array('user' => 'mariano.iglesias','name' => 'Mariano Iglesias'))),
  1741. array('User' => array('id' => 14, 'group_id' => 2,
  1742. 'Data' => array('user' => 'phpnut', 'name' => 'Larry E. Masters'))),
  1743. array('User' => array('id' => 25, 'group_id' => 1,
  1744. 'Data' => array('user' => 'gwoo','name' => 'The Gwoo'))));
  1745. $result = Set::combine($b, 'users.{n}.User.id');
  1746. $expected = array(2 => null, 14 => null, 25 => null);
  1747. $this->assertEquals($expected, $result);
  1748. $result = Set::combine($b, 'users.{n}.User.id', 'users.{n}.User.non-existant');
  1749. $expected = array(2 => null, 14 => null, 25 => null);
  1750. $this->assertEquals($expected, $result);
  1751. $result = Set::combine($a, 'fail', 'fail');
  1752. $this->assertEquals($result, array());
  1753. }
  1754. /**
  1755. * testMapReverse method
  1756. *
  1757. * @return void
  1758. */
  1759. public function testMapReverse() {
  1760. $result = Set::reverse(null);
  1761. $this->assertEquals($result, null);
  1762. $result = Set::reverse(false);
  1763. $this->assertEquals($result, false);
  1764. $expected = array(
  1765. 'Array1' => array(
  1766. 'Array1Data1' => 'Array1Data1 value 1', 'Array1Data2' => 'Array1Data2 value 2'),
  1767. 'Array2' => array(
  1768. 0 => array('Array2Data1' => 1, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4'),
  1769. 1 => array('Array2Data1' => 2, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4'),
  1770. 2 => array('Array2Data1' => 3, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4'),
  1771. 3 => array('Array2Data1' => 4, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4'),
  1772. 4 => array('Array2Data1' => 5, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4')),
  1773. 'Array3' => array(
  1774. 0 => array('Array3Data1' => 1, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4'),
  1775. 1 => array('Array3Data1' => 2, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4'),
  1776. 2 => array('Array3Data1' => 3, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4'),
  1777. 3 => array('Array3Data1' => 4, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4'),
  1778. 4 => array('Array3Data1' => 5, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4')));
  1779. $map = Set::map($expected, true);
  1780. $this->assertEquals($map->Array1->Array1Data1, $expected['Array1']['Array1Data1']);
  1781. $this->assertEquals($map->Array2[0]->Array2Data1, $expected['Array2'][0]['Array2Data1']);
  1782. $result = Set::reverse($map);
  1783. $this->assertEquals($expected, $result);
  1784. $expected = array(
  1785. 'Post' => array('id' => 1, 'title' => 'First Post'),
  1786. 'Comment' => array(
  1787. array('id' => 1, 'title' => 'First Comment'),
  1788. array('id' => 2, 'title' => 'Second Comment')
  1789. ),
  1790. 'Tag' => array(
  1791. array('id' => 1, 'title' => 'First Tag'),
  1792. array('id' => 2, 'title' => 'Second Tag')
  1793. ),
  1794. );
  1795. $map = Set::map($expected);
  1796. $this->assertEquals($map->title, $expected['Post']['title']);
  1797. foreach ($map->Comment as $comment) {
  1798. $ids[] = $comment->id;
  1799. }
  1800. $this->assertEquals($ids, array(1, 2));
  1801. $expected = array(
  1802. 'Array1' => array(
  1803. 'Array1Data1' => 'Array1Data1 value 1', 'Array1Data2' => 'Array1Data2 value 2', 'Array1Data3' => 'Array1Data3 value 3','Array1Data4' => 'Array1Data4 value 4',
  1804. 'Array1Data5' => 'Array1Data5 value 5', 'Array1Data6' => 'Array1Data6 value 6', 'Array1Data7' => 'Array1Data7 value 7', 'Array1Data8' => 'Array1Data8 value 8'),
  1805. 'string' => 1,
  1806. 'another' => 'string',
  1807. 'some' => 'thing else',
  1808. 'Array2' => array(
  1809. 0 => array('Array2Data1' => 1, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4'),
  1810. 1 => array('Array2Data1' => 2, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4'),
  1811. 2 => array('Array2Data1' => 3, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4'),
  1812. 3 => array('Array2Data1' => 4, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4'),
  1813. 4 => array('Array2Data1' => 5, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4')),
  1814. 'Array3' => array(
  1815. 0 => array('Array3Data1' => 1, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4'),
  1816. 1 => array('Array3Data1' => 2, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4'),
  1817. 2 => array('Array3Data1' => 3, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4'),
  1818. 3 => array('Array3Data1' => 4, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4'),
  1819. 4 => array('Array3Data1' => 5, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4')));
  1820. $map = Set::map($expected, true);
  1821. $result = Set::reverse($map);
  1822. $this->assertEquals($expected, $result);
  1823. $expected = array(
  1824. 'Array1' => array(
  1825. 'Array1Data1' => 'Array1Data1 value 1', 'Array1Data2' => 'Array1Data2 value 2', 'Array1Data3' => 'Array1Data3 value 3','Array1Data4' => 'Array1Data4 value 4',
  1826. 'Array1Data5' => 'Array1Data5 value 5', 'Array1Data6' => 'Array1Data6 value 6', 'Array1Data7' => 'Array1Data7 value 7', 'Array1Data8' => 'Array1Data8 value 8'),
  1827. 'string' => 1,
  1828. 'another' => 'string',
  1829. 'some' => 'thing else',
  1830. 'Array2' => array(
  1831. 0 => array('Array2Data1' => 1, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4'),
  1832. 1 => array('Array2Data1' => 2, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4'),
  1833. 2 => array('Array2Data1' => 3, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4'),
  1834. 3 => array('Array2Data1' => 4, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4'),
  1835. 4 => array('Array2Data1' => 5, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4')),
  1836. 'string2' => 1,
  1837. 'another2' => 'string',
  1838. 'some2' => 'thing else',
  1839. 'Array3' => array(
  1840. 0 => array('Array3Data1' => 1, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4'),
  1841. 1 => array('Array3Data1' => 2, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4'),
  1842. 2 => array('Array3Data1' => 3, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4'),
  1843. 3 => array('Array3Data1' => 4, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4'),
  1844. 4 => array('Array3Data1' => 5, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4')),
  1845. 'string3' => 1,
  1846. 'another3' => 'string',
  1847. 'some3' => 'thing else');
  1848. $map = Set::map($expected, true);
  1849. $result = Set::reverse($map);
  1850. $this->assertEquals($expected, $result);
  1851. $expected = array('User' => array('psword' => 'whatever', 'Icon' => array('id' => 851)));
  1852. $map = Set::map($expected);
  1853. $result = Set::reverse($map);
  1854. $this->assertEquals($expected, $result);
  1855. $expected = array('User' => array('psword' => 'whatever', 'Icon' => array('id' => 851)));
  1856. $class = new stdClass;
  1857. $class->User = new stdClass;
  1858. $class->User->psword = 'whatever';
  1859. $class->User->Icon = new stdClass;
  1860. $class->User->Icon->id = 851;
  1861. $result = Set::reverse($class);
  1862. $this->assertEquals($expected, $result);
  1863. $expected = array('User' => array('psword' => 'whatever', 'Icon' => array('id' => 851), 'Profile' => array('name' => 'Some Name', 'address' => 'Some Address')));
  1864. $class = new stdClass;
  1865. $class->User = new stdClass;
  1866. $class->User->psword = 'whatever';
  1867. $class->User->Icon = new stdClass;
  1868. $class->User->Icon->id = 851;
  1869. $class->User->Profile = new stdClass;
  1870. $class->User->Profile->name = 'Some Name';
  1871. $class->User->Profile->address = 'Some Address';
  1872. $result = Set::reverse($class);
  1873. $this->assertEquals($expected, $result);
  1874. $expected = array('User' => array('psword' => 'whatever',
  1875. 'Icon' => array('id' => 851),
  1876. 'Profile' => array('name' => 'Some Name', 'address' => 'Some Address'),
  1877. 'Comment' => array(
  1878. array('id' => 1, 'article_id' => 1, 'user_id' => 1, 'comment' => 'First Comment for First Article', 'published' => 'Y', 'created' => '2007-03-18 10:47:23', 'updated' => '2007-03-18 10:49:31'),
  1879. array('id' => 2, 'article_id' => 1, 'user_id' => 2, 'comment' => 'Second Comment for First Article', 'published' => 'Y', 'created' => '2007-03-18 10:47:23', 'updated' => '2007-03-18 10:49:31'))));
  1880. $class = new stdClass;
  1881. $class->User = new stdClass;
  1882. $class->User->psword = 'whatever';
  1883. $class->User->Icon = new stdClass;
  1884. $class->User->Icon->id = 851;
  1885. $class->User->Profile = new stdClass;
  1886. $class->User->Profile->name = 'Some Name';
  1887. $class->User->Profile->address = 'Some Address';
  1888. $class->User->Comment = new stdClass;
  1889. $class->User->Comment->{'0'} = new stdClass;
  1890. $class->User->Comment->{'0'}->id = 1;
  1891. $class->User->Comment->{'0'}->article_id = 1;
  1892. $class->User->Comment->{'0'}->user_id = 1;
  1893. $class->User->Comment->{'0'}->comment = 'First Comment for First Article';
  1894. $class->User->Comment->{'0'}->published = 'Y';
  1895. $class->User->Comment->{'0'}->created = '2007-03-18 10:47:23';
  1896. $class->User->Comment->{'0'}->updated = '2007-03-18 10:49:31';
  1897. $class->User->Comment->{'1'} = new stdClass;
  1898. $class->User->Comment->{'1'}->id = 2;
  1899. $class->User->Comment->{'1'}->article_id = 1;
  1900. $class->User->Comment->{'1'}->user_id = 2;
  1901. $class->User->Comment->{'1'}->comment = 'Second Comment for First Article';
  1902. $class->User->Comment->{'1'}->published = 'Y';
  1903. $class->User->Comment->{'1'}->created = '2007-03-18 10:47:23';
  1904. $class->User->Comment->{'1'}->updated = '2007-03-18 10:49:31';
  1905. $result = Set::reverse($class);
  1906. $this->assertEquals($expected, $result);
  1907. $expected = array('User' => array('psword' => 'whatever',
  1908. 'Icon' => array('id' => 851),
  1909. 'Profile' => array('name' => 'Some Name', 'address' => 'Some Address'),
  1910. 'Comment' => array(
  1911. array('id' => 1, 'article_id' => 1, 'user_id' => 1, 'comment' => 'First Comment for First Article', 'published' => 'Y', 'created' => '2007-03-18 10:47:23', 'updated' => '2007-03-18 10:49:31'),
  1912. array('id' => 2, 'article_id' => 1, 'user_id' => 2, 'comment' => 'Second Comment for First Article', 'published' => 'Y', 'created' => '2007-03-18 10:47:23', 'updated' => '2007-03-18 10:49:31'))));
  1913. $class = new stdClass;
  1914. $class->User = new stdClass;
  1915. $class->User->psword = 'whatever';
  1916. $class->User->Icon = new stdClass;
  1917. $class->User->Icon->id = 851;
  1918. $class->User->Profile = new stdClass;
  1919. $class->User->Profile->name = 'Some Name';
  1920. $class->User->Profile->address = 'Some Address';
  1921. $class->User->Comment = array();
  1922. $comment = new stdClass;
  1923. $comment->id = 1;
  1924. $comment->article_id = 1;
  1925. $comment->user_id = 1;
  1926. $comment->comment = 'First Comment for First Article';
  1927. $comment->published = 'Y';
  1928. $comment->created = '2007-03-18 10:47:23';
  1929. $comment->updated = '2007-03-18 10:49:31';
  1930. $comment2 = new stdClass;
  1931. $comment2->id = 2;
  1932. $comment2->article_id = 1;
  1933. $comment2->user_id = 2;
  1934. $comment2->comment = 'Second Comment for First Article';
  1935. $comment2->published = 'Y';
  1936. $comment2->created = '2007-03-18 10:47:23';
  1937. $comment2->updated = '2007-03-18 10:49:31';
  1938. $class->User->Comment = array($comment, $comment2);
  1939. $result = Set::reverse($class);
  1940. $this->assertEquals($expected, $result);
  1941. $class = new stdClass;
  1942. $class->User = new stdClass;
  1943. $class->User->id = 100;
  1944. $class->someString = 'this is some string';
  1945. $class->Profile = new stdClass;
  1946. $class->Profile->name = 'Joe Mamma';
  1947. $result = Set::reverse($class);
  1948. $expected = array('User' => array('id' => '100'), 'someString' => 'this is some string', 'Profile' => array('name' => 'Joe Mamma'));
  1949. $this->assertEquals($expected, $result);
  1950. $class = new stdClass;
  1951. $class->User = new stdClass;
  1952. $class->User->id = 100;
  1953. $class->User->_name_ = 'User';
  1954. $class->Profile = new stdClass;
  1955. $class->Profile->name = 'Joe Mamma';
  1956. $class->Profile->_name_ = 'Profile';
  1957. $result = Set::reverse($class);
  1958. $expected = array('User' => array('id' => '100'), 'Profile' => array('name' => 'Joe Mamma'));
  1959. $this->assertEquals($expected, $result);
  1960. }
  1961. /**
  1962. * testFormatting method
  1963. *
  1964. * @return void
  1965. */
  1966. public function testFormatting() {
  1967. $data = array(
  1968. array('Person' => array('first_name' => 'Nate', 'last_name' => 'Abele', 'city' => 'Boston', 'state' => 'MA', 'something' => '42')),
  1969. array('Person' => array('first_name' => 'Larry', 'last_name' => 'Masters', 'city' => 'Boondock', 'state' => 'TN', 'something' => '{0}')),
  1970. array('Person' => array('first_name' => 'Garrett', 'last_name' => 'Woodworth', 'city' => 'Venice Beach', 'state' => 'CA', 'something' => '{1}')));
  1971. $result = Set::format($data, '{1}, {0}', array('{n}.Person.first_name', '{n}.Person.last_name'));
  1972. $expected = array('Abele, Nate', 'Masters, Larry', 'Woodworth, Garrett');
  1973. $this->assertEquals($expected, $result);
  1974. $result = Set::format($data, '{0}, {1}', array('{n}.Person.last_name', '{n}.Person.first_name'));
  1975. $this->assertEquals($expected, $result);
  1976. $result = Set::format($data, '{0}, {1}', array('{n}.Person.city', '{n}.Person.state'));
  1977. $expected = array('Boston, MA', 'Boondock, TN', 'Venice Beach, CA');
  1978. $this->assertEquals($expected, $result);
  1979. $result = Set::format($data, '{{0}, {1}}', array('{n}.Person.city', '{n}.Person.state'));
  1980. $expected = array('{Boston, MA}', '{Boondock, TN}', '{Venice Beach, CA}');
  1981. $this->assertEquals($expected, $result);
  1982. $result = Set::format($data, '{{0}, {1}}', array('{n}.Person.something', '{n}.Person.something'));
  1983. $expected = array('{42, 42}', '{{0}, {0}}', '{{1}, {1}}');
  1984. $this->assertEquals($expected, $result);
  1985. $result = Set::format($data, '{%2$d, %1$s}', array('{n}.Person.something', '{n}.Person.something'));
  1986. $expected = array('{42, 42}', '{0, {0}}', '{0, {1}}');
  1987. $this->assertEquals($expected, $result);
  1988. $result = Set::format($data, '{%1$s, %1$s}', array('{n}.Person.something', '{n}.Person.something'));
  1989. $expected = array('{42, 42}', '{{0}, {0}}', '{{1}, {1}}');
  1990. $this->assertEquals($expected, $result);
  1991. $result = Set::format($data, '%2$d, %1$s', array('{n}.Person.first_name', '{n}.Person.something'));
  1992. $expected = array('42, Nate', '0, Larry', '0, Garrett');
  1993. $this->assertEquals($expected, $result);
  1994. $result = Set::format($data, '%1$s, %2$d', array('{n}.Person.first_name', '{n}.Person.something'));
  1995. $expected = array('Nate, 42', 'Larry, 0', 'Garrett, 0');
  1996. $this->assertEquals($expected, $result);
  1997. }
  1998. /**
  1999. * testFormattingNullValues method
  2000. *
  2001. * @return void
  2002. */
  2003. public function testFormattingNullValues() {
  2004. $data = array(
  2005. array('Person' => array('first_name' => 'Nate', 'last_name' => 'Abele', 'city' => 'Boston', 'state' => 'MA', 'something' => '42')),
  2006. array('Person' => array('first_name' => 'Larry', 'last_name' => 'Masters', 'city' => 'Boondock', 'state' => 'TN', 'something' => null)),
  2007. array('Person' => array('first_name' => 'Garrett', 'last_name' => 'Woodworth', 'city' => 'Venice Beach', 'state' => 'CA', 'something' => null)));
  2008. $result = Set::format($data, '%s', array('{n}.Person.something'));
  2009. $expected = array('42', '', '');
  2010. $this->assertEquals($expected, $result);
  2011. $result = Set::format($data, '{0}, {1}', array('{n}.Person.city', '{n}.Person.something'));
  2012. $expected = array('Boston, 42', 'Boondock, ', 'Venice Beach, ');
  2013. $this->assertEquals($expected, $result);
  2014. }
  2015. /**
  2016. * testCountDim method
  2017. *
  2018. * @return void
  2019. */
  2020. public function testCountDim() {
  2021. $data = array('one', '2', 'three');
  2022. $result = Set::countDim($data);
  2023. $this->assertEquals($result, 1);
  2024. $data = array('1' => '1.1', '2', '3');
  2025. $result = Set::countDim($data);
  2026. $this->assertEquals($result, 1);
  2027. $data = array('1' => array('1.1' => '1.1.1'), '2', '3' => array('3.1' => '3.1.1'));
  2028. $result = Set::countDim($data);
  2029. $this->assertEquals($result, 2);
  2030. $data = array('1' => '1.1', '2', '3' => array('3.1' => '3.1.1'));
  2031. $result = Set::countDim($data);
  2032. $this->assertEquals($result, 1);
  2033. $data = array('1' => '1.1', '2', '3' => array('3.1' => '3.1.1'));
  2034. $result = Set::countDim($data, true);
  2035. $this->assertEquals($result, 2);
  2036. $data = array('1' => array('1.1' => '1.1.1'), '2', '3' => array('3.1' => array('3.1.1' => '3.1.1.1')));
  2037. $result = Set::countDim($data);
  2038. $this->assertEquals($result, 2);
  2039. $data = array('1' => array('1.1' => '1.1.1'), '2', '3' => array('3.1' => array('3.1.1' => '3.1.1.1')));
  2040. $result = Set::countDim($data, true);
  2041. $this->assertEquals($result, 3);
  2042. $data = array('1' => array('1.1' => '1.1.1'), array('2' => array('2.1' => array('2.1.1' => '2.1.1.1'))), '3' => array('3.1' => array('3.1.1' => '3.1.1.1')));
  2043. $result = Set::countDim($data, true);
  2044. $this->assertEquals($result, 4);
  2045. $data = array('1' => array('1.1' => '1.1.1'), array('2' => array('2.1' => array('2.1.1' => array('2.1.1.1')))), '3' => array('3.1' => array('3.1.1' => '3.1.1.1')));
  2046. $result = Set::countDim($data, true);
  2047. $this->assertEquals($result, 5);
  2048. $data = array('1' => array('1.1' => '1.1.1'), array('2' => array('2.1' => array('2.1.1' => array('2.1.1.1' => '2.1.1.1.1')))), '3' => array('3.1' => array('3.1.1' => '3.1.1.1')));
  2049. $result = Set::countDim($data, true);
  2050. $this->assertEquals($result, 5);
  2051. $set = array('1' => array('1.1' => '1.1.1'), array('2' => array('2.1' => array('2.1.1' => array('2.1.1.1' => '2.1.1.1.1')))), '3' => array('3.1' => array('3.1.1' => '3.1.1.1')));
  2052. $result = Set::countDim($set, false, 0);
  2053. $this->assertEquals($result, 2);
  2054. $result = Set::countDim($set, true);
  2055. $this->assertEquals($result, 5);
  2056. }
  2057. /**
  2058. * testMapNesting method
  2059. *
  2060. * @return void
  2061. */
  2062. public function testMapNesting() {
  2063. $expected = array(
  2064. array(
  2065. "IndexedPage" => array(
  2066. "id" => 1,
  2067. "url" => 'http://blah.com/',
  2068. 'hash' => '68a9f053b19526d08e36c6a9ad150737933816a5',
  2069. 'headers' => array(
  2070. 'Date' => "Wed, 14 Nov 2007 15:51:42 GMT",
  2071. 'Server' => "Apache",
  2072. 'Expires' => "Thu, 19 Nov 1981 08:52:00 GMT",
  2073. 'Cache-Control' => "private",
  2074. 'Pragma' => "no-cache",
  2075. 'Content-Type' => "text/html; charset=UTF-8",
  2076. 'X-Original-Transfer-Encoding' => "chunked",
  2077. 'Content-Length' => "50210",
  2078. ),
  2079. 'meta' => array(
  2080. 'keywords' => array('testing','tests'),
  2081. 'description' => 'describe me',
  2082. ),
  2083. 'get_vars' => '',
  2084. 'post_vars' => array(),
  2085. 'cookies' => array('PHPSESSID' => "dde9896ad24595998161ffaf9e0dbe2d"),
  2086. 'redirect' => '',
  2087. 'created' => "1195055503",
  2088. 'updated' => "1195055503",
  2089. )
  2090. ),
  2091. array(
  2092. "IndexedPage" => array(
  2093. "id" => 2,
  2094. "url" => 'http://blah.com/',
  2095. 'hash' => '68a9f053b19526d08e36c6a9ad150737933816a5',
  2096. 'headers' => array(
  2097. 'Date' => "Wed, 14 Nov 2007 15:51:42 GMT",
  2098. 'Server' => "Apache",
  2099. 'Expires' => "Thu, 19 Nov 1981 08:52:00 GMT",
  2100. 'Cache-Control' => "private",
  2101. 'Pragma' => "no-cache",
  2102. 'Content-Type' => "text/html; charset=UTF-8",
  2103. 'X-Original-Transfer-Encoding' => "chunked",
  2104. 'Content-Length' => "50210",
  2105. ),
  2106. 'meta' => array(
  2107. 'keywords' => array('testing','tests'),
  2108. 'description' => 'describe me',
  2109. ),
  2110. 'get_vars' => '',
  2111. 'post_vars' => array(),
  2112. 'cookies' => array('PHPSESSID' => "dde9896ad24595998161ffaf9e0dbe2d"),
  2113. 'redirect' => '',
  2114. 'created' => "1195055503",
  2115. 'updated' => "1195055503",
  2116. ),
  2117. )
  2118. );
  2119. $mapped = Set::map($expected);
  2120. $ids = array();
  2121. foreach ($mapped as $object) {
  2122. $ids[] = $object->id;
  2123. }
  2124. $this->assertEquals($ids, array(1, 2));
  2125. $this->assertEquals(get_object_vars($mapped[0]->headers), $expected[0]['IndexedPage']['headers']);
  2126. $result = Set::reverse($mapped);
  2127. $this->assertEquals($expected, $result);
  2128. $data = array(
  2129. array(
  2130. "IndexedPage" => array(
  2131. "id" => 1,
  2132. "url" => 'http://blah.com/',
  2133. 'hash' => '68a9f053b19526d08e36c6a9ad150737933816a5',
  2134. 'get_vars' => '',
  2135. 'redirect' => '',
  2136. 'created' => "1195055503",
  2137. 'updated' => "1195055503",
  2138. )
  2139. ),
  2140. array(
  2141. "IndexedPage" => array(
  2142. "id" => 2,
  2143. "url" => 'http://blah.com/',
  2144. 'hash' => '68a9f053b19526d08e36c6a9ad150737933816a5',
  2145. 'get_vars' => '',
  2146. 'redirect' => '',
  2147. 'created' => "1195055503",
  2148. 'updated' => "1195055503",
  2149. ),
  2150. )
  2151. );
  2152. $mapped = Set::map($data);
  2153. $expected = new stdClass();
  2154. $expected->_name_ = 'IndexedPage';
  2155. $expected->id = 2;
  2156. $expected->url = 'http://blah.com/';
  2157. $expected->hash = '68a9f053b19526d08e36c6a9ad150737933816a5';
  2158. $expected->get_vars = '';
  2159. $expected->redirect = '';
  2160. $expected->created = "1195055503";
  2161. $expected->updated = "1195055503";
  2162. $this->assertEquals($mapped[1], $expected);
  2163. $ids = array();
  2164. foreach ($mapped as $object) {
  2165. $ids[] = $object->id;
  2166. }
  2167. $this->assertEquals($ids, array(1, 2));
  2168. $result = Set::map(null);
  2169. $expected = null;
  2170. $this->assertEquals($expected, $result);
  2171. }
  2172. /**
  2173. * testNestedMappedData method
  2174. *
  2175. * @return void
  2176. */
  2177. public function testNestedMappedData() {
  2178. $result = Set::map(array(
  2179. array(
  2180. 'Post' => array('id' => '1', 'author_id' => '1', 'title' => 'First Post', 'body' => 'First Post Body', 'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'),
  2181. 'Author' => array('id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31', 'test' => 'working'),
  2182. )
  2183. , array(
  2184. 'Post' => array('id' => '2', 'author_id' => '3', 'title' => 'Second Post', 'body' => 'Second Post Body', 'published' => 'Y', 'created' => '2007-03-18 10:41:23', 'updated' => '2007-03-18 10:43:31'),
  2185. 'Author' => array('id' => '3', 'user' => 'larry', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:20:23', 'updated' => '2007-03-17 01:22:31', 'test' => 'working'),
  2186. )
  2187. ));
  2188. $expected = new stdClass;
  2189. $expected->_name_ = 'Post';
  2190. $expected->id = '1';
  2191. $expected->author_id = '1';
  2192. $expected->title = 'First Post';
  2193. $expected->body = 'First Post Body';
  2194. $expected->published = 'Y';
  2195. $expected->created = "2007-03-18 10:39:23";
  2196. $expected->updated = "2007-03-18 10:41:31";
  2197. $expected->Author = new stdClass;
  2198. $expected->Author->id = '1';
  2199. $expected->Author->user = 'mariano';
  2200. $expected->Author->password = '5f4dcc3b5aa765d61d8327deb882cf99';
  2201. $expected->Author->created = "2007-03-17 01:16:23";
  2202. $expected->Author->updated = "2007-03-17 01:18:31";
  2203. $expected->Author->test = "working";
  2204. $expected->Author->_name_ = 'Author';
  2205. $expected2 = new stdClass;
  2206. $expected2->_name_ = 'Post';
  2207. $expected2->id = '2';
  2208. $expected2->author_id = '3';
  2209. $expected2->title = 'Second Post';
  2210. $expected2->body = 'Second Post Body';
  2211. $expected2->published = 'Y';
  2212. $expected2->created = "2007-03-18 10:41:23";
  2213. $expected2->updated = "2007-03-18 10:43:31";
  2214. $expected2->Author = new stdClass;
  2215. $expected2->Author->id = '3';
  2216. $expected2->Author->user = 'larry';
  2217. $expected2->Author->password = '5f4dcc3b5aa765d61d8327deb882cf99';
  2218. $expected2->Author->created = "2007-03-17 01:20:23";
  2219. $expected2->Author->updated = "2007-03-17 01:22:31";
  2220. $expected2->Author->test = "working";
  2221. $expected2->Author->_name_ = 'Author';
  2222. $test = array();
  2223. $test[0] = $expected;
  2224. $test[1] = $expected2;
  2225. $this->assertEquals($test, $result);
  2226. $result = Set::map(
  2227. array(
  2228. 'Post' => array('id' => '1', 'author_id' => '1', 'title' => 'First Post', 'body' => 'First Post Body', 'published' => 'Y', 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'),
  2229. 'Author' => array('id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31', 'test' => 'working'),
  2230. )
  2231. );
  2232. $expected = new stdClass;
  2233. $expected->_name_ = 'Post';
  2234. $expected->id = '1';
  2235. $expected->author_id = '1';
  2236. $expected->title = 'First Post';
  2237. $expected->body = 'First Post Body';
  2238. $expected->published = 'Y';
  2239. $expected->created = "2007-03-18 10:39:23";
  2240. $expected->updated = "2007-03-18 10:41:31";
  2241. $expected->Author = new stdClass;
  2242. $expected->Author->id = '1';
  2243. $expected->Author->user = 'mariano';
  2244. $expected->Author->password = '5f4dcc3b5aa765d61d8327deb882cf99';
  2245. $expected->Author->created = "2007-03-17 01:16:23";
  2246. $expected->Author->updated = "2007-03-17 01:18:31";
  2247. $expected->Author->test = "working";
  2248. $expected->Author->_name_ = 'Author';
  2249. $this->assertEquals($expected, $result);
  2250. //Case where extra HABTM fields come back in a result
  2251. $data = array(
  2252. 'User' => array(
  2253. 'id' => 1,
  2254. 'email' => 'user@example.com',
  2255. 'first_name' => 'John',
  2256. 'last_name' => 'Smith',
  2257. ),
  2258. 'Piece' => array(
  2259. array(
  2260. 'id' => 1,
  2261. 'title' => 'Moonlight Sonata',
  2262. 'composer' => 'Ludwig van Beethoven',
  2263. 'PiecesUser' => array(
  2264. 'id' => 1,
  2265. 'created' => '2008-01-01 00:00:00',
  2266. 'modified' => '2008-01-01 00:00:00',
  2267. 'piece_id' => 1,
  2268. 'user_id' => 2,
  2269. )
  2270. ),
  2271. array(
  2272. 'id' => 2,
  2273. 'title' => 'Moonlight Sonata 2',
  2274. 'composer' => 'Ludwig van Beethoven',
  2275. 'PiecesUser' => array(
  2276. 'id' => 2,
  2277. 'created' => '2008-01-01 00:00:00',
  2278. 'modified' => '2008-01-01 00:00:00',
  2279. 'piece_id' => 2,
  2280. 'user_id' => 2,
  2281. )
  2282. )
  2283. )
  2284. );
  2285. $result = Set::map($data);
  2286. $expected = new stdClass();
  2287. $expected->_name_ = 'User';
  2288. $expected->id = 1;
  2289. $expected->email = 'user@example.com';
  2290. $expected->first_name = 'John';
  2291. $expected->last_name = 'Smith';
  2292. $piece = new stdClass();
  2293. $piece->id = 1;
  2294. $piece->title = 'Moonlight Sonata';
  2295. $piece->composer = 'Ludwig van Beethoven';
  2296. $piece->PiecesUser = new stdClass();
  2297. $piece->PiecesUser->id = 1;
  2298. $piece->PiecesUser->created = '2008-01-01 00:00:00';
  2299. $piece->PiecesUser->modified = '2008-01-01 00:00:00';
  2300. $piece->PiecesUser->piece_id = 1;
  2301. $piece->PiecesUser->user_id = 2;
  2302. $piece->PiecesUser->_name_ = 'PiecesUser';
  2303. $piece->_name_ = 'Piece';
  2304. $piece2 = new stdClass();
  2305. $piece2->id = 2;
  2306. $piece2->title = 'Moonlight Sonata 2';
  2307. $piece2->composer = 'Ludwig van Beethoven';
  2308. $piece2->PiecesUser = new stdClass();
  2309. $piece2->PiecesUser->id = 2;
  2310. $piece2->PiecesUser->created = '2008-01-01 00:00:00';
  2311. $piece2->PiecesUser->modified = '2008-01-01 00:00:00';
  2312. $piece2->PiecesUser->piece_id = 2;
  2313. $piece2->PiecesUser->user_id = 2;
  2314. $piece2->PiecesUser->_name_ = 'PiecesUser';
  2315. $piece2->_name_ = 'Piece';
  2316. $expected->Piece = array($piece, $piece2);
  2317. $this->assertEquals($expected, $result);
  2318. //Same data, but should work if _name_ has been manually defined:
  2319. $data = array(
  2320. 'User' => array(
  2321. 'id' => 1,
  2322. 'email' => 'user@example.com',
  2323. 'first_name' => 'John',
  2324. 'last_name' => 'Smith',
  2325. '_name_' => 'FooUser',
  2326. ),
  2327. 'Piece' => array(
  2328. array(
  2329. 'id' => 1,
  2330. 'title' => 'Moonlight Sonata',
  2331. 'composer' => 'Ludwig van Beethoven',
  2332. '_name_' => 'FooPiece',
  2333. 'PiecesUser' => array(
  2334. 'id' => 1,
  2335. 'created' => '2008-01-01 00:00:00',
  2336. 'modified' => '2008-01-01 00:00:00',
  2337. 'piece_id' => 1,
  2338. 'user_id' => 2,
  2339. '_name_' => 'FooPiecesUser',
  2340. )
  2341. ),
  2342. array(
  2343. 'id' => 2,
  2344. 'title' => 'Moonlight Sonata 2',
  2345. 'composer' => 'Ludwig van Beethoven',
  2346. '_name_' => 'FooPiece',
  2347. 'PiecesUser' => array(
  2348. 'id' => 2,
  2349. 'created' => '2008-01-01 00:00:00',
  2350. 'modified' => '2008-01-01 00:00:00',
  2351. 'piece_id' => 2,
  2352. 'user_id' => 2,
  2353. '_name_' => 'FooPiecesUser',
  2354. )
  2355. )
  2356. )
  2357. );
  2358. $result = Set::map($data);
  2359. $expected = new stdClass();
  2360. $expected->_name_ = 'FooUser';
  2361. $expected->id = 1;
  2362. $expected->email = 'user@example.com';
  2363. $expected->first_name = 'John';
  2364. $expected->last_name = 'Smith';
  2365. $piece = new stdClass();
  2366. $piece->id = 1;
  2367. $piece->title = 'Moonlight Sonata';
  2368. $piece->composer = 'Ludwig van Beethoven';
  2369. $piece->_name_ = 'FooPiece';
  2370. $piece->PiecesUser = new stdClass();
  2371. $piece->PiecesUser->id = 1;
  2372. $piece->PiecesUser->created = '2008-01-01 00:00:00';
  2373. $piece->PiecesUser->modified = '2008-01-01 00:00:00';
  2374. $piece->PiecesUser->piece_id = 1;
  2375. $piece->PiecesUser->user_id = 2;
  2376. $piece->PiecesUser->_name_ = 'FooPiecesUser';
  2377. $piece2 = new stdClass();
  2378. $piece2->id = 2;
  2379. $piece2->title = 'Moonlight Sonata 2';
  2380. $piece2->composer = 'Ludwig van Beethoven';
  2381. $piece2->_name_ = 'FooPiece';
  2382. $piece2->PiecesUser = new stdClass();
  2383. $piece2->PiecesUser->id = 2;
  2384. $piece2->PiecesUser->created = '2008-01-01 00:00:00';
  2385. $piece2->PiecesUser->modified = '2008-01-01 00:00:00';
  2386. $piece2->PiecesUser->piece_id = 2;
  2387. $piece2->PiecesUser->user_id = 2;
  2388. $piece2->PiecesUser->_name_ = 'FooPiecesUser';
  2389. $expected->Piece = array($piece, $piece2);
  2390. $this->assertEquals($expected, $result);
  2391. }
  2392. /**
  2393. * testPushDiff method
  2394. *
  2395. * @return void
  2396. */
  2397. public function testPushDiff() {
  2398. $array1 = array('ModelOne' => array('id' => 1001, 'field_one' => 'a1.m1.f1', 'field_two' => 'a1.m1.f2'));
  2399. $array2 = array('ModelTwo' => array('id' => 1002, 'field_one' => 'a2.m2.f1', 'field_two' => 'a2.m2.f2'));
  2400. $result = Set::pushDiff($array1, $array2);
  2401. $this->assertEquals($result, $array1 + $array2);
  2402. $array3 = array('ModelOne' => array('id' => 1003, 'field_one' => 'a3.m1.f1', 'field_two' => 'a3.m1.f2', 'field_three' => 'a3.m1.f3'));
  2403. $result = Set::pushDiff($array1, $array3);
  2404. $expected = array('ModelOne' => array('id' => 1001, 'field_one' => 'a1.m1.f1', 'field_two' => 'a1.m1.f2', 'field_three' => 'a3.m1.f3'));
  2405. $this->assertEquals($expected, $result);
  2406. $array1 = array(
  2407. 0 => array('ModelOne' => array('id' => 1001, 'field_one' => 's1.0.m1.f1', 'field_two' => 's1.0.m1.f2')),
  2408. 1 => array('ModelTwo' => array('id' => 1002, 'field_one' => 's1.1.m2.f2', 'field_two' => 's1.1.m2.f2')));
  2409. $array2 = array(
  2410. 0 => array('ModelOne' => array('id' => 1001, 'field_one' => 's2.0.m1.f1', 'field_two' => 's2.0.m1.f2')),
  2411. 1 => array('ModelTwo' => array('id' => 1002, 'field_one' => 's2.1.m2.f2', 'field_two' => 's2.1.m2.f2')));
  2412. $result = Set::pushDiff($array1, $array2);
  2413. $this->assertEquals($result, $array1);
  2414. $array3 = array(0 => array('ModelThree' => array('id' => 1003, 'field_one' => 's3.0.m3.f1', 'field_two' => 's3.0.m3.f2')));
  2415. $result = Set::pushDiff($array1, $array3);
  2416. $expected = array(
  2417. 0 => array('ModelOne' => array('id' => 1001, 'field_one' => 's1.0.m1.f1', 'field_two' => 's1.0.m1.f2'),
  2418. 'ModelThree' => array('id' => 1003, 'field_one' => 's3.0.m3.f1', 'field_two' => 's3.0.m3.f2')),
  2419. 1 => array('ModelTwo' => array('id' => 1002, 'field_one' => 's1.1.m2.f2', 'field_two' => 's1.1.m2.f2')));
  2420. $this->assertEquals($expected, $result);
  2421. $result = Set::pushDiff($array1, null);
  2422. $this->assertEquals($result, $array1);
  2423. $result = Set::pushDiff($array1, $array2);
  2424. $this->assertEquals($result, $array1+$array2);
  2425. }
  2426. /**
  2427. * testSetApply method
  2428. * @return void
  2429. *
  2430. */
  2431. public function testApply() {
  2432. $data = array(
  2433. array('Movie' => array('id' => 1, 'title' => 'movie 3', 'rating' => 5)),
  2434. array('Movie' => array('id' => 1, 'title' => 'movie 1', 'rating' => 1)),
  2435. array('Movie' => array('id' => 1, 'title' => 'movie 2', 'rating' => 3))
  2436. );
  2437. $result = Set::apply('/Movie/rating', $data, 'array_sum');
  2438. $expected = 9;
  2439. $this->assertEquals($expected, $result);
  2440. $result = Set::apply('/Movie/rating', $data, 'array_product');
  2441. $expected = 15;
  2442. $this->assertEquals($expected, $result);
  2443. $result = Set::apply('/Movie/title', $data, 'ucfirst', array('type' => 'map'));
  2444. $expected = array('Movie 3', 'Movie 1', 'Movie 2');
  2445. $this->assertEquals($expected, $result);
  2446. $result = Set::apply('/Movie/title', $data, 'strtoupper', array('type' => 'map'));
  2447. $expected = array('MOVIE 3', 'MOVIE 1', 'MOVIE 2');
  2448. $this->assertEquals($expected, $result);
  2449. $result = Set::apply('/Movie/rating', $data, array('SetTest', '_method'), array('type' => 'reduce'));
  2450. $expected = 9;
  2451. $this->assertEquals($expected, $result);
  2452. $result = Set::apply('/Movie/rating', $data, 'strtoupper', array('type' => 'non existing type'));
  2453. $expected = null;
  2454. $this->assertEquals($expected, $result);
  2455. }
  2456. /**
  2457. * Helper method to test Set::apply()
  2458. *
  2459. * @return void
  2460. */
  2461. static function _method($val1, $val2) {
  2462. $val1 += $val2;
  2463. return $val1;
  2464. }
  2465. /**
  2466. * testXmlSetReverse method
  2467. *
  2468. * @return void
  2469. */
  2470. public function testXmlSetReverse() {
  2471. App::uses('Xml', 'Utility');
  2472. $string = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  2473. <rss version="2.0">
  2474. <channel>
  2475. <title>Cake PHP Google Group</title>
  2476. <link>http://groups.google.com/group/cake-php</link>
  2477. <description>Search this group before posting anything. There are over 20,000 posts and it&amp;#39;s very likely your question was answered before. Visit the IRC channel #cakephp at irc.freenode.net for live chat with users and developers of Cake. If you post, tell us the version of Cake, PHP, and database.</description>
  2478. <language>en</language>
  2479. <item>
  2480. <title>constructng result array when using findall</title>
  2481. <link>http://groups.google.com/group/cake-php/msg/49bc00f3bc651b4f</link>
  2482. <description>i&#39;m using cakephp to construct a logical data model array that will be &lt;br&gt; passed to a flex app. I have the following model association: &lt;br&gt; ServiceDay-&amp;gt;(hasMany)ServiceTi me-&amp;gt;(hasMany)ServiceTimePrice. So what &lt;br&gt; the current output from my findall is something like this example: &lt;br&gt; &lt;p&gt;Array( &lt;br&gt; [0] =&amp;gt; Array(</description>
  2483. <guid isPermaLink="true">http://groups.google.com/group/cake-php/msg/49bc00f3bc651b4f</guid>
  2484. <author>bmil...@gmail.com(bpscrugs)</author>
  2485. <pubDate>Fri, 28 Dec 2007 00:44:14 UT</pubDate>
  2486. </item>
  2487. <item>
  2488. <title>Re: share views between actions?</title>
  2489. <link>http://groups.google.com/group/cake-php/msg/8b350d898707dad8</link>
  2490. <description>Then perhaps you might do us all a favour and refrain from replying to &lt;br&gt; things you do not understand. That goes especially for asinine comments. &lt;br&gt; Indeed. &lt;br&gt; To sum up: &lt;br&gt; No comment. &lt;br&gt; In my day, a simple &amp;quot;RTFM&amp;quot; would suffice. I&#39;ll keep in mind to ignore any &lt;br&gt; further responses from you. &lt;br&gt; You (and I) were referring to the *online documentation*, not other</description>
  2491. <guid isPermaLink="true">http://groups.google.com/group/cake-php/msg/8b350d898707dad8</guid>
  2492. <author>subtropolis.z...@gmail.com(subtropolis zijn)</author>
  2493. <pubDate>Fri, 28 Dec 2007 00:45:01 UT</pubDate>
  2494. </item>
  2495. </channel>
  2496. </rss>';
  2497. $xml = Xml::build($string);
  2498. $result = Set::reverse($xml);
  2499. $expected = array('rss' => array(
  2500. '@version' => '2.0',
  2501. 'channel' => array(
  2502. 'title' => 'Cake PHP Google Group',
  2503. 'link' => 'http://groups.google.com/group/cake-php',
  2504. 'description' => 'Search this group before posting anything. There are over 20,000 posts and it&#39;s very likely your question was answered before. Visit the IRC channel #cakephp at irc.freenode.net for live chat with users and developers of Cake. If you post, tell us the version of Cake, PHP, and database.',
  2505. 'language' => 'en',
  2506. 'item' => array(
  2507. array(
  2508. 'title' => 'constructng result array when using findall',
  2509. 'link' => 'http://groups.google.com/group/cake-php/msg/49bc00f3bc651b4f',
  2510. 'description' => "i'm using cakephp to construct a logical data model array that will be <br> passed to a flex app. I have the following model association: <br> ServiceDay-&gt;(hasMany)ServiceTi me-&gt;(hasMany)ServiceTimePrice. So what <br> the current output from my findall is something like this example: <br> <p>Array( <br> [0] =&gt; Array(",
  2511. 'guid' => array('@isPermaLink' => 'true', '@' => 'http://groups.google.com/group/cake-php/msg/49bc00f3bc651b4f'),
  2512. 'author' => 'bmil...@gmail.com(bpscrugs)',
  2513. 'pubDate' => 'Fri, 28 Dec 2007 00:44:14 UT',
  2514. ),
  2515. array(
  2516. 'title' => 'Re: share views between actions?',
  2517. 'link' => 'http://groups.google.com/group/cake-php/msg/8b350d898707dad8',
  2518. 'description' => 'Then perhaps you might do us all a favour and refrain from replying to <br> things you do not understand. That goes especially for asinine comments. <br> Indeed. <br> To sum up: <br> No comment. <br> In my day, a simple &quot;RTFM&quot; would suffice. I\'ll keep in mind to ignore any <br> further responses from you. <br> You (and I) were referring to the *online documentation*, not other',
  2519. 'guid' => array('@isPermaLink' => 'true', '@' => 'http://groups.google.com/group/cake-php/msg/8b350d898707dad8'),
  2520. 'author' => 'subtropolis.z...@gmail.com(subtropolis zijn)',
  2521. 'pubDate' => 'Fri, 28 Dec 2007 00:45:01 UT'
  2522. )
  2523. )
  2524. )
  2525. ));
  2526. $this->assertEquals($expected, $result);
  2527. $string ='<data><post title="Title of this post" description="cool"/></data>';
  2528. $xml = Xml::build($string);
  2529. $result = Set::reverse($xml);
  2530. $expected = array('data' => array('post' => array('@title' => 'Title of this post', '@description' => 'cool')));
  2531. $this->assertEquals($expected, $result);
  2532. $xml = Xml::build('<example><item><title>An example of a correctly reversed SimpleXMLElement</title><desc/></item></example>');
  2533. $result = Set::reverse($xml);
  2534. $expected = array('example' =>
  2535. array(
  2536. 'item' => array(
  2537. 'title' => 'An example of a correctly reversed SimpleXMLElement',
  2538. 'desc' => '',
  2539. )
  2540. )
  2541. );
  2542. $this->assertEquals($expected, $result);
  2543. $xml = Xml::build('<example><item attr="123"><titles><title>title1</title><title>title2</title></titles></item></example>');
  2544. $result = Set::reverse($xml);
  2545. $expected =
  2546. array('example' => array(
  2547. 'item' => array(
  2548. '@attr' => '123',
  2549. 'titles' => array(
  2550. 'title' => array('title1', 'title2')
  2551. )
  2552. )
  2553. )
  2554. );
  2555. $this->assertEquals($expected, $result);
  2556. $xml = Xml::build('<example attr="ex_attr"><item attr="123"><titles>list</titles>textforitems</item></example>');
  2557. $result = Set::reverse($xml);
  2558. $expected =
  2559. array('example' => array(
  2560. '@attr' => 'ex_attr',
  2561. 'item' => array(
  2562. '@attr' => '123',
  2563. 'titles' => 'list',
  2564. '@' => 'textforitems'
  2565. )
  2566. )
  2567. );
  2568. $this->assertEquals($expected, $result);
  2569. $string = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  2570. <rss version="2.0" xmlns:dc="http://www.cakephp.org/">
  2571. <channel>
  2572. <title>Cake PHP Google Group</title>
  2573. <link>http://groups.google.com/group/cake-php</link>
  2574. <description>Search this group before posting anything. There are over 20,000 posts and it&amp;#39;s very likely your question was answered before. Visit the IRC channel #cakephp at irc.freenode.net for live chat with users and developers of Cake. If you post, tell us the version of Cake, PHP, and database.</description>
  2575. <language>en</language>
  2576. <item>
  2577. <title>constructng result array when using findall</title>
  2578. <link>http://groups.google.com/group/cake-php/msg/49bc00f3bc651b4f</link>
  2579. <description>i&#39;m using cakephp to construct a logical data model array that will be &lt;br&gt; passed to a flex app. I have the following model association: &lt;br&gt; ServiceDay-&amp;gt;(hasMany)ServiceTi me-&amp;gt;(hasMany)ServiceTimePrice. So what &lt;br&gt; the current output from my findall is something like this example: &lt;br&gt; &lt;p&gt;Array( &lt;br&gt; [0] =&amp;gt; Array(</description>
  2580. <dc:creator>cakephp</dc:creator>
  2581. <category><![CDATA[cakephp]]></category>
  2582. <category><![CDATA[model]]></category>
  2583. <guid isPermaLink="true">http://groups.google.com/group/cake-php/msg/49bc00f3bc651b4f</guid>
  2584. <author>bmil...@gmail.com(bpscrugs)</author>
  2585. <pubDate>Fri, 28 Dec 2007 00:44:14 UT</pubDate>
  2586. </item>
  2587. <item>
  2588. <title>Re: share views between actions?</title>
  2589. <link>http://groups.google.com/group/cake-php/msg/8b350d898707dad8</link>
  2590. <description>Then perhaps you might do us all a favour and refrain from replying to &lt;br&gt; things you do not understand. That goes especially for asinine comments. &lt;br&gt; Indeed. &lt;br&gt; To sum up: &lt;br&gt; No comment. &lt;br&gt; In my day, a simple &amp;quot;RTFM&amp;quot; would suffice. I&#39;ll keep in mind to ignore any &lt;br&gt; further responses from you. &lt;br&gt; You (and I) were referring to the *online documentation*, not other</description>
  2591. <dc:creator>cakephp</dc:creator>
  2592. <category><![CDATA[cakephp]]></category>
  2593. <category><![CDATA[model]]></category>
  2594. <guid isPermaLink="true">http://groups.google.com/group/cake-php/msg/8b350d898707dad8</guid>
  2595. <author>subtropolis.z...@gmail.com(subtropolis zijn)</author>
  2596. <pubDate>Fri, 28 Dec 2007 00:45:01 UT</pubDate>
  2597. </item>
  2598. </channel>
  2599. </rss>';
  2600. $xml = Xml::build($string);
  2601. $result = Set::reverse($xml);
  2602. $expected = array('rss' => array(
  2603. '@version' => '2.0',
  2604. 'channel' => array(
  2605. 'title' => 'Cake PHP Google Group',
  2606. 'link' => 'http://groups.google.com/group/cake-php',
  2607. 'description' => 'Search this group before posting anything. There are over 20,000 posts and it&#39;s very likely your question was answered before. Visit the IRC channel #cakephp at irc.freenode.net for live chat with users and developers of Cake. If you post, tell us the version of Cake, PHP, and database.',
  2608. 'language' => 'en',
  2609. 'item' => array(
  2610. array(
  2611. 'title' => 'constructng result array when using findall',
  2612. 'link' => 'http://groups.google.com/group/cake-php/msg/49bc00f3bc651b4f',
  2613. 'description' => "i'm using cakephp to construct a logical data model array that will be <br> passed to a flex app. I have the following model association: <br> ServiceDay-&gt;(hasMany)ServiceTi me-&gt;(hasMany)ServiceTimePrice. So what <br> the current output from my findall is something like this example: <br> <p>Array( <br> [0] =&gt; Array(",
  2614. 'dc:creator' => 'cakephp',
  2615. 'category' => array('cakephp', 'model'),
  2616. 'guid' => array('@isPermaLink' => 'true', '@' => 'http://groups.google.com/group/cake-php/msg/49bc00f3bc651b4f'),
  2617. 'author' => 'bmil...@gmail.com(bpscrugs)',
  2618. 'pubDate' => 'Fri, 28 Dec 2007 00:44:14 UT',
  2619. ),
  2620. array(
  2621. 'title' => 'Re: share views between actions?',
  2622. 'link' => 'http://groups.google.com/group/cake-php/msg/8b350d898707dad8',
  2623. 'description' => 'Then perhaps you might do us all a favour and refrain from replying to <br> things you do not understand. That goes especially for asinine comments. <br> Indeed. <br> To sum up: <br> No comment. <br> In my day, a simple &quot;RTFM&quot; would suffice. I\'ll keep in mind to ignore any <br> further responses from you. <br> You (and I) were referring to the *online documentation*, not other',
  2624. 'dc:creator' => 'cakephp',
  2625. 'category' => array('cakephp', 'model'),
  2626. 'guid' => array('@isPermaLink' => 'true', '@' => 'http://groups.google.com/group/cake-php/msg/8b350d898707dad8'),
  2627. 'author' => 'subtropolis.z...@gmail.com(subtropolis zijn)',
  2628. 'pubDate' => 'Fri, 28 Dec 2007 00:45:01 UT'
  2629. )
  2630. )
  2631. )
  2632. ));
  2633. $this->assertEquals($expected, $result);
  2634. $text = '<?xml version="1.0" encoding="UTF-8"?>
  2635. <XRDS xmlns="xri://$xrds">
  2636. <XRD xml:id="oauth" xmlns="xri://$XRD*($v*2.0)" version="2.0">
  2637. <Type>xri://$xrds*simple</Type>
  2638. <Expires>2008-04-13T07:34:58Z</Expires>
  2639. <Service>
  2640. <Type>http://oauth.net/core/1.0/endpoint/authorize</Type>
  2641. <Type>http://oauth.net/core/1.0/parameters/auth-header</Type>
  2642. <Type>http://oauth.net/core/1.0/parameters/uri-query</Type>
  2643. <URI priority="10">https://ma.gnolia.com/oauth/authorize</URI>
  2644. <URI priority="20">http://ma.gnolia.com/oauth/authorize</URI>
  2645. </Service>
  2646. </XRD>
  2647. <XRD xmlns="xri://$XRD*($v*2.0)" version="2.0">
  2648. <Type>xri://$xrds*simple</Type>
  2649. <Service priority="10">
  2650. <Type>http://oauth.net/discovery/1.0</Type>
  2651. <URI>#oauth</URI>
  2652. </Service>
  2653. </XRD>
  2654. </XRDS>';
  2655. $xml = Xml::build($text);
  2656. $result = Set::reverse($xml);
  2657. $expected = array('XRDS' => array(
  2658. 'XRD' => array(
  2659. array(
  2660. '@xml:id' => 'oauth',
  2661. '@version' => '2.0',
  2662. 'Type' => 'xri://$xrds*simple',
  2663. 'Expires' => '2008-04-13T07:34:58Z',
  2664. 'Service' => array(
  2665. 'Type' => array(
  2666. 'http://oauth.net/core/1.0/endpoint/authorize',
  2667. 'http://oauth.net/core/1.0/parameters/auth-header',
  2668. 'http://oauth.net/core/1.0/parameters/uri-query'
  2669. ),
  2670. 'URI' => array(
  2671. array(
  2672. '@' => 'https://ma.gnolia.com/oauth/authorize',
  2673. '@priority' => '10',
  2674. ),
  2675. array(
  2676. '@' => 'http://ma.gnolia.com/oauth/authorize',
  2677. '@priority' => '20'
  2678. )
  2679. )
  2680. )
  2681. ),
  2682. array(
  2683. '@version' => '2.0',
  2684. 'Type' => 'xri://$xrds*simple',
  2685. 'Service' => array(
  2686. '@priority' => '10',
  2687. 'Type' => 'http://oauth.net/discovery/1.0',
  2688. 'URI' => '#oauth'
  2689. )
  2690. )
  2691. )
  2692. ));
  2693. $this->assertEquals($expected, $result);
  2694. }
  2695. /**
  2696. * testStrictKeyCheck method
  2697. *
  2698. * @return void
  2699. */
  2700. public function testStrictKeyCheck() {
  2701. $set = array('a' => 'hi');
  2702. $this->assertFalse(Set::check($set, 'a.b'));
  2703. }
  2704. /**
  2705. * Tests Set::flatten
  2706. *
  2707. * @return void
  2708. */
  2709. public function testFlatten() {
  2710. $data = array('Larry', 'Curly', 'Moe');
  2711. $result = Set::flatten($data);
  2712. $this->assertEquals($result, $data);
  2713. $data[9] = 'Shemp';
  2714. $result = Set::flatten($data);
  2715. $this->assertEquals($result, $data);
  2716. $data = array(
  2717. array(
  2718. 'Post' => array('id' => '1', 'author_id' => '1', 'title' => 'First Post'),
  2719. 'Author' => array('id' => '1', 'user' => 'nate', 'password' => 'foo'),
  2720. ),
  2721. array(
  2722. 'Post' => array('id' => '2', 'author_id' => '3', 'title' => 'Second Post', 'body' => 'Second Post Body'),
  2723. 'Author' => array('id' => '3', 'user' => 'larry', 'password' => null),
  2724. )
  2725. );
  2726. $result = Set::flatten($data);
  2727. $expected = array(
  2728. '0.Post.id' => '1', '0.Post.author_id' => '1', '0.Post.title' => 'First Post', '0.Author.id' => '1',
  2729. '0.Author.user' => 'nate', '0.Author.password' => 'foo', '1.Post.id' => '2', '1.Post.author_id' => '3',
  2730. '1.Post.title' => 'Second Post', '1.Post.body' => 'Second Post Body', '1.Author.id' => '3',
  2731. '1.Author.user' => 'larry', '1.Author.password' => null
  2732. );
  2733. $this->assertEquals($expected, $result);
  2734. }
  2735. /**
  2736. * test normalization
  2737. *
  2738. * @return void
  2739. */
  2740. public function testNormalizeStrings() {
  2741. $result = Set::normalize('one,two,three');
  2742. $expected = array('one' => null, 'two' => null, 'three' => null);
  2743. $this->assertEquals($expected, $result);
  2744. $result = Set::normalize('one two three', true, ' ');
  2745. $expected = array('one' => null, 'two' => null, 'three' => null);
  2746. $this->assertEquals($expected, $result);
  2747. $result = Set::normalize('one , two , three ', true, ',', true);
  2748. $expected = array('one' => null, 'two' => null, 'three' => null);
  2749. $this->assertEquals($expected, $result);
  2750. }
  2751. /**
  2752. * test normalizing arrays
  2753. *
  2754. * @return void
  2755. */
  2756. public function testNormalizeArrays() {
  2757. $result = Set::normalize(array('one', 'two', 'three'));
  2758. $expected = array('one' => null, 'two' => null, 'three' => null);
  2759. $this->assertEquals($expected, $result);
  2760. $result = Set::normalize(array('one', 'two', 'three'), false);
  2761. $expected = array('one', 'two', 'three');
  2762. $this->assertEquals($expected, $result);
  2763. $result = Set::normalize(array('one' => 1, 'two' => 2, 'three' => 3, 'four'), false);
  2764. $expected = array('one' => 1, 'two' => 2, 'three' => 3, 'four' => null);
  2765. $this->assertEquals($expected, $result);
  2766. $result = Set::normalize(array('one' => 1, 'two' => 2, 'three' => 3, 'four'));
  2767. $expected = array('one' => 1, 'two' => 2, 'three' => 3, 'four' => null);
  2768. $this->assertEquals($expected, $result);
  2769. $result = Set::normalize(array('one' => array('a', 'b', 'c' => 'cee'), 'two' => 2, 'three'));
  2770. $expected = array('one' => array('a', 'b', 'c' => 'cee'), 'two' => 2, 'three' => null);
  2771. $this->assertEquals($expected, $result);
  2772. }
  2773. }