DataTest.php 18 KB

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