DataTest.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Dojo
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. if (!defined('PHPUnit_MAIN_METHOD')) {
  23. define('PHPUnit_MAIN_METHOD', 'Zend_Dojo_DataTest::main');
  24. }
  25. require_once 'Zend/Dojo/Data.php';
  26. /**
  27. * @category Zend
  28. * @package Zend_Dojo
  29. * @subpackage UnitTests
  30. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. * @group Zend_Dojo
  33. */
  34. class Zend_Dojo_DataTest extends PHPUnit_Framework_TestCase
  35. {
  36. public $dojoData;
  37. public static function main()
  38. {
  39. $suite = new PHPUnit_Framework_TestSuite("Zend_Dojo_DataTest");
  40. $result = PHPUnit_TextUI_TestRunner::run($suite);
  41. }
  42. /**
  43. * Sets up the fixture, for example, open a network connection.
  44. * This method is called before a test is executed.
  45. *
  46. * @return void
  47. */
  48. public function setUp()
  49. {
  50. $this->dojoData = new Zend_Dojo_Data;
  51. }
  52. public function testIdentifierShouldBeNullByDefault()
  53. {
  54. $this->assertNull($this->dojoData->getIdentifier());
  55. }
  56. public function testIdentifierShouldBeMutable()
  57. {
  58. $this->testIdentifierShouldBeNullByDefault();
  59. $this->dojoData->setIdentifier('id');
  60. $this->assertEquals('id', $this->dojoData->getIdentifier());
  61. }
  62. public function testNullIdentifierShouldBeAllowed()
  63. {
  64. $this->dojoData->setIdentifier('foo');
  65. $this->assertEquals('foo', $this->dojoData->getIdentifier());
  66. $this->dojoData->setIdentifier(null);
  67. $this->assertNull($this->dojoData->getIdentifier());
  68. }
  69. public function testIntegerIdentifierShouldBeAllowed()
  70. {
  71. $this->dojoData->setIdentifier(2);
  72. $this->assertSame(2, $this->dojoData->getIdentifier());
  73. }
  74. /**
  75. * @expectedException Zend_Dojo_Exception
  76. */
  77. public function testSetIdentifierShouldThrowExceptionOnInvalidType()
  78. {
  79. $this->dojoData->setIdentifier(true);
  80. }
  81. public function testLabelShouldBeNullByDefault()
  82. {
  83. $this->assertNull($this->dojoData->getLabel());
  84. }
  85. public function testLabelShouldBeMutable()
  86. {
  87. $this->testLabelShouldBeNullByDefault();
  88. $this->dojoData->setLabel('title');
  89. $this->assertEquals('title', $this->dojoData->getLabel());
  90. }
  91. public function testLabelShouldBeNullable()
  92. {
  93. $this->testLabelShouldBeMutable();
  94. $this->dojoData->setLabel(null);
  95. $this->assertNull($this->dojoData->getLabel());
  96. }
  97. public function testAddItemShouldThrowExceptionIfNoIdentifierPresentInObject()
  98. {
  99. $item = array(
  100. 'id' => '1',
  101. 'title' => 'foo',
  102. 'url' => 'http://www.example.com/',
  103. );
  104. try {
  105. $this->dojoData->addItem($item);
  106. $this->fail('Should throw exception if no identifier present');
  107. } catch (Zend_Dojo_Exception $e) {
  108. $this->assertContains('identifier', $e->getMessage());
  109. }
  110. }
  111. public function testAddItemShouldThrowExceptionIfNoIdentifierPresentInItem()
  112. {
  113. $item = array(
  114. 'title' => 'foo',
  115. 'url' => 'http://www.example.com/',
  116. );
  117. $this->dojoData->setIdentifier('id');
  118. try {
  119. $this->dojoData->addItem($item);
  120. $this->fail('Should throw exception if no identifier present');
  121. } catch (Zend_Dojo_Exception $e) {
  122. $this->assertContains('identifier', $e->getMessage());
  123. }
  124. }
  125. public function testAddItemShouldAcceptArray()
  126. {
  127. $item = array(
  128. 'id' => '1',
  129. 'title' => 'foo',
  130. 'url' => 'http://www.example.com/',
  131. );
  132. $this->dojoData->setIdentifier('id');
  133. $this->dojoData->addItem($item);
  134. $this->assertEquals(1, count($this->dojoData));
  135. $this->assertSame($item, $this->dojoData->getItem(1));
  136. }
  137. public function testAddItemShouldAcceptStdObject()
  138. {
  139. $item = array(
  140. 'id' => '1',
  141. 'title' => 'foo',
  142. 'url' => 'http://www.example.com/',
  143. );
  144. $obj = (object) $item;
  145. $this->dojoData->setIdentifier('id');
  146. $this->dojoData->addItem($obj);
  147. $this->assertEquals(1, count($this->dojoData));
  148. $this->assertSame($item, $this->dojoData->getItem(1));
  149. }
  150. public function testAddItemShouldAcceptObjectImplementingToArray()
  151. {
  152. $obj = new Zend_Dojo_DataTest_DataObject;
  153. $this->dojoData->setIdentifier('id');
  154. $this->dojoData->addItem($obj);
  155. $this->assertEquals(1, count($this->dojoData));
  156. $this->assertSame($obj->item, $this->dojoData->getItem('foo'));
  157. }
  158. public function testAddItemShouldThrowErrorOnInvalidItem()
  159. {
  160. $this->dojoData->setIdentifier('id');
  161. try {
  162. $this->dojoData->addItem('foo');
  163. $this->fail('Invalid item should throw error');
  164. } catch (Zend_Dojo_Exception $e) {
  165. $this->assertContains('Only arrays and objects', $e->getMessage());
  166. }
  167. }
  168. public function testAddItemShouldAllowSpecifyingIdentifier()
  169. {
  170. $item = array(
  171. 'title' => 'foo',
  172. 'url' => 'http://www.example.com/',
  173. );
  174. $this->dojoData->setIdentifier('id');
  175. $this->dojoData->addItem($item, 'foo');
  176. $this->assertEquals(1, count($this->dojoData));
  177. $stored = $this->dojoData->getItem('foo');
  178. $this->assertTrue(array_key_exists('id', $stored));
  179. $this->assertEquals('foo', $stored['id']);
  180. foreach ($item as $key => $value) {
  181. $this->assertTrue(array_key_exists($key, $stored));
  182. $this->assertEquals($value, $stored[$key]);
  183. }
  184. }
  185. public function testOverwritingItemsShouldNotBeAllowedFromAddItem()
  186. {
  187. $this->testAddItemShouldAcceptArray();
  188. $item = array(
  189. 'id' => '1',
  190. 'title' => 'foo',
  191. 'url' => 'http://www.example.com/',
  192. );
  193. try {
  194. $this->dojoData->addItem($item);
  195. $this->fail('Overwriting items via addItem() should throw error');
  196. } catch (Zend_Dojo_Exception $e) {
  197. $this->assertContains('not allowed', $e->getMessage());
  198. }
  199. }
  200. public function testSetItemShouldOverwriteExistingItem()
  201. {
  202. $this->testAddItemShouldAcceptArray();
  203. $item = array(
  204. 'id' => '1',
  205. 'title' => 'bar',
  206. 'url' => 'http://www.foo.com/',
  207. );
  208. $this->assertNotSame($item, $this->dojoData->getItem(1));
  209. $this->dojoData->setItem($item);
  210. $this->assertEquals(1, count($this->dojoData));
  211. $this->assertSame($item, $this->dojoData->getItem(1));
  212. }
  213. public function testSetItemShouldAddItemIfNonexistent()
  214. {
  215. $item = array(
  216. 'id' => '1',
  217. 'title' => 'bar',
  218. 'url' => 'http://www.foo.com/',
  219. );
  220. $this->dojoData->setIdentifier('id');
  221. $this->assertEquals(0, count($this->dojoData));
  222. $this->dojoData->setItem($item);
  223. $this->assertEquals(1, count($this->dojoData));
  224. $this->assertSame($item, $this->dojoData->getItem(1));
  225. }
  226. public function testAddItemsShouldAcceptArray()
  227. {
  228. $items = array(
  229. array (
  230. 'id' => 1,
  231. 'title' => 'Foo',
  232. 'email' => 'foo@bar',
  233. ),
  234. array (
  235. 'id' => 2,
  236. 'title' => 'Bar',
  237. 'email' => 'bar@bar',
  238. ),
  239. array (
  240. 'id' => 3,
  241. 'title' => 'Baz',
  242. 'email' => 'baz@bar',
  243. ),
  244. );
  245. $this->dojoData->setIdentifier('id');
  246. $this->assertEquals(0, count($this->dojoData));
  247. $this->dojoData->addItems($items);
  248. $this->assertEquals(3, count($this->dojoData));
  249. $this->assertSame($items[0], $this->dojoData->getItem(1));
  250. $this->assertSame($items[1], $this->dojoData->getItem(2));
  251. $this->assertSame($items[2], $this->dojoData->getItem(3));
  252. }
  253. public function testAddItemsShouldAcceptTraversableObject()
  254. {
  255. $obj = new Zend_Dojo_DataTest_DataCollection;
  256. $this->dojoData->setIdentifier('id');
  257. $this->assertEquals(0, count($this->dojoData));
  258. $this->dojoData->addItems($obj);
  259. $this->assertEquals(3, count($this->dojoData));
  260. $this->assertSame($obj->items[0]->toArray(), $this->dojoData->getItem(1));
  261. $this->assertSame($obj->items[1]->toArray(), $this->dojoData->getItem(2));
  262. $this->assertSame($obj->items[2]->toArray(), $this->dojoData->getItem(3));
  263. }
  264. /**
  265. * @expectedException Zend_Dojo_Exception
  266. */
  267. public function testAddItemsShouldThrowExceptionForInvalidItems()
  268. {
  269. $this->dojoData->addItems('foo');
  270. }
  271. public function testSetItemsShouldOverwriteAllCurrentItems()
  272. {
  273. $this->testAddItemsShouldAcceptArray();
  274. $items = $this->dojoData->getItems();
  275. $obj = new Zend_Dojo_DataTest_DataCollection;
  276. $this->dojoData->setItems($obj);
  277. $this->assertEquals(3, count($this->dojoData));
  278. $this->assertNotSame($items, $this->dojoData->getItems());
  279. }
  280. public function testRemoveItemShouldRemoveItemSpecifiedByIdentifier()
  281. {
  282. $this->testAddItemsShouldAcceptArray();
  283. $this->assertNotNull($this->dojoData->getItem(1));
  284. $this->dojoData->removeItem(1);
  285. $this->assertNull($this->dojoData->getItem(1));
  286. $this->assertEquals(2, count($this->dojoData));
  287. }
  288. public function testClearItemsShouldRemoveAllItems()
  289. {
  290. $this->testAddItemsShouldAcceptArray();
  291. $this->dojoData->clearItems();
  292. $this->assertEquals(0, count($this->dojoData));
  293. }
  294. public function testGetItemShouldReturnNullIfNoMatchingItemExists()
  295. {
  296. $this->assertNull($this->dojoData->getItem('bogus'));
  297. }
  298. public function testGetItemsShouldReturnArrayOfItems()
  299. {
  300. $this->testAddItemsShouldAcceptArray();
  301. $items = $this->dojoData->getItems();
  302. $this->assertTrue(is_array($items));
  303. }
  304. public function testConstructorShouldSetIdentifierItemsAndLabelWhenPassed()
  305. {
  306. $items = array(
  307. array (
  308. 'id' => 1,
  309. 'title' => 'Foo',
  310. 'email' => 'foo@bar',
  311. ),
  312. array (
  313. 'id' => 2,
  314. 'title' => 'Bar',
  315. 'email' => 'bar@bar',
  316. ),
  317. array (
  318. 'id' => 3,
  319. 'title' => 'Baz',
  320. 'email' => 'baz@bar',
  321. ),
  322. );
  323. $data = new Zend_Dojo_Data('id', $items, 'title');
  324. $this->assertEquals('id', $data->getIdentifier());
  325. $this->assertEquals('title', $data->getLabel());
  326. foreach ($items as $item) {
  327. $this->assertTrue($data->hasItem($item['id']));
  328. }
  329. }
  330. public function testShouldSerializeToArray()
  331. {
  332. $this->testAddItemsShouldAcceptTraversableObject();
  333. $array = $this->dojoData->toArray();
  334. $this->assertTrue(is_array($array));
  335. $this->assertTrue(array_key_exists('identifier', $array));
  336. $this->assertEquals($this->dojoData->getIdentifier(), $array['identifier']);
  337. $this->assertEquals(array_values($this->dojoData->getItems()), $array['items']);
  338. }
  339. public function testSerializingToArrayShouldIncludeLabelIfPresent()
  340. {
  341. $this->testShouldSerializeToArray();
  342. $this->dojoData->setLabel('title');
  343. $array = $this->dojoData->toArray();
  344. $this->assertTrue(is_array($array));
  345. $this->assertTrue(array_key_exists('label', $array));
  346. $this->assertEquals($this->dojoData->getLabel(), $array['label']);
  347. }
  348. public function testSerializingToArrayShouldThrowErrorIfNoIdentifierInObject()
  349. {
  350. $this->testAddItemsShouldAcceptTraversableObject();
  351. $this->dojoData->setIdentifier(null);
  352. try {
  353. $array = $this->dojoData->toArray();
  354. $this->fail('Serialization to array should throw error when no identifier is present in object');
  355. } catch (Zend_Dojo_Exception $e) {
  356. $this->assertContains('present', $e->getMessage());
  357. }
  358. }
  359. public function testShouldSerializeToJson()
  360. {
  361. $this->testAddItemsShouldAcceptTraversableObject();
  362. $json = $this->dojoData->toJson();
  363. $this->assertSame($this->dojoData->toArray(), Zend_Json::decode($json));
  364. }
  365. public function testShouldSerializeToStringAsJson()
  366. {
  367. $this->testAddItemsShouldAcceptTraversableObject();
  368. $json = $this->dojoData->toJson();
  369. $this->assertSame($json, $this->dojoData->__toString());
  370. }
  371. public function testShouldImplementArrayAccess()
  372. {
  373. $this->assertTrue($this->dojoData instanceof ArrayAccess);
  374. $this->testAddItemsShouldAcceptTraversableObject();
  375. $this->assertEquals($this->dojoData->getItem(1), $this->dojoData[1]);
  376. $this->dojoData[4] = array(
  377. 'title' => 'ArrayAccess',
  378. 'meta' => 'fun',
  379. );
  380. $this->assertTrue(isset($this->dojoData[4]));
  381. $this->assertEquals($this->dojoData->getItem(4), $this->dojoData[4]);
  382. unset($this->dojoData[4]);
  383. $this->assertFalse(isset($this->dojoData[4]));
  384. }
  385. public function testShouldImplementIterator()
  386. {
  387. $this->assertTrue($this->dojoData instanceof Iterator);
  388. $this->testAddItemsShouldAcceptTraversableObject();
  389. foreach ($this->dojoData as $key => $item) {
  390. $this->assertTrue(is_array($item));
  391. $this->assertEquals($key, $item['id']);
  392. }
  393. }
  394. public function testShouldImplementCountable()
  395. {
  396. $this->assertTrue($this->dojoData instanceof Countable);
  397. }
  398. public function testShouldAllowPopulationFromJson()
  399. {
  400. $data = array(
  401. 'identifier' => 'id',
  402. 'label' => 'title',
  403. 'items' => array(
  404. array('id' => 1, 'title' => 'One', 'name' => 'First'),
  405. array('id' => 2, 'title' => 'Two', 'name' => 'Second'),
  406. array('id' => 3, 'title' => 'Three', 'name' => 'Third'),
  407. array('id' => 4, 'title' => 'Four', 'name' => 'Fourth'),
  408. ),
  409. );
  410. require_once 'Zend/Json.php';
  411. $json = Zend_Json::encode($data);
  412. $dojoData = new Zend_Dojo_Data();
  413. $dojoData->fromJson($json);
  414. $test = $dojoData->toArray();
  415. $this->assertEquals($data, $test);
  416. }
  417. /**
  418. * @expectedException Zend_Dojo_Exception
  419. */
  420. public function testFromJsonShouldThrowExceptionOnInvalidData()
  421. {
  422. $this->dojoData->fromJson(new stdClass);
  423. }
  424. /**
  425. * @group ZF-3841
  426. */
  427. public function testDataContainerShouldAcceptAdditionalMetadataPerKey()
  428. {
  429. $this->assertNull($this->dojoData->getMetadata('numRows'));
  430. $this->dojoData->setMetadata('numRows', 100);
  431. $this->assertEquals(100, $this->dojoData->getMetadata('numRows'));
  432. }
  433. /**
  434. * @group ZF-3841
  435. */
  436. public function testDataContainerShouldAcceptAdditionalMetadataEnMasse()
  437. {
  438. $metadata = $this->dojoData->getMetadata();
  439. $this->assertTrue(is_array($metadata));
  440. $this->assertTrue(empty($metadata));
  441. $metadata = array('numRows' => 100, 'sort' => 'name');
  442. $this->dojoData->setMetadata($metadata);
  443. $test = $this->dojoData->getMetadata();
  444. $this->assertEquals($metadata, $test);
  445. }
  446. /**
  447. * @group ZF-3841
  448. */
  449. public function testDataContainerShouldAllowClearingIndividualMetadataItems()
  450. {
  451. $this->testDataContainerShouldAcceptAdditionalMetadataEnMasse();
  452. $this->dojoData->clearMetadata('numRows');
  453. $metadata = $this->dojoData->getMetadata();
  454. $this->assertEquals(1, count($metadata));
  455. $this->assertFalse(array_key_exists('numRows', $metadata));
  456. $this->assertTrue(array_key_exists('sort', $metadata));
  457. }
  458. /**
  459. * @group ZF-3841
  460. */
  461. public function testDataContainerShouldAllowClearingMetadataEnMasse()
  462. {
  463. $this->testDataContainerShouldAcceptAdditionalMetadataEnMasse();
  464. $this->dojoData->clearMetadata();
  465. $metadata = $this->dojoData->getMetadata();
  466. $this->assertEquals(0, count($metadata));
  467. }
  468. /**
  469. * @group ZF-3841
  470. */
  471. public function testSerializingToArrayShouldIncludeMetadata()
  472. {
  473. $this->testDataContainerShouldAcceptAdditionalMetadataEnMasse();
  474. $this->dojoData->setIdentifier('id');
  475. $array = $this->dojoData->toArray();
  476. $this->assertTrue(array_key_exists('numRows', $array));
  477. $this->assertTrue(array_key_exists('sort', $array));
  478. }
  479. }
  480. class Zend_Dojo_DataTest_DataObject
  481. {
  482. public $item = array(
  483. 'id' => 'foo',
  484. 'title' => 'Foo',
  485. 'email' => 'foo@foo.com',
  486. );
  487. public function toArray()
  488. {
  489. return $this->item;
  490. }
  491. }
  492. class Zend_Dojo_DataTest_DataCollection implements Iterator
  493. {
  494. public $items = array();
  495. public function __construct()
  496. {
  497. for ($i = 1; $i < 4; ++$i) {
  498. $item = new Zend_Dojo_DataTest_DataObject;
  499. $item->item['id'] = $i;
  500. $item->item['title'] .= $i;
  501. $this->items[] = $item;
  502. }
  503. }
  504. public function current()
  505. {
  506. return current($this->items);
  507. }
  508. public function key()
  509. {
  510. return key($this->items);
  511. }
  512. public function next()
  513. {
  514. return next($this->items);
  515. }
  516. public function rewind()
  517. {
  518. return reset($this->items);
  519. }
  520. public function valid()
  521. {
  522. return (bool) $this->current();
  523. }
  524. }
  525. if (PHPUnit_MAIN_METHOD == 'Zend_Dojo_DataTest::main') {
  526. Zend_Dojo_DataTest::main();
  527. }