Completed
Pull Request — master (#14)
by Harry
04:01
created

FileNodeTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 2
Bugs 0 Features 2
Metric Value
wmc 4
c 2
b 0
f 2
lcom 1
cbo 5
dl 0
loc 54
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getFilesystem() 0 9 1
A testInstanceOf() 0 10 1
A testEmptyFileWillReturnEmptyArrayForGetContents() 0 11 1
A testWhenCopyFailsItRaisesAnException() 0 15 1
1
<?php
2
/**
3
 * This file is part of graze/data-file
4
 *
5
 * Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license https://github.com/graze/data-file/blob/master/LICENSE.md
11
 * @link    https://github.com/graze/data-file
12
 */
13
14
namespace Graze\DataFile\Test\Unit\Node;
15
16
use Graze\DataFile\Format\FormatAwareInterface;
17
use Graze\DataFile\Modify\Compress\CompressionAwareInterface;
18
use Graze\DataFile\Modify\Encoding\EncodingAwareInterface;
19
use Graze\DataFile\Modify\Exception\CopyFailedException;
20
use Graze\DataFile\Node\FileNode;
21
use Graze\DataFile\Node\FileNodeInterface;
22
use Graze\DataFile\Test\TestCase;
23
use League\Flysystem\AdapterInterface;
24
use League\Flysystem\Filesystem;
25
use Mockery as m;
26
27
class FileNodeTest extends TestCase
28
{
29
    /**
30
     * @return mixed Really a Filesystem|MockInterface but coding standards get confused
31
     */
32
    private function getFilesystem()
33
    {
34
        $fileSystem = m::mock(Filesystem::class);
35
        $fileSystem->shouldReceive('getAdapter')
36
                   ->andReturn(m::mock(AdapterInterface::class));
37
        $fileSystem->shouldReceive('getConfig')
38
                   ->andReturn(null);
39
        return $fileSystem;
40
    }
41
42
    public function testInstanceOf()
43
    {
44
        $fileSystem = $this->getFilesystem();
45
        $file = new FileNode($fileSystem, 'not/nop');
46
47
        static::assertInstanceOf(FileNodeInterface::class, $file);
48
        static::assertInstanceOf(FormatAwareInterface::class, $file);
49
        static::assertInstanceOf(CompressionAwareInterface::class, $file);
50
        static::assertInstanceOf(EncodingAwareInterface::class, $file);
51
    }
52
53
    public function testEmptyFileWillReturnEmptyArrayForGetContents()
54
    {
55
        $fileSystem = $this->getFilesystem();
56
        $file = new FileNode($fileSystem, 'not/exists');
57
58
        $fileSystem->shouldReceive('has')
59
                   ->with('not/exists')
60
                   ->andReturn(false);
61
62
        static::assertEquals([], $file->getContents());
63
    }
64
65
    public function testWhenCopyFailsItRaisesAnException()
66
    {
67
        $fileSystem = $this->getFilesystem();
68
        $localFile = new FileNode($fileSystem, 'some/random');
69
70
        $newPath = new FileNode($fileSystem, 'some/target');
71
72
        $fileSystem->shouldReceive('copy')
73
                   ->with($localFile->getPath(), $newPath->getPath())
74
                   ->andReturn(false);
75
76
        $this->expectException(CopyFailedException::class);
77
78
        $localFile->copy($newPath->getPath());
79
    }
80
}
81