2
0

DataTest.php 18 KB

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