View source
<?php
namespace org\bovigo\vfs\content;
class StringBasedFileContentTestCase extends \PHPUnit_Framework_TestCase {
private $stringBasedFileContent;
public function setUp() {
$this->stringBasedFileContent = new StringBasedFileContent('foobarbaz');
}
public function hasContentOriginallySet() {
$this
->assertEquals('foobarbaz', $this->stringBasedFileContent
->content());
}
public function hasNotReachedEofAfterCreation() {
$this
->assertFalse($this->stringBasedFileContent
->eof());
}
public function sizeEqualsLengthOfGivenString() {
$this
->assertEquals(9, $this->stringBasedFileContent
->size());
}
public function readReturnsSubstringWithRequestedLength() {
$this
->assertEquals('foo', $this->stringBasedFileContent
->read(3));
}
public function readMovesOffset() {
$this
->assertEquals('foo', $this->stringBasedFileContent
->read(3));
$this
->assertEquals('bar', $this->stringBasedFileContent
->read(3));
$this
->assertEquals('baz', $this->stringBasedFileContent
->read(3));
}
public function reaMoreThanSizeReturnsWholeContent() {
$this
->assertEquals('foobarbaz', $this->stringBasedFileContent
->read(10));
}
public function readAfterEndReturnsEmptyString() {
$this->stringBasedFileContent
->read(9);
$this
->assertEquals('', $this->stringBasedFileContent
->read(3));
}
public function readDoesNotChangeSize() {
$this->stringBasedFileContent
->read(3);
$this
->assertEquals(9, $this->stringBasedFileContent
->size());
}
public function readLessThenSizeDoesNotReachEof() {
$this->stringBasedFileContent
->read(3);
$this
->assertFalse($this->stringBasedFileContent
->eof());
}
public function readSizeReachesEof() {
$this->stringBasedFileContent
->read(9);
$this
->assertTrue($this->stringBasedFileContent
->eof());
}
public function readMoreThanSizeReachesEof() {
$this->stringBasedFileContent
->read(10);
$this
->assertTrue($this->stringBasedFileContent
->eof());
}
public function seekWithInvalidOptionReturnsFalse() {
$this
->assertFalse($this->stringBasedFileContent
->seek(0, 55));
}
public function canSeekToGivenOffset() {
$this
->assertTrue($this->stringBasedFileContent
->seek(5, SEEK_SET));
$this
->assertEquals('rbaz', $this->stringBasedFileContent
->read(10));
}
public function canSeekFromCurrentOffset() {
$this
->assertTrue($this->stringBasedFileContent
->seek(5, SEEK_SET));
$this
->assertTrue($this->stringBasedFileContent
->seek(2, SEEK_CUR));
$this
->assertEquals('az', $this->stringBasedFileContent
->read(10));
}
public function canSeekToEnd() {
$this
->assertTrue($this->stringBasedFileContent
->seek(0, SEEK_END));
$this
->assertEquals('', $this->stringBasedFileContent
->read(10));
}
public function writeOverwritesExistingContentWhenOffsetNotAtEof() {
$this
->assertEquals(3, $this->stringBasedFileContent
->write('bar'));
$this
->assertEquals('barbarbaz', $this->stringBasedFileContent
->content());
}
public function writeAppendsContentWhenOffsetAtEof() {
$this
->assertTrue($this->stringBasedFileContent
->seek(0, SEEK_END));
$this
->assertEquals(3, $this->stringBasedFileContent
->write('bar'));
$this
->assertEquals('foobarbazbar', $this->stringBasedFileContent
->content());
}
public function truncateRemovesSuperflouosContent() {
$this
->assertTrue($this->stringBasedFileContent
->truncate(6));
$this
->assertEquals('foobar', $this->stringBasedFileContent
->content());
}
public function truncateDecreasesSize() {
$this
->assertTrue($this->stringBasedFileContent
->truncate(6));
$this
->assertEquals(6, $this->stringBasedFileContent
->size());
}
public function truncateToGreaterSizeAddsZeroBytes() {
$this
->assertTrue($this->stringBasedFileContent
->truncate(25));
$this
->assertEquals("foobarbaz\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", $this->stringBasedFileContent
->content());
}
public function truncateToGreaterSizeIncreasesSize() {
$this
->assertTrue($this->stringBasedFileContent
->truncate(25));
$this
->assertEquals(25, $this->stringBasedFileContent
->size());
}
}