From be58379b79bbfd5010f6a1f07ad8e40e81048572 Mon Sep 17 00:00:00 2001 From: ron Date: Sun, 9 Feb 2014 19:22:43 +0100 Subject: [PATCH] ComplianceTestTest --- src/Kir/Streams/RandomAccessStream.php | 3 +- .../Tests}/Helper/ClosureStreamFactory.php | 4 +- .../Streams/Tests}/Helper/MemoryStream.php | 2 +- .../Helper/NondestructiveReadWriteStream.php | 13 ++++- .../Streams/Tests}/Helper/ResourceStream.php | 27 +++++++--- .../Streams/Tests}/Helper/StreamFactory.php | 4 +- .../Kir/Streams/Tests}/ResourceStreamTest.php | 50 ++++++++++--------- tests.xml | 4 +- .../Tests/RandomAccessStreamTestTest.php | 18 +++++++ 9 files changed, 82 insertions(+), 43 deletions(-) rename {tests/Kir/Streams => src/Kir/Streams/Tests}/Helper/ClosureStreamFactory.php (82%) rename {tests/Kir/Streams => src/Kir/Streams/Tests}/Helper/MemoryStream.php (79%) rename {tests/Kir/Streams => src/Kir/Streams/Tests}/Helper/NondestructiveReadWriteStream.php (88%) rename {tests/Kir/Streams => src/Kir/Streams/Tests}/Helper/ResourceStream.php (84%) rename {tests/Kir/Streams => src/Kir/Streams/Tests}/Helper/StreamFactory.php (56%) rename {tests/Kir/Streams => src/Kir/Streams/Tests}/ResourceStreamTest.php (56%) create mode 100644 tests/Kir/Streams/Tests/RandomAccessStreamTestTest.php diff --git a/src/Kir/Streams/RandomAccessStream.php b/src/Kir/Streams/RandomAccessStream.php index 926b910..7bfd4ca 100644 --- a/src/Kir/Streams/RandomAccessStream.php +++ b/src/Kir/Streams/RandomAccessStream.php @@ -2,5 +2,4 @@ namespace Kir\Streams; interface RandomAccessStream extends InputStream, OutputStream, SeekableStream { - -} \ No newline at end of file +} \ No newline at end of file diff --git a/tests/Kir/Streams/Helper/ClosureStreamFactory.php b/src/Kir/Streams/Tests/Helper/ClosureStreamFactory.php similarity index 82% rename from tests/Kir/Streams/Helper/ClosureStreamFactory.php rename to src/Kir/Streams/Tests/Helper/ClosureStreamFactory.php index 18c4903..d89c3a7 100644 --- a/tests/Kir/Streams/Helper/ClosureStreamFactory.php +++ b/src/Kir/Streams/Tests/Helper/ClosureStreamFactory.php @@ -1,5 +1,5 @@ callback); } } \ No newline at end of file diff --git a/tests/Kir/Streams/Helper/MemoryStream.php b/src/Kir/Streams/Tests/Helper/MemoryStream.php similarity index 79% rename from tests/Kir/Streams/Helper/MemoryStream.php rename to src/Kir/Streams/Tests/Helper/MemoryStream.php index 0d747d3..da0956d 100644 --- a/tests/Kir/Streams/Helper/MemoryStream.php +++ b/src/Kir/Streams/Tests/Helper/MemoryStream.php @@ -1,5 +1,5 @@ stream->getSize(); } + + /** + * @throws IOException + * @return $this + */ + public function truncate() { + $this->stream->truncate(); + } } \ No newline at end of file diff --git a/tests/Kir/Streams/Helper/ResourceStream.php b/src/Kir/Streams/Tests/Helper/ResourceStream.php similarity index 84% rename from tests/Kir/Streams/Helper/ResourceStream.php rename to src/Kir/Streams/Tests/Helper/ResourceStream.php index 164d414..c19e3d8 100644 --- a/tests/Kir/Streams/Helper/ResourceStream.php +++ b/src/Kir/Streams/Tests/Helper/ResourceStream.php @@ -1,11 +1,12 @@ res = fopen($filename, $mode); + $res = fopen($filename, $mode); + $this->setResource($res); } /** @@ -149,11 +151,10 @@ public function getSize() { } /** - * @param int $size * @return $this */ - public function truncate($size = 0) { - ftruncate($this->res, $size); + public function truncate() { + ftruncate($this->res, 0); $this->rewind(); return $this; } @@ -162,7 +163,7 @@ public function truncate($size = 0) { * @return bool */ protected function isSeekable() { - return !!$this->getMetaValue('seekable'); + return !!$this->getMetaValue('seekable', false); } /** @@ -186,4 +187,16 @@ protected function setResource($resource) { private function updateSize() { $this->size = max(ftell($this->res), $this->size); } + + /** + * @param string $string + * @param mixed $default + * @return mixed + */ + private function getMetaValue($string, $default) { + if(array_key_exists($string, $this->meta)) { + return $this->meta[$string]; + } + return $default; + } } diff --git a/tests/Kir/Streams/Helper/StreamFactory.php b/src/Kir/Streams/Tests/Helper/StreamFactory.php similarity index 56% rename from tests/Kir/Streams/Helper/StreamFactory.php rename to src/Kir/Streams/Tests/Helper/StreamFactory.php index d1c38a2..c3ee889 100644 --- a/tests/Kir/Streams/Helper/StreamFactory.php +++ b/src/Kir/Streams/Tests/Helper/StreamFactory.php @@ -1,5 +1,5 @@ factory = $factory; - - $sourceStream = new Helper\ResourceStream(__DIR__.'/../../assets/test-data.txt', 'r'); - $this->referenceResource = new Helper\NondestructiveReadWriteStream($sourceStream); + protected function setUp(Helper\StreamFactory $factory) { + parent::setUp(); + $this->subjectFactory = $factory; } /** */ public function testRead() { - $stream = $this->createStream(); - $data = $stream->read(7); - \PHPUnit_Framework_Assert::assertEquals('This is', $data); + $stream = $this->createSubject(); + $data = $stream->read(26); + \PHPUnit_Framework_Assert::assertEquals('Lorem ipsum dolor sit amet', $data); } /** */ public function testReadAll() { - $stream = $this->createStream(); + $stream = $this->createSubject(); $data = $stream->read(); - \PHPUnit_Framework_Assert::assertEquals('This is a test', $data); + $hash = md5($data); + \PHPUnit_Framework_Assert::assertEquals('cbdd94ceda68ce93d86bc5c84c2537d6', $hash); } /** */ public function testWrite() { - $stream = $this->createStream(); + $stream = $this->createSubject(); + $stream->rewind(); $stream->write('ABCDEFG'); $stream->rewind(); - $data = $stream->read(); + $data = $stream->read(7); \PHPUnit_Framework_Assert::assertEquals('ABCDEFG', $data); } /** */ public function testPositioning() { - $stream = $this->createStream(); + $stream = $this->createSubject(); $stream->write('0987654321'); $stream->setPosition(0); \PHPUnit_Framework_Assert::assertEquals(0, $stream->getPosition(), 'Set position to stream start'); @@ -62,7 +61,7 @@ public function testPositioning() { /** */ public function testTruncate() { - $stream = $this->createStream(); + $stream = $this->createSubject(); $this->truncateStream($stream); $stream->write('This is a test'); @@ -70,6 +69,9 @@ public function testTruncate() { \PHPUnit_Framework_Assert::assertEquals(0, $stream->getPosition()); } + /** + * @param TruncatableStream $stream + */ private function truncateStream(TruncatableStream $stream) { $stream->truncate(); } @@ -77,8 +79,8 @@ private function truncateStream(TruncatableStream $stream) { /** * @return RandomAccessStream */ - private function createStream() { - return $this->factory->getStream(); + private function createSubject() { + return $this->subjectFactory->createStream(); } } \ No newline at end of file diff --git a/tests.xml b/tests.xml index 91de799..ff822df 100644 --- a/tests.xml +++ b/tests.xml @@ -6,9 +6,7 @@ timeoutForSmallTests="5"> - tests/Kir/Streams/Impl/ResourceStreamTest.php - tests/Kir/Streams/Impl/StringStreamTest.php - tests/Kir/Streams/Impl/PhpStreamTest.php + tests/Kir/Streams/Tests/RandomAccessStreamTestTest.php \ No newline at end of file diff --git a/tests/Kir/Streams/Tests/RandomAccessStreamTestTest.php b/tests/Kir/Streams/Tests/RandomAccessStreamTestTest.php new file mode 100644 index 0000000..da4ac36 --- /dev/null +++ b/tests/Kir/Streams/Tests/RandomAccessStreamTestTest.php @@ -0,0 +1,18 @@ +