Completed
Pull Request — master (#3)
by Harry
05:31
created

LocalFileTest   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 161
Duplicated Lines 23.6 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 4
Bugs 1 Features 0
Metric Value
wmc 17
c 4
b 1
f 0
lcom 1
cbo 5
dl 38
loc 161
rs 10

17 Methods

Rating   Name   Duplication   Size   Complexity  
A testInstanceOf() 0 5 1
A testGetFilePathReturnsFullyQualifiedPath() 9 9 1
A testGetFilenameJustReturnsTheFilename() 0 6 1
A testFileExists() 0 10 1
A testFileGetContents() 0 8 1
A testFileGetContentsReturnsEmptyArrayIfFileDoesNotExist() 0 5 1
A testCompression() 0 6 1
A testEncoding() 0 6 1
A testToString() 0 5 1
A testGetContentsForCompressedFile() 0 10 1
A testGetContentsForCompressedFileDeletesTheUncompressedFileAfterwards() 0 15 1
A testSetEncodingModifiesTheEncoding() 0 9 1
A testSetEncodingReturnsIsFluent() 10 10 1
A testSetCompressionModifiesTheEncoding() 9 9 1
A testSetCompressionReturnsIsFluent() 10 10 1
A testGetDirectoryReturnsJustTheDirectory() 0 6 1
A testGetStream() 0 13 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\Node;
15
16
use Graze\DataFile\Modify\Compress\Gzip;
17
use Graze\DataFile\Node\FileNodeInterface;
18
use Graze\DataFile\Node\LocalFile;
19
use Graze\DataFile\Test\AbstractFileTestCase;
20
use Psr\Http\Message\StreamInterface;
21
22
class LocalFileTest extends AbstractFileTestCase
23
{
24
    public function testInstanceOf()
25
    {
26
        $file = new LocalFile('/broken/path/to/nowhere');
27
        static::assertInstanceOf(FileNodeInterface::class, $file);
28
    }
29
30 View Code Duplication
    public function testGetFilePathReturnsFullyQualifiedPath()
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...
31
    {
32
        $file = new LocalFile(static::$dir . 'file_path.test');
33
        $file->put('random');
34
35
        $expected = static::$dir . 'file_path.test';
36
37
        static::assertEquals($expected, $file->getPath());
38
    }
39
40
    public function testGetFilenameJustReturnsTheFilename()
41
    {
42
        $file = new LocalFile(static::$dir . 'file_name.test');
43
44
        static::assertEquals('file_name.test', $file->getFilename());
45
    }
46
47
    public function testFileExists()
48
    {
49
        $file = new LocalFile(static::$dir . 'file_exists.test');
50
51
        static::assertFalse($file->exists());
52
53
        $file->put('random');
54
55
        static::assertTrue($file->exists());
56
    }
57
58
    public function testFileGetContents()
59
    {
60
        $file = new LocalFile(static::$dir . 'file_get_contents.test');
61
62
        $file->put('content stuff');
63
64
        static::assertEquals(['content stuff'], $file->getContents());
65
    }
66
67
    public function testFileGetContentsReturnsEmptyArrayIfFileDoesNotExist()
68
    {
69
        $file = new LocalFile(static::$dir . 'file_get_contents_empty.test');
70
        static::assertEquals([], $file->getContents());
71
    }
72
73
    public function testCompression()
74
    {
75
        $file = (new LocalFile(static::$dir . 'file_compression.test'))
76
            ->setCompression(Gzip::NAME);
77
        static::assertEquals('gzip', $file->getCompression());
78
    }
79
80
    public function testEncoding()
81
    {
82
        $file = (new LocalFile(static::$dir . 'file_encoding.test'))
83
            ->setEncoding('UTF-8');
84
        static::assertEquals('UTF-8', $file->getEncoding());
85
    }
86
87
    public function testToString()
88
    {
89
        $file = new LocalFile(static::$dir . 'to_string.test');
90
        static::assertEquals($file->getPath(), $file);
91
    }
92
93
    public function testGetContentsForCompressedFile()
94
    {
95
        $file = new LocalFile(static::$dir . 'file_uncompressed.test');
96
        $file->put('uncompressed contents');
97
98
        $gzip = new Gzip();
99
        $compressed = $gzip->compress($file, ['keepOldFile' => true]);
100
101
        static::assertEquals(['uncompressed contents'], $compressed->getContents());
102
    }
103
104
    public function testGetContentsForCompressedFileDeletesTheUncompressedFileAfterwards()
105
    {
106
        $file = new LocalFile(static::$dir . 'file_uncompressed_todelete.test');
107
        $file->put('uncompressed contents');
108
109
        $gzip = new Gzip();
110
        $compressed = $gzip->compress($file, ['keepOldFile' => true]);
111
112
        $compressed->getContents();
113
114
        static::assertFalse(
115
            file_exists(static::$dir . 'file_uncompressed_todelete'),
116
            "The uncompressed file should be deleted"
117
        );
118
    }
119
120
    public function testSetEncodingModifiesTheEncoding()
121
    {
122
        $file = new LocalFile(static::$dir . 'file_set_encoding.test');
123
        $file->put('uncompressed contents');
124
125
        $file->setEncoding('us-ascii');
126
127
        static::assertEquals('us-ascii', $file->getEncoding());
128
    }
129
130 View Code Duplication
    public function testSetEncodingReturnsIsFluent()
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...
131
    {
132
        $file = new LocalFile(static::$dir . 'file_set_encoding2.test');
133
        $file->put('uncompressed contents');
134
135
        $newFile = $file->setEncoding('us-ascii');
136
137
        static::assertNotNull($newFile);
138
        static::assertSame($file, $newFile);
139
    }
140
141 View Code Duplication
    public function testSetCompressionModifiesTheEncoding()
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...
142
    {
143
        $file = new LocalFile(static::$dir . 'file_set_compression.test');
144
        $file->put('uncompressed contents');
145
146
        $file->setCompression(Gzip::NAME);
147
148
        static::assertEquals(Gzip::NAME, $file->getCompression());
149
    }
150
151 View Code Duplication
    public function testSetCompressionReturnsIsFluent()
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...
152
    {
153
        $file = new LocalFile(static::$dir . 'file_set_compression2.test');
154
        $file->put('uncompressed contents');
155
156
        $newFile = $file->setCompression(Gzip::NAME);
157
158
        static::assertNotNull($newFile);
159
        static::assertSame($file, $newFile);
160
    }
161
162
    public function testGetDirectoryReturnsJustTheDirectory()
163
    {
164
        $file = new LocalFile(static::$dir . 'file_dont_care.test');
165
166
        static::assertEquals(static::$dir, $file->getDirectory());
167
    }
168
169
    public function testGetStream()
170
    {
171
        $file = new LocalFile(static::$dir . 'file_get_stream.test');
172
173
        $stream = $file->getStream();
174
175
        static::assertInstanceOf(StreamInterface::class, $stream);
176
177
        $stream->write('some text');
178
        $stream->close();
179
180
        static::assertEquals('some text', $file->read());
181
    }
182
}
183