ArrayCharacterStreamTest.php 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. <?php
  2. require_once 'Swift/Tests/SwiftUnitTestCase.php';
  3. require_once 'Swift/CharacterStream/ArrayCharacterStream.php';
  4. require_once 'Swift/CharacterReaderFactory.php';
  5. require_once 'Swift/CharacterReader.php';
  6. require_once 'Swift/OutputByteStream.php';
  7. class Swift_CharacterStream_ArrayCharacterStreamTest
  8. extends Swift_Tests_SwiftUnitTestCase
  9. {
  10. public function testValidatorAlgorithmOnImportString()
  11. {
  12. $reader = $this->_getReader();
  13. $factory = $this->_getFactory($reader);
  14. $stream = new Swift_CharacterStream_ArrayCharacterStream($factory, 'utf-8');
  15. $seq = $this->_mockery()->sequence('read-sequence');
  16. $this->_checking(Expectations::create()
  17. -> ignoring($reader)->getInitialByteSize() -> returns(1)
  18. -> one($reader)->validateByteSequence(array(0xD0), 1) -> inSequence($seq) -> returns(1)
  19. -> one($reader)->validateByteSequence(array(0xD0), 1) -> inSequence($seq) -> returns(1)
  20. -> one($reader)->validateByteSequence(array(0xD0), 1) -> inSequence($seq) -> returns(1)
  21. -> one($reader)->validateByteSequence(array(0xD1), 1) -> inSequence($seq) -> returns(1)
  22. -> one($reader)->validateByteSequence(array(0xD0), 1) -> inSequence($seq) -> returns(1)
  23. -> one($reader)->validateByteSequence(array(0xD0), 1) -> inSequence($seq) -> returns(1)
  24. );
  25. $stream->importString(pack('C*',
  26. 0xD0, 0x94,
  27. 0xD0, 0xB6,
  28. 0xD0, 0xBE,
  29. 0xD1, 0x8D,
  30. 0xD0, 0xBB,
  31. 0xD0, 0xB0
  32. )
  33. );
  34. }
  35. public function testCharactersWrittenUseValidator()
  36. {
  37. $reader = $this->_getReader();
  38. $factory = $this->_getFactory($reader);
  39. $stream = new Swift_CharacterStream_ArrayCharacterStream($factory, 'utf-8');
  40. $seq = $this->_mockery()->sequence('read-sequence');
  41. $this->_checking(Expectations::create()
  42. -> ignoring($reader)->getInitialByteSize() -> returns(1)
  43. -> one($reader)->validateByteSequence(array(0xD0), 1) -> inSequence($seq) -> returns(1)
  44. -> one($reader)->validateByteSequence(array(0xD0), 1) -> inSequence($seq) -> returns(1)
  45. -> one($reader)->validateByteSequence(array(0xD0), 1) -> inSequence($seq) -> returns(1)
  46. -> one($reader)->validateByteSequence(array(0xD0), 1) -> inSequence($seq) -> returns(1)
  47. -> one($reader)->validateByteSequence(array(0xD1), 1) -> inSequence($seq) -> returns(1)
  48. -> one($reader)->validateByteSequence(array(0xD0), 1) -> inSequence($seq) -> returns(1)
  49. -> one($reader)->validateByteSequence(array(0xD1), 1) -> inSequence($seq) -> returns(1)
  50. -> one($reader)->validateByteSequence(array(0xD1), 1) -> inSequence($seq) -> returns(1)
  51. );
  52. $stream->importString(pack('C*', 0xD0, 0x94, 0xD0, 0xB6, 0xD0, 0xBE));
  53. $stream->write(pack('C*',
  54. 0xD0, 0xBB,
  55. 0xD1, 0x8E,
  56. 0xD0, 0xB1,
  57. 0xD1, 0x8B,
  58. 0xD1, 0x85
  59. )
  60. );
  61. }
  62. public function testReadCharactersAreInTact()
  63. {
  64. $reader = $this->_getReader();
  65. $factory = $this->_getFactory($reader);
  66. $stream = new Swift_CharacterStream_ArrayCharacterStream($factory, 'utf-8');
  67. $seq = $this->_mockery()->sequence('read-sequence');
  68. $this->_checking(Expectations::create()
  69. -> ignoring($reader)->getInitialByteSize() -> returns(1)
  70. //String
  71. -> one($reader)->validateByteSequence(array(0xD0), 1) -> inSequence($seq) -> returns(1)
  72. -> one($reader)->validateByteSequence(array(0xD0), 1) -> inSequence($seq) -> returns(1)
  73. -> one($reader)->validateByteSequence(array(0xD0), 1) -> inSequence($seq) -> returns(1)
  74. //Stream
  75. -> one($reader)->validateByteSequence(array(0xD0), 1) -> inSequence($seq) -> returns(1)
  76. -> one($reader)->validateByteSequence(array(0xD1), 1) -> inSequence($seq) -> returns(1)
  77. -> one($reader)->validateByteSequence(array(0xD0), 1) -> inSequence($seq) -> returns(1)
  78. -> one($reader)->validateByteSequence(array(0xD1), 1) -> inSequence($seq) -> returns(1)
  79. -> one($reader)->validateByteSequence(array(0xD1), 1) -> inSequence($seq) -> returns(1)
  80. );
  81. $stream->importString(pack('C*', 0xD0, 0x94, 0xD0, 0xB6, 0xD0, 0xBE));
  82. $stream->write(pack('C*',
  83. 0xD0, 0xBB,
  84. 0xD1, 0x8E,
  85. 0xD0, 0xB1,
  86. 0xD1, 0x8B,
  87. 0xD1, 0x85
  88. )
  89. );
  90. $this->assertIdenticalBinary(pack('C*', 0xD0, 0x94), $stream->read(1));
  91. $this->assertIdenticalBinary(
  92. pack('C*', 0xD0, 0xB6, 0xD0, 0xBE), $stream->read(2)
  93. );
  94. $this->assertIdenticalBinary(pack('C*', 0xD0, 0xBB), $stream->read(1));
  95. $this->assertIdenticalBinary(
  96. pack('C*', 0xD1, 0x8E, 0xD0, 0xB1, 0xD1, 0x8B), $stream->read(3)
  97. );
  98. $this->assertIdenticalBinary(pack('C*', 0xD1, 0x85), $stream->read(1));
  99. $this->assertIdentical(false, $stream->read(1));
  100. }
  101. public function testCharactersCanBeReadAsByteArrays()
  102. {
  103. $reader = $this->_getReader();
  104. $factory = $this->_getFactory($reader);
  105. $stream = new Swift_CharacterStream_ArrayCharacterStream($factory, 'utf-8');
  106. $seq = $this->_mockery()->sequence('read-sequence');
  107. $this->_checking(Expectations::create()
  108. -> ignoring($reader)->getInitialByteSize() -> returns(1)
  109. //String
  110. -> one($reader)->validateByteSequence(array(0xD0), 1) -> inSequence($seq) -> returns(1)
  111. -> one($reader)->validateByteSequence(array(0xD0), 1) -> inSequence($seq) -> returns(1)
  112. -> one($reader)->validateByteSequence(array(0xD0), 1) -> inSequence($seq) -> returns(1)
  113. //Stream
  114. -> one($reader)->validateByteSequence(array(0xD0), 1) -> inSequence($seq) -> returns(1)
  115. -> one($reader)->validateByteSequence(array(0xD1), 1) -> inSequence($seq) -> returns(1)
  116. -> one($reader)->validateByteSequence(array(0xD0), 1) -> inSequence($seq) -> returns(1)
  117. -> one($reader)->validateByteSequence(array(0xD1), 1) -> inSequence($seq) -> returns(1)
  118. -> one($reader)->validateByteSequence(array(0xD1), 1) -> inSequence($seq) -> returns(1)
  119. );
  120. $stream->importString(pack('C*', 0xD0, 0x94, 0xD0, 0xB6, 0xD0, 0xBE));
  121. $stream->write(pack('C*',
  122. 0xD0, 0xBB,
  123. 0xD1, 0x8E,
  124. 0xD0, 0xB1,
  125. 0xD1, 0x8B,
  126. 0xD1, 0x85
  127. )
  128. );
  129. $this->assertEqual(array(0xD0, 0x94), $stream->readBytes(1));
  130. $this->assertEqual(array(0xD0, 0xB6, 0xD0, 0xBE), $stream->readBytes(2));
  131. $this->assertEqual(array(0xD0, 0xBB), $stream->readBytes(1));
  132. $this->assertEqual(
  133. array(0xD1, 0x8E, 0xD0, 0xB1, 0xD1, 0x8B), $stream->readBytes(3)
  134. );
  135. $this->assertEqual(array(0xD1, 0x85), $stream->readBytes(1));
  136. $this->assertIdentical(false, $stream->readBytes(1));
  137. }
  138. public function testRequestingLargeCharCountPastEndOfStream()
  139. {
  140. $reader = $this->_getReader();
  141. $factory = $this->_getFactory($reader);
  142. $stream = new Swift_CharacterStream_ArrayCharacterStream($factory, 'utf-8');
  143. $seq = $this->_mockery()->sequence('read-sequence');
  144. $this->_checking(Expectations::create()
  145. -> ignoring($reader)->getInitialByteSize() -> returns(1)
  146. -> one($reader)->validateByteSequence(array(0xD0), 1) -> inSequence($seq) -> returns(1)
  147. -> one($reader)->validateByteSequence(array(0xD0), 1) -> inSequence($seq) -> returns(1)
  148. -> one($reader)->validateByteSequence(array(0xD0), 1) -> inSequence($seq) -> returns(1)
  149. );
  150. $stream->importString(pack('C*', 0xD0, 0x94, 0xD0, 0xB6, 0xD0, 0xBE));
  151. $this->assertIdenticalBinary(pack('C*', 0xD0, 0x94, 0xD0, 0xB6, 0xD0, 0xBE),
  152. $stream->read(100)
  153. );
  154. $this->assertIdentical(false, $stream->read(1));
  155. }
  156. public function testRequestingByteArrayCountPastEndOfStream()
  157. {
  158. $reader = $this->_getReader();
  159. $factory = $this->_getFactory($reader);
  160. $stream = new Swift_CharacterStream_ArrayCharacterStream($factory, 'utf-8');
  161. $seq = $this->_mockery()->sequence('read-sequence');
  162. $this->_checking(Expectations::create()
  163. -> ignoring($reader)->getInitialByteSize() -> returns(1)
  164. -> one($reader)->validateByteSequence(array(0xD0), 1) -> inSequence($seq) -> returns(1)
  165. -> one($reader)->validateByteSequence(array(0xD0), 1) -> inSequence($seq) -> returns(1)
  166. -> one($reader)->validateByteSequence(array(0xD0), 1) -> inSequence($seq) -> returns(1)
  167. );
  168. $stream->importString(pack('C*', 0xD0, 0x94, 0xD0, 0xB6, 0xD0, 0xBE));
  169. $this->assertEqual(array(0xD0, 0x94, 0xD0, 0xB6, 0xD0, 0xBE),
  170. $stream->readBytes(100)
  171. );
  172. $this->assertIdentical(false, $stream->readBytes(1));
  173. }
  174. public function testPointerOffsetCanBeSet()
  175. {
  176. $reader = $this->_getReader();
  177. $factory = $this->_getFactory($reader);
  178. $stream = new Swift_CharacterStream_ArrayCharacterStream($factory, 'utf-8');
  179. $seq = $this->_mockery()->sequence('read-sequence');
  180. $this->_checking(Expectations::create()
  181. -> ignoring($reader)->getInitialByteSize() -> returns(1)
  182. -> one($reader)->validateByteSequence(array(0xD0), 1) -> inSequence($seq) -> returns(1)
  183. -> one($reader)->validateByteSequence(array(0xD0), 1) -> inSequence($seq) -> returns(1)
  184. -> one($reader)->validateByteSequence(array(0xD0), 1) -> inSequence($seq) -> returns(1)
  185. );
  186. $stream->importString(pack('C*', 0xD0, 0x94, 0xD0, 0xB6, 0xD0, 0xBE));
  187. $this->assertIdenticalBinary(pack('C*', 0xD0, 0x94), $stream->read(1));
  188. $stream->setPointer(0);
  189. $this->assertIdenticalBinary(pack('C*', 0xD0, 0x94), $stream->read(1));
  190. $stream->setPointer(2);
  191. $this->assertIdenticalBinary(pack('C*', 0xD0, 0xBE), $stream->read(1));
  192. }
  193. public function testContentsCanBeFlushed()
  194. {
  195. $reader = $this->_getReader();
  196. $factory = $this->_getFactory($reader);
  197. $stream = new Swift_CharacterStream_ArrayCharacterStream($factory, 'utf-8');
  198. $seq = $this->_mockery()->sequence('read-sequence');
  199. $this->_checking(Expectations::create()
  200. -> ignoring($reader)->getInitialByteSize() -> returns(1)
  201. -> one($reader)->validateByteSequence(array(0xD0), 1) -> inSequence($seq) -> returns(1)
  202. -> one($reader)->validateByteSequence(array(0xD0), 1) -> inSequence($seq) -> returns(1)
  203. -> one($reader)->validateByteSequence(array(0xD0), 1) -> inSequence($seq) -> returns(1)
  204. );
  205. $stream->importString(pack('C*', 0xD0, 0x94, 0xD0, 0xB6, 0xD0, 0xBE));
  206. $stream->flushContents();
  207. $this->assertIdentical(false, $stream->read(1));
  208. }
  209. public function testByteStreamCanBeImportingUsesValidator()
  210. {
  211. $reader = $this->_getReader();
  212. $factory = $this->_getFactory($reader);
  213. $os = $this->_getByteStream();
  214. $stream = new Swift_CharacterStream_ArrayCharacterStream($factory, 'utf-8');
  215. $seq = $this->_mockery()->sequence('read-stream');
  216. $this->_checking(Expectations::create()
  217. -> between(0,1)->of($os)->setReadPointer(0)
  218. -> one($os)->read(any()) -> inSequence($seq) -> returns(pack('C*', 0xD0))
  219. -> one($os)->read(any()) -> inSequence($seq) -> returns(pack('C*', 0x94))
  220. -> one($os)->read(any()) -> inSequence($seq) -> returns(pack('C*', 0xD0))
  221. -> one($os)->read(any()) -> inSequence($seq) -> returns(pack('C*', 0xB6))
  222. -> one($os)->read(any()) -> inSequence($seq) -> returns(pack('C*', 0xD0))
  223. -> one($os)->read(any()) -> inSequence($seq) -> returns(pack('C*', 0xBE))
  224. -> ignoring($os)->read(any()) -> returns(false)
  225. );
  226. $seq = $this->_mockery()->sequence('read-chars');
  227. $this->_checking(Expectations::create()
  228. -> ignoring($reader)->getInitialByteSize() -> returns(1)
  229. -> one($reader)->validateByteSequence(array(0xD0), 1) -> inSequence($seq) -> returns(1)
  230. -> one($reader)->validateByteSequence(array(0xD0), 1) -> inSequence($seq) -> returns(1)
  231. -> one($reader)->validateByteSequence(array(0xD0), 1) -> inSequence($seq) -> returns(1)
  232. );
  233. $stream->importByteStream($os);
  234. }
  235. public function testImportingStreamProducesCorrectCharArray()
  236. {
  237. $reader = $this->_getReader();
  238. $factory = $this->_getFactory($reader);
  239. $os = $this->_getByteStream();
  240. $stream = new Swift_CharacterStream_ArrayCharacterStream($factory, 'utf-8');
  241. $seq = $this->_mockery()->sequence('read-stream');
  242. $this->_checking(Expectations::create()
  243. -> between(0,1)->of($os)->setReadPointer(0)
  244. -> one($os)->read(any()) -> inSequence($seq) -> returns(pack('C*', 0xD0))
  245. -> one($os)->read(any()) -> inSequence($seq) -> returns(pack('C*', 0x94))
  246. -> one($os)->read(any()) -> inSequence($seq) -> returns(pack('C*', 0xD0))
  247. -> one($os)->read(any()) -> inSequence($seq) -> returns(pack('C*', 0xB6))
  248. -> one($os)->read(any()) -> inSequence($seq) -> returns(pack('C*', 0xD0))
  249. -> one($os)->read(any()) -> inSequence($seq) -> returns(pack('C*', 0xBE))
  250. -> ignoring($os)->read(any()) -> returns(false)
  251. );
  252. $seq = $this->_mockery()->sequence('read-chars');
  253. $this->_checking(Expectations::create()
  254. -> ignoring($reader)->getInitialByteSize() -> returns(1)
  255. -> one($reader)->validateByteSequence(array(0xD0), 1) -> inSequence($seq) -> returns(1)
  256. -> one($reader)->validateByteSequence(array(0xD0), 1) -> inSequence($seq) -> returns(1)
  257. -> one($reader)->validateByteSequence(array(0xD0), 1) -> inSequence($seq) -> returns(1)
  258. );
  259. $stream->importByteStream($os);
  260. $this->assertIdenticalBinary(pack('C*', 0xD0, 0x94), $stream->read(1));
  261. $this->assertIdenticalBinary(pack('C*', 0xD0, 0xB6), $stream->read(1));
  262. $this->assertIdenticalBinary(pack('C*', 0xD0, 0xBE), $stream->read(1));
  263. $this->assertIdentical(false, $stream->read(1));
  264. }
  265. public function testAlgorithmWithFixedWidthCharsets()
  266. {
  267. $reader = $this->_getReader();
  268. $factory = $this->_getFactory($reader);
  269. $seq = $this->_mockery()->sequence('read-chars');
  270. $this->_checking(Expectations::create()
  271. -> ignoring($reader)->getInitialByteSize() -> returns(2)
  272. -> one($reader)->validateByteSequence(array(0xD1, 0x8D), 2) -> inSequence($seq)
  273. -> one($reader)->validateByteSequence(array(0xD0, 0xBB), 2) -> inSequence($seq)
  274. -> one($reader)->validateByteSequence(array(0xD0, 0xB0), 2) -> inSequence($seq)
  275. );
  276. $stream = new Swift_CharacterStream_ArrayCharacterStream(
  277. $factory, 'utf-8'
  278. );
  279. $stream->importString(pack('C*', 0xD1, 0x8D, 0xD0, 0xBB, 0xD0, 0xB0));
  280. $this->assertIdenticalBinary(pack('C*', 0xD1, 0x8D), $stream->read(1));
  281. $this->assertIdenticalBinary(pack('C*', 0xD0, 0xBB), $stream->read(1));
  282. $this->assertIdenticalBinary(pack('C*', 0xD0, 0xB0), $stream->read(1));
  283. $this->assertIdentical(false, $stream->read(1));
  284. }
  285. // -- Creation methods
  286. private function _getReader()
  287. {
  288. return $this->_mock('Swift_CharacterReader');
  289. }
  290. private function _getFactory($reader)
  291. {
  292. $factory = $this->_mock('Swift_CharacterReaderFactory');
  293. $this->_checking(Expectations::create()
  294. -> allowing($factory)->getReaderFor('utf-8') -> returns($reader)
  295. );
  296. return $factory;
  297. }
  298. private function _getByteStream()
  299. {
  300. return $this->_mock('Swift_OutputByteStream');
  301. }
  302. }