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 |
||
| 12 | class FileProviderTest extends \PHPUnit_Framework_TestCase |
||
| 13 | { |
||
| 14 | public function testGetImageByExtension() |
||
| 15 | { |
||
| 16 | $method = $this->getMethod('getImageByExtension'); |
||
| 17 | $provider = new FileProvider(); |
||
| 18 | |||
| 19 | $data = [ |
||
| 20 | 'archive.png' => ['zip', 'rar', 'tar', 'gz'], |
||
| 21 | 'audio.png' => ['wav', 'mp3', 'flac', 'aac', 'aiff', 'm4a', 'ogg', 'oga', 'wma'], |
||
| 22 | 'code.png' => ['php', 'html', 'css', 'js', 'vb', 'phar', 'py', 'jar', 'json', 'yml'], |
||
| 23 | 'excel.png' => ['xls', 'xlt', 'xlm', 'xlsx', 'xlsm', 'xltx', 'xltm'], |
||
| 24 | 'image.png' => ['png', 'jpg', 'jpeg', 'gif', 'bmp', 'tiff', 'ai', 'psd'], |
||
| 25 | 'movie.png' => ['mp4', 'avi', 'mkv', 'mpg', 'mpeg'], |
||
| 26 | 'pdf.png' => ['pdf'], |
||
| 27 | 'powerpoint.png' => ['ppt', 'pot', 'pos', 'pps', 'pptx', 'pptm', 'potx', 'potm', 'ppam', 'ppsx', 'ppsm', 'sldx', 'sldm'], |
||
| 28 | 'text.png' => ['txt'], |
||
| 29 | 'word.png' => ['doc', 'dot', 'wbk', 'docx', 'docm', 'dotx', 'dotm', 'docb'], |
||
| 30 | 'default.png' => ['foo', 'bar'] |
||
| 31 | ]; |
||
| 32 | |||
| 33 | foreach ($data as $expected => $extensions) { |
||
| 34 | foreach ($extensions as $extension) { |
||
| 35 | $this->assertEquals($expected, $method->invokeArgs($provider, [$extension])); |
||
| 36 | } |
||
| 37 | } |
||
| 38 | } |
||
| 39 | |||
| 40 | public function testSupports() |
||
| 41 | { |
||
| 42 | $provider = new FileProvider(); |
||
| 43 | $this->assertTrue($provider->supports(AbstractProvider::SUPPORT_DOWNLOAD)); |
||
| 44 | $this->assertFalse($provider->supports(AbstractProvider::SUPPORT_EMBED)); |
||
| 45 | $this->assertTrue($provider->supports(AbstractProvider::SUPPORT_IMAGE)); |
||
| 46 | $this->assertFalse($provider->supports('foo')); |
||
| 47 | } |
||
| 48 | |||
| 49 | public function testWriteToFilesystem() |
||
| 50 | { |
||
| 51 | $this->setExpectedException(FilesystemException::class); |
||
| 52 | |||
| 53 | $filesystem = m::mock(Filesystem::class); |
||
| 54 | $filesystem->shouldReceive('writeStream')->andReturn(0); |
||
| 55 | |||
| 56 | $file = m::mock(UploadedFile::class); |
||
| 57 | $file->shouldReceive('getRealPath')->andReturn('/foo'); |
||
| 58 | |||
| 59 | $provider = new FileProvider(); |
||
| 60 | $provider->setFilesystem($filesystem); |
||
| 61 | $method = $this->getMethod('writeToFilesystem'); |
||
| 62 | $method->invokeArgs($provider, [$file, 'foo']); |
||
| 63 | } |
||
| 64 | |||
| 65 | protected static function getMethod($name) |
||
| 66 | { |
||
| 67 | $class = new \ReflectionClass(FileProvider::class); |
||
| 68 | $method = $class->getMethod($name); |
||
| 69 | $method->setAccessible(true); |
||
| 70 | return $method; |
||
| 71 | } |
||
| 72 | } |
||
| 73 |