diff --git a/.gitignore b/.gitignore index 4dfa10e..70002bd 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ composer.lock vendor/ /coverage/ coverage.xml +coveralls-upload.json .env diff --git a/Taskfile b/Taskfile index 3d5a80a..ae0aa3d 100644 --- a/Taskfile +++ b/Taskfile @@ -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) { @@ -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; diff --git a/composer.json b/composer.json index c1e152d..309a66e 100644 --- a/composer.json +++ b/composer.json @@ -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": { diff --git a/coveralls-upload.json b/coveralls-upload.json deleted file mode 100644 index 29b32ee..0000000 --- a/coveralls-upload.json +++ /dev/null @@ -1 +0,0 @@ -{"service_name":"php-coveralls","service_event_type":"manual","repo_token":"HyrW1vz4tnuMQtEtxREHNjGeaU4nSmzPE","git":{"branch":"master","head":{"id":"6453ebed94a2870b98d7adcf57ebdc8e9ced4658","author_name":"Mike Fisher","author_email":"mike@mbfisher.com","committer_name":"Mike Fisher","committer_email":"mike@mbfisher.com","message":"Add REAMDE"},"remotes":[{"name":"origin","url":"git@github.com:taskphp\/filesystem.git"}]},"run_at":"2014-04-26 14:31:53 +0000","source_files":[{"name":"Filesystem\/File.php","source":"rewind();\n\n $content = '';\n while (!$this->eof()) {\n $content .= $this->fgets();\n }\n\n return $content;\n }\n\n public function write($data)\n {\n if ($data instanceof File) {\n $data = $data->read();\n }\n\n $this->ftruncate(0);\n $this->fwrite($data);\n return $this;\n }\n\n public function append($content)\n {\n while (!$this->eof()) {\n # why doesn't next() work here?\n $this->current();\n }\n\n $this->fwrite($content);\n return $this;\n }\n\n public function pipe(Stream\\WritableInterface $to)\n {\n return $to->write($this->read());\n }\n}","coverage":[null,null,null,null,null,null,null,null,null,null,null,2,2,null,2,null,null,null,0,null,0,0,0,0,null,0,null,null,null,null,0,0,0,null,0,0,0,null,null,null,null,0,null,0,0,null,0,0,null,null,null,null,0,null,null]},{"name":"Filesystem\/FilesystemIterator.php","source":"path = $path;\n }\n\n public function getPath()\n {\n return $this->path;\n }\n\n public function read()\n {\n return $this;\n }\n\n public function pipe(WritableInterface $to)\n {\n return $to->write($this->read());\n }\n}","coverage":[null,null,null,null,null,null,null,null,null,null,null,null,null,null,1,1,1,null,1,null,1,null,1,1,null,null,null,1,null,null,null,null,0,null,null,null,null,0,null,null]},{"name":"FilesystemPlugin.php","source":"open($filename);\n }\n\n public function ls($dir)\n {\n return new FilesystemIterator($dir);\n }\n\n public function copy($source, $target, $override = false)\n {\n $target = rtrim($target, '\/');\n $source = rtrim($source, '\/');\n\n if (is_file($source)) {\n if (is_dir($target)) {\n return parent::copy($source, $target.DIRECTORY_SEPARATOR.basename($source), $override);\n } elseif (is_link($source)) {\n return $this->symlink(readlink($source), $target);\n } else {\n return parent::copy($source, $target, $override);\n }\n } elseif (is_dir($source)) {\n if (is_file($target)) {\n throw new \\LogicException(\"Cannot copy directory to file\");\n } else {\n return $this->mirror($source, $target);\n }\n }\n\n throw new FileNotFoundException(\"Could not copy $source to $target\");\n }\n\n public function copyTree($baseDir, $target, Finder $finder)\n {\n foreach ($finder as $file) {\n if (!$file->isDir()) {\n $path = substr($file->getPathname(), strlen(\"$baseDir\/\"));\n $this->copy(\"$baseDir\/$path\", \"$target\/$path\");\n }\n }\n }\n}","coverage":[null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,2,null,null,null,null,2,1,null,null,1,1,null,null,null,null,1,null,null,null,null,7,7,null,7,5,1,4,1,null,3,null,3,2,1,null,1,null,null,null,1,null,null,null,null,1,1,1,1,1,1,1,null]}]} \ No newline at end of file diff --git a/spec/Task/Plugin/Filesystem/FileSpec.php b/spec/Task/Plugin/Filesystem/FileSpec.php new file mode 100644 index 0000000..3d37173 --- /dev/null +++ b/spec/Task/Plugin/Filesystem/FileSpec.php @@ -0,0 +1,80 @@ +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); + } +} diff --git a/spec/Task/Plugin/Filesystem/FilesystemIteratorSpec.php b/spec/Task/Plugin/Filesystem/FilesystemIteratorSpec.php new file mode 100644 index 0000000..4ce6d43 --- /dev/null +++ b/spec/Task/Plugin/Filesystem/FilesystemIteratorSpec.php @@ -0,0 +1,47 @@ +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); + } +} diff --git a/src/Filesystem/File.php b/src/Filesystem/File.php index 8d7ddb4..5e10203 100644 --- a/src/Filesystem/File.php +++ b/src/Filesystem/File.php @@ -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() @@ -28,7 +27,7 @@ public function read() public function write($data) { - if ($data instanceof File) { + if ($data instanceof ReadableInterface) { $data = $data->read(); } @@ -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()); }