Skip to content

Commit

Permalink
ComplianceTestTest
Browse files Browse the repository at this point in the history
  • Loading branch information
rkrx committed Feb 9, 2014
1 parent f520a7d commit be58379
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 43 deletions.
3 changes: 1 addition & 2 deletions src/Kir/Streams/RandomAccessStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@
namespace Kir\Streams;

interface RandomAccessStream extends InputStream, OutputStream, SeekableStream {

}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace Kir\Streams\Helper;
namespace Kir\Streams\Tests\Helper;

use Closure;
use Kir\Streams\Stream;
Expand All @@ -19,7 +19,7 @@ public function __construct(Closure $callback) {
/**
* @return Stream
*/
public function getStream() {
public function createStream() {
return call_user_func($this->callback);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace Kir\Streams\Helper;
namespace Kir\Streams\Tests\Helper;

class MemoryStream extends ResourceStream {
/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?php
namespace Kir\Streams\Helper;
namespace Kir\Streams\Tests\Helper;

use Kir\Streams\Exceptions\IOException;
use Kir\Streams\RandomAccessStream;
use Kir\Streams\TruncatableStream;

class NondestructiveReadWriteStream implements RandomAccessStream {
class NondestructiveReadWriteStream implements RandomAccessStream, TruncatableStream {
/**
* @var RandomAccessStream
*/
Expand Down Expand Up @@ -89,4 +90,12 @@ public function rewind() {
public function getSize() {
return $this->stream->getSize();
}

/**
* @throws IOException
* @return $this
*/
public function truncate() {
$this->stream->truncate();
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<?php
namespace Kir\Streams\Helper;
namespace Kir\Streams\Tests\Helper;

use Kir\Streams\Exceptions\InvalidStreamOperationException;
use Kir\Streams\Exceptions\IOException;
use Kir\Streams\RandomAccessStream;
use Kir\Streams\TruncatableStream;

class ResourceStream implements RandomAccessStream {
class ResourceStream implements RandomAccessStream, TruncatableStream {
/**
* @var resource
*/
Expand All @@ -26,7 +27,8 @@ class ResourceStream implements RandomAccessStream {
* @param string $mode
*/
public function __construct($filename, $mode) {
$this->res = fopen($filename, $mode);
$res = fopen($filename, $mode);
$this->setResource($res);
}

/**
Expand Down Expand Up @@ -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;
}
Expand All @@ -162,7 +163,7 @@ public function truncate($size = 0) {
* @return bool
*/
protected function isSeekable() {
return !!$this->getMetaValue('seekable');
return !!$this->getMetaValue('seekable', false);
}

/**
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php
namespace Kir\Streams\Helper;
namespace Kir\Streams\Tests\Helper;

use Kir\Streams\Stream;

interface StreamFactory {
/**
* @return Stream
*/
public function getStream();
public function createStream();
}
Original file line number Diff line number Diff line change
@@ -1,56 +1,55 @@
<?php
namespace Kir\Streams;
namespace Kir\Streams\Tests;

class ResourceStreamTest {
/**
* @var Helper\StreamFactory
*/
private $factory = null;
use Kir\Streams\Tests\Helper;
use Kir\Streams\RandomAccessStream;
use Kir\Streams\TruncatableStream;

class ResourceStreamTest extends \PHPUnit_Framework_TestCase {
/**
* @var Helper\NondestructiveReadWriteStream
* @var Helper\StreamFactory
*/
private $referenceResource;
private $subjectFactory = null;

/**
*/
public function __construct(Helper\StreamFactory $factory) {
$this->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');
Expand All @@ -62,23 +61,26 @@ public function testPositioning() {
/**
*/
public function testTruncate() {
$stream = $this->createStream();
$stream = $this->createSubject();

$this->truncateStream($stream);
$stream->write('This is a test');
$this->truncateStream($stream);
\PHPUnit_Framework_Assert::assertEquals(0, $stream->getPosition());
}

/**
* @param TruncatableStream $stream
*/
private function truncateStream(TruncatableStream $stream) {
$stream->truncate();
}

/**
* @return RandomAccessStream
*/
private function createStream() {
return $this->factory->getStream();
private function createSubject() {
return $this->subjectFactory->createStream();
}
}

4 changes: 1 addition & 3 deletions tests.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
timeoutForSmallTests="5">
<testsuites>
<testsuite name="Streams">
<file>tests/Kir/Streams/Impl/ResourceStreamTest.php</file>
<file>tests/Kir/Streams/Impl/StringStreamTest.php</file>
<file>tests/Kir/Streams/Impl/PhpStreamTest.php</file>
<file>tests/Kir/Streams/Tests/RandomAccessStreamTestTest.php</file>
</testsuite>
</testsuites>
</phpunit>
18 changes: 18 additions & 0 deletions tests/Kir/Streams/Tests/RandomAccessStreamTestTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
namespace Kir\Streams\Tests;

use Kir\Streams\Tests\Helper\ClosureStreamFactory;
use Kir\Streams\Tests\Helper\NondestructiveReadWriteStream;
use Kir\Streams\Tests\Helper\ResourceStream;

class RandomAccessStreamTestTest extends ResourceStreamTest {
protected function setUp() {
$factory = new ClosureStreamFactory(function () {
$stream = new ResourceStream(__DIR__.'/../../../../tests/assets/test-data.txt', 'r');
$stream = new NondestructiveReadWriteStream($stream);
return $stream;
});
parent::setUp($factory);
}
}

0 comments on commit be58379

Please sign in to comment.