123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377 |
- <?php
-
- require_once 'Swift/Tests/SwiftUnitTestCase.php';
- require_once 'Swift/CharacterStream/ArrayCharacterStream.php';
- require_once 'Swift/CharacterReaderFactory.php';
- require_once 'Swift/CharacterReader.php';
- require_once 'Swift/OutputByteStream.php';
-
- class Swift_CharacterStream_ArrayCharacterStreamTest
- extends Swift_Tests_SwiftUnitTestCase
- {
-
- public function testValidatorAlgorithmOnImportString()
- {
- $reader = $this->_getReader();
- $factory = $this->_getFactory($reader);
-
- $stream = new Swift_CharacterStream_ArrayCharacterStream($factory, 'utf-8');
-
- $seq = $this->_mockery()->sequence('read-sequence');
- $this->_checking(Expectations::create()
- -> ignoring($reader)->getInitialByteSize() -> returns(1)
- -> one($reader)->validateByteSequence(array(0xD0), 1) -> inSequence($seq) -> returns(1)
- -> one($reader)->validateByteSequence(array(0xD0), 1) -> inSequence($seq) -> returns(1)
- -> one($reader)->validateByteSequence(array(0xD0), 1) -> inSequence($seq) -> returns(1)
- -> one($reader)->validateByteSequence(array(0xD1), 1) -> inSequence($seq) -> returns(1)
- -> one($reader)->validateByteSequence(array(0xD0), 1) -> inSequence($seq) -> returns(1)
- -> one($reader)->validateByteSequence(array(0xD0), 1) -> inSequence($seq) -> returns(1)
- );
-
- $stream->importString(pack('C*',
- 0xD0, 0x94,
- 0xD0, 0xB6,
- 0xD0, 0xBE,
- 0xD1, 0x8D,
- 0xD0, 0xBB,
- 0xD0, 0xB0
- )
- );
- }
-
- public function testCharactersWrittenUseValidator()
- {
- $reader = $this->_getReader();
- $factory = $this->_getFactory($reader);
-
- $stream = new Swift_CharacterStream_ArrayCharacterStream($factory, 'utf-8');
-
- $seq = $this->_mockery()->sequence('read-sequence');
- $this->_checking(Expectations::create()
- -> ignoring($reader)->getInitialByteSize() -> returns(1)
- -> one($reader)->validateByteSequence(array(0xD0), 1) -> inSequence($seq) -> returns(1)
- -> one($reader)->validateByteSequence(array(0xD0), 1) -> inSequence($seq) -> returns(1)
- -> one($reader)->validateByteSequence(array(0xD0), 1) -> inSequence($seq) -> returns(1)
- -> one($reader)->validateByteSequence(array(0xD0), 1) -> inSequence($seq) -> returns(1)
- -> one($reader)->validateByteSequence(array(0xD1), 1) -> inSequence($seq) -> returns(1)
- -> one($reader)->validateByteSequence(array(0xD0), 1) -> inSequence($seq) -> returns(1)
- -> one($reader)->validateByteSequence(array(0xD1), 1) -> inSequence($seq) -> returns(1)
- -> one($reader)->validateByteSequence(array(0xD1), 1) -> inSequence($seq) -> returns(1)
- );
-
- $stream->importString(pack('C*', 0xD0, 0x94, 0xD0, 0xB6, 0xD0, 0xBE));
-
- $stream->write(pack('C*',
- 0xD0, 0xBB,
- 0xD1, 0x8E,
- 0xD0, 0xB1,
- 0xD1, 0x8B,
- 0xD1, 0x85
- )
- );
- }
-
- public function testReadCharactersAreInTact()
- {
- $reader = $this->_getReader();
- $factory = $this->_getFactory($reader);
-
- $stream = new Swift_CharacterStream_ArrayCharacterStream($factory, 'utf-8');
-
- $seq = $this->_mockery()->sequence('read-sequence');
- $this->_checking(Expectations::create()
- -> ignoring($reader)->getInitialByteSize() -> returns(1)
- //String
- -> one($reader)->validateByteSequence(array(0xD0), 1) -> inSequence($seq) -> returns(1)
- -> one($reader)->validateByteSequence(array(0xD0), 1) -> inSequence($seq) -> returns(1)
- -> one($reader)->validateByteSequence(array(0xD0), 1) -> inSequence($seq) -> returns(1)
- //Stream
- -> one($reader)->validateByteSequence(array(0xD0), 1) -> inSequence($seq) -> returns(1)
- -> one($reader)->validateByteSequence(array(0xD1), 1) -> inSequence($seq) -> returns(1)
- -> one($reader)->validateByteSequence(array(0xD0), 1) -> inSequence($seq) -> returns(1)
- -> one($reader)->validateByteSequence(array(0xD1), 1) -> inSequence($seq) -> returns(1)
- -> one($reader)->validateByteSequence(array(0xD1), 1) -> inSequence($seq) -> returns(1)
- );
-
- $stream->importString(pack('C*', 0xD0, 0x94, 0xD0, 0xB6, 0xD0, 0xBE));
-
- $stream->write(pack('C*',
- 0xD0, 0xBB,
- 0xD1, 0x8E,
- 0xD0, 0xB1,
- 0xD1, 0x8B,
- 0xD1, 0x85
- )
- );
-
- $this->assertIdenticalBinary(pack('C*', 0xD0, 0x94), $stream->read(1));
- $this->assertIdenticalBinary(
- pack('C*', 0xD0, 0xB6, 0xD0, 0xBE), $stream->read(2)
- );
- $this->assertIdenticalBinary(pack('C*', 0xD0, 0xBB), $stream->read(1));
- $this->assertIdenticalBinary(
- pack('C*', 0xD1, 0x8E, 0xD0, 0xB1, 0xD1, 0x8B), $stream->read(3)
- );
- $this->assertIdenticalBinary(pack('C*', 0xD1, 0x85), $stream->read(1));
-
- $this->assertIdentical(false, $stream->read(1));
- }
-
- public function testCharactersCanBeReadAsByteArrays()
- {
- $reader = $this->_getReader();
- $factory = $this->_getFactory($reader);
-
- $stream = new Swift_CharacterStream_ArrayCharacterStream($factory, 'utf-8');
-
- $seq = $this->_mockery()->sequence('read-sequence');
- $this->_checking(Expectations::create()
- -> ignoring($reader)->getInitialByteSize() -> returns(1)
- //String
- -> one($reader)->validateByteSequence(array(0xD0), 1) -> inSequence($seq) -> returns(1)
- -> one($reader)->validateByteSequence(array(0xD0), 1) -> inSequence($seq) -> returns(1)
- -> one($reader)->validateByteSequence(array(0xD0), 1) -> inSequence($seq) -> returns(1)
- //Stream
- -> one($reader)->validateByteSequence(array(0xD0), 1) -> inSequence($seq) -> returns(1)
- -> one($reader)->validateByteSequence(array(0xD1), 1) -> inSequence($seq) -> returns(1)
- -> one($reader)->validateByteSequence(array(0xD0), 1) -> inSequence($seq) -> returns(1)
- -> one($reader)->validateByteSequence(array(0xD1), 1) -> inSequence($seq) -> returns(1)
- -> one($reader)->validateByteSequence(array(0xD1), 1) -> inSequence($seq) -> returns(1)
- );
-
- $stream->importString(pack('C*', 0xD0, 0x94, 0xD0, 0xB6, 0xD0, 0xBE));
-
- $stream->write(pack('C*',
- 0xD0, 0xBB,
- 0xD1, 0x8E,
- 0xD0, 0xB1,
- 0xD1, 0x8B,
- 0xD1, 0x85
- )
- );
-
- $this->assertEqual(array(0xD0, 0x94), $stream->readBytes(1));
- $this->assertEqual(array(0xD0, 0xB6, 0xD0, 0xBE), $stream->readBytes(2));
- $this->assertEqual(array(0xD0, 0xBB), $stream->readBytes(1));
- $this->assertEqual(
- array(0xD1, 0x8E, 0xD0, 0xB1, 0xD1, 0x8B), $stream->readBytes(3)
- );
- $this->assertEqual(array(0xD1, 0x85), $stream->readBytes(1));
-
- $this->assertIdentical(false, $stream->readBytes(1));
- }
-
- public function testRequestingLargeCharCountPastEndOfStream()
- {
- $reader = $this->_getReader();
- $factory = $this->_getFactory($reader);
-
- $stream = new Swift_CharacterStream_ArrayCharacterStream($factory, 'utf-8');
-
- $seq = $this->_mockery()->sequence('read-sequence');
- $this->_checking(Expectations::create()
- -> ignoring($reader)->getInitialByteSize() -> returns(1)
- -> one($reader)->validateByteSequence(array(0xD0), 1) -> inSequence($seq) -> returns(1)
- -> one($reader)->validateByteSequence(array(0xD0), 1) -> inSequence($seq) -> returns(1)
- -> one($reader)->validateByteSequence(array(0xD0), 1) -> inSequence($seq) -> returns(1)
- );
-
- $stream->importString(pack('C*', 0xD0, 0x94, 0xD0, 0xB6, 0xD0, 0xBE));
-
- $this->assertIdenticalBinary(pack('C*', 0xD0, 0x94, 0xD0, 0xB6, 0xD0, 0xBE),
- $stream->read(100)
- );
-
- $this->assertIdentical(false, $stream->read(1));
- }
-
- public function testRequestingByteArrayCountPastEndOfStream()
- {
- $reader = $this->_getReader();
- $factory = $this->_getFactory($reader);
-
- $stream = new Swift_CharacterStream_ArrayCharacterStream($factory, 'utf-8');
-
- $seq = $this->_mockery()->sequence('read-sequence');
- $this->_checking(Expectations::create()
- -> ignoring($reader)->getInitialByteSize() -> returns(1)
- -> one($reader)->validateByteSequence(array(0xD0), 1) -> inSequence($seq) -> returns(1)
- -> one($reader)->validateByteSequence(array(0xD0), 1) -> inSequence($seq) -> returns(1)
- -> one($reader)->validateByteSequence(array(0xD0), 1) -> inSequence($seq) -> returns(1)
- );
-
- $stream->importString(pack('C*', 0xD0, 0x94, 0xD0, 0xB6, 0xD0, 0xBE));
-
- $this->assertEqual(array(0xD0, 0x94, 0xD0, 0xB6, 0xD0, 0xBE),
- $stream->readBytes(100)
- );
-
- $this->assertIdentical(false, $stream->readBytes(1));
- }
-
- public function testPointerOffsetCanBeSet()
- {
- $reader = $this->_getReader();
- $factory = $this->_getFactory($reader);
-
- $stream = new Swift_CharacterStream_ArrayCharacterStream($factory, 'utf-8');
-
- $seq = $this->_mockery()->sequence('read-sequence');
- $this->_checking(Expectations::create()
- -> ignoring($reader)->getInitialByteSize() -> returns(1)
- -> one($reader)->validateByteSequence(array(0xD0), 1) -> inSequence($seq) -> returns(1)
- -> one($reader)->validateByteSequence(array(0xD0), 1) -> inSequence($seq) -> returns(1)
- -> one($reader)->validateByteSequence(array(0xD0), 1) -> inSequence($seq) -> returns(1)
- );
-
- $stream->importString(pack('C*', 0xD0, 0x94, 0xD0, 0xB6, 0xD0, 0xBE));
-
- $this->assertIdenticalBinary(pack('C*', 0xD0, 0x94), $stream->read(1));
-
- $stream->setPointer(0);
-
- $this->assertIdenticalBinary(pack('C*', 0xD0, 0x94), $stream->read(1));
-
- $stream->setPointer(2);
-
- $this->assertIdenticalBinary(pack('C*', 0xD0, 0xBE), $stream->read(1));
- }
-
- public function testContentsCanBeFlushed()
- {
- $reader = $this->_getReader();
- $factory = $this->_getFactory($reader);
-
- $stream = new Swift_CharacterStream_ArrayCharacterStream($factory, 'utf-8');
-
- $seq = $this->_mockery()->sequence('read-sequence');
- $this->_checking(Expectations::create()
- -> ignoring($reader)->getInitialByteSize() -> returns(1)
- -> one($reader)->validateByteSequence(array(0xD0), 1) -> inSequence($seq) -> returns(1)
- -> one($reader)->validateByteSequence(array(0xD0), 1) -> inSequence($seq) -> returns(1)
- -> one($reader)->validateByteSequence(array(0xD0), 1) -> inSequence($seq) -> returns(1)
- );
-
- $stream->importString(pack('C*', 0xD0, 0x94, 0xD0, 0xB6, 0xD0, 0xBE));
-
- $stream->flushContents();
-
- $this->assertIdentical(false, $stream->read(1));
- }
-
- public function testByteStreamCanBeImportingUsesValidator()
- {
- $reader = $this->_getReader();
- $factory = $this->_getFactory($reader);
- $os = $this->_getByteStream();
-
- $stream = new Swift_CharacterStream_ArrayCharacterStream($factory, 'utf-8');
-
- $seq = $this->_mockery()->sequence('read-stream');
- $this->_checking(Expectations::create()
- -> between(0,1)->of($os)->setReadPointer(0)
- -> one($os)->read(any()) -> inSequence($seq) -> returns(pack('C*', 0xD0))
- -> one($os)->read(any()) -> inSequence($seq) -> returns(pack('C*', 0x94))
- -> one($os)->read(any()) -> inSequence($seq) -> returns(pack('C*', 0xD0))
- -> one($os)->read(any()) -> inSequence($seq) -> returns(pack('C*', 0xB6))
- -> one($os)->read(any()) -> inSequence($seq) -> returns(pack('C*', 0xD0))
- -> one($os)->read(any()) -> inSequence($seq) -> returns(pack('C*', 0xBE))
- -> ignoring($os)->read(any()) -> returns(false)
- );
-
- $seq = $this->_mockery()->sequence('read-chars');
- $this->_checking(Expectations::create()
- -> ignoring($reader)->getInitialByteSize() -> returns(1)
- -> one($reader)->validateByteSequence(array(0xD0), 1) -> inSequence($seq) -> returns(1)
- -> one($reader)->validateByteSequence(array(0xD0), 1) -> inSequence($seq) -> returns(1)
- -> one($reader)->validateByteSequence(array(0xD0), 1) -> inSequence($seq) -> returns(1)
- );
-
- $stream->importByteStream($os);
- }
-
- public function testImportingStreamProducesCorrectCharArray()
- {
- $reader = $this->_getReader();
- $factory = $this->_getFactory($reader);
- $os = $this->_getByteStream();
-
- $stream = new Swift_CharacterStream_ArrayCharacterStream($factory, 'utf-8');
-
- $seq = $this->_mockery()->sequence('read-stream');
- $this->_checking(Expectations::create()
- -> between(0,1)->of($os)->setReadPointer(0)
- -> one($os)->read(any()) -> inSequence($seq) -> returns(pack('C*', 0xD0))
- -> one($os)->read(any()) -> inSequence($seq) -> returns(pack('C*', 0x94))
- -> one($os)->read(any()) -> inSequence($seq) -> returns(pack('C*', 0xD0))
- -> one($os)->read(any()) -> inSequence($seq) -> returns(pack('C*', 0xB6))
- -> one($os)->read(any()) -> inSequence($seq) -> returns(pack('C*', 0xD0))
- -> one($os)->read(any()) -> inSequence($seq) -> returns(pack('C*', 0xBE))
- -> ignoring($os)->read(any()) -> returns(false)
- );
-
- $seq = $this->_mockery()->sequence('read-chars');
- $this->_checking(Expectations::create()
- -> ignoring($reader)->getInitialByteSize() -> returns(1)
- -> one($reader)->validateByteSequence(array(0xD0), 1) -> inSequence($seq) -> returns(1)
- -> one($reader)->validateByteSequence(array(0xD0), 1) -> inSequence($seq) -> returns(1)
- -> one($reader)->validateByteSequence(array(0xD0), 1) -> inSequence($seq) -> returns(1)
- );
-
- $stream->importByteStream($os);
-
- $this->assertIdenticalBinary(pack('C*', 0xD0, 0x94), $stream->read(1));
- $this->assertIdenticalBinary(pack('C*', 0xD0, 0xB6), $stream->read(1));
- $this->assertIdenticalBinary(pack('C*', 0xD0, 0xBE), $stream->read(1));
-
- $this->assertIdentical(false, $stream->read(1));
- }
-
- public function testAlgorithmWithFixedWidthCharsets()
- {
- $reader = $this->_getReader();
- $factory = $this->_getFactory($reader);
-
- $seq = $this->_mockery()->sequence('read-chars');
- $this->_checking(Expectations::create()
- -> ignoring($reader)->getInitialByteSize() -> returns(2)
- -> one($reader)->validateByteSequence(array(0xD1, 0x8D), 2) -> inSequence($seq)
- -> one($reader)->validateByteSequence(array(0xD0, 0xBB), 2) -> inSequence($seq)
- -> one($reader)->validateByteSequence(array(0xD0, 0xB0), 2) -> inSequence($seq)
- );
-
- $stream = new Swift_CharacterStream_ArrayCharacterStream(
- $factory, 'utf-8'
- );
- $stream->importString(pack('C*', 0xD1, 0x8D, 0xD0, 0xBB, 0xD0, 0xB0));
-
- $this->assertIdenticalBinary(pack('C*', 0xD1, 0x8D), $stream->read(1));
- $this->assertIdenticalBinary(pack('C*', 0xD0, 0xBB), $stream->read(1));
- $this->assertIdenticalBinary(pack('C*', 0xD0, 0xB0), $stream->read(1));
-
- $this->assertIdentical(false, $stream->read(1));
- }
-
- // -- Creation methods
-
- private function _getReader()
- {
- return $this->_mock('Swift_CharacterReader');
- }
-
- private function _getFactory($reader)
- {
- $factory = $this->_mock('Swift_CharacterReaderFactory');
- $this->_checking(Expectations::create()
- -> allowing($factory)->getReaderFor('utf-8') -> returns($reader)
- );
- return $factory;
- }
-
- private function _getByteStream()
- {
- return $this->_mock('Swift_OutputByteStream');
- }
-
- }
|