ArrayCollectionTest.php 7.1 KB

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