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 |
||
| 20 | class FileTest extends \PHPUnit_Framework_TestCase |
||
| 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 |