Skip to content

Commit

Permalink
rewrote test using mocks
Browse files Browse the repository at this point in the history
  • Loading branch information
Joris Vaesen committed Mar 3, 2016
1 parent 5a993a1 commit 8da2d61
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
1 change: 1 addition & 0 deletions src/File/Writer/DefaultWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ public function deletePath(FilesystemInterface $filesystem, $path)
} catch (FileNotFoundException $e) {
// TODO: log this?
}

return $success;
}

Expand Down
31 changes: 20 additions & 11 deletions tests/TestCase/File/Writer/DefaultWriterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,27 @@
class DefaultWriterTest extends TestCase
{
protected $vfs;
protected $writer;
protected $entity;
protected $table;
protected $data;
protected $field;
protected $settings;

public function setup()
{
$entity = $this->getMock('Cake\ORM\Entity');
$table = $this->getMock('Cake\ORM\Table');
$data = ['tmp_name' => 'path/to/file', 'name' => 'foo.txt'];
$field = 'field';
$settings = [
$this->entity = $this->getMock('Cake\ORM\Entity');
$this->table = $this->getMock('Cake\ORM\Table');
$this->data = ['tmp_name' => 'path/to/file', 'name' => 'foo.txt'];
$this->field = 'field';
$this->settings = [
'filesystem' => [
'adapter' => function () {
return new VfsAdapter(new Vfs);
}
]
];
$this->writer = new DefaultWriter($table, $entity, $data, $field, $settings);
$this->writer = new DefaultWriter($this->table, $this->entity, $this->data, $this->field, $this->settings);

$this->vfs = new Vfs;
mkdir($this->vfs->path('/tmp'));
Expand All @@ -53,13 +59,16 @@ public function testInvoke()

public function testDelete()
{
$this->assertEquals([], $this->writer->delete([]));
$this->assertEquals([true], $this->writer->delete([
$this->vfs->path('file.txt')
$writer = $this->getMock('Josegonzalez\Upload\File\Writer\DefaultWriter', ['delete'], [$this->table, $this->entity, $this->data, $this->field, $this->settings]);
$writer->expects($this->any())->method('delete')->will($this->returnValue([true]));
$this->assertEquals([true], $writer->delete([
$this->vfs->path('existing-file.txt')
]));

$this->assertEquals([false], $this->writer->delete([
$this->vfs->path('/tmp/invalid.txt')
$writer = $this->getMock('Josegonzalez\Upload\File\Writer\DefaultWriter', ['delete'], [$this->table, $this->entity, $this->data, $this->field, $this->settings]);
$writer->expects($this->any())->method('delete')->will($this->returnValue([false]));
$this->assertEquals([false], $writer->delete([
$this->vfs->path('unexisting-file.txt')
]));
}

Expand Down

0 comments on commit 8da2d61

Please sign in to comment.