Completed
Pull Request — master (#14)
by Harry
03:55
created

FileNodeTest::getFilesystem()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 9
rs 9.6666
cc 1
eloc 7
nc 1
nop 0
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
    public function testSetFileSystemUpdatesTheFileSystem()
82
    {
83
        $first = $this->getFilesystem();
84
        $second = $this->getFilesystem();
85
86
        $file = new FileNode($first, 'file/check');
87
88
        $first->shouldReceive('has')
89
              ->with('file/check')
90
              ->once()
91
              ->andReturn(true);
92
93
        static::assertTrue($file->getFilesystem()->has('file/check'));
94
95
        $second->shouldReceive('has')
96
               ->with('file/check')
97
               ->once()
98
               ->andReturn(false);
99
100
        static::assertSame($file, $file->setFilesystem($second));
101
102
        static::assertFalse($file->getFilesystem()->has('file/check'));
103
    }
104
}
105