PageRenderTime 58ms CodeModel.GetById 21ms 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

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

  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);

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