Skip to content

Commit 8da2d61

Browse files
author
Joris Vaesen
committed
rewrote test using mocks
1 parent 5a993a1 commit 8da2d61

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

src/File/Writer/DefaultWriter.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ public function deletePath(FilesystemInterface $filesystem, $path)
143143
} catch (FileNotFoundException $e) {
144144
// TODO: log this?
145145
}
146+
146147
return $success;
147148
}
148149

tests/TestCase/File/Writer/DefaultWriterTest.php

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,27 @@
1313
class DefaultWriterTest extends TestCase
1414
{
1515
protected $vfs;
16+
protected $writer;
17+
protected $entity;
18+
protected $table;
19+
protected $data;
20+
protected $field;
21+
protected $settings;
1622

1723
public function setup()
1824
{
19-
$entity = $this->getMock('Cake\ORM\Entity');
20-
$table = $this->getMock('Cake\ORM\Table');
21-
$data = ['tmp_name' => 'path/to/file', 'name' => 'foo.txt'];
22-
$field = 'field';
23-
$settings = [
25+
$this->entity = $this->getMock('Cake\ORM\Entity');
26+
$this->table = $this->getMock('Cake\ORM\Table');
27+
$this->data = ['tmp_name' => 'path/to/file', 'name' => 'foo.txt'];
28+
$this->field = 'field';
29+
$this->settings = [
2430
'filesystem' => [
2531
'adapter' => function () {
2632
return new VfsAdapter(new Vfs);
2733
}
2834
]
2935
];
30-
$this->writer = new DefaultWriter($table, $entity, $data, $field, $settings);
36+
$this->writer = new DefaultWriter($this->table, $this->entity, $this->data, $this->field, $this->settings);
3137

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

5460
public function testDelete()
5561
{
56-
$this->assertEquals([], $this->writer->delete([]));
57-
$this->assertEquals([true], $this->writer->delete([
58-
$this->vfs->path('file.txt')
62+
$writer = $this->getMock('Josegonzalez\Upload\File\Writer\DefaultWriter', ['delete'], [$this->table, $this->entity, $this->data, $this->field, $this->settings]);
63+
$writer->expects($this->any())->method('delete')->will($this->returnValue([true]));
64+
$this->assertEquals([true], $writer->delete([
65+
$this->vfs->path('existing-file.txt')
5966
]));
6067

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

0 commit comments

Comments
 (0)