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 League\Flysystem\FilesystemInterface; |
26
|
|
|
use Mockery as m; |
27
|
|
|
|
28
|
|
|
class FileNodeTest extends TestCase |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @return FileSystemInterface|m\MockInterface |
32
|
|
|
*/ |
33
|
|
|
private function getFilesystem() |
34
|
|
|
{ |
35
|
|
|
$fileSystem = m::mock(Filesystem::class); |
36
|
|
|
$fileSystem->shouldReceive('getAdapter') |
37
|
|
|
->andReturn(m::mock(AdapterInterface::class)); |
38
|
|
|
$fileSystem->shouldReceive('getConfig') |
39
|
|
|
->andReturn(null); |
40
|
|
|
return $fileSystem; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function testInstanceOf() |
44
|
|
|
{ |
45
|
|
|
$fileSystem = $this->getFilesystem(); |
46
|
|
|
$file = new FileNode($fileSystem, 'not/nop'); |
47
|
|
|
|
48
|
|
|
static::assertInstanceOf(FileNodeInterface::class, $file); |
49
|
|
|
static::assertInstanceOf(FormatAwareInterface::class, $file); |
50
|
|
|
static::assertInstanceOf(CompressionAwareInterface::class, $file); |
51
|
|
|
static::assertInstanceOf(EncodingAwareInterface::class, $file); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function testEmptyFileWillReturnEmptyArrayForGetContents() |
55
|
|
|
{ |
56
|
|
|
$fileSystem = $this->getFilesystem(); |
57
|
|
|
$file = new FileNode($fileSystem, 'not/exists'); |
58
|
|
|
|
59
|
|
|
$fileSystem->shouldReceive('has') |
60
|
|
|
->with('not/exists') |
61
|
|
|
->andReturn(false); |
62
|
|
|
|
63
|
|
|
static::assertEquals([], $file->getContents()); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function testWhenCopyFailsItRaisesAnException() |
67
|
|
|
{ |
68
|
|
|
$fileSystem = $this->getFilesystem(); |
69
|
|
|
$localFile = new FileNode($fileSystem, 'some/random'); |
70
|
|
|
|
71
|
|
|
$newPath = new FileNode($fileSystem, 'some/target'); |
72
|
|
|
|
73
|
|
|
$fileSystem->shouldReceive('copy') |
74
|
|
|
->with($localFile->getPath(), $newPath->getPath()) |
75
|
|
|
->andReturn(false); |
76
|
|
|
|
77
|
|
|
$this->expectException(CopyFailedException::class); |
78
|
|
|
|
79
|
|
|
$localFile->copy($newPath->getPath()); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|