LocalFileTest   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 161
Duplicated Lines 23.6 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

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