ArrayByteStreamTest.php 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. require_once 'Swift/Tests/SwiftUnitTestCase.php';
  3. require_once 'Swift/InputByteStream.php';
  4. require_once 'Swift/ByteStream/ArrayByteStream.php';
  5. class Swift_ByteStream_ArrayByteStreamTest
  6. extends Swift_Tests_SwiftUnitTestCase
  7. {
  8. public function testReadingSingleBytesFromBaseInput()
  9. {
  10. $input = array('a', 'b', 'c');
  11. $bs = $this->_createArrayStream($input);
  12. $output = array();
  13. while (false !== $bytes = $bs->read(1)) {
  14. $output[] = $bytes;
  15. }
  16. $this->assertEqual($input, $output,
  17. '%s: Bytes read from stream should be the same as bytes in constructor'
  18. );
  19. }
  20. public function testReadingMultipleBytesFromBaseInput()
  21. {
  22. $input = array('a', 'b', 'c', 'd');
  23. $bs = $this->_createArrayStream($input);
  24. $output = array();
  25. while (false !== $bytes = $bs->read(2)) {
  26. $output[] = $bytes;
  27. }
  28. $this->assertEqual(array('ab', 'cd'), $output,
  29. '%s: Bytes read from stream should be in pairs'
  30. );
  31. }
  32. public function testReadingOddOffsetOnLastByte()
  33. {
  34. $input = array('a', 'b', 'c', 'd', 'e');
  35. $bs = $this->_createArrayStream($input);
  36. $output = array();
  37. while (false !== $bytes = $bs->read(2)) {
  38. $output[] = $bytes;
  39. }
  40. $this->assertEqual(array('ab', 'cd', 'e'), $output,
  41. '%s: Bytes read from stream should be in pairs except final read'
  42. );
  43. }
  44. public function testSettingPointerPartway()
  45. {
  46. $input = array('a', 'b', 'c');
  47. $bs = $this->_createArrayStream($input);
  48. $bs->setReadPointer(1);
  49. $this->assertEqual('b', $bs->read(1),
  50. '%s: Byte should be second byte since pointer as at offset 1'
  51. );
  52. }
  53. public function testResettingPointerAfterExhaustion()
  54. {
  55. $input = array('a', 'b', 'c');
  56. $bs = $this->_createArrayStream($input);
  57. while (false !== $bs->read(1));
  58. $bs->setReadPointer(0);
  59. $this->assertEqual('a', $bs->read(1),
  60. '%s: Byte should be first byte since pointer as at offset 0'
  61. );
  62. }
  63. public function testPointerNeverSetsBelowZero()
  64. {
  65. $input = array('a', 'b', 'c');
  66. $bs = $this->_createArrayStream($input);
  67. $bs->setReadPointer(-1);
  68. $this->assertEqual('a', $bs->read(1),
  69. '%s: Byte should be first byte since pointer should be at offset 0'
  70. );
  71. }
  72. public function testPointerNeverSetsAboveStackSize()
  73. {
  74. $input = array('a', 'b', 'c');
  75. $bs = $this->_createArrayStream($input);
  76. $bs->setReadPointer(3);
  77. $this->assertIdentical(false, $bs->read(1),
  78. '%s: Stream should be at end and thus return false'
  79. );
  80. }
  81. public function testBytesCanBeWrittenToStream()
  82. {
  83. $input = array('a', 'b', 'c');
  84. $bs = $this->_createArrayStream($input);
  85. $bs->write('de');
  86. $output = array();
  87. while (false !== $bytes = $bs->read(1)) {
  88. $output[] = $bytes;
  89. }
  90. $this->assertEqual(array('a', 'b', 'c', 'd', 'e'), $output,
  91. '%s: Bytes read from stream should be from initial stack + written'
  92. );
  93. }
  94. public function testContentsCanBeFlushed()
  95. {
  96. $input = array('a', 'b', 'c');
  97. $bs = $this->_createArrayStream($input);
  98. $bs->flushBuffers();
  99. $this->assertIdentical(false, $bs->read(1),
  100. '%s: Contents have been flushed so read() should return false'
  101. );
  102. }
  103. public function testConstructorCanTakeStringArgument()
  104. {
  105. $bs = $this->_createArrayStream('abc');
  106. $output = array();
  107. while (false !== $bytes = $bs->read(1)) {
  108. $output[] = $bytes;
  109. }
  110. $this->assertEqual(array('a', 'b', 'c'), $output,
  111. '%s: Bytes read from stream should be the same as bytes in constructor'
  112. );
  113. }
  114. public function testBindingOtherStreamsMirrorsWriteOperations()
  115. {
  116. $bs = $this->_createArrayStream('');
  117. $is1 = $this->_createMockInputStream();
  118. $is2 = $this->_createMockInputStream();
  119. $this->_checking(Expectations::create()
  120. -> one($is1)->write('x')
  121. -> one($is2)->write('x')
  122. -> one($is1)->write('y')
  123. -> one($is2)->write('y')
  124. );
  125. $bs->bind($is1);
  126. $bs->bind($is2);
  127. $bs->write('x');
  128. $bs->write('y');
  129. }
  130. public function testBindingOtherStreamsMirrorsFlushOperations()
  131. {
  132. $bs = $this->_createArrayStream('');
  133. $is1 = $this->_createMockInputStream();
  134. $is2 = $this->_createMockInputStream();
  135. $this->_checking(Expectations::create()
  136. -> one($is1)->flushBuffers()
  137. -> one($is2)->flushBuffers()
  138. );
  139. $bs->bind($is1);
  140. $bs->bind($is2);
  141. $bs->flushBuffers();
  142. }
  143. public function testUnbindingStreamPreventsFurtherWrites()
  144. {
  145. $bs = $this->_createArrayStream('');
  146. $is1 = $this->_createMockInputStream();
  147. $is2 = $this->_createMockInputStream();
  148. $this->_checking(Expectations::create()
  149. -> one($is1)->write('x')
  150. -> one($is2)->write('x')
  151. -> one($is1)->write('y')
  152. );
  153. $bs->bind($is1);
  154. $bs->bind($is2);
  155. $bs->write('x');
  156. $bs->unbind($is2);
  157. $bs->write('y');
  158. }
  159. // -- Creation Methods
  160. private function _createArrayStream($input)
  161. {
  162. return new Swift_ByteStream_ArrayByteStream($input);
  163. }
  164. private function _createMockInputStream()
  165. {
  166. return $this->_mock('Swift_InputByteStream');
  167. }
  168. }