Skip to content

Commit

Permalink
Expand tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mbfisher committed Apr 26, 2014
1 parent f88f6a1 commit a363e85
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -2,4 +2,5 @@ composer.lock
vendor/
/coverage/
coverage.xml
coveralls-upload.json
.env
12 changes: 12 additions & 0 deletions Taskfile
Expand Up @@ -3,11 +3,13 @@
require 'vendor/autoload.php';

use Task\Plugin\PhpSpecPlugin;
use Task\Plugin\WatchPlugin;

$project = new Task\Project('task/filesystem');

$project->inject(function ($container) {
$container['phpspec'] = new PhpSpecPlugin;
$container['watch'] = new WatchPlugin;
});

$project->addTask('test', ['phpspec', function ($phpspec) {
Expand All @@ -17,4 +19,14 @@ $project->addTask('test', ['phpspec', function ($phpspec) {
->pipe($this->getOutput());
}]);

$project->addTask('test.watch', ['watch', 'phpspec', function ($watch, $phpspec) use ($project) {
$output = $this->getOutput();

$watch->init('spec/')
->addListener('modify', function () use ($project, $output) {
$project->runTask('test', $output);
})
->start();
}]);

return $project;
3 changes: 2 additions & 1 deletion composer.json
Expand Up @@ -20,7 +20,8 @@
"bossa/phpspec2-expect": "~1.0",
"henrikbjorn/phpspec-code-coverage" : "1.0.*@dev",
"mikey179/vfsStream": "~1.2",
"satooshi/php-coveralls": "~0.6"
"satooshi/php-coveralls": "~0.6",
"task/watch": "~0.1"
},
"autoload": {
"psr-4": {
Expand Down
1 change: 0 additions & 1 deletion coveralls-upload.json

This file was deleted.

80 changes: 80 additions & 0 deletions spec/Task/Plugin/Filesystem/FileSpec.php
@@ -0,0 +1,80 @@
<?php

namespace spec\Task\Plugin\Filesystem;

use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

use Task\Plugin\Filesystem\File;
use org\bovigo\vfs\vfsStream;
use Task\Plugin\Stream\WritableInterface;

class FileSpec extends ObjectBehavior
{
private $root;
private $path;

function let()
{
$this->root = vfsStream::setup('tmp');
$this->path = vfsStream::url('tmp').'/test';
$this->beConstructedWith($this->path);
}

function it_is_initializable()
{
$this->shouldHaveType('Task\Plugin\Filesystem\File');
}

function it_should_be_an_spl_file_object()
{
$this->shouldHaveType('SplFileObject');
}

function it_should_be_readable()
{
$this->shouldHaveType('Task\Plugin\Stream\ReadableInterface');
}

function it_should_be_writable()
{
$this->shouldHaveType('Task\Plugin\Stream\WritableInterface');
}

function it_should_read_content()
{
file_put_contents($this->path, 'foo');
$this->read()->shouldReturn('foo');
}

function it_should_write_content()
{
$this->write('foo')->shouldReturn($this);
expect(file_get_contents($this->path))->toBe('foo');
}

function it_should_write_file_content()
{
$src = vfsStream::url('tmp').'/src';
file_put_contents($src, 'foo');

$src = new File($src);
$this->write($src);

$this->read()->shouldReturn('foo');
}

function it_should_append_content()
{
file_put_contents($this->path, 'foo');
$this->append('bar')->shouldReturn($this);
expect(file_get_contents($this->path))->toBe('foobar');
}

function it_should_pipe(WritableInterface $to)
{
file_put_contents($this->path, 'foo');
$to->write('foo')->willReturn($to);
$this->pipe($to)->shouldReturn($to);
}
}
47 changes: 47 additions & 0 deletions spec/Task/Plugin/Filesystem/FilesystemIteratorSpec.php
@@ -0,0 +1,47 @@
<?php

namespace spec\Task\Plugin\Filesystem;

use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

use Task\Plugin\Stream\WritableInterface;

class FilesystemIteratorSpec extends ObjectBehavior
{
function let()
{
$this->beConstructedWith(sys_get_temp_dir());
}

function it_is_initializable()
{
$this->shouldHaveType('Task\Plugin\Filesystem\FilesystemIterator');
}

function it_should_be_a_recursive_iterator()
{
$this->shouldHaveType('RecursiveIteratorIterator');
}

function it_should_be_readable()
{
$this->shouldHaveType('Task\Plugin\Stream\ReadableInterface');
}

function it_should_preserve_original_path()
{
$this->getPath()->shouldEqual(sys_get_temp_dir());
}

function it_should_read()
{
$this->read()->shouldEqual($this);
}

function it_should_pipe(WritableInterface $to)
{
$to->write($this)->willReturn($to);
$this->pipe($to)->shouldReturn($to);
}
}
15 changes: 7 additions & 8 deletions src/Filesystem/File.php
Expand Up @@ -2,16 +2,15 @@

namespace Task\Plugin\Filesystem;

use Task\Plugin\Stream;
use Task\Plugin\Stream\WritableInterface;
use Task\Plugin\Stream\ReadableInterface;

class File extends \SplFileObject implements Stream\ReadableInterface, Stream\WritableInterface
class File extends \SplFileObject implements ReadableInterface, WritableInterface
{
public function __construct($filename, $mode = 'r+')
{
try {
parent::__construct($filename, $mode);
} catch (\RuntimeException $ex) {
}
touch($filename);
parent::__construct($filename, $mode);
}

public function read()
Expand All @@ -28,7 +27,7 @@ public function read()

public function write($data)
{
if ($data instanceof File) {
if ($data instanceof ReadableInterface) {
$data = $data->read();
}

Expand All @@ -48,7 +47,7 @@ public function append($content)
return $this;
}

public function pipe(Stream\WritableInterface $to)
public function pipe(WritableInterface $to)
{
return $to->write($this->read());
}
Expand Down

0 comments on commit a363e85

Please sign in to comment.