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\Integration\Modify\Transfer; |
15
|
|
|
|
16
|
|
|
use Graze\DataFile\Modify\Exception\TransferFailedException; |
17
|
|
|
use Graze\DataFile\Modify\Transfer\FileTransferInterface; |
18
|
|
|
use Graze\DataFile\Modify\Transfer\Transfer; |
19
|
|
|
use Graze\DataFile\Node\FileNode; |
20
|
|
|
use Graze\DataFile\Node\LocalFile; |
21
|
|
|
use Graze\DataFile\Test\AbstractFileTestCase; |
22
|
|
|
use League\Flysystem\FileNotFoundException; |
23
|
|
|
use League\Flysystem\Filesystem; |
24
|
|
|
use League\Flysystem\Memory\MemoryAdapter; |
25
|
|
|
use Mockery as m; |
26
|
|
|
|
27
|
|
|
class TransferTest extends AbstractFileTestCase |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var FileTransferInterface |
31
|
|
|
*/ |
32
|
|
|
protected $transfer; |
33
|
|
|
|
34
|
|
|
public function setUp() |
35
|
|
|
{ |
36
|
|
|
$this->transfer = new Transfer(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function testInstanceOf() |
40
|
|
|
{ |
41
|
|
|
static::assertInstanceOf(FileTransferInterface::class, $this->transfer); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
View Code Duplication |
public function testCopyBetweenFileSystems() |
|
|
|
|
45
|
|
|
{ |
46
|
|
|
$fromFile = new LocalFile(static::$dir . 'from_between.text'); |
47
|
|
|
$fromFile->put('Some Text In Here'); |
48
|
|
|
|
49
|
|
|
$toFile = new FileNode(new Filesystem(new MemoryAdapter()), 'some_file'); |
50
|
|
|
|
51
|
|
|
$this->transfer->copyTo($fromFile, $toFile); |
52
|
|
|
|
53
|
|
|
static::assertEquals('Some Text In Here', $toFile->read()); |
54
|
|
|
static::assertEquals($fromFile->read(), $toFile->read()); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
View Code Duplication |
public function testCopyBetweenSameFileSystem() |
|
|
|
|
58
|
|
|
{ |
59
|
|
|
$fromFile = new LocalFile(static::$dir . 'from_same.text'); |
60
|
|
|
$fromFile->put('Some Text In Here'); |
61
|
|
|
|
62
|
|
|
$toFile = new LocalFile(static::$dir . 'to_same.text'); |
63
|
|
|
|
64
|
|
|
$this->transfer->copyTo($fromFile, $toFile); |
65
|
|
|
|
66
|
|
|
static::assertEquals('Some Text In Here', $toFile->read()); |
67
|
|
|
static::assertEquals($fromFile->read(), $toFile->read()); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
View Code Duplication |
public function testMoveDeletesTheOldFile() |
|
|
|
|
71
|
|
|
{ |
72
|
|
|
$fromFile = new LocalFile(static::$dir . 'delete_from.text'); |
73
|
|
|
$fromFile->put('Some Text In Here'); |
74
|
|
|
|
75
|
|
|
$toFile = new LocalFile(static::$dir . 'delete_to.text'); |
76
|
|
|
|
77
|
|
|
$this->transfer->moveTo($fromFile, $toFile); |
78
|
|
|
|
79
|
|
|
static::assertEquals('Some Text In Here', $toFile->read()); |
80
|
|
|
static::assertFalse($fromFile->exists()); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
View Code Duplication |
public function testCopyWhenOriginalFileDoesNotExistThrowsAnException() |
|
|
|
|
84
|
|
|
{ |
85
|
|
|
$fromFile = new LocalFile(static::$dir . 'fail_from.text'); |
86
|
|
|
|
87
|
|
|
$toFile = new LocalFile(static::$dir . 'fail_to.text'); |
88
|
|
|
|
89
|
|
|
$this->expectException(FileNotFoundException::class); |
90
|
|
|
|
91
|
|
|
$this->transfer->copyTo($fromFile, $toFile); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
View Code Duplication |
public function testMoveWhenOriginalFileDoesNotExistThrowsAnException() |
|
|
|
|
95
|
|
|
{ |
96
|
|
|
$fromFile = new LocalFile(static::$dir . 'fail_move_from.text'); |
97
|
|
|
|
98
|
|
|
$toFile = new LocalFile(static::$dir . 'fail_move_to.text'); |
99
|
|
|
|
100
|
|
|
$this->expectException(FileNotFoundException::class); |
101
|
|
|
|
102
|
|
|
$this->transfer->moveTo($fromFile, $toFile); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
View Code Duplication |
public function testCopyWhenFilesystemDoesNotReadStreamThrowsAnException() |
|
|
|
|
106
|
|
|
{ |
107
|
|
|
$filesystem = m::mock('League\Flysystem\FileSystemInterface')->makePartial(); |
108
|
|
|
|
109
|
|
|
$fromFile = new FileNode($filesystem, 'some/file'); |
|
|
|
|
110
|
|
|
|
111
|
|
|
$toFile = new LocalFile(static::$dir . 'fail_copy_file.text'); |
112
|
|
|
|
113
|
|
|
$filesystem->shouldReceive('readStream')->with($fromFile->getPath())->andReturn(false); |
114
|
|
|
|
115
|
|
|
$this->expectException(TransferFailedException::class); |
116
|
|
|
|
117
|
|
|
$this->transfer->copyTo($fromFile, $toFile); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
View Code Duplication |
public function testMoveWhenFilesystemDoesNotReadStreamThrowsAnException() |
|
|
|
|
121
|
|
|
{ |
122
|
|
|
$filesystem = m::mock('League\Flysystem\FileSystemInterface')->makePartial(); |
123
|
|
|
|
124
|
|
|
$fromFile = new FileNode($filesystem, 'some/file'); |
|
|
|
|
125
|
|
|
|
126
|
|
|
$toFile = new LocalFile(static::$dir . 'fail_move_file.text'); |
127
|
|
|
|
128
|
|
|
$filesystem->shouldReceive('readStream')->with($fromFile->getPath())->andReturn(false); |
129
|
|
|
|
130
|
|
|
$this->expectException(TransferFailedException::class); |
131
|
|
|
|
132
|
|
|
$this->transfer->moveTo($fromFile, $toFile); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.