SetTest.php 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495
  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2013, Union of RAD (http://union-of-rad.org)
  6. * @license http://opensource.org/licenses/bsd-license.php The BSD License
  7. */
  8. namespace lithium\tests\cases\util;
  9. use stdClass;
  10. use lithium\util\Set;
  11. class SetTest extends \lithium\test\Unit {
  12. public function testDepthWithEmptyData() {
  13. $data = array();
  14. $result = Set::depth($data);
  15. $this->assertEqual(0, $result);
  16. }
  17. public function testDepthOneLevelWithDefaults() {
  18. $data = array();
  19. $result = Set::depth($data);
  20. $this->assertEqual(0, $result);
  21. $data = array('one', '2', 'three');
  22. $result = Set::depth($data);
  23. $this->assertEqual(1, $result);
  24. $data = array('1' => '1.1', '2', '3');
  25. $result = Set::depth($data);
  26. $this->assertEqual(1, $result);
  27. $data = array('1' => '1.1', '2', '3' => array('3.1' => '3.1.1'));
  28. $result = Set::depth($data, array('all' => false));
  29. $this->assertEqual(1, $result);
  30. }
  31. public function testDepthTwoLevelsWithDefaults() {
  32. $data = array('1' => array('1.1' => '1.1.1'), '2', '3' => array('3.1' => '3.1.1'));
  33. $result = Set::depth($data);
  34. $this->assertEqual(2, $result);
  35. $data = array('1' => array('1.1' => '1.1.1'), '2', '3' => array('3.1' => array(
  36. '3.1.1' => '3.1.1.1'
  37. )));
  38. $result = Set::depth($data);
  39. $this->assertEqual($result, 2);
  40. $data = array(
  41. '1' => array('1.1' => '1.1.1'),
  42. array('2' => array(
  43. '2.1' => array('2.1.1' => array('2.1.1.1' => '2.1.1.1.1'))
  44. )),
  45. '3' => array('3.1' => array('3.1.1' => '3.1.1.1'))
  46. );
  47. $result = Set::depth($data, array('all' => false));
  48. $this->assertEqual($result, 2);
  49. }
  50. public function testDepthTwoLevelsWithAll() {
  51. $data = array('1' => '1.1', '2', '3' => array('3.1' => '3.1.1'));
  52. $result = Set::depth($data, array('all' => true));
  53. $this->assertEqual(2, $result);
  54. }
  55. public function testDepthThreeLevelsWithAll() {
  56. $data = array(
  57. '1' => array('1.1' => '1.1.1'), '2', '3' => array('3.1' => array('3.1.1' => '3.1.1.1'))
  58. );
  59. $result = Set::depth($data, array('all' => true));
  60. $this->assertEqual(3, $result);
  61. $data = array(
  62. '1' => array('1.1' => '1.1.1'),
  63. array('2' => array('2.1' => array('2.1.1' => '2.1.1.1'))),
  64. '3' => array('3.1' => array('3.1.1' => '3.1.1.1'))
  65. );
  66. $result = Set::depth($data, array('all' => true));
  67. $this->assertEqual(4, $result);
  68. $data = array(
  69. '1' => array('1.1' => '1.1.1'),
  70. array('2' => array('2.1' => array('2.1.1' => array('2.1.1.1')))),
  71. '3' => array('3.1' => array('3.1.1' => '3.1.1.1'))
  72. );
  73. $result = Set::depth($data, array('all' => true));
  74. $this->assertEqual(5, $result);
  75. $data = array(
  76. '1' => array('1.1' => '1.1.1'), array(
  77. '2' => array('2.1' => array('2.1.1' => array('2.1.1.1' => '2.1.1.1.1')))),
  78. '3' => array('3.1' => array('3.1.1' => '3.1.1.1'))
  79. );
  80. $result = Set::depth($data, array('all' => true));
  81. $this->assertEqual(5, $result);
  82. }
  83. public function testDepthFourLevelsWithAll() {
  84. $data = array(
  85. '1' => array('1.1' => '1.1.1'), array(
  86. '2' => array('2.1' => array('2.1.1' => '2.1.1.1'))),
  87. '3' => array('3.1' => array('3.1.1' => '3.1.1.1'))
  88. );
  89. $result = Set::depth($data, array('all' => true));
  90. $this->assertEqual(4, $result);
  91. }
  92. public function testDepthFiveLevelsWithAll() {
  93. $data = array(
  94. '1' => array('1.1' => '1.1.1'), array(
  95. '2' => array('2.1' => array('2.1.1' => array('2.1.1.1')))),
  96. '3' => array('3.1' => array('3.1.1' => '3.1.1.1'))
  97. );
  98. $result = Set::depth($data, array('all' => true));
  99. $this->assertEqual(5, $result);
  100. $data = array('1' => array('1.1' => '1.1.1'), array(
  101. '2' => array('2.1' => array('2.1.1' => array('2.1.1.1' => '2.1.1.1.1')))),
  102. '3' => array('3.1' => array('3.1.1' => '3.1.1.1'))
  103. );
  104. $result = Set::depth($data, array('all' => true));
  105. $this->assertEqual(5, $result);
  106. }
  107. public function testFlattenOneLevel() {
  108. $data = array('Larry', 'Curly', 'Moe');
  109. $result = Set::flatten($data);
  110. $this->assertEqual($result, $data);
  111. $data[9] = 'Shemp';
  112. $result = Set::flatten($data);
  113. $this->assertEqual($result, $data);
  114. }
  115. public function testFlattenTwoLevels() {
  116. $data = array(
  117. array(
  118. 'Post' => array('id' => '1', 'author_id' => '1', 'title' => 'First Post'),
  119. 'Author' => array('id' => '1', 'user' => 'nate', 'password' => 'foo')
  120. ),
  121. array(
  122. 'Post' => array(
  123. 'id' => '2',
  124. 'author_id' => '3',
  125. 'title' => 'Second Post',
  126. 'body' => 'Second Post Body'
  127. ),
  128. 'Author' => array('id' => '3', 'user' => 'joel', 'password' => null)
  129. )
  130. );
  131. $expected = array(
  132. '0.Post.id' => '1', '0.Post.author_id' => '1', '0.Post.title' => 'First Post',
  133. '0.Author.id' => '1', '0.Author.user' => 'nate', '0.Author.password' => 'foo',
  134. '1.Post.id' => '2', '1.Post.author_id' => '3', '1.Post.title' => 'Second Post',
  135. '1.Post.body' => 'Second Post Body', '1.Author.id' => '3',
  136. '1.Author.user' => 'joel', '1.Author.password' => null
  137. );
  138. $result = Set::flatten($data);
  139. $this->assertEqual($expected, $result);
  140. $result = Set::expand($result);
  141. $this->assertEqual($data, $result);
  142. $result = Set::flatten($data[0], array('separator' => '/'));
  143. $expected = array(
  144. 'Post/id' => '1', 'Post/author_id' => '1', 'Post/title' => 'First Post',
  145. 'Author/id' => '1', 'Author/user' => 'nate', 'Author/password' => 'foo'
  146. );
  147. $this->assertEqual($expected, $result);
  148. $result = Set::expand($expected, array('separator' => '/'));
  149. $this->assertEqual($data[0], $result);
  150. }
  151. public function testExpand() {
  152. $data = array(
  153. 'Gallery.Image' => null,
  154. 'Gallery.Image.Tag' => null,
  155. 'Gallery.Image.Tag.Author' => null
  156. );
  157. $expected = array('Gallery' => array('Image' => array('Tag' => array('Author' => null))));
  158. $this->assertEqual($expected, Set::expand($data));
  159. $data = array(
  160. 'Gallery.Image.Tag' => null,
  161. 'Gallery.Image' => null,
  162. 'Gallery.Image.Tag.Author' => null
  163. );
  164. $expected = array('Gallery' => array('Image' => array('Tag' => array('Author' => null))));
  165. $this->assertEqual($expected, Set::expand($data));
  166. $data = array(
  167. 'Gallery.Image.Tag.Author' => null,
  168. 'Gallery.Image.Tag' => null,
  169. 'Gallery.Image' => null
  170. );
  171. $expected = array('Gallery' => array('Image' => array('Tag' => array('Author' => null))));
  172. $this->assertEqual($expected, Set::expand($data));
  173. }
  174. public function testFormat() {
  175. $data = array(
  176. array('Person' => array(
  177. 'first_name' => 'Nate', 'last_name' => 'Abele',
  178. 'city' => 'Queens', 'state' => 'NY', 'something' => '42'
  179. )),
  180. array('Person' => array(
  181. 'first_name' => 'Joel', 'last_name' => 'Perras',
  182. 'city' => 'Montreal', 'state' => 'Quebec', 'something' => '{0}'
  183. )),
  184. array('Person' => array(
  185. 'first_name' => 'Garrett', 'last_name' => 'Woodworth',
  186. 'city' => 'Venice Beach', 'state' => 'CA', 'something' => '{1}'
  187. ))
  188. );
  189. $result = Set::format($data, '{1}, {0}', array('/Person/first_name', '/Person/last_name'));
  190. $expected = array('Abele, Nate', 'Perras, Joel', 'Woodworth, Garrett');
  191. $this->assertEqual($expected, $result);
  192. $result = Set::format($data, '{0}, {1}', array('/Person/last_name', '/Person/first_name'));
  193. $this->assertEqual($expected, $result);
  194. $result = Set::format($data, '{0}, {1}', array('/Person/city', '/Person/state'));
  195. $expected = array('Queens, NY', 'Montreal, Quebec', 'Venice Beach, CA');
  196. $this->assertEqual($expected, $result);
  197. $result = Set::format($data, '{{0}, {1}}', array('/Person/city', '/Person/state'));
  198. $expected = array('{Queens, NY}', '{Montreal, Quebec}', '{Venice Beach, CA}');
  199. $this->assertEqual($expected, $result);
  200. $result = Set::format($data, '{{0}, {1}}', array(
  201. '/Person/something', '/Person/something'
  202. ));
  203. $expected = array('{42, 42}', '{{0}, {0}}', '{{1}, {1}}');
  204. $this->assertEqual($expected, $result);
  205. $result = Set::format($data, '{%2$d, %1$s}', array(
  206. '/Person/something', '/Person/something'
  207. ));
  208. $expected = array('{42, 42}', '{0, {0}}', '{0, {1}}');
  209. $this->assertEqual($expected, $result);
  210. $result = Set::format($data, '{%1$s, %1$s}', array(
  211. '/Person/something', '/Person/something'
  212. ));
  213. $expected = array('{42, 42}', '{{0}, {0}}', '{{1}, {1}}');
  214. $this->assertEqual($expected, $result);
  215. $result = Set::format($data, '%2$d, %1$s', array(
  216. '/Person/first_name', '/Person/something'
  217. ));
  218. $expected = array('42, Nate', '0, Joel', '0, Garrett');
  219. $this->assertEqual($expected, $result);
  220. $result = Set::format($data, '%1$s, %2$d', array(
  221. '/Person/first_name', '/Person/something'
  222. ));
  223. $expected = array('Nate, 42', 'Joel, 0', 'Garrett, 0');
  224. $this->assertEqual($expected, $result);
  225. }
  226. public function testMatchesBasic() {
  227. $a = array(
  228. array('Article' => array('id' => 1, 'title' => 'Article 1')),
  229. array('Article' => array('id' => 2, 'title' => 'Article 2')),
  230. array('Article' => array('id' => 3, 'title' => 'Article 3'))
  231. );
  232. $this->assertTrue(Set::matches($a[1]['Article'], array('id=2')));
  233. $this->assertFalse(Set::matches($a[1]['Article'], array('id>2')));
  234. $this->assertTrue(Set::matches($a[1]['Article'], array('id>=2')));
  235. $this->assertFalse(Set::matches($a[1]['Article'], array('id>=3')));
  236. $this->assertTrue(Set::matches($a[1]['Article'], array('id<=2')));
  237. $this->assertFalse(Set::matches($a[1]['Article'], array('id<2')));
  238. $this->assertTrue(Set::matches($a[1]['Article'], array('id>1')));
  239. $this->assertTrue(Set::matches($a[1]['Article'], array('id>1', 'id<3', 'id!=0')));
  240. $this->assertTrue(Set::matches(array(), array('3'), 3));
  241. $this->assertTrue(Set::matches(array(), array('5'), 5));
  242. $this->assertTrue(Set::matches($a[1]['Article'], array('id')));
  243. $this->assertTrue(Set::matches($a[1]['Article'], array('id', 'title')));
  244. $this->assertFalse(Set::matches($a[1]['Article'], array('non-existant')));
  245. $this->assertTrue(Set::matches($a, '/Article[id=2]'));
  246. $this->assertFalse(Set::matches($a, '/Article[id=4]'));
  247. $this->assertTrue(Set::matches($a, array()));
  248. }
  249. public function testMatchesMultipleLevels() {
  250. $result = array(
  251. 'Attachment' => array(
  252. 'keep' => array()
  253. ),
  254. 'Comment' => array(
  255. 'keep' => array('Attachment' => array('fields' => array('attachment')))
  256. ),
  257. 'User' => array('keep' => array()),
  258. 'Article' => array(
  259. 'keep' => array(
  260. 'Comment' => array('fields' => array('comment', 'published')),
  261. 'User' => array('fields' => array('user'))
  262. )
  263. )
  264. );
  265. $this->assertTrue(Set::matches($result, '/Article/keep/Comment'));
  266. $result = Set::matches($result, '/Article/keep/Comment/fields/user');
  267. $this->assertFalse($result);
  268. }
  269. public function testExtractReturnsEmptyArray() {
  270. $expected = array();
  271. $result = Set::extract(array(), '/Post/id');
  272. $this->assertIdentical($expected, $result);
  273. $result = Set::extract(array(
  274. array('Post' => array('name' => 'bob')),
  275. array('Post' => array('name' => 'jim'))
  276. ), '/Post/id');
  277. $this->assertIdentical($expected, $result);
  278. $result = Set::extract(array(), 'Message.flash');
  279. $this->assertIdentical($expected, $result);
  280. }
  281. public function testExtractionOfNotNull() {
  282. $data = array(
  283. 'plugin' => null, 'admin' => false, 'controller' => 'posts',
  284. 'action' => 'index', 1, 'whatever'
  285. );
  286. $expected = array('controller' => 'posts', 'action' => 'index', 1, 'whatever');
  287. $result = Set::extract($data, '/');
  288. $this->assertIdentical($expected, $result);
  289. }
  290. public function testExtractOfNumericKeys() {
  291. $data = array(1, 'whatever');
  292. $expected = array(1, 'whatever');
  293. $result = Set::extract($data, '/');
  294. $this->assertIdentical($expected, $result);
  295. }
  296. public function testExtract() {
  297. $a = array(
  298. array(
  299. 'Article' => array(
  300. 'id' => '1', 'user_id' => '1', 'title' => 'First Article',
  301. 'body' => 'First Article Body', 'published' => 'Y',
  302. 'created' => '2007-03-18 10:39:23', 'updated' => '2007-03-18 10:41:31'
  303. ),
  304. 'User' => array(
  305. 'id' => '1', 'user' => 'mariano',
  306. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  307. 'created' => '2007-03-17 01:16:23',
  308. 'updated' => '2007-03-17 01:18:31'
  309. ),
  310. 'Comment' => array(
  311. array(
  312. 'id' => '1', 'article_id' => '1', 'user_id' => '2',
  313. 'comment' => 'First Comment for First Article',
  314. 'published' => 'Y', 'created' => '2007-03-18 10:45:23',
  315. 'updated' => '2007-03-18 10:47:31'
  316. ),
  317. array(
  318. 'id' => '2', 'article_id' => '1', 'user_id' => '4',
  319. 'comment' => 'Second Comment for First Article', 'published' => 'Y',
  320. 'created' => '2007-03-18 10:47:23',
  321. 'updated' => '2007-03-18 10:49:31'
  322. )
  323. ),
  324. 'Tag' => array(
  325. array(
  326. 'id' => '1', 'tag' => 'tag1', 'created' => '2007-03-18 12:22:23',
  327. 'updated' => '2007-03-18 12:24:31'
  328. ),
  329. array(
  330. 'id' => '2', 'tag' => 'tag2', 'created' => '2007-03-18 12:24:23',
  331. 'updated' => '2007-03-18 12:26:31'
  332. )
  333. ),
  334. 'Deep' => array(
  335. 'Nesting' => array(
  336. 'test' => array(1 => 'foo', 2 => array('and' => array('more' => 'stuff')))
  337. )
  338. )
  339. ),
  340. array(
  341. 'Article' => array(
  342. 'id' => '3', 'user_id' => '1', 'title' => 'Third Article',
  343. 'body' => 'Third Article Body', 'published' => 'Y',
  344. 'created' => '2007-03-18 10:43:23', 'updated' => '2007-03-18 10:45:31'
  345. ),
  346. 'User' => array(
  347. 'id' => '2', 'user' => 'mariano',
  348. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  349. 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
  350. ),
  351. 'Comment' => array(),
  352. 'Tag' => array()
  353. ),
  354. array(
  355. 'Article' => array(
  356. 'id' => '3', 'user_id' => '1', 'title' => 'Third Article',
  357. 'body' => 'Third Article Body', 'published' => 'Y',
  358. 'created' => '2007-03-18 10:43:23', 'updated' => '2007-03-18 10:45:31'
  359. ),
  360. 'User' => array(
  361. 'id' => '3', 'user' => 'mariano',
  362. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  363. 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
  364. ),
  365. 'Comment' => array(),
  366. 'Tag' => array()
  367. ),
  368. array(
  369. 'Article' => array(
  370. 'id' => '3', 'user_id' => '1', 'title' => 'Third Article',
  371. 'body' => 'Third Article Body', 'published' => 'Y',
  372. 'created' => '2007-03-18 10:43:23', 'updated' => '2007-03-18 10:45:31'
  373. ),
  374. 'User' => array(
  375. 'id' => '4', 'user' => 'mariano',
  376. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  377. 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
  378. ),
  379. 'Comment' => array(),
  380. 'Tag' => array()
  381. ),
  382. array(
  383. 'Article' => array(
  384. 'id' => '3', 'user_id' => '1', 'title' => 'Third Article',
  385. 'body' => 'Third Article Body', 'published' => 'Y',
  386. 'created' => '2007-03-18 10:43:23', 'updated' => '2007-03-18 10:45:31'
  387. ),
  388. 'User' => array(
  389. 'id' => '5', 'user' => 'mariano',
  390. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  391. 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
  392. ),
  393. 'Comment' => array(),
  394. 'Tag' => array()
  395. )
  396. );
  397. $b = array('Deep' => $a[0]['Deep']);
  398. $c = array(
  399. array('a' => array('I' => array('a' => 1))),
  400. array('a' => array(2)),
  401. array('a' => array('II' => array('a' => 3, 'III' => array('a' => array('foo' => 4)))))
  402. );
  403. $expected = array(array('a' => $c[2]['a']));
  404. $result = Set::extract($c, '/a/II[a=3]/..');
  405. $this->assertEqual($expected, $result);
  406. $expected = array(1, 2, 3, 4, 5);
  407. $result = Set::extract($a, '/User/id');
  408. $this->assertEqual($expected, $result);
  409. $expected = array(1, 2, 3, 4, 5);
  410. $result = Set::extract($a, '/User/id');
  411. $this->assertEqual($expected, $result);
  412. $expected = array(
  413. array('id' => 1), array('id' => 2), array('id' => 3), array('id' => 4), array('id' => 5)
  414. );
  415. $result = Set::extract($a, '/User/id', array('flatten' => false));
  416. $this->assertEqual($expected, $result);
  417. $expected = array(array('test' => $a[0]['Deep']['Nesting']['test']));
  418. $this->assertEqual(Set::extract($a, '/Deep/Nesting/test'), $expected);
  419. $this->assertEqual(Set::extract($b, '/Deep/Nesting/test'), $expected);
  420. $expected = array(array('test' => $a[0]['Deep']['Nesting']['test']));
  421. $result = Set::extract($a, '/Deep/Nesting/test/1/..');
  422. $this->assertEqual($expected, $result);
  423. $expected = array(array('test' => $a[0]['Deep']['Nesting']['test']));
  424. $result = Set::extract($a, '/Deep/Nesting/test/2/and/../..');
  425. $this->assertEqual($expected, $result);
  426. $expected = array(array('test' => $a[0]['Deep']['Nesting']['test']));
  427. $result = Set::extract($a, '/Deep/Nesting/test/2/../../../Nesting/test/2/..');
  428. $this->assertEqual($expected, $result);
  429. $expected = array(2);
  430. $result = Set::extract($a, '/User[2]/id');
  431. $this->assertEqual($expected, $result);
  432. $expected = array(4, 5);
  433. $result = Set::extract($a, '/User[id>3]/id');
  434. $this->assertEqual($expected, $result);
  435. $expected = array(2, 3);
  436. $result = Set::extract($a, '/User[id>1][id<=3]/id');
  437. $this->assertEqual($expected, $result);
  438. $expected = array(array('I'), array('II'));
  439. $result = Set::extract($c, '/a/@*');
  440. $this->assertEqual($expected, $result);
  441. }
  442. public function testExtractWithNonSequentialKeys() {
  443. $nonSequential = array(
  444. 'User' => array(
  445. 0 => array('id' => 1),
  446. 2 => array('id' => 2),
  447. 6 => array('id' => 3),
  448. 9 => array('id' => 4),
  449. 3 => array('id' => 5)
  450. )
  451. );
  452. $expected = array(1, 2, 3, 4, 5);
  453. $result = Set::extract($nonSequential, '/User/id');
  454. $this->assertEqual($expected, $result);
  455. }
  456. public function testExtractWithNoZeroKey() {
  457. $noZero = array(
  458. 'User' => array(
  459. 2 => array('id' => 1),
  460. 4 => array('id' => 2),
  461. 6 => array('id' => 3),
  462. 9 => array('id' => 4),
  463. 3 => array('id' => 5)
  464. )
  465. );
  466. $expected = array(1, 2, 3, 4, 5);
  467. $result = Set::extract($noZero, '/User/id');
  468. $this->assertEqual($expected, $result);
  469. }
  470. public function testExtractSingle() {
  471. $single = array('User' => array('id' => 4, 'name' => 'Neo'));
  472. $expected = array(4);
  473. $result = Set::extract($single, '/User/id');
  474. $this->assertEqual($expected, $result);
  475. }
  476. public function testExtractHasMany() {
  477. $tricky = array(
  478. 0 => array('User' => array('id' => 1, 'name' => 'John')),
  479. 1 => array('User' => array('id' => 2, 'name' => 'Bob')),
  480. 2 => array('User' => array('id' => 3, 'name' => 'Tony')),
  481. 'User' => array('id' => 4, 'name' => 'Neo')
  482. );
  483. $expected = array(1, 2, 3, 4);
  484. $result = Set::extract($tricky, '/User/id');
  485. $this->assertEqual($expected, $result);
  486. $expected = array(1, 3);
  487. $result = Set::extract($tricky, '/User[name=/n/]/id');
  488. $this->assertEqual($expected, $result);
  489. $expected = array(4);
  490. $result = Set::extract($tricky, '/User[name=/N/]/id');
  491. $this->assertEqual($expected, $result);
  492. $expected = array(1, 3, 4);
  493. $result = Set::extract($tricky, '/User[name=/N/i]/id');
  494. $this->assertEqual($expected, $result);
  495. $expected = array(
  496. array('id', 'name'), array('id', 'name'), array('id', 'name'), array('id', 'name')
  497. );
  498. $result = Set::extract($tricky, '/User/@*');
  499. $this->assertEqual($expected, $result);
  500. }
  501. public function testExtractAssociatedHasMany() {
  502. $common = array(
  503. array(
  504. 'Article' => array('id' => 1, 'name' => 'Article 1'),
  505. 'Comment' => array(
  506. array('id' => 1, 'user_id' => 5, 'article_id' => 1, 'text' => 'Comment 1'),
  507. array('id' => 2, 'user_id' => 23, 'article_id' => 1, 'text' => 'Comment 2'),
  508. array('id' => 3, 'user_id' => 17, 'article_id' => 1, 'text' => 'Comment 3')
  509. )
  510. ),
  511. array(
  512. 'Article' => array('id' => 2, 'name' => 'Article 2'),
  513. 'Comment' => array(
  514. array(
  515. 'id' => 4,
  516. 'user_id' => 2,
  517. 'article_id' => 2,
  518. 'text' => 'Comment 4',
  519. 'addition' => ''
  520. ),
  521. array(
  522. 'id' => 5,
  523. 'user_id' => 23,
  524. 'article_id' => 2,
  525. 'text' => 'Comment 5',
  526. 'addition' => 'foo'
  527. )
  528. )
  529. ),
  530. array(
  531. 'Article' => array('id' => 3, 'name' => 'Article 3'),
  532. 'Comment' => array()
  533. )
  534. );
  535. $result = Set::extract($common, '/');
  536. $this->assertEqual($result, $common);
  537. $expected = array(1);
  538. $result = Set::extract($common, '/Comment/id[:first]');
  539. $this->assertEqual($expected, $result);
  540. $expected = array(5);
  541. $result = Set::extract($common, '/Comment/id[:last]');
  542. $this->assertEqual($expected, $result);
  543. $result = Set::extract($common, '/Comment/id');
  544. $expected = array(1, 2, 3, 4, 5);
  545. $this->assertEqual($expected, $result);
  546. $expected = array(1, 2, 4, 5);
  547. $result = Set::extract($common, '/Comment[id!=3]/id');
  548. $this->assertEqual($expected, $result);
  549. $expected = array($common[0]['Comment'][2]);
  550. $result = Set::extract($common, '/Comment/2');
  551. $this->assertEqual($expected, $result);
  552. $expected = array($common[0]['Comment'][0]);
  553. $result = Set::extract($common, '/Comment[1]/.[id=1]');
  554. $this->assertEqual($expected, $result);
  555. $expected = array($common[1]['Comment'][1]);
  556. $result = Set::extract($common, '/1/Comment/.[2]');
  557. $this->assertEqual($expected, $result);
  558. $expected = array(array('Comment' => $common[1]['Comment'][0]));
  559. $result = Set::extract($common, '/Comment[addition=]');
  560. $this->assertEqual($expected, $result);
  561. $expected = array(3);
  562. $result = Set::extract($common, '/Article[:last]/id');
  563. $this->assertEqual($expected, $result);
  564. $expected = array();
  565. $result = Set::extract(array(), '/User/id');
  566. $this->assertEqual($expected, $result);
  567. }
  568. public function testExtractHabtm() {
  569. $habtm = array(
  570. array(
  571. 'Post' => array('id' => 1, 'title' => 'great post'),
  572. 'Comment' => array(
  573. array('id' => 1, 'text' => 'foo', 'User' => array('id' => 1, 'name' => 'bob')),
  574. array('id' => 2, 'text' => 'bar', 'User' => array('id' => 2, 'name' => 'tod'))
  575. )
  576. ),
  577. array(
  578. 'Post' => array('id' => 2, 'title' => 'fun post'),
  579. 'Comment' => array(
  580. array('id' => 3, 'text' => '123', 'User' => array('id' => 3, 'name' => 'dan')),
  581. array('id' => 4, 'text' => '987', 'User' => array('id' => 4, 'name' => 'jim'))
  582. )
  583. )
  584. );
  585. $result = Set::extract($habtm, '/Comment/User[name=/\w+/]/..');
  586. $this->assertEqual(count($result), 4);
  587. $this->assertEqual($result[0]['Comment']['User']['name'], 'bob');
  588. $this->assertEqual($result[1]['Comment']['User']['name'], 'tod');
  589. $this->assertEqual($result[2]['Comment']['User']['name'], 'dan');
  590. $this->assertEqual($result[3]['Comment']['User']['name'], 'jim');
  591. $result = Set::extract($habtm, '/Comment/User[name=/[a-z]+/]/..');
  592. $this->assertEqual(count($result), 4);
  593. $this->assertEqual($result[0]['Comment']['User']['name'], 'bob');
  594. $this->assertEqual($result[1]['Comment']['User']['name'], 'tod');
  595. $this->assertEqual($result[2]['Comment']['User']['name'], 'dan');
  596. $this->assertEqual($result[3]['Comment']['User']['name'], 'jim');
  597. $result = Set::extract($habtm, '/Comment/User[name=/bob|dan/]/..');
  598. $this->assertEqual(count($result), 2);
  599. $this->assertEqual($result[0]['Comment']['User']['name'], 'bob');
  600. $this->assertEqual($result[1]['Comment']['User']['name'], 'dan');
  601. $result = Set::extract($habtm, '/Comment/User[name=/bob|tod/]/..');
  602. $this->assertEqual(count($result), 2);
  603. $this->assertEqual($result[0]['Comment']['User']['name'], 'bob');
  604. $this->assertEqual($result[1]['Comment']['User']['name'], 'tod');
  605. }
  606. public function testExtractFromTree() {
  607. $tree = array(
  608. array(
  609. 'Category' => array('name' => 'Category 1'),
  610. 'children' => array(array('Category' => array('name' => 'Category 1.1')))
  611. ),
  612. array(
  613. 'Category' => array('name' => 'Category 2'),
  614. 'children' => array(
  615. array('Category' => array('name' => 'Category 2.1')),
  616. array('Category' => array('name' => 'Category 2.2'))
  617. )
  618. ),
  619. array(
  620. 'Category' => array('name' => 'Category 3'),
  621. 'children' => array(array('Category' => array('name' => 'Category 3.1')))
  622. )
  623. );
  624. $expected = array(array('Category' => $tree[1]['Category']));
  625. $result = Set::extract($tree, '/Category[name=Category 2]');
  626. $this->assertEqual($expected, $result);
  627. $expected = array(array(
  628. 'Category' => $tree[1]['Category'], 'children' => $tree[1]['children']
  629. ));
  630. $result = Set::extract($tree, '/Category[name=Category 2]/..');
  631. $this->assertEqual($expected, $result);
  632. $expected = array(
  633. array('children' => $tree[1]['children'][0]),
  634. array('children' => $tree[1]['children'][1])
  635. );
  636. $result = Set::extract($tree, '/Category[name=Category 2]/../children');
  637. $this->assertEqual($expected, $result);
  638. }
  639. public function testExtractOnMixedKeys() {
  640. $mixedKeys = array(
  641. 'User' => array(
  642. 0 => array('id' => 4, 'name' => 'Neo'),
  643. 1 => array('id' => 5, 'name' => 'Morpheus'),
  644. 'stringKey' => array()
  645. )
  646. );
  647. $expected = array('Neo', 'Morpheus');
  648. $result = Set::extract($mixedKeys, '/User/name');
  649. $this->assertEqual($expected, $result);
  650. }
  651. public function testExtractSingleWithNameCondition() {
  652. $single = array(
  653. array('CallType' => array('name' => 'Internal Voice'), 'x' => array('hour' => 7))
  654. );
  655. $expected = array(7);
  656. $result = Set::extract($single, '/CallType[name=Internal Voice]/../x/hour');
  657. $this->assertEqual($expected, $result);
  658. }
  659. public function testExtractWithNameCondition() {
  660. $multiple = array(
  661. array('CallType' => array('name' => 'Internal Voice'), 'x' => array('hour' => 7)),
  662. array('CallType' => array('name' => 'Internal Voice'), 'x' => array('hour' => 2)),
  663. array('CallType' => array('name' => 'Internal Voice'), 'x' => array('hour' => 1))
  664. );
  665. $expected = array(7, 2, 1);
  666. $result = Set::extract($multiple, '/CallType[name=Internal Voice]/../x/hour');
  667. $this->assertEqual($expected, $result);
  668. }
  669. public function testExtractWithTypeCondition() {
  670. $f = array(
  671. array(
  672. 'file' => array(
  673. 'name' => 'zipfile.zip',
  674. 'type' => 'application/zip',
  675. 'tmp_name' => '/tmp/php178.tmp',
  676. 'error' => 0,
  677. 'size' => '564647'
  678. )
  679. ),
  680. array(
  681. 'file' => array(
  682. 'name' => 'zipfile2.zip',
  683. 'type' => 'application/x-zip-compressed',
  684. 'tmp_name' => '/tmp/php179.tmp',
  685. 'error' => 0,
  686. 'size' => '354784'
  687. )
  688. ),
  689. array(
  690. 'file' => array(
  691. 'name' => 'picture.jpg',
  692. 'type' => 'image/jpeg',
  693. 'tmp_name' => '/tmp/php180.tmp',
  694. 'error' => 0,
  695. 'size' => '21324'
  696. )
  697. )
  698. );
  699. $expected = array(array(
  700. 'name' => 'zipfile2.zip', 'type' => 'application/x-zip-compressed',
  701. 'tmp_name' => '/tmp/php179.tmp', 'error' => 0, 'size' => '354784'
  702. ));
  703. $result = Set::extract($f, '/file/.[type=application/x-zip-compressed]');
  704. $this->assertEqual($expected, $result);
  705. $expected = array(array(
  706. 'name' => 'zipfile.zip', 'type' => 'application/zip',
  707. 'tmp_name' => '/tmp/php178.tmp', 'error' => 0, 'size' => '564647'
  708. ));
  709. $result = Set::extract($f, '/file/.[type=application/zip]');
  710. $this->assertEqual($expected, $result);
  711. }
  712. public function testIsNumericArrayCheck() {
  713. $data = array('one');
  714. $this->assertTrue(Set::isNumeric(array_keys($data)));
  715. $data = array(1 => 'one');
  716. $this->assertFalse(Set::isNumeric($data));
  717. $data = array('one');
  718. $this->assertFalse(Set::isNumeric($data));
  719. $data = array('one' => 'two');
  720. $this->assertFalse(Set::isNumeric($data));
  721. $data = array('one' => 1);
  722. $this->assertTrue(Set::isNumeric($data));
  723. $data = array(0);
  724. $this->assertTrue(Set::isNumeric($data));
  725. $data = array('one', 'two', 'three', 'four', 'five');
  726. $this->assertTrue(Set::isNumeric(array_keys($data)));
  727. $data = array(1 => 'one', 2 => 'two', 3 => 'three', 4 => 'four', 5 => 'five');
  728. $this->assertTrue(Set::isNumeric(array_keys($data)));
  729. $data = array('1' => 'one', 2 => 'two', 3 => 'three', 4 => 'four', 5 => 'five');
  730. $this->assertTrue(Set::isNumeric(array_keys($data)));
  731. $data = array('one', 2 => 'two', 3 => 'three', 4 => 'four', 'a' => 'five');
  732. $this->assertFalse(Set::isNumeric(array_keys($data)));
  733. $data = array();
  734. $this->assertNull(Set::isNumeric($data));
  735. }
  736. public function testCheckKeys() {
  737. $data = array('Multi' => array('dimensonal' => array('array')));
  738. $this->assertTrue(Set::check($data, 'Multi.dimensonal'));
  739. $this->assertFalse(Set::check($data, 'Multi.dimensonal.array'));
  740. $data = array(
  741. array(
  742. 'Article' => array(
  743. 'id' => '1', 'user_id' => '1', 'title' => 'First Article',
  744. 'body' => 'First Article Body', 'published' => 'Y',
  745. 'created' => '2007-03-18 10:39:23',
  746. 'updated' => '2007-03-18 10:41:31'
  747. ),
  748. 'User' => array(
  749. 'id' => '1', 'user' => 'mariano',
  750. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  751. 'created' => '2007-03-17 01:16:23',
  752. 'updated' => '2007-03-17 01:18:31'
  753. ),
  754. 'Comment' => array(
  755. array(
  756. 'id' => '1', 'article_id' => '1', 'user_id' => '2',
  757. 'comment' => 'First Comment for First Article',
  758. 'published' => 'Y', 'created' => '2007-03-18 10:45:23',
  759. 'updated' => '2007-03-18 10:47:31'
  760. ),
  761. array(
  762. 'id' => '2', 'article_id' => '1', 'user_id' => '4',
  763. 'comment' => 'Second Comment for First Article',
  764. 'published' => 'Y', 'created' => '2007-03-18 10:47:23',
  765. 'updated' => '2007-03-18 10:49:31'
  766. )
  767. ),
  768. 'Tag' => array(
  769. array(
  770. 'id' => '1', 'tag' => 'tag1', 'created' => '2007-03-18 12:22:23',
  771. 'updated' => '2007-03-18 12:24:31'
  772. ),
  773. array(
  774. 'id' => '2', 'tag' => 'tag2', 'created' => '2007-03-18 12:24:23',
  775. 'updated' => '2007-03-18 12:26:31'
  776. )
  777. )
  778. ),
  779. array(
  780. 'Article' => array(
  781. 'id' => '3', 'user_id' => '1', 'title' => 'Third Article',
  782. 'body' => 'Third Article Body', 'published' => 'Y',
  783. 'created' => '2007-03-18 10:43:23', 'updated' => '2007-03-18 10:45:31'
  784. ),
  785. 'User' => array(
  786. 'id' => '1', 'user' => 'mariano',
  787. 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
  788. 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
  789. ),
  790. 'Comment' => array(),
  791. 'Tag' => array()
  792. )
  793. );
  794. $this->assertTrue(Set::check($data, '0.Article.user_id'));
  795. $this->assertTrue(Set::check($data, '0.Comment.0.id'));
  796. $this->assertFalse(Set::check($data, '0.Comment.0.id.0'));
  797. $this->assertTrue(Set::check($data, '0.Article.user_id'));
  798. $this->assertFalse(Set::check($data, '0.Article.user_id.a'));
  799. }
  800. public function testMerge() {
  801. $result = Set::merge(array('foo'), array());
  802. $this->assertIdentical($result, array('foo'));
  803. $result = Set::merge((array) 'foo', (array) 'bar');
  804. $this->assertIdentical($result, array('foo', 'bar'));
  805. $result = Set::merge((array) 'foo', array('user' => 'bob', 'no-bar'));
  806. $this->assertIdentical($result, array('foo', 'user' => 'bob', 'no-bar'));
  807. $a = array('foo', 'foo2');
  808. $b = array('bar', 'bar2');
  809. $this->assertIdentical(Set::merge($a, $b), array('foo', 'foo2', 'bar', 'bar2'));
  810. $a = array('foo' => 'bar', 'bar' => 'foo');
  811. $b = array('foo' => 'no-bar', 'bar' => 'no-foo');
  812. $this->assertIdentical(Set::merge($a, $b), array('foo' => 'no-bar', 'bar' => 'no-foo'));
  813. $a = array('users' => array('bob', 'jim'));
  814. $b = array('users' => array('lisa', 'tina'));
  815. $this->assertIdentical(
  816. Set::merge($a, $b), array('users' => array('bob', 'jim', 'lisa', 'tina'))
  817. );
  818. $a = array('users' => array('jim', 'bob'));
  819. $b = array('users' => 'none');
  820. $this->assertIdentical(Set::merge($a, $b), array('users' => 'none'));
  821. $a = array('users' => array('lisa' => array('id' => 5, 'pw' => 'secret')), 'lithium');
  822. $b = array('users' => array('lisa' => array('pw' => 'new-pass', 'age' => 23)), 'ice-cream');
  823. $this->assertIdentical(
  824. Set::merge($a, $b),
  825. array(
  826. 'users' => array('lisa' => array('id' => 5, 'pw' => 'new-pass', 'age' => 23)),
  827. 'lithium',
  828. 'ice-cream'
  829. )
  830. );
  831. $c = array(
  832. 'users' => array(
  833. 'lisa' => array('pw' => 'you-will-never-guess', 'age' => 25, 'pet' => 'dog')
  834. ),
  835. 'chocolate'
  836. );
  837. $expected = array(
  838. 'users' => array(
  839. 'lisa' => array(
  840. 'id' => 5, 'pw' => 'you-will-never-guess', 'age' => 25, 'pet' => 'dog'
  841. )
  842. ),
  843. 'lithium',
  844. 'ice-cream',
  845. 'chocolate'
  846. );
  847. $this->assertIdentical($expected, Set::merge(Set::merge($a, $b), $c));
  848. $this->assertIdentical($expected, Set::merge(Set::merge($a, $b), Set::merge(array(), $c)));
  849. $result = Set::merge($a, Set::merge($b, $c));
  850. $this->assertIdentical($expected, $result);
  851. $a = array('Tree', 'CounterCache', 'Upload' => array(
  852. 'folder' => 'products', 'fields' => array(
  853. 'image_1_id', 'image_2_id', 'image_3_id', 'image_4_id', 'image_5_id'
  854. )
  855. ));
  856. $b = array(
  857. 'Cacheable' => array('enabled' => false),
  858. 'Limit', 'Bindable', 'Validator', 'Transactional'
  859. );
  860. $expected = array('Tree', 'CounterCache', 'Upload' => array(
  861. 'folder' => 'products', 'fields' => array(
  862. 'image_1_id', 'image_2_id', 'image_3_id', 'image_4_id', 'image_5_id'
  863. )),
  864. 'Cacheable' => array('enabled' => false),
  865. 'Limit',
  866. 'Bindable',
  867. 'Validator',
  868. 'Transactional'
  869. );
  870. $this->assertIdentical(Set::merge($a, $b), $expected);
  871. $expected = array('Tree' => null, 'CounterCache' => null, 'Upload' => array(
  872. 'folder' => 'products', 'fields' => array(
  873. 'image_1_id', 'image_2_id', 'image_3_id', 'image_4_id', 'image_5_id'
  874. )),
  875. 'Cacheable' => array('enabled' => false),
  876. 'Limit' => null,
  877. 'Bindable' => null,
  878. 'Validator' => null,
  879. 'Transactional' => null
  880. );
  881. $this->assertIdentical(Set::normalize(Set::merge($a, $b)), $expected);
  882. }
  883. public function testSort() {
  884. $a = array(
  885. array('Person' => array('name' => 'Jeff'), 'Friend' => array(array('name' => 'Nate'))),
  886. array('Person' => array('name' => 'Tracy'), 'Friend' => array(
  887. array('name' => 'Lindsay')
  888. ))
  889. );
  890. $b = array(
  891. array('Person' => array('name' => 'Tracy'),'Friend' => array(
  892. array('name' => 'Lindsay')
  893. )),
  894. array('Person' => array('name' => 'Jeff'), 'Friend' => array(array('name' => 'Nate')))
  895. );
  896. $a = Set::sort($a, '/Friend/name', 'asc');
  897. $this->assertIdentical($a, $b);
  898. $b = array(
  899. array('Person' => array('name' => 'Jeff'), 'Friend' => array(array('name' => 'Nate'))),
  900. array('Person' => array('name' => 'Tracy'), 'Friend' => array(
  901. array('name' => 'Lindsay')
  902. ))
  903. );
  904. $a = array(
  905. array('Person' => array('name' => 'Tracy'), 'Friend' => array(
  906. array('name' => 'Lindsay')
  907. )),
  908. array('Person' => array('name' => 'Jeff'), 'Friend' => array(array('name' => 'Nate')))
  909. );
  910. $a = Set::sort($a, '/Friend/name', 'desc');
  911. $this->assertIdentical($a, $b);
  912. $a = array(
  913. array('Person' => array('name' => 'Jeff'), 'Friend' => array(array('name' => 'Nate'))),
  914. array('Person' => array('name' => 'Tracy'), 'Friend' => array(
  915. array('name' => 'Lindsay')
  916. )),
  917. array('Person' => array('name' => 'Adam'), 'Friend' => array(array('name' => 'Bob')))
  918. );
  919. $b = array(
  920. array('Person' => array('name' => 'Adam'),'Friend' => array(array('name' => 'Bob'))),
  921. array('Person' => array('name' => 'Jeff'), 'Friend' => array(array('name' => 'Nate'))),
  922. array('Person' => array('name' => 'Tracy'), 'Friend' => array(
  923. array('name' => 'Lindsay')
  924. ))
  925. );
  926. $a = Set::sort($a, '/Person/name', 'asc');
  927. $this->assertIdentical($a, $b);
  928. $a = array(array(7, 6, 4), array(3, 4, 5), array(3, 2, 1));
  929. $b = array(array(3, 2, 1), array(3, 4, 5), array(7, 6, 4));
  930. $a = Set::sort($a, '/', 'asc');
  931. $this->assertIdentical($a, $b);
  932. $a = array(array(7, 6, 4), array(3, 4, 5), array(3, 2, array(1, 1, 1)));
  933. $b = array(array(3, 2, array(1, 1, 1)), array(3, 4, 5), array(7, 6, 4));
  934. $a = Set::sort($a, '/.', 'asc');
  935. $this->assertIdentical($a, $b);
  936. $a = array(
  937. array('Person' => array('name' => 'Jeff')),
  938. array('Shirt' => array('color' => 'black'))
  939. );
  940. $b = array(array('Person' => array('name' => 'Jeff')));
  941. $a = Set::sort($a, '/Person/name', 'asc');
  942. $this->assertIdentical($a, $b);
  943. }
  944. public function testInsert() {
  945. $a = array('pages' => array('name' => 'page'));
  946. $result = Set::insert($a, 'files', array('name' => 'files'));
  947. $expected = array('pages' => array('name' => 'page'), 'files' => array('name' => 'files'));
  948. $this->assertIdentical($expected, $result);
  949. $a = array('pages' => array('name' => 'page'));
  950. $result = Set::insert($a, 'pages.name', array());
  951. $expected = array('pages' => array('name' => array()));
  952. $this->assertIdentical($expected, $result);
  953. $a = array('pages' => array(array('name' => 'main'), array('name' => 'about')));
  954. $result = Set::insert($a, 'pages.1.vars', array('title' => 'page title'));
  955. $expected = array(
  956. 'pages' => array(
  957. array('name' => 'main'),
  958. array('name' => 'about', 'vars' => array('title' => 'page title'))
  959. )
  960. );
  961. $this->assertIdentical($expected, $result);
  962. }
  963. public function testRemove() {
  964. $a = array('pages' => array('name' => 'page'), 'files' => array('name' => 'files'));
  965. $result = Set::remove($a, 'files', array('name' => 'files'));
  966. $expected = array('pages' => array('name' => 'page'));
  967. $this->assertIdentical($expected, $result);
  968. $a = array(
  969. 'pages' => array(
  970. array('name' => 'main'),
  971. array('name' => 'about', 'vars' => array('title' => 'page title'))
  972. )
  973. );
  974. $result = Set::remove($a, 'pages.1.vars', array('title' => 'page title'));
  975. $expected = array('pages' => array(array('name' => 'main'), array('name' => 'about')));
  976. $this->assertIdentical($expected, $result);
  977. $result = Set::remove($a, 'pages.2.vars', array('title' => 'page title'));
  978. $expected = $a;
  979. $this->assertIdentical($expected, $result);
  980. }
  981. public function testCheck() {
  982. $set = array('My Index 1' => array(
  983. 'First' => 'The first item'
  984. ));
  985. $result = Set::check($set, 'My Index 1.First');
  986. $this->assertTrue($result);
  987. $this->assertTrue(Set::check($set, 'My Index 1'));
  988. $this->assertTrue(Set::check($set, array()));
  989. $set = array('My Index 1' => array('First' => array('Second' => array('Third' => array(
  990. 'Fourth' => 'Heavy. Nesting.'
  991. )))));
  992. $this->assertTrue(Set::check($set, 'My Index 1.First.Second'));
  993. $this->assertTrue(Set::check($set, 'My Index 1.First.Second.Third'));
  994. $this->assertTrue(Set::check($set, 'My Index 1.First.Second.Third.Fourth'));
  995. $this->assertFalse(Set::check($set, 'My Index 1.First.Seconds.Third.Fourth'));
  996. }
  997. public function testInsertAndRemoveWithFunkyKeys() {
  998. $set = Set::insert(array(), 'Session Test', "test");
  999. $result = Set::extract($set, '/Session Test');
  1000. $this->assertEqual($result, array('test'));
  1001. $set = Set::remove($set, 'Session Test');
  1002. $this->assertFalse(Set::check($set, 'Session Test'));
  1003. $this->assertTrue($set = Set::insert(array(), 'Session Test.Test Case', "test"));
  1004. $this->assertTrue(Set::check($set, 'Session Test.Test Case'));
  1005. }
  1006. public function testDiff() {
  1007. $a = array(array('name' => 'main'), array('name' => 'about'));
  1008. $b = array(array('name' => 'main'), array('name' => 'about'), array('name' => 'contact'));
  1009. $result = Set::diff($a, $b);
  1010. $expected = array(2 => array('name' => 'contact'));
  1011. $this->assertIdentical($expected, $result);
  1012. $result = Set::diff($a, array());
  1013. $expected = $a;
  1014. $this->assertIdentical($expected, $result);
  1015. $result = Set::diff(array(), $b);
  1016. $expected = $b;
  1017. $this->assertIdentical($expected, $result);
  1018. $b = array(array('name' => 'me'), array('name' => 'about'));
  1019. $result = Set::diff($a, $b);
  1020. $expected = array(array('name' => 'main'));
  1021. $this->assertIdentical($expected, $result);
  1022. }
  1023. public function testContains() {
  1024. $a = array(
  1025. 0 => array('name' => 'main'),
  1026. 1 => array('name' => 'about')
  1027. );
  1028. $b = array(
  1029. 0 => array('name' => 'main'),
  1030. 1 => array('name' => 'about'),
  1031. 2 => array('name' => 'contact'),
  1032. 'a' => 'b'
  1033. );
  1034. $this->assertTrue(Set::contains($a, $a));
  1035. $this->assertFalse(Set::contains($a, $b));
  1036. $this->assertTrue(Set::contains($b, $a));
  1037. }
  1038. public function testCombine() {
  1039. $result = Set::combine(array(), '/User/id', '/User/Data');
  1040. $this->assertFalse($result);
  1041. $result = Set::combine('', '/User/id', '/User/Data');
  1042. $this->assertFalse($result);
  1043. $a = array(
  1044. array('User' => array('id' => 2, 'group_id' => 1,
  1045. 'Data' => array('user' => 'mariano.iglesias','name' => 'Mariano Iglesias'))),
  1046. array('User' => array('id' => 14, 'group_id' => 2,
  1047. 'Data' => array('user' => 'jperras', 'name' => 'Joel Perras'))),
  1048. array('User' => array('id' => 25, 'group_id' => 1,
  1049. 'Data' => array('user' => 'gwoo','name' => 'The Gwoo'))));
  1050. $result = Set::combine($a, '/User/id');
  1051. $expected = array(2 => null, 14 => null, 25 => null);
  1052. $this->assertIdentical($expected, $result);
  1053. $result = Set::combine($a, '/User/id', '/User/non-existant');
  1054. $expected = array(2 => null, 14 => null, 25 => null);
  1055. $this->assertIdentical($expected, $result);
  1056. $result = Set::combine($a, '/User/id', '/User/Data/.');
  1057. $expected = array(
  1058. 2 => array('user' => 'mariano.iglesias', 'name' => 'Mariano Iglesias'),
  1059. 14 => array('user' => 'jperras', 'name' => 'Joel Perras'),
  1060. 25 => array('user' => 'gwoo', 'name' => 'The Gwoo'));
  1061. $this->assertIdentical($expected, $result);
  1062. $result = Set::combine($a, '/User/id', '/User/Data/name/.');
  1063. $expected = array(
  1064. 2 => 'Mariano Iglesias',
  1065. 14 => 'Joel Perras',
  1066. 25 => 'The Gwoo');
  1067. $this->assertIdentical($expected, $result);
  1068. $result = Set::combine($a, '/User/id', '/User/Data/.', '/User/group_id');
  1069. $expected = array(
  1070. 1 => array(
  1071. 2 => array('user' => 'mariano.iglesias', 'name' => 'Mariano Iglesias'),
  1072. 25 => array('user' => 'gwoo', 'name' => 'The Gwoo')),
  1073. 2 => array(
  1074. 14 => array('user' => 'jperras', 'name' => 'Joel Perras')));
  1075. $this->assertIdentical($expected, $result);
  1076. $result = Set::combine($a, '/User/id', '/User/Data/name/.', '/User/group_id');
  1077. $expected = array(
  1078. 1 => array(
  1079. 2 => 'Mariano Iglesias',
  1080. 25 => 'The Gwoo'),
  1081. 2 => array(
  1082. 14 => 'Joel Perras'));
  1083. $this->assertIdentical($expected, $result);
  1084. $result = Set::combine(
  1085. $a,
  1086. '/User/id',
  1087. array('{0}: {1}', '/User/Data/user', '/User/Data/name'),
  1088. '/User/group_id'
  1089. );
  1090. $expected = array(
  1091. 1 => array(2 => 'mariano.iglesias: Mariano Iglesias', 25 => 'gwoo: The Gwoo'),
  1092. 2 => array(14 => 'jperras: Joel Perras')
  1093. );
  1094. $this->assertIdentical($expected, $result);
  1095. $result = Set::combine(
  1096. $a,
  1097. array('{0}: {1}', '/User/Data/user', '/User/Data/name'),
  1098. '/User/id'
  1099. );
  1100. $expected = array(
  1101. 'mariano.iglesias: Mariano Iglesias' => 2,
  1102. 'jperras: Joel Perras' => 14,
  1103. 'gwoo: The Gwoo' => 25
  1104. );
  1105. $this->assertIdentical($expected, $result);
  1106. $result = Set::combine(
  1107. $a,
  1108. array('{1}: {0}', '/User/Data/user', '/User/Data/name'),
  1109. '/User/id'
  1110. );
  1111. $expected = array(
  1112. 'Mariano Iglesias: mariano.iglesias' => 2,
  1113. 'Joel Perras: jperras' => 14,
  1114. 'The Gwoo: gwoo' => 25
  1115. );
  1116. $this->assertIdentical($expected, $result);
  1117. $result = Set::combine($a, array(
  1118. '%1$s: %2$d', '/User/Data/user', '/User/id'), '/User/Data/name'
  1119. );
  1120. $expected = array(
  1121. 'mariano.iglesias: 2' => 'Mariano Iglesias',
  1122. 'jperras: 14' => 'Joel Perras',
  1123. 'gwoo: 25' => 'The Gwoo'
  1124. );
  1125. $this->assertIdentical($expected, $result);
  1126. $result = Set::combine($a, array(
  1127. '%2$d: %1$s', '/User/Data/user', '/User/id'), '/User/Data/name'
  1128. );
  1129. $expected = array(
  1130. '2: mariano.iglesias' => 'Mariano Iglesias',
  1131. '14: jperras' => 'Joel Perras',
  1132. '25: gwoo' => 'The Gwoo'
  1133. );
  1134. $this->assertIdentical($expected, $result);
  1135. $b = new stdClass();
  1136. $b->users = array(
  1137. array('User' => array(
  1138. 'id' => 2, 'group_id' => 1, 'Data' => array(
  1139. 'user' => 'mariano.iglesias','name' => 'Mariano Iglesias'
  1140. )
  1141. )),
  1142. array('User' => array('id' => 14, 'group_id' => 2, 'Data' => array(
  1143. 'user' => 'jperras', 'name' => 'Joel Perras'
  1144. ))),
  1145. array('User' => array('id' => 25, 'group_id' => 1, 'Data' => array(
  1146. 'user' => 'gwoo','name' => 'The Gwoo'
  1147. )))
  1148. );
  1149. $result = Set::combine($b, '/users/User/id');
  1150. $expected = array(2 => null, 14 => null, 25 => null);
  1151. $this->assertIdentical($expected, $result);
  1152. $result = Set::combine($b, '/users/User/id', '/users/User/non-existant');
  1153. $expected = array(2 => null, 14 => null, 25 => null);
  1154. $this->assertIdentical($expected, $result);
  1155. }
  1156. public function testAppend() {
  1157. $array1 = array('ModelOne' => array(
  1158. 'id' => 1001, 'field_one' => 'a1.m1.f1', 'field_two' => 'a1.m1.f2'
  1159. ));
  1160. $array2 = array('ModelTwo' => array(
  1161. 'id' => 1002, 'field_one' => 'a2.m2.f1', 'field_two' => 'a2.m2.f2'
  1162. ));
  1163. $result = Set::append($array1, $array2);
  1164. $this->assertIdentical($result, $array1 + $array2);
  1165. $array3 = array('ModelOne' => array(
  1166. 'id' => 1003, 'field_one' => 'a3.m1.f1',
  1167. 'field_two' => 'a3.m1.f2', 'field_three' => 'a3.m1.f3'
  1168. ));
  1169. $result = Set::append($array1, $array3);
  1170. $expected = array('ModelOne' => array(
  1171. 'id' => 1001, 'field_one' => 'a1.m1.f1',
  1172. 'field_two' => 'a1.m1.f2', 'field_three' => 'a3.m1.f3'
  1173. ));
  1174. $this->assertIdentical($expected, $result);
  1175. $array1 = array(
  1176. array('ModelOne' => array(
  1177. 'id' => 1001, 'field_one' => 's1.0.m1.f1', 'field_two' => 's1.0.m1.f2'
  1178. )),
  1179. array('ModelTwo' => array(
  1180. 'id' => 1002, 'field_one' => 's1.1.m2.f2', 'field_two' => 's1.1.m2.f2'
  1181. ))
  1182. );
  1183. $array2 = array(
  1184. array('ModelOne' => array(
  1185. 'id' => 1001, 'field_one' => 's2.0.m1.f1', 'field_two' => 's2.0.m1.f2'
  1186. )),
  1187. array('ModelTwo' => array(
  1188. 'id' => 1002, 'field_one' => 's2.1.m2.f2', 'field_two' => 's2.1.m2.f2'
  1189. ))
  1190. );
  1191. $result = Set::append($array1, $array2);
  1192. $this->assertIdentical($result, $array1);
  1193. $array3 = array(array('ModelThree' => array(
  1194. 'id' => 1003, 'field_one' => 's3.0.m3.f1', 'field_two' => 's3.0.m3.f2'
  1195. )));
  1196. $result = Set::append($array1, $array3);
  1197. $expected = array(
  1198. array(
  1199. 'ModelOne' => array(
  1200. 'id' => 1001, 'field_one' => 's1.0.m1.f1', 'field_two' => 's1.0.m1.f2'
  1201. ),
  1202. 'ModelThree' => array(
  1203. 'id' => 1003, 'field_one' => 's3.0.m3.f1', 'field_two' => 's3.0.m3.f2'
  1204. )
  1205. ),
  1206. array('ModelTwo' => array(
  1207. 'id' => 1002, 'field_one' => 's1.1.m2.f2', 'field_two' => 's1.1.m2.f2'
  1208. ))
  1209. );
  1210. $this->assertIdentical($expected, $result);
  1211. $result = Set::append($array1, array());
  1212. $this->assertIdentical($result, $array1);
  1213. $result = Set::append($array1, $array2);
  1214. $this->assertIdentical($result, $array1 + $array2);
  1215. $result = Set::append(array(), array('2'));
  1216. $this->assertIdentical(array('2'), $result);
  1217. $array1 = array(
  1218. 'ModelOne' => array(
  1219. 'id' => 1001, 'field_one' => 's1.0.m1.f1', 'field_two' => 's1.0.m1.f2'
  1220. ),
  1221. 'ModelTwo' => array(
  1222. 'id' => 1002, 'field_one' => 's1.0.m2.f1', 'field_two' => 's1.0.m2.f2'
  1223. )
  1224. );
  1225. $array2 = array(
  1226. 'ModelTwo' => array(
  1227. 'field_three' => 's1.0.m2.f3'
  1228. )
  1229. );
  1230. $array3 = array(
  1231. 'ModelOne' => array(
  1232. 'field_three' => 's1.0.m1.f3'
  1233. )
  1234. );
  1235. $result = Set::append($array1, $array2, $array3);
  1236. $expected = array(
  1237. 'ModelOne' => array(
  1238. 'id' => 1001,
  1239. 'field_one' => 's1.0.m1.f1',
  1240. 'field_two' => 's1.0.m1.f2',
  1241. 'field_three' => 's1.0.m1.f3'
  1242. ),
  1243. 'ModelTwo' => array(
  1244. 'id' => 1002,
  1245. 'field_one' => 's1.0.m2.f1',
  1246. 'field_two' => 's1.0.m2.f2',
  1247. 'field_three' => 's1.0.m2.f3'
  1248. )
  1249. );
  1250. $this->assertIdentical($expected, $result);
  1251. }
  1252. public function testStrictKeyCheck() {
  1253. $set = array('a' => 'hi');
  1254. $this->assertFalse(Set::check($set, 'a.b'));
  1255. $this->assertTrue(Set::check($set, 'a'));
  1256. }
  1257. public function testMixedKeyNormalization() {
  1258. $input = array('"string"' => array('before' => '=>'), 1 => array('before' => '=>'));
  1259. $result = Set::normalize($input);
  1260. $this->assertEqual($input, $result);
  1261. $input = 'Foo,Bar,Baz';
  1262. $result = Set::normalize($input);
  1263. $this->assertEqual(array('Foo' => null, 'Bar' => null, 'Baz' => null), $result);
  1264. $input = array('baz' => 'foo', 'bar');
  1265. $result = Set::normalize($input, false);
  1266. $this->assertEqual(array('baz' => 'foo', 'bar' => null), $result);
  1267. }
  1268. public function testSetSlice() {
  1269. $data = array('key1' => 'val1', 'key2' => 'val2', 'key3' => 'val3');
  1270. list($kept, $removed) = Set::slice($data, array('key3'));
  1271. $this->assertEqual(array('key3' => 'val3'), $removed);
  1272. $this->assertEqual(array('key1' => 'val1', 'key2' => 'val2'), $kept);
  1273. $data = array('key1' => 'val1', 'key2' => 'val2', 'key3' => 'val3');
  1274. list($kept, $removed) = Set::slice($data, array('key1', 'key3'));
  1275. $this->assertEqual(array('key1' => 'val1', 'key3' => 'val3'), $removed);
  1276. $this->assertEqual(array('key2' => 'val2'), $kept);
  1277. $data = array('key1' => 'val1', 'key2' => 'val2', 'key3' => 'val3');
  1278. list($kept, $removed) = Set::slice($data, 'key2');
  1279. $this->assertEqual(array('key2' => 'val2'), $removed);
  1280. $this->assertEqual(array('key1' => 'val1', 'key3' => 'val3'), $kept);
  1281. $data = array('key1' => 'val1', 'key2' => 'val2', 'key3' => array('foo' => 'bar'));
  1282. list($kept, $removed) = Set::slice($data, array('key1', 'key3'));
  1283. $this->assertEqual(array('key1' => 'val1', 'key3' => array('foo' => 'bar')), $removed);
  1284. $this->assertEqual(array('key2' => 'val2'), $kept);
  1285. }
  1286. }
  1287. ?>