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 |
||
22 | class GzipArchiveProcessorTest extends \PHPUnit_Framework_TestCase |
||
23 | { |
||
24 | /** |
||
25 | * @test |
||
26 | */ |
||
27 | public function gzipAndCleanUp() |
||
28 | { |
||
29 | $source = new GlobSource(realpath(__DIR__ . '/../Fixtures/glob/globSet1') . '/*'); |
||
30 | $files = $source->fetch(); |
||
31 | |||
32 | $this->assertSame(3, count($files), 'There are 3 files to archive.'); |
||
33 | |||
34 | $processor = new GzipArchiveProcessor('-czvf', 'archive.tar.gz'); |
||
35 | $processor->setEventDispatcher($eventDispatcher = new EventDispatcher()); |
||
36 | |||
37 | $processedFiles = $processor->process($files); |
||
38 | |||
39 | $this->assertSame(1, count($processedFiles), 'There is one compressed file'); |
||
40 | |||
41 | /** |
||
42 | * @var FileInterface $processedFile |
||
43 | */ |
||
44 | $processedFile = $processedFiles[0]; |
||
45 | |||
46 | $this->assertTrue(file_exists($processedFile->getPath()), 'Gzip archive exists.'); |
||
47 | |||
48 | $eventDispatcher->dispatch(BackupEvents::TERMINATE, new BackupEvent()); |
||
49 | |||
50 | $this->assertFalse(file_exists($processedFile->getPath()), 'Gzip archive is cleaned up.'); |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * @test |
||
55 | * |
||
56 | * @expectedException \RunOpenCode\Backup\Exception\ProcessorException |
||
57 | */ |
||
58 | public function couldNotProcessEmptyCollection() |
||
59 | { |
||
60 | $processor = new GzipArchiveProcessor('-czvf', 'archive.tar.gz'); |
||
61 | $processor->setEventDispatcher($eventDispatcher = new EventDispatcher()); |
||
62 | |||
63 | $processor->process(array()); |
||
64 | } |
||
65 | } |