2
0

ArrayCollectionTest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. if (!defined('PHPUnit_MAIN_METHOD')) {
  3. define('PHPUnit_MAIN_METHOD', 'Zend_Amf_Value_ArrayCollectionTest::main');
  4. }
  5. require_once dirname(__FILE__) . '/../../../TestHelper.php';
  6. require_once 'Zend/Amf/Value/Messaging/ArrayCollection.php';
  7. /**
  8. * Test case for Zend_Amf_Value_MessageBody
  9. *
  10. * @package Zend_Amf
  11. * @subpackage UnitTests
  12. * @version $Id: MessageBodyTest.php 12004 2008-10-18 14:29:41Z mikaelkael $
  13. */
  14. class Zend_Amf_Value_ArrayCollectionTest extends PHPUnit_Framework_TestCase
  15. {
  16. /**
  17. * Refrence to the array collection
  18. * @var Zend_Amf_Value_Message_ArrayCollection
  19. */
  20. protected $_arrayCollection;
  21. /**
  22. * Data to be used to populate the ArrayCollection
  23. */
  24. protected $_data;
  25. /**
  26. * Runs the test methods of this class.
  27. *
  28. * @return void
  29. */
  30. public static function main()
  31. {
  32. $suite = new PHPUnit_Framework_TestSuite("Zend_Amf_Value_ArrayCollectionTest");
  33. $result = PHPUnit_TextUI_TestRunner::run($suite);
  34. }
  35. public function setUp()
  36. {
  37. $data = array();
  38. $data[] = array('foo' => 'foo1', 'bar' => 'bar1');
  39. $data[] = array('foo' => 'foo2', 'bar' => 'bar2');
  40. $this->_data = $data;
  41. }
  42. public function tearDown()
  43. {
  44. unset($this->_arrayCollection);
  45. unset($this->_data);
  46. }
  47. public function testConstructorArrayCollectionTwo()
  48. {
  49. $this->_arrayCollection = new Zend_Amf_Value_Messaging_ArrayCollectionTwo($this->_data);
  50. $this->assertEquals('bar2', $this->_arrayCollection[1]['bar']);
  51. }
  52. /**
  53. * Check that the ArrayCollection can be accessed like a standard array.
  54. */
  55. public function testConstructorArrayCollection()
  56. {
  57. $this->_arrayCollection = new Zend_Amf_Value_Messaging_ArrayCollection($this->_data);
  58. $this->assertEquals('bar2', $this->_arrayCollection[1]['bar']);
  59. }
  60. /**
  61. * Check that we can get the count of the ArrayCollection
  62. */
  63. public function testCountable()
  64. {
  65. $this->_arrayCollection = new Zend_Amf_Value_Messaging_ArrayCollection($this->_data);
  66. $this->assertEquals(2, count($this->_arrayCollection));
  67. }
  68. /**
  69. * Test that we can foreach through the ArrayCollection
  70. */
  71. public function testIteratorArray()
  72. {
  73. $this->_arrayCollection = new Zend_Amf_Value_Messaging_ArrayCollection($this->_data);
  74. $total = count($this->_arrayCollection);
  75. $count = 0;
  76. foreach($this->_arrayCollection as $row) {
  77. $count++;
  78. }
  79. $this->assertEquals(2, $count);
  80. }
  81. /**
  82. * Test that we can alter an item based on it's offset
  83. */
  84. public function testOffsetExists()
  85. {
  86. $this->_arrayCollection = new Zend_Amf_Value_Messaging_ArrayCollection($this->_data);
  87. $this->assertTrue($this->_arrayCollection->offsetExists(1));
  88. }
  89. /**
  90. * Check that you can set and get the changes to an offset key.
  91. */
  92. public function testOffsetSetGet()
  93. {
  94. $this->_arrayCollection = new Zend_Amf_Value_Messaging_ArrayCollection($this->_data);
  95. $data = array('fooSet' => 'fooSet2', 'barSet' => 'barSet2');
  96. $this->_arrayCollection->offsetSet(1,$data);
  97. $this->assertEquals($data, $this->_arrayCollection->offsetGet(1));
  98. }
  99. /**
  100. * Check that you can delete an item from the arraycollection based on key.
  101. */
  102. public function testOffsetUnset()
  103. {
  104. $this->_arrayCollection = new Zend_Amf_Value_Messaging_ArrayCollection($this->_data);
  105. $data = array('foo' => 'foo1', 'bar' => 'bar1');
  106. $this->assertEquals($data, $this->_arrayCollection->offsetGet(0));
  107. $this->assertEquals(2, count($this->_arrayCollection));
  108. $this->_arrayCollection->offsetUnset(0);
  109. $this->assertEquals(1, count($this->_arrayCollection));
  110. }
  111. /**
  112. * Check that you can transform an ArrayCollection into a standard array with iterator_to_array
  113. */
  114. public function testIteratorToArray()
  115. {
  116. $this->_arrayCollection = new Zend_Amf_Value_Messaging_ArrayCollection($this->_data);
  117. $standardArray = iterator_to_array($this->_arrayCollection);
  118. $this->assertTrue(is_array($standardArray));
  119. }
  120. /**
  121. * Make sure that you can append more name values to the arraycollection
  122. */
  123. public function testAppend()
  124. {
  125. $this->_arrayCollection = new Zend_Amf_Value_Messaging_ArrayCollection($this->_data);
  126. $arrayCollectionTwo = new Zend_Amf_Value_Messaging_ArrayCollection();
  127. $arrayCollectionTwo->append(array('foo' => 'foo1', 'bar' => 'bar1'));
  128. $arrayCollectionTwo->append(array('foo' => 'foo2', 'bar' => 'bar2'));
  129. $this->assertEquals($arrayCollectionTwo, $this->_arrayCollection);
  130. }
  131. /**
  132. * Test to make sure that when the iterator as data it is a valid iterator
  133. *
  134. public function testValid()
  135. {
  136. unset($this->_arrayCollection);
  137. $this->_arrayCollection = new Zend_Amf_Value_Messaging_ArrayCollection();
  138. $this->assertFalse($this->_arrayCollection->valid());
  139. unset($this->_arrayCollection);
  140. $this->_arrayCollection = new Zend_Amf_Value_Messaging_ArrayCollection($this->_data);
  141. $this->assertTrue($this->_arrayCollection->valid());
  142. }
  143. */
  144. /*
  145. public function testArrayIterator()
  146. {
  147. $this->_arrayCollection = new Zend_Amf_Value_Messaging_ArrayCollection($this->_data);
  148. $data0 = array('foo' => 'foo1', 'bar' => 'bar1');
  149. $data1 = array('foo' => 'foo2', 'bar' => 'bar2');
  150. $data3 = array('kung' => 'foo', 'Bruce' => 'Lee');
  151. $this->_arrayCollection->offsetSet(3,$data3);
  152. $this->assertEquals($data0,$this->_arrayCollection->current());
  153. $this->_arrayCollection->next();
  154. $this->assertEquals($data1,$this->_arrayCollection->current());
  155. $this->_arrayCollection->next();
  156. var_dump($this->_arrayCollection->key());
  157. $this->assertEquals($data3,$this->_arrayCollection->current());
  158. $this->_arrayCollection->rewind();
  159. $this->assertEquals($data0,$this->_arrayCollection->current());
  160. }
  161. */
  162. }
  163. class Zend_Amf_Value_ArrayCollectionTest_SerializableData
  164. {
  165. public function __toString()
  166. {
  167. return __CLASS__;
  168. }
  169. }
  170. if (PHPUnit_MAIN_METHOD == 'Zend_Amf_Value_ArrayCollectionTest::main') {
  171. Zend_Amf_Value_ArrayCollectionTest::main();
  172. }