|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Violet\StreamingJsonEncoder; |
| 4 | + |
| 5 | +use PHPUnit\Framework\TestCase; |
| 6 | + |
| 7 | +/** |
| 8 | + * JsonStreamTest. |
| 9 | + * |
| 10 | + * @author Riikka Kalliomäki <[email protected]> |
| 11 | + * @copyright Copyright (c) 2016, Riikka Kalliomäki |
| 12 | + * @license http://opensource.org/licenses/mit-license.php MIT License |
| 13 | + */ |
| 14 | +class JsonStreamTest extends TestCase |
| 15 | +{ |
| 16 | + public function testExactReads() |
| 17 | + { |
| 18 | + $stream = new JsonStream(['key' => 'value']); |
| 19 | + |
| 20 | + $this->assertSame(false, $stream->eof()); |
| 21 | + $this->assertSame(0, $stream->tell()); |
| 22 | + $this->assertSame('{', $stream->read(1)); |
| 23 | + $this->assertSame(1, $stream->tell()); |
| 24 | + $this->assertSame(false, $stream->eof()); |
| 25 | + $this->assertSame('"key":"value"}', $stream->read(14)); |
| 26 | + $this->assertSame(15, $stream->tell()); |
| 27 | + $this->assertSame(true, $stream->eof()); |
| 28 | + $this->assertSame('', $stream->read(1)); |
| 29 | + } |
| 30 | + |
| 31 | + public function testSeek() |
| 32 | + { |
| 33 | + $stream = new JsonStream(['key' => 'value']); |
| 34 | + |
| 35 | + $stream->seek(8); |
| 36 | + $this->assertSame('value', $stream->read(5)); |
| 37 | + $this->assertSame(13, $stream->tell()); |
| 38 | + |
| 39 | + $stream->seek(-6, SEEK_CUR); |
| 40 | + $this->assertSame('"', $stream->read(1)); |
| 41 | + $this->assertSame(8, $stream->tell()); |
| 42 | + } |
| 43 | + |
| 44 | + public function testReadAfterClose() |
| 45 | + { |
| 46 | + $stream = new JsonStream('value'); |
| 47 | + $stream->close(); |
| 48 | + |
| 49 | + $this->expectException(\RuntimeException::class); |
| 50 | + $stream->read(1); |
| 51 | + } |
| 52 | + |
| 53 | + public function testToString() |
| 54 | + { |
| 55 | + $stream = new JsonStream(['key' => 'value']); |
| 56 | + $this->assertSame('{"key":"value"}', (string) $stream); |
| 57 | + } |
| 58 | + |
| 59 | + public function testToStringAfterClose() |
| 60 | + { |
| 61 | + $stream = new JsonStream('value'); |
| 62 | + $stream->close(); |
| 63 | + $this->assertSame('', (string) $stream); |
| 64 | + } |
| 65 | + |
| 66 | + public function testConstantValueMethods() |
| 67 | + { |
| 68 | + $stream = new JsonStream('value'); |
| 69 | + |
| 70 | + $this->assertSame(null, $stream->detach()); |
| 71 | + $this->assertSame(null, $stream->getSize()); |
| 72 | + $this->assertSame(true, $stream->isSeekable()); |
| 73 | + $this->assertSame(false, $stream->isWritable()); |
| 74 | + $this->assertSame(true, $stream->isReadable()); |
| 75 | + $this->assertSame([], $stream->getMetadata()); |
| 76 | + $this->assertSame(null, $stream->getMetadata('seekable')); |
| 77 | + } |
| 78 | + |
| 79 | + public function testWriting() |
| 80 | + { |
| 81 | + $stream = new JsonStream('value'); |
| 82 | + |
| 83 | + $this->expectException(\RuntimeException::class); |
| 84 | + $stream->write('string'); |
| 85 | + } |
| 86 | + |
| 87 | + public function testSeekFromEnd() |
| 88 | + { |
| 89 | + $stream = new JsonStream('value'); |
| 90 | + |
| 91 | + $this->expectException(\RuntimeException::class); |
| 92 | + $stream->seek(2, SEEK_END); |
| 93 | + } |
| 94 | +} |
0 commit comments