ArrayCollectionTest.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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_Amf
  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_Amf_Value_ArrayCollectionTest::main');
  24. }
  25. require_once 'Zend/Amf/Value/Messaging/ArrayCollection.php';
  26. /**
  27. * Test case for Zend_Amf_Value_MessageBody
  28. *
  29. * @category Zend
  30. * @package Zend_Amf
  31. * @subpackage UnitTests
  32. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. * @group Zend_Amf
  35. */
  36. class Zend_Amf_Value_ArrayCollectionTest extends PHPUnit_Framework_TestCase
  37. {
  38. /**
  39. * Refrence to the array collection
  40. * @var Zend_Amf_Value_Message_ArrayCollection
  41. */
  42. protected $_arrayCollection;
  43. /**
  44. * Data to be used to populate the ArrayCollection
  45. */
  46. protected $_data;
  47. /**
  48. * Runs the test methods of this class.
  49. *
  50. * @return void
  51. */
  52. public static function main()
  53. {
  54. $suite = new PHPUnit_Framework_TestSuite("Zend_Amf_Value_ArrayCollectionTest");
  55. $result = PHPUnit_TextUI_TestRunner::run($suite);
  56. }
  57. public function setUp()
  58. {
  59. $data = array();
  60. $data[] = array('foo' => 'foo1', 'bar' => 'bar1');
  61. $data[] = array('foo' => 'foo2', 'bar' => 'bar2');
  62. $this->_data = $data;
  63. }
  64. public function tearDown()
  65. {
  66. unset($this->_arrayCollection);
  67. unset($this->_data);
  68. }
  69. public function testConstructorArrayCollectionTwo()
  70. {
  71. $this->_arrayCollection = new Zend_Amf_Value_Messaging_ArrayCollection($this->_data);
  72. $this->assertEquals('bar2', $this->_arrayCollection[1]['bar']);
  73. }
  74. /**
  75. * Check that the ArrayCollection can be accessed like a standard array.
  76. */
  77. public function testConstructorArrayCollection()
  78. {
  79. $this->_arrayCollection = new Zend_Amf_Value_Messaging_ArrayCollection($this->_data);
  80. $this->assertEquals('bar2', $this->_arrayCollection[1]['bar']);
  81. }
  82. /**
  83. * Check that we can get the count of the ArrayCollection
  84. */
  85. public function testCountable()
  86. {
  87. $this->_arrayCollection = new Zend_Amf_Value_Messaging_ArrayCollection($this->_data);
  88. $this->assertEquals(2, count($this->_arrayCollection));
  89. }
  90. /**
  91. * Test that we can foreach through the ArrayCollection
  92. */
  93. public function testIteratorArray()
  94. {
  95. $this->_arrayCollection = new Zend_Amf_Value_Messaging_ArrayCollection($this->_data);
  96. $total = count($this->_arrayCollection);
  97. $count = 0;
  98. foreach($this->_arrayCollection as $row) {
  99. $count++;
  100. }
  101. $this->assertEquals(2, $count);
  102. }
  103. /**
  104. * Test that we can alter an item based on it's offset
  105. */
  106. public function testOffsetExists()
  107. {
  108. $this->_arrayCollection = new Zend_Amf_Value_Messaging_ArrayCollection($this->_data);
  109. $this->assertTrue($this->_arrayCollection->offsetExists(1));
  110. }
  111. /**
  112. * Check that you can set and get the changes to an offset key.
  113. */
  114. public function testOffsetSetGet()
  115. {
  116. $this->_arrayCollection = new Zend_Amf_Value_Messaging_ArrayCollection($this->_data);
  117. $data = array('fooSet' => 'fooSet2', 'barSet' => 'barSet2');
  118. $this->_arrayCollection->offsetSet(1,$data);
  119. $this->assertEquals($data, $this->_arrayCollection->offsetGet(1));
  120. }
  121. /**
  122. * Check that you can delete an item from the arraycollection based on key.
  123. */
  124. public function testOffsetUnset()
  125. {
  126. $this->_arrayCollection = new Zend_Amf_Value_Messaging_ArrayCollection($this->_data);
  127. $data = array('foo' => 'foo1', 'bar' => 'bar1');
  128. $this->assertEquals($data, $this->_arrayCollection->offsetGet(0));
  129. $this->assertEquals(2, count($this->_arrayCollection));
  130. $this->_arrayCollection->offsetUnset(0);
  131. $this->assertEquals(1, count($this->_arrayCollection));
  132. }
  133. /**
  134. * Check that you can transform an ArrayCollection into a standard array with iterator_to_array
  135. */
  136. public function testIteratorToArray()
  137. {
  138. $this->_arrayCollection = new Zend_Amf_Value_Messaging_ArrayCollection($this->_data);
  139. $standardArray = iterator_to_array($this->_arrayCollection);
  140. $this->assertTrue(is_array($standardArray));
  141. }
  142. /**
  143. * Make sure that you can append more name values to the arraycollection
  144. */
  145. public function testAppend()
  146. {
  147. $this->_arrayCollection = new Zend_Amf_Value_Messaging_ArrayCollection($this->_data);
  148. $arrayCollectionTwo = new Zend_Amf_Value_Messaging_ArrayCollection();
  149. $arrayCollectionTwo->append(array('foo' => 'foo1', 'bar' => 'bar1'));
  150. $arrayCollectionTwo->append(array('foo' => 'foo2', 'bar' => 'bar2'));
  151. $this->assertEquals($arrayCollectionTwo, $this->_arrayCollection);
  152. }
  153. /**
  154. * Test to make sure that when the iterator as data it is a valid iterator
  155. *
  156. public function testValid()
  157. {
  158. unset($this->_arrayCollection);
  159. $this->_arrayCollection = new Zend_Amf_Value_Messaging_ArrayCollection();
  160. $this->assertFalse($this->_arrayCollection->valid());
  161. unset($this->_arrayCollection);
  162. $this->_arrayCollection = new Zend_Amf_Value_Messaging_ArrayCollection($this->_data);
  163. $this->assertTrue($this->_arrayCollection->valid());
  164. }
  165. */
  166. /*
  167. public function testArrayIterator()
  168. {
  169. $this->_arrayCollection = new Zend_Amf_Value_Messaging_ArrayCollection($this->_data);
  170. $data0 = array('foo' => 'foo1', 'bar' => 'bar1');
  171. $data1 = array('foo' => 'foo2', 'bar' => 'bar2');
  172. $data3 = array('kung' => 'foo', 'Bruce' => 'Lee');
  173. $this->_arrayCollection->offsetSet(3,$data3);
  174. $this->assertEquals($data0,$this->_arrayCollection->current());
  175. $this->_arrayCollection->next();
  176. $this->assertEquals($data1,$this->_arrayCollection->current());
  177. $this->_arrayCollection->next();
  178. var_dump($this->_arrayCollection->key());
  179. $this->assertEquals($data3,$this->_arrayCollection->current());
  180. $this->_arrayCollection->rewind();
  181. $this->assertEquals($data0,$this->_arrayCollection->current());
  182. }
  183. */
  184. }
  185. class Zend_Amf_Value_ArrayCollectionTest_SerializableData
  186. {
  187. public function __toString()
  188. {
  189. return __CLASS__;
  190. }
  191. }
  192. if (PHPUnit_MAIN_METHOD == 'Zend_Amf_Value_ArrayCollectionTest::main') {
  193. Zend_Amf_Value_ArrayCollectionTest::main();
  194. }