PageRenderTime 39ms CodeModel.GetById 107ms RepoModel.GetById 1ms app.codeStats 0ms

/cake/tests/cases/libs/set.test.php

https://github.com/mariuz/firetube
PHP | 2926 lines | 2409 code | 296 blank | 221 comment | 1 complexity | 57b86fcd1d1761d8e90227084ca7b280 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /* SVN FILE: $Id$ */
  3. /**
  4. * SetTest file
  5. *
  6. * Long description for file
  7. *
  8. * PHP versions 4 and 5
  9. *
  10. * CakePHP(tm) Tests <https://trac.cakephp.org/wiki/Developement/TestSuite>
  11. * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  12. *
  13. * Licensed under The Open Group Test Suite License
  14. * Redistributions of files must retain the above copyright notice.
  15. *
  16. * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  17. * @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests
  18. * @package cake
  19. * @subpackage cake.tests.cases.libs
  20. * @since CakePHP(tm) v 1.2.0.4206
  21. * @version $Revision$
  22. * @modifiedby $LastChangedBy$
  23. * @lastmodified $Date$
  24. * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
  25. */
  26. App::import('Core', 'Set');
  27. /**
  28. * SetTest class
  29. *
  30. * @package cake
  31. * @subpackage cake.tests.cases.libs
  32. */
  33. class SetTest extends CakeTestCase {
  34. /**
  35. * testNumericKeyExtraction method
  36. *
  37. * @access public
  38. * @return void
  39. */
  40. function testNumericKeyExtraction() {
  41. $data = array('plugin' => null, 'controller' => '', 'action' => '', 1, 'whatever');
  42. $this->assertIdentical(Set::extract($data, '{n}'), array(1, 'whatever'));
  43. $this->assertIdentical(Set::diff($data, Set::extract($data, '{n}')), array('plugin' => null, 'controller' => '', 'action' => ''));
  44. }
  45. /**
  46. * testEnum method
  47. *
  48. * @access public
  49. * @return void
  50. */
  51. function testEnum() {
  52. $result = Set::enum(1, 'one, two');
  53. $this->assertIdentical($result, 'two');
  54. $result = Set::enum(2, 'one, two');
  55. $this->assertNull($result);
  56. $set = array('one', 'two');
  57. $result = Set::enum(0, $set);
  58. $this->assertIdentical($result, 'one');
  59. $result = Set::enum(1, $set);
  60. $this->assertIdentical($result, 'two');
  61. $result = Set::enum(1, array('one', 'two'));
  62. $this->assertIdentical($result, 'two');
  63. $result = Set::enum(2, array('one', 'two'));
  64. $this->assertNull($result);
  65. $result = Set::enum('first', array('first' => 'one', 'second' => 'two'));
  66. $this->assertIdentical($result, 'one');
  67. $result = Set::enum('third', array('first' => 'one', 'second' => 'two'));
  68. $this->assertNull($result);
  69. $result = Set::enum('no', array('no' => 0, 'yes' => 1));
  70. $this->assertIdentical($result, 0);
  71. $result = Set::enum('not sure', array('no' => 0, 'yes' => 1));
  72. $this->assertNull($result);
  73. $result = Set::enum(0);
  74. $this->assertIdentical($result, 'no');
  75. $result = Set::enum(1);
  76. $this->assertIdentical($result, 'yes');
  77. $result = Set::enum(2);
  78. $this->assertNull($result);
  79. }
  80. /**
  81. * testFilter method
  82. *
  83. * @access public
  84. * @return void
  85. */
  86. function testFilter() {
  87. $result = Set::filter(array('0', false, true, 0, array('one thing', 'I can tell you', 'is you got to be', false)));
  88. $expected = array('0', 2 => true, 3 => 0, 4 => array('one thing', 'I can tell you', 'is you got to be', false));
  89. $this->assertIdentical($result, $expected);
  90. }
  91. /**
  92. * testNumericArrayCheck method
  93. *
  94. * @access public
  95. * @return void
  96. */
  97. function testNumericArrayCheck() {
  98. $data = array('one');
  99. $this->assertTrue(Set::numeric(array_keys($data)));
  100. $data = array(1 => 'one');
  101. $this->assertFalse(Set::numeric($data));
  102. $data = array('one');
  103. $this->assertFalse(Set::numeric($data));
  104. $data = array('one' => 'two');
  105. $this->assertFalse(Set::numeric($data));
  106. $data = array('one' => 1);
  107. $this->assertTrue(Set::numeric($data));
  108. $data = array(0);
  109. $this->assertTrue(Set::numeric($data));
  110. $data = array('one', 'two', 'three', 'four', 'five');
  111. $this->assertTrue(Set::numeric(array_keys($data)));
  112. $data = array(1 => 'one', 2 => 'two', 3 => 'three', 4 => 'four', 5 => '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('one', 2 => 'two', 3 => 'three', 4 => 'four', 'a' => 'five');
  117. $this->assertFalse(Set::numeric(array_keys($data)));
  118. }
  119. /**
  120. * testKeyCheck method
  121. *
  122. * @access public
  123. * @return void
  124. */
  125. function testKeyCheck() {
  126. $data = array('Multi' => array('dimensonal' => array('array')));
  127. $this->assertTrue(Set::check($data, 'Multi.dimensonal'));
  128. $this->assertFalse(Set::check($data, 'Multi.dimensonal.array'));
  129. $data = array(
  130. array(
  131. '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'),
  132. 'User' => array('id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'),
  133. 'Comment' => array(
  134. 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'),
  135. 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'),
  136. ),
  137. 'Tag' => array(
  138. array('id' => '1', 'tag' => 'tag1', 'created' => '2007-03-18 12:22:23', 'updated' => '2007-03-18 12:24:31'),
  139. array('id' => '2', 'tag' => 'tag2', 'created' => '2007-03-18 12:24:23', 'updated' => '2007-03-18 12:26:31')
  140. )
  141. ),
  142. array(
  143. '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'),
  144. 'User' => array('id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'),
  145. 'Comment' => array(),
  146. 'Tag' => array()
  147. )
  148. );
  149. $this->assertTrue(Set::check($data, '0.Article.user_id'));
  150. $this->assertTrue(Set::check($data, '0.Comment.0.id'));
  151. $this->assertFalse(Set::check($data, '0.Comment.0.id.0'));
  152. $this->assertTrue(Set::check($data, '0.Article.user_id'));
  153. $this->assertFalse(Set::check($data, '0.Article.user_id.a'));
  154. }
  155. /**
  156. * testMerge method
  157. *
  158. * @access public
  159. * @return void
  160. */
  161. function testMerge() {
  162. $r = Set::merge(array('foo'));
  163. $this->assertIdentical($r, array('foo'));
  164. $r = Set::merge('foo');
  165. $this->assertIdentical($r, array('foo'));
  166. $r = Set::merge('foo', 'bar');
  167. $this->assertIdentical($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->assertIdentical($r, array('foo', 'bar'));
  171. }
  172. $r = Set::merge('foo', array('user' => 'bob', 'no-bar'), 'bar');
  173. $this->assertIdentical($r, array('foo', 'user' => 'bob', 'no-bar', 'bar'));
  174. $a = array('foo', 'foo2');
  175. $b = array('bar', 'bar2');
  176. $this->assertIdentical(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->assertIdentical(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->assertIdentical(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->assertIdentical(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->assertIdentical(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->assertIdentical(Set::merge($a, $b, $c), $expected);
  192. $this->assertIdentical(Set::merge($a, $b, array(), $c), $expected);
  193. $r = Set::merge($a, $b, $c);
  194. $this->assertIdentical($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->assertIdentical(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->assertIdentical(Set::normalize(Set::merge($a, $b)), $expected);
  221. }
  222. /**
  223. * testSort method
  224. *
  225. * @access public
  226. * @return void
  227. */
  228. function testSort() {
  229. $a = array(
  230. 0 => array('Person' => array('name' => 'Jeff'), 'Friend' => array(array('name' => 'Nate'))),
  231. 1 => array('Person' => array('name' => 'Tracy'),'Friend' => array(array('name' => 'Lindsay')))
  232. );
  233. $b = array(
  234. 0 => array('Person' => array('name' => 'Tracy'),'Friend' => array(array('name' => 'Lindsay'))),
  235. 1 => array('Person' => array('name' => 'Jeff'), 'Friend' => array(array('name' => 'Nate')))
  236. );
  237. $a = Set::sort($a, '{n}.Friend.{n}.name', 'asc');
  238. $this->assertIdentical($a, $b);
  239. $b = array(
  240. 0 => array('Person' => array('name' => 'Jeff'), 'Friend' => array(array('name' => 'Nate'))),
  241. 1 => array('Person' => array('name' => 'Tracy'),'Friend' => array(array('name' => 'Lindsay')))
  242. );
  243. $a = array(
  244. 0 => array('Person' => array('name' => 'Tracy'),'Friend' => array(array('name' => 'Lindsay'))),
  245. 1 => array('Person' => array('name' => 'Jeff'), 'Friend' => array(array('name' => 'Nate')))
  246. );
  247. $a = Set::sort($a, '{n}.Friend.{n}.name', 'desc');
  248. $this->assertIdentical($a, $b);
  249. $a = array(
  250. 0 => array('Person' => array('name' => 'Jeff'), 'Friend' => array(array('name' => 'Nate'))),
  251. 1 => array('Person' => array('name' => 'Tracy'),'Friend' => array(array('name' => 'Lindsay'))),
  252. 2 => array('Person' => array('name' => 'Adam'),'Friend' => array(array('name' => 'Bob')))
  253. );
  254. $b = array(
  255. 0 => array('Person' => array('name' => 'Adam'),'Friend' => array(array('name' => 'Bob'))),
  256. 1 => array('Person' => array('name' => 'Jeff'), 'Friend' => array(array('name' => 'Nate'))),
  257. 2 => array('Person' => array('name' => 'Tracy'),'Friend' => array(array('name' => 'Lindsay')))
  258. );
  259. $a = Set::sort($a, '{n}.Person.name', 'asc');
  260. $this->assertIdentical($a, $b);
  261. $a = array(
  262. array(7,6,4),
  263. array(3,4,5),
  264. array(3,2,1),
  265. );
  266. $b = array(
  267. array(3,2,1),
  268. array(3,4,5),
  269. array(7,6,4),
  270. );
  271. $a = Set::sort($a, '{n}.{n}', 'asc');
  272. $this->assertIdentical($a, $b);
  273. $a = array(
  274. array(7,6,4),
  275. array(3,4,5),
  276. array(3,2,array(1,1,1)),
  277. );
  278. $b = array(
  279. array(3,2,array(1,1,1)),
  280. array(3,4,5),
  281. array(7,6,4),
  282. );
  283. $a = Set::sort($a, '{n}', 'asc');
  284. $this->assertIdentical($a, $b);
  285. $a = array(
  286. 0 => array('Person' => array('name' => 'Jeff')),
  287. 1 => array('Shirt' => array('color' => 'black'))
  288. );
  289. $b = array(
  290. 0 => array('Shirt' => array('color' => 'black')),
  291. 1 => array('Person' => array('name' => 'Jeff')),
  292. );
  293. $a = Set::sort($a, '{n}.Person.name', 'ASC');
  294. $this->assertIdentical($a, $b);
  295. $names = array(
  296. array('employees' => array(array('name' => array('first' => 'John', 'last' => 'Doe')))),
  297. array('employees' => array(array('name' => array('first' => 'Jane', 'last' => 'Doe')))),
  298. array('employees' => array(array('name' => array()))),
  299. array('employees' => array(array('name' => array())))
  300. );
  301. $result = Set::sort($names, '{n}.employees.0.name', 'asc', 1);
  302. $expected = array(
  303. array('employees' => array(array('name' => array('first' => 'John', 'last' => 'Doe')))),
  304. array('employees' => array(array('name' => array('first' => 'Jane', 'last' => 'Doe')))),
  305. array('employees' => array(array('name' => array()))),
  306. array('employees' => array(array('name' => array())))
  307. );
  308. $this->assertEqual($result, $expected);
  309. }
  310. /**
  311. * testExtract method
  312. *
  313. * @access public
  314. * @return void
  315. */
  316. function testExtract() {
  317. $a = array(
  318. array(
  319. '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'),
  320. 'User' => array('id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'),
  321. 'Comment' => array(
  322. 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'),
  323. 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'),
  324. ),
  325. 'Tag' => array(
  326. array('id' => '1', 'tag' => 'tag1', 'created' => '2007-03-18 12:22:23', 'updated' => '2007-03-18 12:24:31'),
  327. array('id' => '2', 'tag' => 'tag2', 'created' => '2007-03-18 12:24:23', 'updated' => '2007-03-18 12:26:31')
  328. ),
  329. 'Deep' => array(
  330. 'Nesting' => array(
  331. 'test' => array(
  332. 1 => 'foo',
  333. 2 => array(
  334. 'and' => array('more' => 'stuff')
  335. )
  336. )
  337. )
  338. )
  339. ),
  340. array(
  341. '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'),
  342. 'User' => array('id' => '2', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'),
  343. 'Comment' => array(),
  344. 'Tag' => array()
  345. ),
  346. array(
  347. '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'),
  348. 'User' => array('id' => '3', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'),
  349. 'Comment' => array(),
  350. 'Tag' => array()
  351. ),
  352. array(
  353. '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'),
  354. 'User' => array('id' => '4', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'),
  355. 'Comment' => array(),
  356. 'Tag' => array()
  357. ),
  358. array(
  359. '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'),
  360. 'User' => array('id' => '5', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'),
  361. 'Comment' => array(),
  362. 'Tag' => array()
  363. )
  364. );
  365. $b = array('Deep' => $a[0]['Deep']);
  366. $c = array(
  367. array('a' => array('I' => array('a' => 1))),
  368. array(
  369. 'a' => array(
  370. 2
  371. )
  372. ),
  373. array('a' => array('II' => array('a' => 3, 'III' => array('a' => array('foo' => 4))))),
  374. );
  375. $expected = array(array('a' => $c[2]['a']));
  376. $r = Set::extract('/a/II[a=3]/..', $c);
  377. $this->assertEqual($r, $expected);
  378. $expected = array(1, 2, 3, 4, 5);
  379. $this->assertEqual(Set::extract('/User/id', $a), $expected);
  380. $expected = array(1, 2, 3, 4, 5);
  381. $this->assertEqual(Set::extract('/User/id', $a), $expected);
  382. $expected = array(
  383. array('id' => 1), array('id' => 2), array('id' => 3), array('id' => 4), array('id' => 5)
  384. );
  385. $r = Set::extract('/User/id', $a, array('flatten' => false));
  386. $this->assertEqual($r, $expected);
  387. $expected = array(array('test' => $a[0]['Deep']['Nesting']['test']));
  388. $this->assertEqual(Set::extract('/Deep/Nesting/test', $a), $expected);
  389. $this->assertEqual(Set::extract('/Deep/Nesting/test', $b), $expected);
  390. $expected = array(array('test' => $a[0]['Deep']['Nesting']['test']));
  391. $r = Set::extract('/Deep/Nesting/test/1/..', $a);
  392. $this->assertEqual($r, $expected);
  393. $expected = array(array('test' => $a[0]['Deep']['Nesting']['test']));
  394. $r = Set::extract('/Deep/Nesting/test/2/and/../..', $a);
  395. $this->assertEqual($r, $expected);
  396. $expected = array(array('test' => $a[0]['Deep']['Nesting']['test']));
  397. $r = Set::extract('/Deep/Nesting/test/2/../../../Nesting/test/2/..', $a);
  398. $this->assertEqual($r, $expected);
  399. $expected = array(2);
  400. $r = Set::extract('/User[2]/id', $a);
  401. $this->assertEqual($r, $expected);
  402. $expected = array(4, 5);
  403. $r = Set::extract('/User[id>3]/id', $a);
  404. $this->assertEqual($r, $expected);
  405. $expected = array(2, 3);
  406. $r = Set::extract('/User[id>1][id<=3]/id', $a);
  407. $this->assertEqual($r, $expected);
  408. $expected = array(array('I'), array('II'));
  409. $r = Set::extract('/a/@*', $c);
  410. $this->assertEqual($r, $expected);
  411. $single = array(
  412. 'User' => array(
  413. 'id' => 4,
  414. 'name' => 'Neo',
  415. )
  416. );
  417. $tricky = array(
  418. 0 => array(
  419. 'User' => array(
  420. 'id' => 1,
  421. 'name' => 'John',
  422. )
  423. ),
  424. 1 => array(
  425. 'User' => array(
  426. 'id' => 2,
  427. 'name' => 'Bob',
  428. )
  429. ),
  430. 2 => array(
  431. 'User' => array(
  432. 'id' => 3,
  433. 'name' => 'Tony',
  434. )
  435. ),
  436. 'User' => array(
  437. 'id' => 4,
  438. 'name' => 'Neo',
  439. )
  440. );
  441. $expected = array(1, 2, 3, 4);
  442. $r = Set::extract('/User/id', $tricky);
  443. $this->assertEqual($r, $expected);
  444. $expected = array(4);
  445. $r = Set::extract('/User/id', $single);
  446. $this->assertEqual($r, $expected);
  447. $expected = array(1, 3);
  448. $r = Set::extract('/User[name=/n/]/id', $tricky);
  449. $this->assertEqual($r, $expected);
  450. $expected = array(4);
  451. $r = Set::extract('/User[name=/N/]/id', $tricky);
  452. $this->assertEqual($r, $expected);
  453. $expected = array(1, 3, 4);
  454. $r = Set::extract('/User[name=/N/i]/id', $tricky);
  455. $this->assertEqual($r, $expected);
  456. $expected = array(array('id', 'name'), array('id', 'name'), array('id', 'name'), array('id', 'name'));
  457. $r = Set::extract('/User/@*', $tricky);
  458. $this->assertEqual($r, $expected);
  459. $common = array(
  460. array(
  461. 'Article' => array(
  462. 'id' => 1,
  463. 'name' => 'Article 1',
  464. ),
  465. 'Comment' => array(
  466. array(
  467. 'id' => 1,
  468. 'user_id' => 5,
  469. 'article_id' => 1,
  470. 'text' => 'Comment 1',
  471. ),
  472. array(
  473. 'id' => 2,
  474. 'user_id' => 23,
  475. 'article_id' => 1,
  476. 'text' => 'Comment 2',
  477. ),
  478. array(
  479. 'id' => 3,
  480. 'user_id' => 17,
  481. 'article_id' => 1,
  482. 'text' => 'Comment 3',
  483. ),
  484. ),
  485. ),
  486. array(
  487. 'Article' => array(
  488. 'id' => 2,
  489. 'name' => 'Article 2',
  490. ),
  491. 'Comment' => array(
  492. array(
  493. 'id' => 4,
  494. 'user_id' => 2,
  495. 'article_id' => 2,
  496. 'text' => 'Comment 4',
  497. 'addition' => '',
  498. ),
  499. array(
  500. 'id' => 5,
  501. 'user_id' => 23,
  502. 'article_id' => 2,
  503. 'text' => 'Comment 5',
  504. 'addition' => 'foo',
  505. ),
  506. ),
  507. ),
  508. array(
  509. 'Article' => array(
  510. 'id' => 3,
  511. 'name' => 'Article 3',
  512. ),
  513. 'Comment' => array(),
  514. )
  515. );
  516. $r = Set::extract('/Comment/id', $common);
  517. $expected = array(1, 2, 3, 4, 5);
  518. $this->assertEqual($r, $expected);
  519. $expected = array(1, 2, 4, 5);
  520. $r = Set::extract('/Comment[id!=3]/id', $common);
  521. $this->assertEqual($r, $expected);
  522. $r = Set::extract('/', $common);
  523. $this->assertEqual($r, $common);
  524. $expected = array(1, 2, 4, 5);
  525. $r = Set::extract($common, '/Comment[id!=3]/id');
  526. $this->assertEqual($r, $expected);
  527. $expected = array($common[0]['Comment'][2]);
  528. $r = Set::extract($common, '/Comment/2');
  529. $this->assertEqual($r, $expected);
  530. $expected = array($common[0]['Comment'][0]);
  531. $r = Set::extract($common, '/Comment[1]/.[id=1]');
  532. $this->assertEqual($r, $expected);
  533. $expected = array($common[1]['Comment'][1]);
  534. $r = Set::extract($common, '/1/Comment/.[2]');
  535. $this->assertEqual($r, $expected);
  536. $expected = array();
  537. $r = Set::extract('/User/id', array());
  538. $this->assertEqual($r, $expected);
  539. $expected = array(5);
  540. $r = Set::extract('/Comment/id[:last]', $common);
  541. $this->assertEqual($r, $expected);
  542. $expected = array(1);
  543. $r = Set::extract('/Comment/id[:first]', $common);
  544. $this->assertEqual($r, $expected);
  545. $expected = array(3);
  546. $r = Set::extract('/Article[:last]/id', $common);
  547. $this->assertEqual($r, $expected);
  548. $expected = array(array('Comment' => $common[1]['Comment'][0]));
  549. $r = Set::extract('/Comment[addition=]', $common);
  550. $this->assertEqual($r, $expected);
  551. $habtm = array(
  552. array(
  553. 'Post' => array(
  554. 'id' => 1,
  555. 'title' => 'great post',
  556. ),
  557. 'Comment' => array(
  558. array(
  559. 'id' => 1,
  560. 'text' => 'foo',
  561. 'User' => array(
  562. 'id' => 1,
  563. 'name' => 'bob'
  564. ),
  565. ),
  566. array(
  567. 'id' => 2,
  568. 'text' => 'bar',
  569. 'User' => array(
  570. 'id' => 2,
  571. 'name' => 'tod'
  572. ),
  573. ),
  574. ),
  575. ),
  576. array(
  577. 'Post' => array(
  578. 'id' => 2,
  579. 'title' => 'fun post',
  580. ),
  581. 'Comment' => array(
  582. array(
  583. 'id' => 3,
  584. 'text' => '123',
  585. 'User' => array(
  586. 'id' => 3,
  587. 'name' => 'dan'
  588. ),
  589. ),
  590. array(
  591. 'id' => 4,
  592. 'text' => '987',
  593. 'User' => array(
  594. 'id' => 4,
  595. 'name' => 'jim'
  596. ),
  597. ),
  598. ),
  599. ),
  600. );
  601. $r = Set::extract('/Comment/User[name=/bob|dan/]/..', $habtm);
  602. $this->assertEqual($r[0]['Comment']['User']['name'], 'bob');
  603. $this->assertEqual($r[1]['Comment']['User']['name'], 'dan');
  604. $this->assertEqual(count($r), 2);
  605. $r = Set::extract('/Comment/User[name=/bob|tod/]/..', $habtm);
  606. $this->assertEqual($r[0]['Comment']['User']['name'], 'bob');
  607. $this->assertEqual($r[1]['Comment']['User']['name'], 'tod');
  608. $this->assertEqual(count($r), 2);
  609. $tree = array(
  610. array(
  611. 'Category' => array('name' => 'Category 1'),
  612. 'children' => array(array('Category' => array('name' => 'Category 1.1')))
  613. ),
  614. array(
  615. 'Category' => array('name' => 'Category 2'),
  616. 'children' => array(
  617. array('Category' => array('name' => 'Category 2.1')),
  618. array('Category' => array('name' => 'Category 2.2'))
  619. )
  620. ),
  621. array(
  622. 'Category' => array('name' => 'Category 3'),
  623. 'children' => array(array('Category' => array('name' => 'Category 3.1')))
  624. )
  625. );
  626. $expected = array(array('Category' => $tree[1]['Category']));
  627. $r = Set::extract('/Category[name=Category 2]', $tree);
  628. $this->assertEqual($r, $expected);
  629. $expected = array(
  630. array('Category' => $tree[1]['Category'], 'children' => $tree[1]['children'])
  631. );
  632. $r = Set::extract('/Category[name=Category 2]/..', $tree);
  633. $this->assertEqual($r, $expected);
  634. $expected = array(
  635. array('children' => $tree[1]['children'][0]),
  636. array('children' => $tree[1]['children'][1])
  637. );
  638. $r = Set::extract('/Category[name=Category 2]/../children', $tree);
  639. $this->assertEqual($r, $expected);
  640. $habtm = array(
  641. array(
  642. 'Post' => array(
  643. 'id' => 1,
  644. 'title' => 'great post',
  645. ),
  646. 'Comment' => array(
  647. array(
  648. 'id' => 1,
  649. 'text' => 'foo',
  650. 'User' => array(
  651. 'id' => 1,
  652. 'name' => 'bob'
  653. ),
  654. ),
  655. array(
  656. 'id' => 2,
  657. 'text' => 'bar',
  658. 'User' => array(
  659. 'id' => 2,
  660. 'name' => 'tod'
  661. ),
  662. ),
  663. ),
  664. ),
  665. array(
  666. 'Post' => array(
  667. 'id' => 2,
  668. 'title' => 'fun post',
  669. ),
  670. 'Comment' => array(
  671. array(
  672. 'id' => 3,
  673. 'text' => '123',
  674. 'User' => array(
  675. 'id' => 3,
  676. 'name' => 'dan'
  677. ),
  678. ),
  679. array(
  680. 'id' => 4,
  681. 'text' => '987',
  682. 'User' => array(
  683. 'id' => 4,
  684. 'name' => 'jim'
  685. ),
  686. ),
  687. ),
  688. ),
  689. );
  690. $r = Set::extract('/Comment/User[name=/\w+/]/..', $habtm);
  691. $this->assertEqual($r[0]['Comment']['User']['name'], 'bob');
  692. $this->assertEqual($r[1]['Comment']['User']['name'], 'tod');
  693. $this->assertEqual($r[2]['Comment']['User']['name'], 'dan');
  694. $this->assertEqual($r[3]['Comment']['User']['name'], 'dan');
  695. $this->assertEqual(count($r), 4);
  696. $r = Set::extract('/Comment/User[name=/[a-z]+/]/..', $habtm);
  697. $this->assertEqual($r[0]['Comment']['User']['name'], 'bob');
  698. $this->assertEqual($r[1]['Comment']['User']['name'], 'tod');
  699. $this->assertEqual($r[2]['Comment']['User']['name'], 'dan');
  700. $this->assertEqual($r[3]['Comment']['User']['name'], 'dan');
  701. $this->assertEqual(count($r), 4);
  702. $r = Set::extract('/Comment/User[name=/bob|dan/]/..', $habtm);
  703. $this->assertEqual($r[0]['Comment']['User']['name'], 'bob');
  704. $this->assertEqual($r[1]['Comment']['User']['name'], 'dan');
  705. $this->assertEqual(count($r), 2);
  706. $r = Set::extract('/Comment/User[name=/bob|tod/]/..', $habtm);
  707. $this->assertEqual($r[0]['Comment']['User']['name'], 'bob');
  708. $this->assertEqual($r[1]['Comment']['User']['name'], 'tod');
  709. $this->assertEqual(count($r), 2);
  710. $mixedKeys = array(
  711. 'User' => array(
  712. 0 => array(
  713. 'id' => 4,
  714. 'name' => 'Neo'
  715. ),
  716. 1 => array(
  717. 'id' => 5,
  718. 'name' => 'Morpheus'
  719. ),
  720. 'stringKey' => array()
  721. )
  722. );
  723. $expected = array('Neo', 'Morpheus');
  724. $r = Set::extract('/User/name', $mixedKeys);
  725. $this->assertEqual($r, $expected);
  726. $f = array(
  727. array(
  728. 'file' => array(
  729. 'name' => 'zipfile.zip',
  730. 'type' => 'application/zip',
  731. 'tmp_name' => '/tmp/php178.tmp',
  732. 'error' => 0,
  733. 'size' => '564647'
  734. )
  735. ),
  736. array(
  737. 'file' => array(
  738. 'name' => 'zipfile2.zip',
  739. 'type' => 'application/x-zip-compressed',
  740. 'tmp_name' => '/tmp/php179.tmp',
  741. 'error' => 0,
  742. 'size' => '354784'
  743. )
  744. ),
  745. array(
  746. 'file' => array(
  747. 'name' => 'picture.jpg',
  748. 'type' => 'image/jpeg',
  749. 'tmp_name' => '/tmp/php180.tmp',
  750. 'error' => 0,
  751. 'size' => '21324'
  752. )
  753. )
  754. );
  755. $expected = array(array('name' => 'zipfile2.zip','type' => 'application/x-zip-compressed','tmp_name' => '/tmp/php179.tmp','error' => 0,'size' => '354784'));
  756. $r = Set::extract('/file/.[type=application/x-zip-compressed]', $f);
  757. $this->assertEqual($r, $expected);
  758. $expected = array(array('name' => 'zipfile.zip','type' => 'application/zip','tmp_name' => '/tmp/php178.tmp','error' => 0,'size' => '564647'));
  759. $r = Set::extract('/file/.[type=application/zip]', $f);
  760. $this->assertEqual($r, $expected);
  761. $f = array(
  762. array(
  763. 'file' => array(
  764. 'name' => 'zipfile.zip',
  765. 'type' => 'application/zip',
  766. 'tmp_name' => '/tmp/php178.tmp',
  767. 'error' => 0,
  768. 'size' => '564647'
  769. )
  770. ),
  771. array(
  772. 'file' => array(
  773. 'name' => 'zipfile2.zip',
  774. 'type' => 'application/x zip compressed',
  775. 'tmp_name' => '/tmp/php179.tmp',
  776. 'error' => 0,
  777. 'size' => '354784'
  778. )
  779. ),
  780. array(
  781. 'file' => array(
  782. 'name' => 'picture.jpg',
  783. 'type' => 'image/jpeg',
  784. 'tmp_name' => '/tmp/php180.tmp',
  785. 'error' => 0,
  786. 'size' => '21324'
  787. )
  788. )
  789. );
  790. $expected = array(array('name' => 'zipfile2.zip','type' => 'application/x zip compressed','tmp_name' => '/tmp/php179.tmp','error' => 0,'size' => '354784'));
  791. $r = Set::extract('/file/.[type=application/x zip compressed]', $f);
  792. $this->assertEqual($r, $expected);
  793. $hasMany = array(
  794. 'Node' => array(
  795. 'id' => 1,
  796. 'name' => 'First',
  797. 'state' => 50
  798. ),
  799. 'ParentNode' => array(
  800. 0 => array(
  801. 'id' => 2,
  802. 'name' => 'Second',
  803. 'state' => 60,
  804. )
  805. )
  806. );
  807. $result = Set::extract('/ParentNode/name', $hasMany);
  808. $expected = array('Second');
  809. $this->assertEqual($result, $expected);
  810. $data = array(
  811. array(
  812. 'Category' => array(
  813. 'id' => 1,
  814. 'name' => 'First'
  815. ),
  816. 0 => array(
  817. 'value' => 50
  818. )
  819. ),
  820. array(
  821. 'Category' => array(
  822. 'id' => 2,
  823. 'name' => 'Second'
  824. ),
  825. 0 => array(
  826. 'value' => 60
  827. )
  828. )
  829. );
  830. $expected = array(
  831. array(
  832. 'Category' => array(
  833. 'id' => 1,
  834. 'name' => 'First'
  835. ),
  836. 0 => array(
  837. 'value' => 50
  838. )
  839. )
  840. );
  841. $result = Set::extract('/Category[id=1]/..', $data);
  842. $this->assertEqual($result, $expected);
  843. }
  844. /**
  845. * test parent selectors with extract
  846. *
  847. * @return void
  848. */
  849. function testExtractParentSelector() {
  850. $tree = array(
  851. array(
  852. 'Category' => array(
  853. 'name' => 'Category 1'
  854. ),
  855. 'children' => array(
  856. array(
  857. 'Category' => array(
  858. 'name' => 'Category 1.1'
  859. )
  860. )
  861. )
  862. ),
  863. array(
  864. 'Category' => array(
  865. 'name' => 'Category 2'
  866. ),
  867. 'children' => array(
  868. array(
  869. 'Category' => array(
  870. 'name' => 'Category 2.1'
  871. )
  872. ),
  873. array(
  874. 'Category' => array(
  875. 'name' => 'Category 2.2'
  876. )
  877. ),
  878. )
  879. ),
  880. array(
  881. 'Category' => array(
  882. 'name' => 'Category 3'
  883. ),
  884. 'children' => array(
  885. array(
  886. 'Category' => array(
  887. 'name' => 'Category 3.1'
  888. )
  889. )
  890. )
  891. )
  892. );
  893. $expected = array(array('Category' => $tree[1]['Category']));
  894. $r = Set::extract('/Category[name=Category 2]', $tree);
  895. $this->assertEqual($r, $expected);
  896. $expected = array(array('Category' => $tree[1]['Category'], 'children' => $tree[1]['children']));
  897. $r = Set::extract('/Category[name=Category 2]/..', $tree);
  898. $this->assertEqual($r, $expected);
  899. $expected = array(array('children' => $tree[1]['children'][0]), array('children' => $tree[1]['children'][1]));
  900. $r = Set::extract('/Category[name=Category 2]/../children', $tree);
  901. $this->assertEqual($r, $expected);
  902. $single = array(
  903. array(
  904. 'CallType' => array(
  905. 'name' => 'Internal Voice'
  906. ),
  907. 'x' => array(
  908. 'hour' => 7
  909. )
  910. )
  911. );
  912. $expected = array(7);
  913. $r = Set::extract('/CallType[name=Internal Voice]/../x/hour', $single);
  914. $this->assertEqual($r, $expected);
  915. $multiple = array(
  916. array(
  917. 'CallType' => array(
  918. 'name' => 'Internal Voice'
  919. ),
  920. 'x' => array(
  921. 'hour' => 7
  922. )
  923. ),
  924. array(
  925. 'CallType' => array(
  926. 'name' => 'Internal Voice'
  927. ),
  928. 'x' => array(
  929. 'hour' => 2
  930. )
  931. ),
  932. array(
  933. 'CallType' => array(
  934. 'name' => 'Internal Voice'
  935. ),
  936. 'x' => array(
  937. 'hour' => 1
  938. )
  939. )
  940. );
  941. $expected = array(7,2,1);
  942. $r = Set::extract('/CallType[name=Internal Voice]/../x/hour', $multiple);
  943. $this->assertEqual($r, $expected);
  944. $a = array(
  945. 'Model' => array(
  946. '0' => array(
  947. 'id' => 18,
  948. 'SubModelsModel' => array(
  949. 'id' => 1,
  950. 'submodel_id' => 66,
  951. 'model_id' => 18,
  952. 'type' => 1
  953. ),
  954. ),
  955. '1' => array(
  956. 'id' => 0,
  957. 'SubModelsModel' => array(
  958. 'id' => 2,
  959. 'submodel_id' => 66,
  960. 'model_id' => 0,
  961. 'type' => 1
  962. ),
  963. ),
  964. '2' => array(
  965. 'id' => 17,
  966. 'SubModelsModel' => array(
  967. 'id' => 3,
  968. 'submodel_id' => 66,
  969. 'model_id' => 17,
  970. 'type' => 2
  971. ),
  972. ),
  973. '3' => array(
  974. 'id' => 0,
  975. 'SubModelsModel' => array(
  976. 'id' => 4,
  977. 'submodel_id' => 66,
  978. 'model_id' => 0,
  979. 'type' => 2
  980. )
  981. )
  982. )
  983. );
  984. $expected = array(
  985. array(
  986. 'Model' => array(
  987. 'id' => 17,
  988. 'SubModelsModel' => array(
  989. 'id' => 3,
  990. 'submodel_id' => 66,
  991. 'model_id' => 17,
  992. 'type' => 2
  993. ),
  994. )
  995. ),
  996. array(
  997. 'Model' => array(
  998. 'id' => 0,
  999. 'SubModelsModel' => array(
  1000. 'id' => 4,
  1001. 'submodel_id' => 66,
  1002. 'model_id' => 0,
  1003. 'type' => 2
  1004. )
  1005. )
  1006. )
  1007. );
  1008. $r = Set::extract('/Model/SubModelsModel[type=2]/..', $a);
  1009. $this->assertEqual($r, $expected);
  1010. }
  1011. /**
  1012. * test that extract() still works when arrays don't contain a 0 index.
  1013. *
  1014. * @return void
  1015. */
  1016. function testExtractWithNonZeroArrays() {
  1017. $nonZero = array(
  1018. 1 => array(
  1019. 'User' => array(
  1020. 'id' => 1,
  1021. 'name' => 'John',
  1022. )
  1023. ),
  1024. 2 => array(
  1025. 'User' => array(
  1026. 'id' => 2,
  1027. 'name' => 'Bob',
  1028. )
  1029. ),
  1030. 3 => array(
  1031. 'User' => array(
  1032. 'id' => 3,
  1033. 'name' => 'Tony',
  1034. )
  1035. )
  1036. );
  1037. $expected = array(1, 2, 3);
  1038. $r = Set::extract('/User/id', $nonZero);
  1039. $this->assertEqual($r, $expected);
  1040. $expected = array(
  1041. array('User' => array('id' => 1, 'name' => 'John')),
  1042. array('User' => array('id' => 2, 'name' => 'Bob')),
  1043. array('User' => array('id' => 3, 'name' => 'Tony')),
  1044. );
  1045. $result = Set::extract('/User', $nonZero);
  1046. $this->assertEqual($result, $expected);
  1047. $nonSequential = array(
  1048. 'User' => array(
  1049. 0 => array('id' => 1),
  1050. 2 => array('id' => 2),
  1051. 6 => array('id' => 3),
  1052. 9 => array('id' => 4),
  1053. 3 => array('id' => 5),
  1054. ),
  1055. );
  1056. $nonZero = array(
  1057. 'User' => array(
  1058. 2 => array('id' => 1),
  1059. 4 => array('id' => 2),
  1060. 6 => array('id' => 3),
  1061. 9 => array('id' => 4),
  1062. 3 => array('id' => 5),
  1063. ),
  1064. );
  1065. $expected = array(1, 2, 3, 4, 5);
  1066. $this->assertEqual(Set::extract('/User/id', $nonSequential), $expected);
  1067. $result = Set::extract('/User/id', $nonZero);
  1068. $this->assertEqual($result, $expected, 'Failed non zero array key extract');
  1069. $expected = array(1, 2, 3, 4, 5);
  1070. $this->assertEqual(Set::extract('/User/id', $nonSequential), $expected);
  1071. $result = Set::extract('/User/id', $nonZero);
  1072. $this->assertEqual($result, $expected, 'Failed non zero array key extract');
  1073. $startingAtOne = array(
  1074. 'Article' => array(
  1075. 1 => array(
  1076. 'id' => 1,
  1077. 'approved' => 1,
  1078. ),
  1079. )
  1080. );
  1081. $expected = array(0 => array('Article' => array('id' => 1, 'approved' => 1)));
  1082. $result = Set::extract('/Article[approved=1]', $startingAtOne);
  1083. $this->assertEqual($result, $expected);
  1084. $items = array(
  1085. 240 => array(
  1086. 'A' => array(
  1087. 'field1' => 'a240',
  1088. 'field2' => 'a240',
  1089. ),
  1090. 'B' => array(
  1091. 'field1' => 'b240',
  1092. 'field2' => 'b240'
  1093. ),
  1094. )
  1095. );
  1096. $expected = array(
  1097. 0 => 'b240'
  1098. );
  1099. $result = Set::extract('/B/field1', $items);
  1100. $this->assertIdentical($result, $expected);
  1101. $this->assertIdentical($result, Set::extract('{n}.B.field1', $items));
  1102. }
  1103. /**
  1104. * testExtractWithArrays method
  1105. *
  1106. * @access public
  1107. * @return void
  1108. */
  1109. function testExtractWithArrays() {
  1110. $data = array(
  1111. 'Level1' => array(
  1112. 'Level2' => array('test1', 'test2'),
  1113. 'Level2bis' => array('test3', 'test4')
  1114. )
  1115. );
  1116. $this->assertEqual(Set::extract('/Level1/Level2', $data), array(array('Level2' => array('test1', 'test2'))));
  1117. $this->assertEqual(Set::extract('/Level1/Level2bis', $data), array(array('Level2bis' => array('test3', 'test4'))));
  1118. }
  1119. /**
  1120. * test extract() with elements that have non-array children.
  1121. *
  1122. * @return void
  1123. */
  1124. function testExtractWithNonArrayElements() {
  1125. $data = array(
  1126. 'node' => array(
  1127. array('foo'),
  1128. 'bar'
  1129. )
  1130. );
  1131. $result = Set::extract('/node', $data);
  1132. $expected = array(
  1133. array('node' => array('foo')),
  1134. 'bar'
  1135. );
  1136. $this->assertEqual($result, $expected);
  1137. $data = array(
  1138. 'node' => array(
  1139. 'foo' => array('bar'),
  1140. 'bar' => array('foo')
  1141. )
  1142. );
  1143. $result = Set::extract('/node', $data);
  1144. $expected = array(
  1145. array('foo' => array('bar')),
  1146. array('bar' => array('foo')),
  1147. );
  1148. $this->assertEqual($result, $expected);
  1149. $data = array(
  1150. 'node' => array(
  1151. 'foo' => array(
  1152. 'bar'
  1153. ),
  1154. 'bar' => 'foo'
  1155. )
  1156. );
  1157. $result = Set::extract('/node', $data);
  1158. $expected = array(
  1159. array('foo' => array('bar')),
  1160. 'foo'
  1161. );
  1162. $this->assertEqual($result, $expected);
  1163. }
  1164. /**
  1165. * testMatches method
  1166. *
  1167. * @access public
  1168. * @return void
  1169. */
  1170. function testMatches() {
  1171. $a = array(
  1172. array('Article' => array('id' => 1, 'title' => 'Article 1')),
  1173. array('Article' => array('id' => 2, 'title' => 'Article 2')),
  1174. array('Article' => array('id' => 3, 'title' => 'Article 3'))
  1175. );
  1176. $this->assertTrue(Set::matches(array('id=2'), $a[1]['Article']));
  1177. $this->assertFalse(Set::matches(array('id>2'), $a[1]['Article']));
  1178. $this->assertTrue(Set::matches(array('id>=2'), $a[1]['Article']));
  1179. $this->assertFalse(Set::matches(array('id>=3'), $a[1]['Article']));
  1180. $this->assertTrue(Set::matches(array('id<=2'), $a[1]['Article']));
  1181. $this->assertFalse(Set::matches(array('id<2'), $a[1]['Article']));
  1182. $this->assertTrue(Set::matches(array('id>1'), $a[1]['Article']));
  1183. $this->assertTrue(Set::matches(array('id>1', 'id<3', 'id!=0'), $a[1]['Article']));
  1184. $this->assertTrue(Set::matches(array('3'), null, 3));
  1185. $this->assertTrue(Set::matches(array('5'), null, 5));
  1186. $this->assertTrue(Set::matches(array('id'), $a[1]['Article']));
  1187. $this->assertTrue(Set::matches(array('id', 'title'), $a[1]['Article']));
  1188. $this->assertFalse(Set::matches(array('non-existant'), $a[1]['Article']));
  1189. $this->assertTrue(Set::matches('/Article[id=2]', $a));
  1190. $this->assertFalse(Set::matches('/Article[id=4]', $a));
  1191. $this->assertTrue(Set::matches(array(), $a));
  1192. $r = array(
  1193. 'Attachment' => array(
  1194. 'keep' => array()
  1195. ),
  1196. 'Comment' => array(
  1197. 'keep' => array(
  1198. 'Attachment' => array(
  1199. 'fields' => array(
  1200. 0 => 'attachment',
  1201. ),
  1202. ),
  1203. )
  1204. ),
  1205. 'User' => array(
  1206. 'keep' => array()
  1207. ),
  1208. 'Article' => array(
  1209. 'keep' => array(
  1210. 'Comment' => array(
  1211. 'fields' => array(
  1212. 0 => 'comment',
  1213. 1 => 'published',
  1214. ),
  1215. ),
  1216. 'User' => array(
  1217. 'fields' => array(
  1218. 0 => 'user',
  1219. ),
  1220. ),
  1221. )
  1222. )
  1223. );
  1224. $this->assertTrue(Set::matches('/Article/keep/Comment', $r));
  1225. $this->assertEqual(Set::extract('/Article/keep/Comment/fields', $r), array('comment', 'published'));
  1226. $this->assertEqual(Set::extract('/Article/keep/User/fields', $r), array('user'));
  1227. }
  1228. /**
  1229. * testSetExtractReturnsEmptyArray method
  1230. *
  1231. * @access public
  1232. * @return void
  1233. */
  1234. function testSetExtractReturnsEmptyArray() {
  1235. $this->assertIdentical(Set::extract(array(), '/Post/id'), array());
  1236. $this->assertIdentical(Set::extract('/Post/id', array()), array());
  1237. $this->assertIdentical(Set::extract('/Post/id', array(
  1238. array('Post' => array('name' => 'bob')),
  1239. array('Post' => array('name' => 'jim'))
  1240. )), array());
  1241. $this->assertIdentical(Set::extract(array(), 'Message.flash'), null);
  1242. }
  1243. /**
  1244. * testClassicExtract method
  1245. *
  1246. * @access public
  1247. * @return void
  1248. */
  1249. function testClassicExtract() {
  1250. $a = array(
  1251. array('Article' => array('id' => 1, 'title' => 'Article 1')),
  1252. array('Article' => array('id' => 2, 'title' => 'Article 2')),
  1253. array('Article' => array('id' => 3, 'title' => 'Article 3'))
  1254. );
  1255. $result = Set::extract($a, '{n}.Article.id');
  1256. $expected = array( 1, 2, 3 );
  1257. $this->assertIdentical($result, $expected);
  1258. $result = Set::extract($a, '{n}.Article.title');
  1259. $expected = array( 'Article 1', 'Article 2', 'Article 3' );
  1260. $this->assertIdentical($result, $expected);
  1261. $result = Set::extract($a, '1.Article.title');
  1262. $expected = 'Article 2';
  1263. $this->assertIdentical($result, $expected);
  1264. $result = Set::extract($a, '3.Article.title');
  1265. $expected = null;
  1266. $this->assertIdentical($result, $expected);
  1267. $a = array(
  1268. array(
  1269. 'Article' => array('id' => 1, 'title' => 'Article 1',
  1270. 'User' => array('id' => 1, 'username' => 'mariano.iglesias'))
  1271. ),
  1272. array(
  1273. 'Article' => array('id' => 2, 'title' => 'Article 2',
  1274. 'User' => array('id' => 1, 'username' => 'mariano.iglesias'))
  1275. ),
  1276. array(
  1277. 'Article' => array('id' => 3, 'title' => 'Article 3',
  1278. 'User' => array('id' => 2, 'username' => 'phpnut'))
  1279. )
  1280. );
  1281. $result = Set::extract($a, '{n}.Article.User.username');
  1282. $expected = array( 'mariano.iglesias', 'mariano.iglesias', 'phpnut' );
  1283. $this->assertIdentical($result, $expected);
  1284. $a = array(
  1285. array('Article' => array('id' => 1, 'title' => 'Article 1',
  1286. 'Comment' => array(
  1287. array('id' => 10, 'title' => 'Comment 10'),
  1288. array('id' => 11, 'title' => 'Comment 11'),
  1289. array('id' => 12, 'title' => 'Comment 12')))),
  1290. array('Article' => array('id' => 2, 'title' => 'Article 2',
  1291. 'Comment' => array(
  1292. array('id' => 13, 'title' => 'Comment 13'),
  1293. array('id' => 14, 'title' => 'Comment 14')))),
  1294. array('Article' => array('id' => 3, 'title' => 'Article 3')));
  1295. $result = Set::extract($a, '{n}.Article.Comment.{n}.id');
  1296. $expected = array (array(10, 11, 12), array(13, 14), null);
  1297. $this->assertIdentical($result, $expected);
  1298. $result = Set::extract($a, '{n}.Article.Comment.{n}.title');
  1299. $expected = array(
  1300. array('Comment 10', 'Comment 11', 'Comment 12'),
  1301. array('Comment 13', 'Comment 14'),
  1302. null
  1303. );
  1304. $this->assertIdentical($result, $expected);
  1305. $a = array(array('1day' => '20 sales'), array('1day' => '2 sales'));
  1306. $result = Set::extract($a, '{n}.1day');
  1307. $expected = array('20 sales', '2 sales');
  1308. $this->assertIdentical($result, $expected);
  1309. $a = array(
  1310. 'pages' => array('name' => 'page'),
  1311. 'fruites' => array('name' => 'fruit'),
  1312. 0 => array('name' => 'zero')
  1313. );
  1314. $result = Set::extract($a, '{s}.name');
  1315. $expected = array('page','fruit');
  1316. $this->assertIdentical($result, $expected);
  1317. $a = array(
  1318. 0 => array('pages' => array('name' => 'page')),
  1319. 1 => array('fruites'=> array('name' => 'fruit')),
  1320. 'test' => array(array('name' => 'jippi')),
  1321. 'dot.test' => array(array('name' => 'jippi'))
  1322. );
  1323. $result = Set::extract($a, '{n}.{s}.name');
  1324. $expected = array(0 => array('page'), 1 => array('fruit'));
  1325. $this->assertIdentical($result, $expected);
  1326. $result = Set::extract($a, '{s}.{n}.name');
  1327. $expected = array(array('jippi'), array('jippi'));
  1328. $this->assertIdentical($result, $expected);
  1329. $result = Set::extract($a,'{\w+}.{\w+}.name');
  1330. $expected = array(
  1331. array('pages' => 'page'),
  1332. array('fruites' => 'fruit'),
  1333. 'test' => array('jippi'),
  1334. 'dot.test' => array('jippi')
  1335. );
  1336. $this->assertIdentical($result, $expected);
  1337. $result = Set::extract($a,'{\d+}.{\w+}.name');
  1338. $expected = array(array('pages' => 'page'), array('fruites' => 'fruit'));
  1339. $this->assertIdentical($result, $expected);
  1340. $result = Set::extract($a,'{n}.{\w+}.name');
  1341. $expected = array(array('pages' => 'page'), array('fruites' => 'fruit'));
  1342. $this->assertIdentical($result, $expected);
  1343. $result = Set::extract($a,'{s}.{\d+}.name');
  1344. $expected = array(array('jippi'), array('jippi'));
  1345. $this->assertIdentical($result, $expected);
  1346. $result = Set::extract($a,'{s}');
  1347. $expected = array(array(array('name' => 'jippi')), array(array('name' => 'jippi')));
  1348. $this->assertIdentical($result, $expected);
  1349. $result = Set::extract($a,'{[a-z]}');
  1350. $expected = array(
  1351. 'test' => array(array('name' => 'jippi')),
  1352. 'dot.test' => array(array('name' => 'jippi'))
  1353. );
  1354. $this->assertIdentical($result, $expected);
  1355. $result = Set::extract($a, '{dot\.test}.{n}');
  1356. $expected = array('dot.test' => array(array('name' => 'jippi')));
  1357. $this->assertIdentical($result, $expected);
  1358. $a = new stdClass();
  1359. $a->articles = array(
  1360. array('Article' => array('id' => 1, 'title' => 'Article 1')),
  1361. array('Article' => array('id' => 2, 'title' => 'Article 2')),
  1362. array('Article' => array('id' => 3, 'title' => 'Article 3')));
  1363. $result = Set::extract($a, 'articles.{n}.Article.id');
  1364. $expected = array( 1, 2, 3 );
  1365. $this->assertIdentical($result, $expected);
  1366. $result = Set::extract($a, 'articles.{n}.Article.title');
  1367. $expected = array( 'Article 1', 'Article 2', 'Article 3' );
  1368. $this->assertIdentical($result, $expected);
  1369. }
  1370. /**
  1371. * testInsert method
  1372. *
  1373. * @access public
  1374. * @return void
  1375. */
  1376. function testInsert() {
  1377. $a = array(
  1378. 'pages' => array('name' => 'page')
  1379. );
  1380. $result = Set::insert($a, 'files', array('name' => 'files'));
  1381. $expected = array(
  1382. 'pages' => array('name' => 'page'),
  1383. 'files' => array('name' => 'files')
  1384. );
  1385. $this->assertIdentical($result, $expected);
  1386. $a = array(
  1387. 'pages' => array('name' => 'page')
  1388. );
  1389. $result = Set::insert($a, 'pages.name', array());
  1390. $expected = array(
  1391. 'pages' => array('name' => array()),
  1392. );
  1393. $this->assertIdentical($result, $expected);
  1394. $a = array(
  1395. 'pages' => array(
  1396. 0 => array('name' => 'main'),
  1397. 1 => array('name' => 'about')
  1398. )
  1399. );
  1400. $result = Set::insert($a, 'pages.1.vars', array('title' => 'page title'));
  1401. $expected = array(
  1402. 'pages' => array(
  1403. 0 => array('name' => 'main'),
  1404. 1 => array('name' => 'about', 'vars' => array('title' => 'page title'))
  1405. )
  1406. );
  1407. $this->assertIdentical($result, $expected);
  1408. }
  1409. /**
  1410. * testRemove method
  1411. *
  1412. * @access public
  1413. * @return void
  1414. */
  1415. function testRemove() {
  1416. $a = array(
  1417. 'pages' => array('name' => 'page'),
  1418. 'files' => array('name' => 'files')
  1419. );
  1420. $result = Set::remove($a, 'files', array('name' => 'files'));
  1421. $expected = array(
  1422. 'pages' => array('name' => 'page')
  1423. );
  1424. $this->assertIdentical($result, $expected);
  1425. $a = array(
  1426. 'pages' => array(
  1427. 0 => array('name' => 'main'),
  1428. 1 => array('name' => 'about', 'vars' => array('title' => 'page title'))
  1429. )
  1430. );
  1431. $result = Set::remove($a, 'pages.1.vars', array('title' => 'page title'));
  1432. $expected = array(
  1433. 'pages' => array(
  1434. 0 => array('name' => 'main'),
  1435. 1 => array('name' => 'about')
  1436. )
  1437. );
  1438. $this->assertIdentical($result, $expected);
  1439. $result = Set::remove($a, 'pages.2.vars', array('title' => 'page title'));
  1440. $expected = $a;
  1441. $this->assertIdentical($result, $expected);
  1442. }
  1443. /**
  1444. * testCheck method
  1445. *
  1446. * @access public
  1447. * @return void
  1448. */
  1449. function testCheck() {
  1450. $set = array(
  1451. 'My Index 1' => array('First' => 'The first item')
  1452. );
  1453. $this->assertTrue(Set::check($set, 'My Index 1.First'));
  1454. $this->assertTrue(Set::check($set, 'My Index 1'));
  1455. $this->assertTrue(Set::check($set, array()));
  1456. $set = array(
  1457. 'My Index 1' => array('First' => array('Second' => array('Third' => array('Fourth' => 'Heavy. Nesting.'))))
  1458. );
  1459. $this->assertTrue(Set::check($set, 'My Index 1.First.Second'));
  1460. $this->assertTrue(Set::check($set, 'My Index 1.First.Second.Third'));
  1461. $this->assertTrue(Set::check($set, 'My Index 1.First.Second.Third.Fourth'));
  1462. $this->assertFalse(Set::check($set, 'My Index 1.First.Seconds.Third.Fourth'));
  1463. }
  1464. /**
  1465. * testWritingWithFunkyKeys method
  1466. *
  1467. * @access public
  1468. * @return void
  1469. */
  1470. function testWritingWithFunkyKeys() {
  1471. $set = Set::insert(array(), 'Session Test', "test");
  1472. $this->assertEqual(Set::extract($set, 'Session Test'), 'test');
  1473. $set = Set::remove($set, 'Session Test');
  1474. $this->assertFalse(Set::check($set, 'Session Test'));
  1475. $this->assertTrue($set = Set::insert(array(), 'Session Test.Test Case', "test"));
  1476. $this->assertTrue(Set::check($set, 'Session Test.Test Case'));
  1477. }
  1478. /**
  1479. * testDiff method
  1480. *
  1481. * @access public
  1482. * @return void
  1483. */
  1484. function testDiff() {
  1485. $a = array(
  1486. 0 => array('name' => 'main'),
  1487. 1 => array('name' => 'about')
  1488. );
  1489. $b = array(
  1490. 0 => array('name' => 'main'),
  1491. 1 => array('name' => 'about'),
  1492. 2 => array('name' => 'contact')
  1493. );
  1494. $result = Set::diff($a, $b);
  1495. $expected = array(
  1496. 2 => array('name' => 'contact')
  1497. );
  1498. $this->assertIdentical($result, $expected);
  1499. $result = Set::diff($a, array());
  1500. $expected = $a;
  1501. $this->assertIdentical($result, $expected);
  1502. $result = Set::diff(array(), $b);
  1503. $expected = $b;
  1504. $this->assertIdentical($result, $expected);
  1505. $b = array(
  1506. 0 => array('name' => 'me'),
  1507. 1 => array('name' => 'about')
  1508. );
  1509. $result = Set::diff($a, $b);
  1510. $expected = array(
  1511. 0 => array('name' => 'main')
  1512. );
  1513. $this->assertIdentical($result, $expected);
  1514. }
  1515. /**
  1516. * testIsEqual method
  1517. *
  1518. * @access public
  1519. * @return void
  1520. */
  1521. function testIsEqual() {
  1522. $a = array(
  1523. 0 => array('name' => 'main'),
  1524. 1 => array('name' => 'about')
  1525. );
  1526. $b = array(
  1527. 0 => array('name' => 'main'),
  1528. 1 => array('name' => 'about'),
  1529. 2 => array('name' => 'contact')
  1530. );
  1531. $this->assertTrue(Set::isEqual($a, $a));
  1532. $this->assertTrue(Set::isEqual($b, $b));
  1533. $this->assertFalse(Set::isEqual($a, $b));
  1534. $this->assertFalse(Set::isEqual($b, $a));
  1535. }
  1536. /**
  1537. * testContains method
  1538. *
  1539. * @access public
  1540. * @return void
  1541. */
  1542. function testContains() {
  1543. $a = array(
  1544. 0 => array('name' => 'main'),
  1545. 1 => array('name' => 'about')
  1546. );
  1547. $b = array(
  1548. 0 => array('name' => 'main'),
  1549. 1 => array('name' => 'about'),
  1550. 2 => array('name' => 'contact'),
  1551. 'a' => 'b'
  1552. );
  1553. $this->assertTrue(Set::contains($a, $a));
  1554. $this->assertFalse(Set::contains($a, $b));
  1555. $this->assertTrue(Set::contains($b, $a));
  1556. }
  1557. /**
  1558. * testCombine method
  1559. *
  1560. * @access public
  1561. * @return void
  1562. */
  1563. function testCombine() {
  1564. $result = Set::combine(array(), '{n}.User.id', '{n}.User.Data');
  1565. $this->assertFalse($result);
  1566. $result = Set::combine('', '{n}.User.id', '{n}.User.Data');
  1567. $this->assertFalse($result);
  1568. $a = array(
  1569. array('User' => array('id' => 2, 'group_id' => 1,
  1570. 'Data' => array('user' => 'mariano.iglesias','name' => 'Mariano Iglesias'))),
  1571. array('User' => array('id' => 14, 'group_id' => 2,
  1572. 'Data' => array('user' => 'phpnut', 'name' => 'Larry E. Masters'))),
  1573. array('User' => array('id' => 25, 'group_id' => 1,
  1574. 'Data' => array('user' => 'gwoo','name' => 'The Gwoo'))));
  1575. $result = Set::combine($a, '{n}.User.id');
  1576. $expected = array(2 => null, 14 => null, 25 => null);
  1577. $this->assertIdentical($result, $expected);
  1578. $result = Set::combine($a, '{n}.User.id', '{n}.User.non-existant');
  1579. $expected = array(2 => null, 14 => null, 25 => null);
  1580. $this->assertIdentical($result, $expected);
  1581. $result = Set::combine($a, '{n}.User.id', '{n}.User.Data');
  1582. $expected = array(
  1583. 2 => array('user' => 'mariano.iglesias', 'name' => 'Mariano Iglesias'),
  1584. 14 => array('user' => 'phpnut', 'name' => 'Larry E. Masters'),
  1585. 25 => array('user' => 'gwoo', 'name' => 'The Gwoo'));
  1586. $this->assertIdentical($result, $expected);
  1587. $result = Set::combine($a, '{n}.User.id', '{n}.User.Data.name');
  1588. $expected = array(
  1589. 2 => 'Mariano Iglesias',
  1590. 14 => 'Larry E. Masters',
  1591. 25 => 'The Gwoo');
  1592. $this->assertIdentical($result, $expected);
  1593. $result = Set::combine($a, '{n}.User.id', '{n}.User.Data', '{n}.User.group_id');
  1594. $expected = array(
  1595. 1 => array(
  1596. 2 => array('user' => 'mariano.iglesias', 'name' => 'Mariano Iglesias'),
  1597. 25 => array('user' => 'gwoo', 'name' => 'The Gwoo')),
  1598. 2 => array(
  1599. 14 => array('user' => 'phpnut', 'name' => 'Larry E. Masters')));
  1600. $this->assertIdentical($result, $expected);
  1601. $result = Set::combine($a, '{n}.User.id', '{n}.User.Data.name', '{n}.User.group_id');
  1602. $expected = array(
  1603. 1 => array(
  1604. 2 => 'Mariano Iglesias',
  1605. 25 => 'The Gwoo'),
  1606. 2 => array(
  1607. 14 => 'Larry E. Masters'));
  1608. $this->assertIdentical($result, $expected);
  1609. $result = Set::combine($a, '{n}.User.id');
  1610. $expected = array(2 => null, 14 => null, 25 => null);
  1611. $this->assertIdentical($result, $expected);
  1612. $result = Set::combine($a, '{n}.User.id', '{n}.User.Data');
  1613. $expected = array(
  1614. 2 => array('user' => 'mariano.iglesias', 'name' => 'Mariano Iglesias'),
  1615. 14 => array('user' => 'phpnut', 'name' => 'Larry E. Masters'),
  1616. 25 => array('user' => 'gwoo', 'name' => 'The Gwoo'));
  1617. $this->assertIdentical($result, $expected);
  1618. $result = Set::combine($a, '{n}.User.id', '{n}.User.Data.name');
  1619. $expected = array(2 => 'Mariano Iglesias', 14 => 'Larry E. Masters', 25 => 'The Gwoo');
  1620. $this->assertIdentical($result, $expected);
  1621. $result = Set::combine($a, '{n}.User.id', '{n}.User.Data', '{n}.User.group_id');
  1622. $expected = array(
  1623. 1 => array(
  1624. 2 => array('user' => 'mariano.iglesias', 'name' => 'Mariano Iglesias'),
  1625. 25 => array('user' => 'gwoo', 'name' => 'The Gwoo')),
  1626. 2 => array(
  1627. 14 => array('user' => 'phpnut', 'name' => 'Larry E. Masters')));
  1628. $this->assertIdentical($result, $expected);
  1629. $result = Set::combine($a, '{n}.User.id', '{n}.User.Data.name', '{n}.User.group_id');
  1630. $expected = array(
  1631. 1 => array(
  1632. 2 => 'Mariano Iglesias',
  1633. 25 => 'The Gwoo'),
  1634. 2 => array(
  1635. 14 => 'Larry E. Masters'));
  1636. $this->assertIdentical($result, $expected);
  1637. $result = Set::combine($a, '{n}.User.id', array('{0}: {1}', '{n}.User.Data.user', '{n}.User.Data.name'), '{n}.User.group_id');
  1638. $expected = array (
  1639. 1 => array (
  1640. 2 => 'mariano.iglesias: Mariano Iglesias',
  1641. 25 => 'gwoo: The Gwoo'),
  1642. 2 => array (14 => 'phpnut: Larry E. Masters'));
  1643. $this->assertIdentical($result, $expected);
  1644. $result = Set::combine($a, array('{0}: {1}', '{n}.User.Data.user', '{n}.User.Data.name'), '{n}.User.id');
  1645. $expected = array('mariano.iglesias: Mariano Iglesias' => 2, 'phpnut: Larry E. Masters' => 14, 'gwoo: The Gwoo' => 25);
  1646. $this->assertIdentical($result, $expected);
  1647. $result = Set::combine($a, array('{1}: {0}', '{n}.User.Data.user', '{n}.User.Data.name'), '{n}.User.id');
  1648. $expected = array('Mariano Iglesias: mariano.iglesias' => 2, 'Larry E. Masters: phpnut' => 14, 'The Gwoo: gwoo' => 25);
  1649. $this->assertIdentical($result, $expected);
  1650. $result = Set::combine($a, array('%1$s: %2$d', '{n}.User.Data.user', '{n}.User.id'), '{n}.User.Data.name');
  1651. $expected = array('mariano.iglesias: 2' => 'Mariano Iglesias', 'phpnut: 14' => 'Larry E. Masters', 'gwoo: 25' => 'The Gwoo');
  1652. $this->assertIdentical($result, $expected);
  1653. $result = Set::combine($a, array('%2$d: %1$s', '{n}.User.Data.user', '{n}.User.id'), '{n}.User.Data.name');
  1654. $expected = array('2: mariano.iglesias' => 'Mariano Iglesias', '14: phpnut' => 'Larry E. Masters', '25: gwoo' => 'The Gwoo');
  1655. $this->assertIdentical($result, $expected);
  1656. $b = new stdClass();
  1657. $b->users = array(
  1658. array('User' => array('id' => 2, 'group_id' => 1,
  1659. 'Data' => array('user' => 'mariano.iglesias','name' => 'Mariano Iglesias'))),
  1660. array('User' => array('id' => 14, 'group_id' => 2,
  1661. 'Data' => array('user' => 'phpnut', 'name' => 'Larry E. Masters'))),
  1662. array('User' => array('id' => 25, 'group_id' => 1,
  1663. 'Data' => array('user' => 'gwoo','name' => 'The Gwoo'))));
  1664. $result = Set::combine($b, 'users.{n}.User.id');
  1665. $expected = array(2 => null, 14 => null, 25 => null);
  1666. $this->assertIdentical($result, $expected);
  1667. $result = Set::combine($b, 'users.{n}.User.id', 'users.{n}.User.non-existant');
  1668. $expected = array(2 => null, 14 => null, 25 => null);
  1669. $this->assertIdentical($result, $expected);
  1670. $result = Set::combine($a, 'fail', 'fail');
  1671. $this->assertEqual($result, array());
  1672. }
  1673. /**
  1674. * testMapReverse method
  1675. *
  1676. * @access public
  1677. * @return void
  1678. */
  1679. function testMapReverse() {
  1680. $result = Set::reverse(null);
  1681. $this->assertEqual($result, null);
  1682. $result = Set::reverse(false);
  1683. $this->assertEqual($result, false);
  1684. $expected = array(
  1685. 'Array1' => array(
  1686. 'Array1Data1' => 'Array1Data1 value 1', 'Array1Data2' => 'Array1Data2 value 2'),
  1687. 'Array2' => array(
  1688. 0 => array('Array2Data1' => 1, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4'),
  1689. 1 => array('Array2Data1' => 2, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4'),
  1690. 2 => array('Array2Data1' => 3, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4'),
  1691. 3 => array('Array2Data1' => 4, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4'),
  1692. 4 => array('Array2Data1' => 5, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4')),
  1693. 'Array3' => array(
  1694. 0 => array('Array3Data1' => 1, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4'),
  1695. 1 => array('Array3Data1' => 2, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4'),
  1696. 2 => array('Array3Data1' => 3, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4'),
  1697. 3 => array('Array3Data1' => 4, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4'),
  1698. 4 => array('Array3Data1' => 5, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4')));
  1699. $map = Set::map($expected, true);
  1700. $this->assertEqual($map->Array1->Array1Data1, $expected['Array1']['Array1Data1']);
  1701. $this->assertEqual($map->Array2[0]->Array2Data1, $expected['Array2'][0]['Array2Data1']);
  1702. $result = Set::reverse($map);
  1703. $this->assertIdentical($result, $expected);
  1704. $expected = array(
  1705. 'Post' => array('id'=> 1, 'title' => 'First Post'),
  1706. 'Comment' => array(
  1707. array('id'=> 1, 'title' => 'First Comment'),
  1708. array('id'=> 2, 'title' => 'Second Comment')
  1709. ),
  1710. 'Tag' => array(
  1711. array('id'=> 1, 'title' => 'First Tag'),
  1712. array('id'=> 2, 'title' => 'Second Tag')
  1713. ),
  1714. );
  1715. $map = Set::map($expected);
  1716. $this->assertIdentical($map->title, $expected['Post']['title']);
  1717. foreach ($map->Comment as $comment) {
  1718. $ids[] = $comment->id;
  1719. }
  1720. $this->assertIdentical($ids, array(1, 2));
  1721. $expected = array(
  1722. 'Array1' => array(
  1723. 'Array1Data1' => 'Array1Data1 value 1', 'Array1Data2' => 'Array1Data2 value 2', 'Array1Data3' => 'Array1Data3 value 3','Array1Data4' => 'Array1Data4 value 4',
  1724. 'Array1Data5' => 'Array1Data5 value 5', 'Array1Data6' => 'Array1Data6 value 6', 'Array1Data7' => 'Array1Data7 value 7', 'Array1Data8' => 'Array1Data8 value 8'),
  1725. 'string' => 1,
  1726. 'another' => 'string',
  1727. 'some' => 'thing else',
  1728. 'Array2' => array(
  1729. 0 => array('Array2Data1' => 1, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4'),
  1730. 1 => array('Array2Data1' => 2, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4'),
  1731. 2 => array('Array2Data1' => 3, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4'),
  1732. 3 => array('Array2Data1' => 4, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4'),
  1733. 4 => array('Array2Data1' => 5, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4')),
  1734. 'Array3' => array(
  1735. 0 => array('Array3Data1' => 1, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4'),
  1736. 1 => array('Array3Data1' => 2, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4'),
  1737. 2 => array('Array3Data1' => 3, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4'),
  1738. 3 => array('Array3Data1' => 4, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4'),
  1739. 4 => array('Array3Data1' => 5, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4')));
  1740. $map = Set::map($expected, true);
  1741. $result = Set::reverse($map);
  1742. $this->assertIdentical($result, $expected);
  1743. $expected = array(
  1744. 'Array1' => array(
  1745. 'Array1Data1' => 'Array1Data1 value 1', 'Array1Data2' => 'Array1Data2 value 2', 'Array1Data3' => 'Array1Data3 value 3','Array1Data4' => 'Array1Data4 value 4',
  1746. 'Array1Data5' => 'Array1Data5 value 5', 'Array1Data6' => 'Array1Data6 value 6', 'Array1Data7' => 'Array1Data7 value 7', 'Array1Data8' => 'Array1Data8 value 8'),
  1747. 'string' => 1,
  1748. 'another' => 'string',
  1749. 'some' => 'thing else',
  1750. 'Array2' => array(
  1751. 0 => array('Array2Data1' => 1, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4'),
  1752. 1 => array('Array2Data1' => 2, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4'),
  1753. 2 => array('Array2Data1' => 3, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4'),
  1754. 3 => array('Array2Data1' => 4, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4'),
  1755. 4 => array('Array2Data1' => 5, 'Array2Data2' => 'Array2Data2 value 2', 'Array2Data3' => 'Array2Data3 value 2', 'Array2Data4' => 'Array2Data4 value 4')),
  1756. 'string2' => 1,
  1757. 'another2' => 'string',
  1758. 'some2' => 'thing else',
  1759. 'Array3' => array(
  1760. 0 => array('Array3Data1' => 1, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4'),
  1761. 1 => array('Array3Data1' => 2, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4'),
  1762. 2 => array('Array3Data1' => 3, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4'),
  1763. 3 => array('Array3Data1' => 4, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4'),
  1764. 4 => array('Array3Data1' => 5, 'Array3Data2' => 'Array3Data2 value 2', 'Array3Data3' => 'Array3Data3 value 2', 'Array3Data4' => 'Array3Data4 value 4')),
  1765. 'string3' => 1,
  1766. 'another3' => 'string',
  1767. 'some3' => 'thing else');
  1768. $map = Set::map($expected, true);
  1769. $result = Set::reverse($map);
  1770. $this->assertIdentical($result, $expected);
  1771. $expected = array('User' => array('psword'=> 'whatever', 'Icon' => array('id'=> 851)));
  1772. $map = Set::map($expected);
  1773. $result = Set::reverse($map);
  1774. $this->assertIdentical($result, $expected);
  1775. $expected = array('User' => array('psword'=> 'whatever', 'Icon' => array('id'=> 851)));
  1776. $class = new stdClass;
  1777. $class->User = new stdClass;
  1778. $class->User->psword = 'whatever';
  1779. $class->User->Icon = new stdClass;
  1780. $class->User->Icon->id = 851;
  1781. $result = Set::reverse($class);
  1782. $this->assertIdentical($result, $expected);
  1783. $expected = array('User' => array('psword'=> 'whatever', 'Icon' => array('id'=> 851), 'Profile' => array('name' => 'Some Name', 'address' => 'Some Address')));
  1784. $class = new stdClass;
  1785. $class->User = new stdClass;
  1786. $class->User->psword = 'whatever';
  1787. $class->User->Icon = new stdClass;
  1788. $class->User->Icon->id = 851;
  1789. $class->User->Profile = new stdClass;
  1790. $class->User->Profile->name = 'Some Name';
  1791. $class->User->Profile->address = 'Some Address';
  1792. $result = Set::reverse($class);
  1793. $this->assertIdentical($result, $expected);
  1794. $expected = array('User' => array('psword'=> 'whatever',
  1795. 'Icon' => array('id'=> 851),
  1796. 'Profile' => array('name' => 'Some Name', 'address' => 'Some Address'),
  1797. 'Comment' => array(
  1798. 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'),
  1799. 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'))));
  1800. $class = new stdClass;
  1801. $class->User = new stdClass;
  1802. $class->User->psword = 'whatever';
  1803. $class->User->Icon = new stdClass;
  1804. $class->User->Icon->id = 851;
  1805. $class->User->Profile = new stdClass;
  1806. $class->User->Profile->name = 'Some Name';
  1807. $class->User->Profile->address = 'Some Address';
  1808. $class->User->Comment = new stdClass;
  1809. $class->User->Comment->{'0'} = new stdClass;
  1810. $class->User->Comment->{'0'}->id = 1;
  1811. $class->User->Comment->{'0'}->article_id = 1;
  1812. $class->User->Comment->{'0'}->user_id = 1;
  1813. $class->User->Comment->{'0'}->comment = 'First Comment for First Article';
  1814. $class->User->Comment->{'0'}->published = 'Y';
  1815. $class->User->Comment->{'0'}->created = '2007-03-18 10:47:23';
  1816. $class->User->Comment->{'0'}->updated = '2007-03-18 10:49:31';
  1817. $class->User->Comment->{'1'} = new stdClass;
  1818. $class->User->Comment->{'1'}->id = 2;
  1819. $class->User->Comment->{'1'}->article_id = 1;
  1820. $class->User->Comment->{'1'}->user_id = 2;
  1821. $class->User->Comment->{'1'}->comment = 'Second Comment for First Article';
  1822. $class->User->Comment->{'1'}->published = 'Y';
  1823. $class->User->Comment->{'1'}->created = '2007-03-18 10:47:23';
  1824. $class->User->Comment->{'1'}->updated = '2007-03-18 10:49:31';
  1825. $result = Set::reverse($class);
  1826. $this->assertIdentical($result, $expected);
  1827. $expected = array('User' => array('psword'=> 'whatever',
  1828. 'Icon' => array('id'=> 851),
  1829. 'Profile' => array('name' => 'Some Name', 'address' => 'Some Address'),
  1830. 'Comment' => array(
  1831. 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'),
  1832. 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'))));
  1833. $class = new stdClass;
  1834. $class->User = new stdClass;
  1835. $class->User->psword = 'whatever';
  1836. $class->User->Icon = new stdClass;
  1837. $class->User->Icon->id = 851;
  1838. $class->User->Profile = new stdClass;
  1839. $class->User->Profile->name = 'Some Name';
  1840. $class->User->Profile->address = 'Some Address';
  1841. $class->User->Comment = array();
  1842. $comment = new stdClass;
  1843. $comment->id = 1;
  1844. $comment->article_id = 1;
  1845. $comment->user_id = 1;
  1846. $comment->comment = 'First Comment for First Article';
  1847. $comment->published = 'Y';
  1848. $comment->created = '2007-03-18 10:47:23';
  1849. $comment->updated = '2007-03-18 10:49:31';
  1850. $comment2 = new stdClass;
  1851. $comment2->id = 2;
  1852. $comment2->article_id = 1;
  1853. $comment2->user_id = 2;
  1854. $comment2->comment = 'Second Comment for First Article';
  1855. $comment2->published = 'Y';
  1856. $comment2->created = '2007-03-18 10:47:23';
  1857. $comment2->updated = '2007-03-18 10:49:31';
  1858. $class->User->Comment = array($comment, $comment2);
  1859. $result = Set::reverse($class);
  1860. $this->assertIdentical($result, $expected);
  1861. uses('model'.DS.'model');
  1862. $model = new Model(array('id' => false, 'name' => 'Model', 'table' => false));
  1863. $expected = array(
  1864. 'Behaviors' => array('modelName' => 'Model', '_attached' => array(), '_disabled' => array(), '__methods' => array(), '__mappedMethods' => array(), '_log' => null),
  1865. 'useDbConfig' => 'default', 'useTable' => false, 'displayField' => null, 'id' => false, 'data' => array(), 'table' => 'models', 'primaryKey' => 'id', '_schema' => null, 'validate' => array(),
  1866. 'validationErrors' => array(), 'tablePrefix' => null, 'name' => 'Model', 'alias' => 'Model', 'tableToModel' => array(), 'logTransactions' => false, 'transactional' => false, 'cacheQueries' => false,
  1867. 'belongsTo' => array(), 'hasOne' => array(), 'hasMany' => array(), 'hasAndBelongsToMany' => array(), 'actsAs' => null, 'whitelist' => array(), 'cacheSources' => true,
  1868. 'findQueryType' => null, 'recursive' => 1, 'order' => null, '__exists' => null,
  1869. '__associationKeys' => array(
  1870. 'belongsTo' => array('className', 'foreignKey', 'conditions', 'fields', 'order', 'counterCache'),
  1871. 'hasOne' => array('className', 'foreignKey', 'conditions', 'fields', 'order', 'dependent'),
  1872. 'hasMany' => array('className', 'foreignKey', 'conditions', 'fields', 'order', 'limit', 'offset', 'dependent', 'exclusive', 'finderQuery', 'counterQuery'),
  1873. 'hasAndBelongsToMany' => array('className', 'joinTable', 'with', 'foreignKey', 'associationForeignKey', 'conditions', 'fields', 'order', 'limit', 'offset', 'unique', 'finderQuery', 'deleteQuery', 'insertQuery')),
  1874. '__associations' => array('belongsTo', 'hasOne', 'hasMany', 'hasAndBelongsToMany'), '__backAssociation' => array(), '__insertID' => null, '__numRows' => null, '__affectedRows' => null,
  1875. '_findMethods' => array('all' => true, 'first' => true, 'count' => true, 'neighbors' => true, 'list' => true, 'threaded' => true), '_log' => null);
  1876. $result = Set::reverse($model);
  1877. ksort($result);
  1878. ksort($expected);
  1879. $this->assertIdentical($result, $expected);
  1880. $class = new stdClass;
  1881. $class->User = new stdClass;
  1882. $class->User->id = 100;
  1883. $class->someString = 'this is some string';
  1884. $class->Profile = new stdClass;
  1885. $class->Profile->name = 'Joe Mamma';
  1886. $result = Set::reverse($class);
  1887. $expected = array('User' => array('id' => '100'), 'someString'=> 'this is some string', 'Profile' => array('name' => 'Joe Mamma'));
  1888. $this->assertEqual($result, $expected);
  1889. $class = new stdClass;
  1890. $class->User = new stdClass;
  1891. $class->User->id = 100;
  1892. $class->User->_name_ = 'User';
  1893. $class->Profile = new stdClass;
  1894. $class->Profile->name = 'Joe Mamma';
  1895. $class->Profile->_name_ = 'Profile';
  1896. $result = Set::reverse($class);
  1897. $expected = array('User' => array('id' => '100'), 'Profile' => array('name' => 'Joe Mamma'));
  1898. $this->assertEqual($result, $expected);
  1899. }
  1900. /**
  1901. * testFormatting method
  1902. *
  1903. * @access public
  1904. * @return void
  1905. */
  1906. function testFormatting() {
  1907. $data = array(
  1908. array('Person' => array('first_name' => 'Nate', 'last_name' => 'Abele', 'city' => 'Boston', 'state' => 'MA', 'something' => '42')),
  1909. array('Person' => array('first_name' => 'Larry', 'last_name' => 'Masters', 'city' => 'Boondock', 'state' => 'TN', 'something' => '{0}')),
  1910. array('Person' => array('first_name' => 'Garrett', 'last_name' => 'Woodworth', 'city' => 'Venice Beach', 'state' => 'CA', 'something' => '{1}')));
  1911. $result = Set::format($data, '{1}, {0}', array('{n}.Person.first_name', '{n}.Person.last_name'));
  1912. $expected = array('Abele, Nate', 'Masters, Larry', 'Woodworth, Garrett');
  1913. $this->assertEqual($result, $expected);
  1914. $result = Set::format($data, '{0}, {1}', array('{n}.Person.last_name', '{n}.Person.first_name'));
  1915. $this->assertEqual($result, $expected);
  1916. $result = Set::format($data, '{0}, {1}', array('{n}.Person.city', '{n}.Person.state'));
  1917. $expected = array('Boston, MA', 'Boondock, TN', 'Venice Beach, CA');
  1918. $this->assertEqual($result, $expected);
  1919. $result = Set::format($data, '{{0}, {1}}', array('{n}.Person.city', '{n}.Person.state'));
  1920. $expected = array('{Boston, MA}', '{Boondock, TN}', '{Venice Beach, CA}');
  1921. $this->assertEqual($result, $expected);
  1922. $result = Set::format($data, '{{0}, {1}}', array('{n}.Person.something', '{n}.Person.something'));
  1923. $expected = array('{42, 42}', '{{0}, {0}}', '{{1}, {1}}');
  1924. $this->assertEqual($result, $expected);
  1925. $result = Set::format($data, '{%2$d, %1$s}', array('{n}.Person.something', '{n}.Person.something'));
  1926. $expected = array('{42, 42}', '{0, {0}}', '{0, {1}}');
  1927. $this->assertEqual($result, $expected);
  1928. $result = Set::format($data, '{%1$s, %1$s}', array('{n}.Person.something', '{n}.Person.something'));
  1929. $expected = array('{42, 42}', '{{0}, {0}}', '{{1}, {1}}');
  1930. $this->assertEqual($result, $expected);
  1931. $result = Set::format($data, '%2$d, %1$s', array('{n}.Person.first_name', '{n}.Person.something'));
  1932. $expected = array('42, Nate', '0, Larry', '0, Garrett');
  1933. $this->assertEqual($result, $expected);
  1934. $result = Set::format($data, '%1$s, %2$d', array('{n}.Person.first_name', '{n}.Person.something'));
  1935. $expected = array('Nate, 42', 'Larry, 0', 'Garrett, 0');
  1936. $this->assertEqual($result, $expected);
  1937. }
  1938. /**
  1939. * testCountDim method
  1940. *
  1941. * @access public
  1942. * @return void
  1943. */
  1944. function testCountDim() {
  1945. $data = array('one', '2', 'three');
  1946. $result = Set::countDim($data);
  1947. $this->assertEqual($result, 1);
  1948. $data = array('1' => '1.1', '2', '3');
  1949. $result = Set::countDim($data);
  1950. $this->assertEqual($result, 1);
  1951. $data = array('1' => array('1.1' => '1.1.1'), '2', '3' => array('3.1' => '3.1.1'));
  1952. $result = Set::countDim($data);
  1953. $this->assertEqual($result, 2);
  1954. $data = array('1' => '1.1', '2', '3' => array('3.1' => '3.1.1'));
  1955. $result = Set::countDim($data);
  1956. $this->assertEqual($result, 1);
  1957. $data = array('1' => '1.1', '2', '3' => array('3.1' => '3.1.1'));
  1958. $result = Set::countDim($data, true);
  1959. $this->assertEqual($result, 2);
  1960. $data = array('1' => array('1.1' => '1.1.1'), '2', '3' => array('3.1' => array('3.1.1' => '3.1.1.1')));
  1961. $result = Set::countDim($data);
  1962. $this->assertEqual($result, 2);
  1963. $data = array('1' => array('1.1' => '1.1.1'), '2', '3' => array('3.1' => array('3.1.1' => '3.1.1.1')));
  1964. $result = Set::countDim($data, true);
  1965. $this->assertEqual($result, 3);
  1966. $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')));
  1967. $result = Set::countDim($data, true);
  1968. $this->assertEqual($result, 4);
  1969. $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')));
  1970. $result = Set::countDim($data, true);
  1971. $this->assertEqual($result, 5);
  1972. $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')));
  1973. $result = Set::countDim($data, true);
  1974. $this->assertEqual($result, 5);
  1975. $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')));
  1976. $result = Set::countDim($set, false, 0);
  1977. $this->assertEqual($result, 2);
  1978. $result = Set::countDim($set, true);
  1979. $this->assertEqual($result, 5);
  1980. }
  1981. /**
  1982. * testMapNesting method
  1983. *
  1984. * @access public
  1985. * @return void
  1986. */
  1987. function testMapNesting() {
  1988. $expected = array(
  1989. array(
  1990. "IndexedPage" => array(
  1991. "id" => 1,
  1992. "url" => 'http://blah.com/',
  1993. 'hash' => '68a9f053b19526d08e36c6a9ad150737933816a5',
  1994. 'headers' => array(
  1995. 'Date' => "Wed, 14 Nov 2007 15:51:42 GMT",
  1996. 'Server' => "Apache",
  1997. 'Expires' => "Thu, 19 Nov 1981 08:52:00 GMT",
  1998. 'Cache-Control' => "private",
  1999. 'Pragma' => "no-cache",
  2000. 'Content-Type' => "text/html; charset=UTF-8",
  2001. 'X-Original-Transfer-Encoding' => "chunked",
  2002. 'Content-Length' => "50210",
  2003. ),
  2004. 'meta' => array(
  2005. 'keywords' => array('testing','tests'),
  2006. 'description'=>'describe me',
  2007. ),
  2008. 'get_vars' => '',
  2009. 'post_vars' => array(),
  2010. 'cookies' => array('PHPSESSID' => "dde9896ad24595998161ffaf9e0dbe2d"),
  2011. 'redirect' => '',
  2012. 'created' => "1195055503",
  2013. 'updated' => "1195055503",
  2014. )
  2015. ),
  2016. array(
  2017. "IndexedPage" => array(
  2018. "id" => 2,
  2019. "url" => 'http://blah.com/',
  2020. 'hash' => '68a9f053b19526d08e36c6a9ad150737933816a5',
  2021. 'headers' => array(
  2022. 'Date' => "Wed, 14 Nov 2007 15:51:42 GMT",
  2023. 'Server' => "Apache",
  2024. 'Expires' => "Thu, 19 Nov 1981 08:52:00 GMT",
  2025. 'Cache-Control' => "private",
  2026. 'Pragma' => "no-cache",
  2027. 'Content-Type' => "text/html; charset=UTF-8",
  2028. 'X-Original-Transfer-Encoding' => "chunked",
  2029. 'Content-Length' => "50210",
  2030. ),
  2031. 'meta' => array(
  2032. 'keywords' => array('testing','tests'),
  2033. 'description'=>'describe me',
  2034. ),
  2035. 'get_vars' => '',
  2036. 'post_vars' => array(),
  2037. 'cookies' => array('PHPSESSID' => "dde9896ad24595998161ffaf9e0dbe2d"),
  2038. 'redirect' => '',
  2039. 'created' => "1195055503",
  2040. 'updated' => "1195055503",
  2041. ),
  2042. )
  2043. );
  2044. $mapped = Set::map($expected);
  2045. $ids = array();
  2046. foreach($mapped as $object) {
  2047. $ids[] = $object->id;
  2048. }
  2049. $this->assertEqual($ids, array(1, 2));
  2050. $this->assertEqual(get_object_vars($mapped[0]->headers), $expected[0]['IndexedPage']['headers']);
  2051. $result = Set::reverse($mapped);
  2052. $this->assertIdentical($result, $expected);
  2053. $data = array(
  2054. array(
  2055. "IndexedPage" => array(
  2056. "id" => 1,
  2057. "url" => 'http://blah.com/',
  2058. 'hash' => '68a9f053b19526d08e36c6a9ad150737933816a5',
  2059. 'get_vars' => '',
  2060. 'redirect' => '',
  2061. 'created' => "1195055503",
  2062. 'updated' => "1195055503",
  2063. )
  2064. ),
  2065. array(
  2066. "IndexedPage" => array(
  2067. "id" => 2,
  2068. "url" => 'http://blah.com/',
  2069. 'hash' => '68a9f053b19526d08e36c6a9ad150737933816a5',
  2070. 'get_vars' => '',
  2071. 'redirect' => '',
  2072. 'created' => "1195055503",
  2073. 'updated' => "1195055503",
  2074. ),
  2075. )
  2076. );
  2077. $mapped = Set::map($data);
  2078. $expected = new stdClass();
  2079. $expected->_name_ = 'IndexedPage';
  2080. $expected->id = 2;
  2081. $expected->url = 'http://blah.com/';
  2082. $expected->hash = '68a9f053b19526d08e36c6a9ad150737933816a5';
  2083. $expected->get_vars = '';
  2084. $expected->redirect = '';
  2085. $expected->created = "1195055503";
  2086. $expected->updated = "1195055503";
  2087. $this->assertIdentical($mapped[1], $expected);
  2088. $ids = array();
  2089. foreach($mapped as $object) {
  2090. $ids[] = $object->id;
  2091. }
  2092. $this->assertEqual($ids, array(1, 2));
  2093. $result = Set::map(null);
  2094. $expected = null;
  2095. $this->assertEqual($result, $expected);
  2096. }
  2097. /**
  2098. * testNestedMappedData method
  2099. *
  2100. * @access public
  2101. * @return void
  2102. */
  2103. function testNestedMappedData() {
  2104. $result = Set::map(array(
  2105. array(
  2106. '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'),
  2107. 'Author' => array('id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31', 'test' => 'working'),
  2108. )
  2109. , array(
  2110. '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'),
  2111. 'Author' => array('id' => '3', 'user' => 'larry', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:20:23', 'updated' => '2007-03-17 01:22:31', 'test' => 'working'),
  2112. )
  2113. ));
  2114. $expected = new stdClass;
  2115. $expected->_name_ = 'Post';
  2116. $expected->id = '1';
  2117. $expected->author_id = '1';
  2118. $expected->title = 'First Post';
  2119. $expected->body = 'First Post Body';
  2120. $expected->published = 'Y';
  2121. $expected->created = "2007-03-18 10:39:23";
  2122. $expected->updated = "2007-03-18 10:41:31";
  2123. $expected->Author = new stdClass;
  2124. $expected->Author->id = '1';
  2125. $expected->Author->user = 'mariano';
  2126. $expected->Author->password = '5f4dcc3b5aa765d61d8327deb882cf99';
  2127. $expected->Author->created = "2007-03-17 01:16:23";
  2128. $expected->Author->updated = "2007-03-17 01:18:31";
  2129. $expected->Author->test = "working";
  2130. $expected->Author->_name_ = 'Author';
  2131. $expected2 = new stdClass;
  2132. $expected2->_name_ = 'Post';
  2133. $expected2->id = '2';
  2134. $expected2->author_id = '3';
  2135. $expected2->title = 'Second Post';
  2136. $expected2->body = 'Second Post Body';
  2137. $expected2->published = 'Y';
  2138. $expected2->created = "2007-03-18 10:41:23";
  2139. $expected2->updated = "2007-03-18 10:43:31";
  2140. $expected2->Author = new stdClass;
  2141. $expected2->Author->id = '3';
  2142. $expected2->Author->user = 'larry';
  2143. $expected2->Author->password = '5f4dcc3b5aa765d61d8327deb882cf99';
  2144. $expected2->Author->created = "2007-03-17 01:20:23";
  2145. $expected2->Author->updated = "2007-03-17 01:22:31";
  2146. $expected2->Author->test = "working";
  2147. $expected2->Author->_name_ = 'Author';
  2148. $test = array();
  2149. $test[0] = $expected;
  2150. $test[1] = $expected2;
  2151. $this->assertIdentical($test, $result);
  2152. $result = Set::map(
  2153. array(
  2154. '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'),
  2155. 'Author' => array('id' => '1', 'user' => 'mariano', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31', 'test' => 'working'),
  2156. )
  2157. );
  2158. $expected = new stdClass;
  2159. $expected->_name_ = 'Post';
  2160. $expected->id = '1';
  2161. $expected->author_id = '1';
  2162. $expected->title = 'First Post';
  2163. $expected->body = 'First Post Body';
  2164. $expected->published = 'Y';
  2165. $expected->created = "2007-03-18 10:39:23";
  2166. $expected->updated = "2007-03-18 10:41:31";
  2167. $expected->Author = new stdClass;
  2168. $expected->Author->id = '1';
  2169. $expected->Author->user = 'mariano';
  2170. $expected->Author->password = '5f4dcc3b5aa765d61d8327deb882cf99';
  2171. $expected->Author->created = "2007-03-17 01:16:23";
  2172. $expected->Author->updated = "2007-03-17 01:18:31";
  2173. $expected->Author->test = "working";
  2174. $expected->Author->_name_ = 'Author';
  2175. $this->assertIdentical($expected, $result);
  2176. //Case where extra HABTM fields come back in a result
  2177. $data = array(
  2178. 'User' => array(
  2179. 'id' => 1,
  2180. 'email' => 'user@example.com',
  2181. 'first_name' => 'John',
  2182. 'last_name' => 'Smith',
  2183. ),
  2184. 'Piece' => array(
  2185. array(
  2186. 'id' => 1,
  2187. 'title' => 'Moonlight Sonata',
  2188. 'composer' => 'Ludwig van Beethoven',
  2189. 'PiecesUser' => array(
  2190. 'id' => 1,
  2191. 'created' => '2008-01-01 00:00:00',
  2192. 'modified' => '2008-01-01 00:00:00',
  2193. 'piece_id' => 1,
  2194. 'user_id' => 2,
  2195. )
  2196. ),
  2197. array(
  2198. 'id' => 2,
  2199. 'title' => 'Moonlight Sonata 2',
  2200. 'composer' => 'Ludwig van Beethoven',
  2201. 'PiecesUser' => array(
  2202. 'id' => 2,
  2203. 'created' => '2008-01-01 00:00:00',
  2204. 'modified' => '2008-01-01 00:00:00',
  2205. 'piece_id' => 2,
  2206. 'user_id' => 2,
  2207. )
  2208. )
  2209. )
  2210. );
  2211. $result = Set::map($data);
  2212. $expected = new stdClass();
  2213. $expected->_name_ = 'User';
  2214. $expected->id = 1;
  2215. $expected->email = 'user@example.com';
  2216. $expected->first_name = 'John';
  2217. $expected->last_name = 'Smith';
  2218. $piece = new stdClass();
  2219. $piece->id = 1;
  2220. $piece->title = 'Moonlight Sonata';
  2221. $piece->composer = 'Ludwig van Beethoven';
  2222. $piece->PiecesUser = new stdClass();
  2223. $piece->PiecesUser->id = 1;
  2224. $piece->PiecesUser->created = '2008-01-01 00:00:00';
  2225. $piece->PiecesUser->modified = '2008-01-01 00:00:00';
  2226. $piece->PiecesUser->piece_id = 1;
  2227. $piece->PiecesUser->user_id = 2;
  2228. $piece->PiecesUser->_name_ = 'PiecesUser';
  2229. $piece->_name_ = 'Piece';
  2230. $piece2 = new stdClass();
  2231. $piece2->id = 2;
  2232. $piece2->title = 'Moonlight Sonata 2';
  2233. $piece2->composer = 'Ludwig van Beethoven';
  2234. $piece2->PiecesUser = new stdClass();
  2235. $piece2->PiecesUser->id = 2;
  2236. $piece2->PiecesUser->created = '2008-01-01 00:00:00';
  2237. $piece2->PiecesUser->modified = '2008-01-01 00:00:00';
  2238. $piece2->PiecesUser->piece_id = 2;
  2239. $piece2->PiecesUser->user_id = 2;
  2240. $piece2->PiecesUser->_name_ = 'PiecesUser';
  2241. $piece2->_name_ = 'Piece';
  2242. $expected->Piece = array($piece, $piece2);
  2243. $this->assertIdentical($expected, $result);
  2244. //Same data, but should work if _name_ has been manually defined:
  2245. $data = array(
  2246. 'User' => array(
  2247. 'id' => 1,
  2248. 'email' => 'user@example.com',
  2249. 'first_name' => 'John',
  2250. 'last_name' => 'Smith',
  2251. '_name_' => 'FooUser',
  2252. ),
  2253. 'Piece' => array(
  2254. array(
  2255. 'id' => 1,
  2256. 'title' => 'Moonlight Sonata',
  2257. 'composer' => 'Ludwig van Beethoven',
  2258. '_name_' => 'FooPiece',
  2259. 'PiecesUser' => array(
  2260. 'id' => 1,
  2261. 'created' => '2008-01-01 00:00:00',
  2262. 'modified' => '2008-01-01 00:00:00',
  2263. 'piece_id' => 1,
  2264. 'user_id' => 2,
  2265. '_name_' => 'FooPiecesUser',
  2266. )
  2267. ),
  2268. array(
  2269. 'id' => 2,
  2270. 'title' => 'Moonlight Sonata 2',
  2271. 'composer' => 'Ludwig van Beethoven',
  2272. '_name_' => 'FooPiece',
  2273. 'PiecesUser' => array(
  2274. 'id' => 2,
  2275. 'created' => '2008-01-01 00:00:00',
  2276. 'modified' => '2008-01-01 00:00:00',
  2277. 'piece_id' => 2,
  2278. 'user_id' => 2,
  2279. '_name_' => 'FooPiecesUser',
  2280. )
  2281. )
  2282. )
  2283. );
  2284. $result = Set::map($data);
  2285. $expected = new stdClass();
  2286. $expected->_name_ = 'FooUser';
  2287. $expected->id = 1;
  2288. $expected->email = 'user@example.com';
  2289. $expected->first_name = 'John';
  2290. $expected->last_name = 'Smith';
  2291. $piece = new stdClass();
  2292. $piece->id = 1;
  2293. $piece->title = 'Moonlight Sonata';
  2294. $piece->composer = 'Ludwig van Beethoven';
  2295. $piece->_name_ = 'FooPiece';
  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_ = 'FooPiecesUser';
  2303. $piece2 = new stdClass();
  2304. $piece2->id = 2;
  2305. $piece2->title = 'Moonlight Sonata 2';
  2306. $piece2->composer = 'Ludwig van Beethoven';
  2307. $piece2->_name_ = 'FooPiece';
  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_ = 'FooPiecesUser';
  2315. $expected->Piece = array($piece, $piece2);
  2316. $this->assertIdentical($expected, $result);
  2317. }
  2318. /**
  2319. * testPushDiff method
  2320. *
  2321. * @access public
  2322. * @return void
  2323. */
  2324. function testPushDiff() {
  2325. $array1 = array('ModelOne' => array('id'=>1001, 'field_one'=>'a1.m1.f1', 'field_two'=>'a1.m1.f2'));
  2326. $array2 = array('ModelTwo' => array('id'=>1002, 'field_one'=>'a2.m2.f1', 'field_two'=>'a2.m2.f2'));
  2327. $result = Set::pushDiff($array1, $array2);
  2328. $this->assertIdentical($result, $array1 + $array2);
  2329. $array3 = array('ModelOne' => array('id'=>1003, 'field_one'=>'a3.m1.f1', 'field_two'=>'a3.m1.f2', 'field_three'=>'a3.m1.f3'));
  2330. $result = Set::pushDiff($array1, $array3);
  2331. $expected = array('ModelOne' => array('id'=>1001, 'field_one'=>'a1.m1.f1', 'field_two'=>'a1.m1.f2', 'field_three'=>'a3.m1.f3'));
  2332. $this->assertIdentical($result, $expected);
  2333. $array1 = array(
  2334. 0 => array('ModelOne' => array('id'=>1001, 'field_one'=>'s1.0.m1.f1', 'field_two'=>'s1.0.m1.f2')),
  2335. 1 => array('ModelTwo' => array('id'=>1002, 'field_one'=>'s1.1.m2.f2', 'field_two'=>'s1.1.m2.f2')));
  2336. $array2 = array(
  2337. 0 => array('ModelOne' => array('id'=>1001, 'field_one'=>'s2.0.m1.f1', 'field_two'=>'s2.0.m1.f2')),
  2338. 1 => array('ModelTwo' => array('id'=>1002, 'field_one'=>'s2.1.m2.f2', 'field_two'=>'s2.1.m2.f2')));
  2339. $result = Set::pushDiff($array1, $array2);
  2340. $this->assertIdentical($result, $array1);
  2341. $array3 = array(0 => array('ModelThree' => array('id'=>1003, 'field_one'=>'s3.0.m3.f1', 'field_two'=>'s3.0.m3.f2')));
  2342. $result = Set::pushDiff($array1, $array3);
  2343. $expected = array(
  2344. 0 => array('ModelOne' => array('id'=>1001, 'field_one'=>'s1.0.m1.f1', 'field_two'=>'s1.0.m1.f2'),
  2345. 'ModelThree' => array('id'=>1003, 'field_one'=>'s3.0.m3.f1', 'field_two'=>'s3.0.m3.f2')),
  2346. 1 => array('ModelTwo' => array('id'=>1002, 'field_one'=>'s1.1.m2.f2', 'field_two'=>'s1.1.m2.f2')));
  2347. $this->assertIdentical($result, $expected);
  2348. $result = Set::pushDiff($array1, null);
  2349. $this->assertIdentical($result, $array1);
  2350. $result = Set::pushDiff($array1, $array2);
  2351. $this->assertIdentical($result, $array1+$array2);
  2352. }
  2353. /**
  2354. * testXmlSetReverse method
  2355. *
  2356. * @access public
  2357. * @return void
  2358. */
  2359. function testXmlSetReverse() {
  2360. App::import('Core', 'Xml');
  2361. $string = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  2362. <rss version="2.0">
  2363. <channel>
  2364. <title>Cake PHP Google Group</title>
  2365. <link>http://groups.google.com/group/cake-php</link>
  2366. <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>
  2367. <language>en</language>
  2368. <item>
  2369. <title>constructng result array when using findall</title>
  2370. <link>http://groups.google.com/group/cake-php/msg/49bc00f3bc651b4f</link>
  2371. <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>
  2372. <guid isPermaLink="true">http://groups.google.com/group/cake-php/msg/49bc00f3bc651b4f</guid>
  2373. <author>bmil...@gmail.com(bpscrugs)</author>
  2374. <pubDate>Fri, 28 Dec 2007 00:44:14 UT</pubDate>
  2375. </item>
  2376. <item>
  2377. <title>Re: share views between actions?</title>
  2378. <link>http://groups.google.com/group/cake-php/msg/8b350d898707dad8</link>
  2379. <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>
  2380. <guid isPermaLink="true">http://groups.google.com/group/cake-php/msg/8b350d898707dad8</guid>
  2381. <author>subtropolis.z...@gmail.com(subtropolis zijn)</author>
  2382. <pubDate>Fri, 28 Dec 2007 00:45:01 UT</pubDate>
  2383. </item>
  2384. </channel>
  2385. </rss>';
  2386. $xml = new Xml($string);
  2387. $result = Set::reverse($xml);
  2388. $expected = array('Rss' => array(
  2389. 'version' => '2.0',
  2390. 'Channel' => array(
  2391. 'title' => 'Cake PHP Google Group',
  2392. 'link' => 'http://groups.google.com/group/cake-php',
  2393. '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.',
  2394. 'language' => 'en',
  2395. 'Item' => array(
  2396. array(
  2397. 'title' => 'constructng result array when using findall',
  2398. 'link' => 'http://groups.google.com/group/cake-php/msg/49bc00f3bc651b4f',
  2399. '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(",
  2400. 'guid' => array('isPermaLink' => 'true', 'value' => 'http://groups.google.com/group/cake-php/msg/49bc00f3bc651b4f'),
  2401. 'author' => 'bmil...@gmail.com(bpscrugs)',
  2402. 'pubDate' => 'Fri, 28 Dec 2007 00:44:14 UT',
  2403. ),
  2404. array(
  2405. 'title' => 'Re: share views between actions?',
  2406. 'link' => 'http://groups.google.com/group/cake-php/msg/8b350d898707dad8',
  2407. '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',
  2408. 'guid' => array('isPermaLink' => 'true', 'value' => 'http://groups.google.com/group/cake-php/msg/8b350d898707dad8'),
  2409. 'author' => 'subtropolis.z...@gmail.com(subtropolis zijn)',
  2410. 'pubDate' => 'Fri, 28 Dec 2007 00:45:01 UT'
  2411. )
  2412. )
  2413. )
  2414. ));
  2415. $this->assertEqual($result, $expected);
  2416. $string ='<data><post title="Title of this post" description="cool"/></data>';
  2417. $xml = new Xml($string);
  2418. $result = Set::reverse($xml);
  2419. $expected = array('Data' => array('Post' => array('title' => 'Title of this post', 'description' => 'cool')));
  2420. $this->assertEqual($result, $expected);
  2421. $xml = new Xml('<example><item><title>An example of a correctly reversed XMLNode</title><desc/></item></example>');
  2422. $result = Set::reverse($xml);
  2423. $expected = array('Example' =>
  2424. array(
  2425. 'Item' => array(
  2426. 'title' => 'An example of a correctly reversed XMLNode',
  2427. 'desc' => array(),
  2428. )
  2429. )
  2430. );
  2431. $this->assertIdentical($result, $expected);
  2432. $xml = new Xml('<example><item attr="123"><titles><title>title1</title><title>title2</title></titles></item></example>');
  2433. $result = Set::reverse($xml);
  2434. $expected =
  2435. array('Example' => array(
  2436. 'Item' => array(
  2437. 'attr' => '123',
  2438. 'Titles' => array(
  2439. 'Title' => array('title1', 'title2')
  2440. )
  2441. )
  2442. )
  2443. );
  2444. $this->assertIdentical($result, $expected);
  2445. $xml = new Xml('<example attr="ex_attr"><item attr="123"><titles>list</titles>textforitems</item></example>');
  2446. $result = Set::reverse($xml);
  2447. $expected =
  2448. array('Example' => array(
  2449. 'attr' => 'ex_attr',
  2450. 'Item' => array(
  2451. 'attr' => '123',
  2452. 'titles' => 'list',
  2453. 'value' => 'textforitems'
  2454. )
  2455. )
  2456. );
  2457. $this->assertIdentical($result, $expected);
  2458. $string = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  2459. <rss version="2.0">
  2460. <channel>
  2461. <title>Cake PHP Google Group</title>
  2462. <link>http://groups.google.com/group/cake-php</link>
  2463. <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>
  2464. <language>en</language>
  2465. <item>
  2466. <title>constructng result array when using findall</title>
  2467. <link>http://groups.google.com/group/cake-php/msg/49bc00f3bc651b4f</link>
  2468. <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>
  2469. <dc:creator>cakephp</dc:creator>
  2470. <category><![CDATA[cakephp]]></category>
  2471. <category><![CDATA[model]]></category>
  2472. <guid isPermaLink="true">http://groups.google.com/group/cake-php/msg/49bc00f3bc651b4f</guid>
  2473. <author>bmil...@gmail.com(bpscrugs)</author>
  2474. <pubDate>Fri, 28 Dec 2007 00:44:14 UT</pubDate>
  2475. </item>
  2476. <item>
  2477. <title>Re: share views between actions?</title>
  2478. <link>http://groups.google.com/group/cake-php/msg/8b350d898707dad8</link>
  2479. <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>
  2480. <dc:creator>cakephp</dc:creator>
  2481. <category><![CDATA[cakephp]]></category>
  2482. <category><![CDATA[model]]></category>
  2483. <guid isPermaLink="true">http://groups.google.com/group/cake-php/msg/8b350d898707dad8</guid>
  2484. <author>subtropolis.z...@gmail.com(subtropolis zijn)</author>
  2485. <pubDate>Fri, 28 Dec 2007 00:45:01 UT</pubDate>
  2486. </item>
  2487. </channel>
  2488. </rss>';
  2489. $xml = new Xml($string);
  2490. $result = Set::reverse($xml);
  2491. $expected = array('Rss' => array(
  2492. 'version' => '2.0',
  2493. 'Channel' => array(
  2494. 'title' => 'Cake PHP Google Group',
  2495. 'link' => 'http://groups.google.com/group/cake-php',
  2496. '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.',
  2497. 'language' => 'en',
  2498. 'Item' => array(
  2499. array(
  2500. 'title' => 'constructng result array when using findall',
  2501. 'link' => 'http://groups.google.com/group/cake-php/msg/49bc00f3bc651b4f',
  2502. '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(",
  2503. 'creator' => 'cakephp',
  2504. 'Category' => array('cakephp', 'model'),
  2505. 'guid' => array('isPermaLink' => 'true', 'value' => 'http://groups.google.com/group/cake-php/msg/49bc00f3bc651b4f'),
  2506. 'author' => 'bmil...@gmail.com(bpscrugs)',
  2507. 'pubDate' => 'Fri, 28 Dec 2007 00:44:14 UT',
  2508. ),
  2509. array(
  2510. 'title' => 'Re: share views between actions?',
  2511. 'link' => 'http://groups.google.com/group/cake-php/msg/8b350d898707dad8',
  2512. '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',
  2513. 'creator' => 'cakephp',
  2514. 'Category' => array('cakephp', 'model'),
  2515. 'guid' => array('isPermaLink' => 'true', 'value' => 'http://groups.google.com/group/cake-php/msg/8b350d898707dad8'),
  2516. 'author' => 'subtropolis.z...@gmail.com(subtropolis zijn)',
  2517. 'pubDate' => 'Fri, 28 Dec 2007 00:45:01 UT'
  2518. )
  2519. )
  2520. )
  2521. ));
  2522. $this->assertEqual($result, $expected);
  2523. $text = '<?xml version="1.0" encoding="UTF-8"?>
  2524. <XRDS xmlns="xri://$xrds">
  2525. <XRD xml:id="oauth" xmlns="xri://$XRD*($v*2.0)" version="2.0">
  2526. <Type>xri://$xrds*simple</Type>
  2527. <Expires>2008-04-13T07:34:58Z</Expires>
  2528. <Service>
  2529. <Type>http://oauth.net/core/1.0/endpoint/authorize</Type>
  2530. <Type>http://oauth.net/core/1.0/parameters/auth-header</Type>
  2531. <Type>http://oauth.net/core/1.0/parameters/uri-query</Type>
  2532. <URI priority="10">https://ma.gnolia.com/oauth/authorize</URI>
  2533. <URI priority="20">http://ma.gnolia.com/oauth/authorize</URI>
  2534. </Service>
  2535. </XRD>
  2536. <XRD xmlns="xri://$XRD*($v*2.0)" version="2.0">
  2537. <Type>xri://$xrds*simple</Type>
  2538. <Service priority="10">
  2539. <Type>http://oauth.net/discovery/1.0</Type>
  2540. <URI>#oauth</URI>
  2541. </Service>
  2542. </XRD>
  2543. </XRDS>';
  2544. $xml = new Xml($text);
  2545. $result = Set::reverse($xml);
  2546. $expected = array('XRDS' => array(
  2547. 'xmlns' => 'xri://$xrds',
  2548. 'XRD' => array(
  2549. array(
  2550. 'xml:id' => 'oauth',
  2551. 'xmlns' => 'xri://$XRD*($v*2.0)',
  2552. 'version' => '2.0',
  2553. 'Type' => 'xri://$xrds*simple',
  2554. 'Expires' => '2008-04-13T07:34:58Z',
  2555. 'Service' => array(
  2556. 'Type' => array(
  2557. 'http://oauth.net/core/1.0/endpoint/authorize',
  2558. 'http://oauth.net/core/1.0/parameters/auth-header',
  2559. 'http://oauth.net/core/1.0/parameters/uri-query'
  2560. ),
  2561. 'URI' => array(
  2562. array(
  2563. 'value' => 'https://ma.gnolia.com/oauth/authorize',
  2564. 'priority' => '10',
  2565. ),
  2566. array(
  2567. 'value' => 'http://ma.gnolia.com/oauth/authorize',
  2568. 'priority' => '20'
  2569. )
  2570. )
  2571. )
  2572. ),
  2573. array(
  2574. 'xmlns' => 'xri://$XRD*($v*2.0)',
  2575. 'version' => '2.0',
  2576. 'Type' => 'xri://$xrds*simple',
  2577. 'Service' => array(
  2578. 'priority' => '10',
  2579. 'Type' => 'http://oauth.net/discovery/1.0',
  2580. 'URI' => '#oauth'
  2581. )
  2582. )
  2583. )
  2584. ));
  2585. $this->assertEqual($result, $expected);
  2586. }
  2587. /**
  2588. * testStrictKeyCheck method
  2589. *
  2590. * @access public
  2591. * @return void
  2592. */
  2593. function testStrictKeyCheck() {
  2594. $set = array('a' => 'hi');
  2595. $this->assertFalse(Set::check($set, 'a.b'));
  2596. }
  2597. /**
  2598. * Tests Set::flatten
  2599. *
  2600. * @access public
  2601. * @return void
  2602. */
  2603. function testFlatten() {
  2604. $data = array('Larry', 'Curly', 'Moe');
  2605. $result = Set::flatten($data);
  2606. $this->assertEqual($result, $data);
  2607. $data[9] = 'Shemp';
  2608. $result = Set::flatten($data);
  2609. $this->assertEqual($result, $data);
  2610. $data = array(
  2611. array(
  2612. 'Post' => array('id' => '1', 'author_id' => '1', 'title' => 'First Post'),
  2613. 'Author' => array('id' => '1', 'user' => 'nate', 'password' => 'foo'),
  2614. ),
  2615. array(
  2616. 'Post' => array('id' => '2', 'author_id' => '3', 'title' => 'Second Post', 'body' => 'Second Post Body'),
  2617. 'Author' => array('id' => '3', 'user' => 'larry', 'password' => null),
  2618. )
  2619. );
  2620. $result = Set::flatten($data);
  2621. $expected = array(
  2622. '0.Post.id' => '1', '0.Post.author_id' => '1', '0.Post.title' => 'First Post', '0.Author.id' => '1',
  2623. '0.Author.user' => 'nate', '0.Author.password' => 'foo', '1.Post.id' => '2', '1.Post.author_id' => '3',
  2624. '1.Post.title' => 'Second Post', '1.Post.body' => 'Second Post Body', '1.Author.id' => '3',
  2625. '1.Author.user' => 'larry', '1.Author.password' => null
  2626. );
  2627. $this->assertEqual($result, $expected);
  2628. }
  2629. }
  2630. ?>