Completed
Push — feature/0.7.0 ( 38440f...02d6a4 )
by Ryuichi
07:28
created

FileTest   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 202
Duplicated Lines 100 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 0
Metric Value
dl 202
loc 202
rs 10
c 0
b 0
f 0
wmc 16
lcom 2
cbo 4

16 Methods

Rating   Name   Duplication   Size   Complexity  
A okFileName() 4 4 1
A okFileExtension() 4 4 1
A okFilePath() 4 4 1
A okFileAbsolutePath() 7 7 1
A okFileReadable() 5 5 1
A okFileWritable() 5 5 1
A okFileExecutable() 5 5 1
A okFile() 5 5 1
A okDirectory() 5 5 1
A okLink() 8 8 1
A okFileSize() 4 4 1
A okFileDelete() 6 6 1
A okDirectoryDelete() 5 5 1
A okFileRename() 9 9 1
A okLastModified() 4 4 1
A ngFileRename() 5 5 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
namespace WebStream\IO\Test;
3
4
require_once dirname(__FILE__) . '/../File.php';
5
require_once dirname(__FILE__) . '/../Writer/OutputStreamWriter.php';
6
require_once dirname(__FILE__) . '/../Writer/SimpleFileWriter.php';
7
require_once dirname(__FILE__) . '/../Test/Providers/FileProvider.php';
8
require_once dirname(__FILE__) . '/../Test/Modules/IOException.php';
9
10
use WebStream\IO\File;
11
use WebStream\IO\Writer\SimpleFileWriter;
12
use WebStream\IO\Test\Providers\FileProvider;
13
14
/**
15
 * FileTest
16
 * @author Ryuichi TANAKA.
17
 * @since 2016/08/19
18
 * @version 0.7
19
 */
20 View Code Duplication
class FileTest extends \PHPUnit_Framework_TestCase
0 ignored issues
show
Duplication introduced by
This class 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...
21
{
22
    use FileProvider;
23
24
    /**
25
     * 正常系
26
     * ファイル名を取得できること
27
     * @test
28
     * @dataProvider fileProvider
29
     */
30
    public function okFileName($file)
31
    {
32
        $this->assertEquals($file->getFileName(), "file-test1.txt");
33
    }
34
35
    /**
36
     * 正常系
37
     * ファイル拡張子を取得できること
38
     * @test
39
     * @dataProvider fileProvider
40
     */
41
    public function okFileExtension($file)
42
    {
43
        $this->assertEquals($file->getFileExtension(), "txt");
44
    }
45
46
    /**
47
     * 正常系
48
     * ファイルパスを取得できること
49
     * @test
50
     * @dataProvider fileProvider
51
     */
52
    public function okFilePath($file)
53
    {
54
        $this->assertEquals($file->getFilePath(), dirname(__FILE__) . "/Providers/../Fixtures/" . $file->getFileName());
55
    }
56
57
    /**
58
     * 正常系
59
     * 絶対ファイルパスを取得できること
60
     * @test
61
     * @dataProvider fileLinkProvider
62
     */
63
    public function okFileAbsolutePath($file, $linkPath)
64
    {
65
        symlink($file->getFilePath(), $linkPath);
66
        $linkFile = new File($linkPath);
67
        $this->assertEquals($file->getAbsoluteFilePath(), $linkFile->getAbsoluteFilePath());
68
        unlink($linkPath);
69
    }
70
71
    /**
72
     * 正常系
73
     * ファイルに読み込み権限をチェックできること
74
     * @test
75
     * @dataProvider fileProvider
76
     */
77
    public function okFileReadable($file)
78
    {
79
        chmod($file->getFilePath(), 0444);
80
        $this->assertTrue($file->isReadable());
81
    }
82
83
    /**
84
     * 正常系
85
     * ファイルに書き込み権限をチェックできること
86
     * @test
87
     * @dataProvider fileProvider
88
     */
89
    public function okFileWritable($file)
90
    {
91
        chmod($file->getFilePath(), 0666);
92
        $this->assertTrue($file->isWritable());
93
    }
94
95
    /**
96
     * 正常系
97
     * ファイルに実行権限をチェックできること
98
     * @test
99
     * @dataProvider fileProvider
100
     */
101
    public function okFileExecutable($file)
102
    {
103
        chmod($file->getFilePath(), 0555);
104
        $this->assertTrue($file->isExecutable());
105
    }
106
107
    /**
108
     * 正常系
109
     * ファイルであるかどうかチェックできること
110
     * @test
111
     * @dataProvider fileProvider
112
     */
113
    public function okFile($file)
114
    {
115
        $this->assertTrue($file->exists());
116
        $this->assertTrue($file->isFile());
117
    }
118
119
    /**
120
     * 正常系
121
     * ディレクトリであるかどうかチェックできること
122
     * @test
123
     * @dataProvider directoryProvider
124
     */
125
    public function okDirectory($file)
126
    {
127
        $this->assertTrue($file->exists());
128
        $this->assertTrue($file->isDirectory());
129
    }
130
131
    /**
132
     * 正常系
133
     * リンクであるかどうかチェックできること
134
     * @test
135
     * @dataProvider fileLinkProvider
136
     */
137
    public function okLink($file, $linkPath)
138
    {
139
        symlink($file->getFilePath(), $linkPath);
140
        $linkFile = new File($linkPath);
141
        $this->assertTrue($linkFile->exists());
142
        $this->assertTrue($linkFile->isLink());
143
        unlink($linkPath);
144
    }
145
146
    /**
147
     * 正常系
148
     * ファイルサイズを取得できること
149
     * @test
150
     * @dataProvider fileProvider
151
     */
152
    public function okFileSize($file)
153
    {
154
        $this->assertEquals($file->length(), 5);
155
    }
156
157
    /**
158
     * 正常系
159
     * ファイルを削除できること
160
     * @test
161
     * @dataProvider tmpFileProvider
162
     */
163
    public function okFileDelete($file)
164
    {
165
        $writer = new SimpleFileWriter($file->getFilePath());
166
        $writer->write("test");
167
        $this->assertTrue($file->delete());
168
    }
169
170
    /**
171
     * 正常系
172
     * ディレクトリを削除できること
173
     * @test
174
     * @dataProvider tmpDirectoryProvider
175
     */
176
    public function okDirectoryDelete($file)
177
    {
178
        mkdir($file->getFilePath(), 0777);
179
        $this->assertTrue($file->delete());
180
    }
181
182
    /**
183
     * 正常系
184
     * ファイルサイズを取得できること
185
     * @test
186
     * @dataProvider tmpFileProvider
187
     */
188
    public function okFileRename($file)
189
    {
190
        $writer = new SimpleFileWriter($file->getFilePath());
191
        $writer->write("test");
192
        $this->assertTrue($file->renameTo("/tmp/file-test-rename.txt"));
193
194
        $renameFile = new File("/tmp/file-test-rename.txt");
195
        $renameFile->delete();
196
    }
197
198
    /**
199
     * 正常系
200
     * ファイル最終更新日時を取得できること
201
     * @test
202
     * @dataProvider fileProvider
203
     */
204
    public function okLastModified($file)
205
    {
206
        $this->assertEquals(gettype($file->lastModified()), "integer");
207
    }
208
209
    /**
210
     * 異常系
211
     * ファイルのリネームに失敗すること
212
     * @test
213
     * @dataProvider renameFailureProvider
214
     * @expectedException WebStream\Exception\Extend\IOException
215
     */
216
    public function ngFileRename($file)
217
    {
218
        chmod($file->getFilePath(), 0444);
219
        $file->renameTo("/tmp/file-test-rename/" . $file->getFileName());
220
    }
221
}
222