Skip to content

Commit

Permalink
minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
mikey179 committed Jul 19, 2016
1 parent 1d8f545 commit bb53b64
Show file tree
Hide file tree
Showing 14 changed files with 173 additions and 133 deletions.
4 changes: 2 additions & 2 deletions src/main/php/file/FileInputStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ public static function castFrom($value): InputStream
}

throw new \InvalidArgumentException(
'Given value is neither an instance of'
. ' stubbles\streams\InputStream nor a string denoting a file'
'Given value is neither an instance of' . InputStream::class
. ' nor a string denoting a file'
);
}

Expand Down
1 change: 0 additions & 1 deletion src/main/php/file/FileStreamFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ class FileStreamFactory implements StreamFactory
* constructor
*
* @param int $fileMode default file mode if directory for output stream should be created
* @Named('stubbles.filemode')
* @Property('stubbles.filemode')
*/
public function __construct(int $fileMode = 0700)
Expand Down
5 changes: 1 addition & 4 deletions src/test/php/DecodingInputStreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,7 @@ public function eofReturnsEofFromDecoratedStream()
public function closeClosesDecoratedStream()
{
$inputStream = NewInstance::of(InputStream::class);
$decodingInputStream = new DecodingInputStream(
$inputStream,
'iso-8859-1'
);
$decodingInputStream = new DecodingInputStream($inputStream, 'iso-8859-1');
$decodingInputStream->close();
verify($inputStream, 'close')->wasCalledOnce();
}
Expand Down
33 changes: 30 additions & 3 deletions src/test/php/DecoratedOutputStreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,37 +53,64 @@ private function createDecoratedOutputStream(OutputStream $outputStream): Decora
* @test
*/
public function writeCallsDecoratedStream()
{
$this->decoratedOutputStream->write('foo');
assert($this->memory->buffer(), equals('foo'));
}

/**
* @test
*/
public function writeReturnsAmountOfDataWrittenFromDecoratedStream()
{
assert(
$this->decoratedOutputStream->write('foo'),
equals(3)
);
assert($this->memory->buffer(), equals('foo'));
}


/**
* @test
*/
public function writeLineCallsDecoratedStream()
{
$this->decoratedOutputStream->writeLine('foo');
assert($this->memory->buffer(), equals("foo\n"));
}


/**
* @test
*/
public function writeLineReturnsAmountOfDataWrittenFromDecoratedStream()
{
assert(
$this->decoratedOutputStream->writeLine('foo'),
equals(4)
);
assert($this->memory->buffer(), equals("foo\n"));
}

/**
* @test
* @since 3.2.0
*/
public function writeLinesCallsDecoratedStream()
{
$this->decoratedOutputStream->writeLines(['foo', 'bar']);
assert($this->memory->buffer(), equals("foo\nbar\n"));
}

/**
* @test
* @since 3.2.0
*/
public function writeLinesReturnsAmountOfDataWrittenFromDecoratedStream()
{
assert(
$this->decoratedOutputStream->writeLines(['foo', 'bar']),
equals(8)
);
assert($this->memory->buffer(), equals("foo\nbar\n"));
}

/**
Expand Down
9 changes: 6 additions & 3 deletions src/test/php/FunctionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
use stubbles\sequence\Sequence;

use function bovigo\assert\assert;
use function bovigo\assert\predicate\each;
use function bovigo\assert\predicate\equals;
use function bovigo\assert\predicate\isInstanceOf;
use function bovigo\assert\predicate\isNotEmpty;
/**
* Tests for stubbles\streams\*().
*
Expand Down Expand Up @@ -50,8 +52,9 @@ public function linesOfReturnsSequence()
*/
public function nonEmptyLinesOfReturnsNonEmptyLinesOnly()
{
foreach (nonEmptyLinesOf($this->file->url()) as $line) {
assert($line, equals('foo'));
}
assert(
nonEmptyLinesOf($this->file->url()),
isNotEmpty()->and(each(equals('foo')))
);
}
}
14 changes: 8 additions & 6 deletions src/test/php/ResourceInputStreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@
namespace stubbles\streams;
use org\bovigo\vfs\vfsStream;

use function bovigo\assert\assert;
use function bovigo\assert\assertEmptyString;
use function bovigo\assert\assertFalse;
use function bovigo\assert\assertTrue;
use function bovigo\assert\expect;
use function bovigo\assert\predicate\equals;
use function bovigo\assert\{
assert,
assertEmptyString,
assertFalse,
assertTrue,
expect,
predicate\equals
};
/**
* Test for stubbles\streams\ResourceInputStream.
*
Expand Down
8 changes: 4 additions & 4 deletions src/test/php/ResourceOutputStreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ public function invalidHandleThrowsIllegalArgumentException()
*/
public function writeToClosedStreamThrowsIllegalStateException()
{
$this->resourceOutputStream->close();
expect(function() {
$this->resourceOutputStream->close();
$this->resourceOutputStream->write('foobarbaz');
})
->throws(\LogicException::class);
Expand All @@ -87,8 +87,8 @@ public function writeToClosedStreamThrowsIllegalStateException()
*/
public function writeLineToClosedStreamThrowsIllegalStateException()
{
$this->resourceOutputStream->close();
expect(function() {
$this->resourceOutputStream->close();
$this->resourceOutputStream->writeLine('foobarbaz');
})
->throws(\LogicException::class);
Expand All @@ -99,8 +99,8 @@ public function writeLineToClosedStreamThrowsIllegalStateException()
*/
public function writeToExternalClosedStreamThrowsIOException()
{
fclose($this->handle);
expect(function() {
fclose($this->handle);
$this->resourceOutputStream->write('foobarbaz');
})
->throws(StreamException::class);
Expand All @@ -111,8 +111,8 @@ public function writeToExternalClosedStreamThrowsIOException()
*/
public function writeLineToExternalClosedStreamThrowsIOException()
{
fclose($this->handle);
expect(function() {
fclose($this->handle);
$this->resourceOutputStream->writeLine('foobarbaz');
})
->throws(StreamException::class);
Expand Down
28 changes: 10 additions & 18 deletions src/test/php/StandardInputStreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,44 +36,36 @@ public function setUp()
*/
public function seekAfterCloseThrowsLogicException()
{
expect(function() {
$this->standardInputStream->close();
$this->standardInputStream->seek(0);
})
->throws(\LogicException::class);
$this->standardInputStream->close();
expect(function() { $this->standardInputStream->seek(0); })
->throws(\LogicException::class);
}

/**
* @test
*/
public function canSeekToStartOfStream()
{
expect(function() {
$this->standardInputStream->seek(0);
})
->doesNotThrow();
expect(function() { $this->standardInputStream->seek(0); })
->doesNotThrow();
}

/**
* @test
*/
public function canSeekToAnyPosition()
{
expect(function() {
$this->standardInputStream->seek(100);
})
->doesNotThrow();
expect(function() { $this->standardInputStream->seek(100); })
->doesNotThrow();
}

/**
* @test
*/
public function tellAfterCloseThrowsLogicException()
{
expect(function() {
$this->standardInputStream->close();
$this->standardInputStream->tell();
})
->throws(\LogicException::class);
$this->standardInputStream->close();
expect(function() { $this->standardInputStream->tell(); })
->throws(\LogicException::class);
}
}
48 changes: 19 additions & 29 deletions src/test/php/file/FileInputStreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,12 @@ public function constructWithString()
*/
public function constructWithStringFailsAndThrowsIOException()
{
expect(function() {
new FileInputStream('doesNotExist', 'r');
})
->throws(StreamException::class)
->withMessage('Can not open file doesNotExist with mode r: failed to open stream: No such file or directory');
expect(function() { new FileInputStream('doesNotExist', 'r'); })
->throws(StreamException::class)
->withMessage(
'Can not open file doesNotExist with mode r: failed to'
. ' open stream: No such file or directory'
);
}

/**
Expand All @@ -64,28 +65,21 @@ public function constructWithResource()

/**
* @test
* @requires extension gd
*/
public function constructWithIllegalResource()
{
if (extension_loaded('gd') === false) {
$this->markTestSkipped('No known extension with other resource type available.');
}

expect(function() {
new FileInputStream(imagecreate(2, 2));
})
->throws(\InvalidArgumentException::class);
expect(function() { new FileInputStream(imagecreate(2, 2)); })
->throws(\InvalidArgumentException::class);
}

/**
* @test
*/
public function constructWithIllegalArgument()
{
expect(function() {
new FileInputStream(0);
})
->throws(\InvalidArgumentException::class);
expect(function() { new FileInputStream(0); })
->throws(\InvalidArgumentException::class);
}

/**
Expand Down Expand Up @@ -130,24 +124,20 @@ public function seek_END()
*/
public function seekOnClosedStreamFailsThrowsIllegalStateException()
{
expect(function() {
$fileInputStream = new FileInputStream(vfsStream::url('home/test.txt'));
$fileInputStream->close();
$fileInputStream->seek(3);
})
->throws(\LogicException::class);
$fileInputStream = new FileInputStream(vfsStream::url('home/test.txt'));
$fileInputStream->close();
expect(function() use ($fileInputStream) { $fileInputStream->seek(3); })
->throws(\LogicException::class);
}

/**
* @test
*/
public function tellOnClosedStreamThrowsIllegalStateException()
{
expect(function() {
$fileInputStream = new FileInputStream(vfsStream::url('home/test.txt'));
$fileInputStream->close();
$fileInputStream->tell();
})
->throws(\LogicException::class);
$fileInputStream = new FileInputStream(vfsStream::url('home/test.txt'));
$fileInputStream->close();
expect(function() use ($fileInputStream) { $fileInputStream->tell(); })
->throws(\LogicException::class);
}
}
43 changes: 20 additions & 23 deletions src/test/php/file/FileOutputStreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
use org\bovigo\vfs\vfsStream;
use stubbles\streams\StreamException;

use function bovigo\assert\assert;
use function bovigo\assert\assertFalse;
use function bovigo\assert\assertTrue;
use function bovigo\assert\expect;
use function bovigo\assert\predicate\equals;
use function bovigo\assert\{
assert,
assertFalse,
assertTrue,
expect,
predicate\equals
};
/**
* Test for stubbles\streams\file\FileOutputStream.
*
Expand Down Expand Up @@ -84,12 +86,14 @@ public function constructWithStringDelayedCreatesFileOnWrite()
*/
public function constructWithStringFailsAndThrowsIOException()
{
expect(function() {
vfsStream::newFile('test.txt', 0000)->at(vfsStream::setup());
new FileOutputStream($this->fileUrl, 'r');
})
->throws(StreamException::class)
->withMessage('Can not open file vfs://home/test.txt with mode r: failed to open stream: "org\bovigo\vfs\vfsStreamWrapper::stream_open" call failed');
vfsStream::newFile('test.txt', 0000)->at(vfsStream::setup());
expect(function() { new FileOutputStream($this->fileUrl, 'r'); })
->throws(StreamException::class)
->withMessage(
'Can not open file vfs://home/test.txt with mode r:'
. ' failed to open stream: "org\bovigo\vfs\vfsStreamWrapper::stream_open"'
. ' call failed'
);
}

/**
Expand All @@ -104,27 +108,20 @@ public function constructWithResource()

/**
* @test
* @requires extension gd
*/
public function constructWithIllegalResource()
{
if (extension_loaded('gd') === false) {
$this->markTestSkipped('No known extension with other resource type available.');
}

expect(function() {
new FileOutputStream(imagecreate(2, 2));
})
->throws(\InvalidArgumentException::class);
expect(function() { new FileOutputStream(imagecreate(2, 2)); })
->throws(\InvalidArgumentException::class);
}

/**
* @test
*/
public function constructWithIllegalArgument()
{
expect(function() {
new FileOutputStream(0);
})
->throws(\InvalidArgumentException::class);
expect(function() { new FileOutputStream(0); })
->throws(\InvalidArgumentException::class);
}
}

0 comments on commit bb53b64

Please sign in to comment.