Completed
Push — master ( 7a3cb1...e42624 )
by Harry
9s
created

TransferTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 108
Duplicated Lines 77.78 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
c 1
b 0
f 0
lcom 1
cbo 10
dl 84
loc 108
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 4 1
A testInstanceOf() 0 4 1
A testCopyBetweenFileSystems() 12 12 1
A testCopyBetweenSameFileSystem() 12 12 1
A testMoveDeletesTheOldFile() 12 12 1
A testCopyWhenOriginalFileDoesNotExistThrowsAnException() 10 10 1
A testMoveWhenOriginalFileDoesNotExistThrowsAnException() 10 10 1
A testCopyWhenFilesystemDoesNotReadStreamThrowsAnException() 14 14 1
A testMoveWhenFilesystemDoesNotReadStreamThrowsAnException() 14 14 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
106
    {
107
        $filesystem = m::mock('League\Flysystem\FileSystemInterface')->makePartial();
108
109
        $fromFile = new FileNode($filesystem, 'some/file');
0 ignored issues
show
Documentation introduced by
$filesystem is of type object<Mockery\Mock>, but the function expects a null|object<League\Flysystem\FilesystemInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
121
    {
122
        $filesystem = m::mock('League\Flysystem\FileSystemInterface')->makePartial();
123
124
        $fromFile = new FileNode($filesystem, 'some/file');
0 ignored issues
show
Documentation introduced by
$filesystem is of type object<Mockery\Mock>, but the function expects a null|object<League\Flysystem\FilesystemInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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