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