ArrayByteStreamTest.php 5.2KB

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