PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/vendor/swiftmailer/swiftmailer/tests/unit/Swift/CharacterStream/ArrayCharacterStreamTest.php

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