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 |
||
| 21 | class MySqlDumpSourceTest extends \PHPUnit_Framework_TestCase |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * @test |
||
| 25 | */ |
||
| 26 | public function successfulDumpAndCleanup() |
||
| 27 | { |
||
| 28 | $settings = require_once file_exists($config = __DIR__ .'/../Fixtures/config/mysqldump.php') ? $config : __DIR__ .'/../Fixtures/config/mysqldump.dist.php'; |
||
| 29 | $source = new MySqlDumpSource($settings['database'], $settings['username'], $settings['password'], $settings['host'], $settings['port']); |
||
| 30 | |||
| 31 | $source->setEventDispatcher($eventDispatcher = new EventDispatcher()); |
||
| 32 | |||
| 33 | $files = $source->fetch(); |
||
| 34 | |||
| 35 | $this->assertSame(1, count($files), 'It should dump one mysql file.'); |
||
| 36 | |||
| 37 | $this->assertTrue(file_exists($files[0]->getPath()), 'That file should exist prior to termination of backup process.'); |
||
| 38 | |||
| 39 | $eventDispatcher->dispatch(BackupEvents::TERMINATE, new BackupEvent()); |
||
| 40 | |||
| 41 | $this->assertFalse(file_exists($files[0]->getPath()), 'That file should not exist after termination of backup process.'); |
||
| 42 | } |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @test |
||
| 46 | * |
||
| 47 | * @expectedException \RunOpenCode\Backup\Exception\SourceException |
||
| 48 | */ |
||
| 49 | public function connectionError() |
||
| 50 | { |
||
| 51 | $settings = require_once file_exists($config = __DIR__ .'/../Fixtures/config/mysqldump.php') ? $config : __DIR__ .'/../Fixtures/config/mysqldump.dist.php'; |
||
| 52 | $source = new MySqlDumpSource($settings['database'], $settings['username'], $settings['password'], 'www.non-existing-domain.com', $settings['port']); |
||
| 53 | |||
| 54 | $source->setEventDispatcher($eventDispatcher = new EventDispatcher()); |
||
| 55 | |||
| 56 | $source->fetch(); |
||
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @test |
||
| 61 | * |
||
| 62 | * @expectedException \RunOpenCode\Backup\Exception\SourceException |
||
| 63 | */ |
||
| 64 | public function databaseError() |
||
| 65 | { |
||
| 66 | $settings = require_once file_exists($config = __DIR__ .'/../Fixtures/config/mysqldump.php') ? $config : __DIR__ .'/../Fixtures/config/mysqldump.dist.php'; |
||
| 67 | $source = new MySqlDumpSource('There is no way that you have database with this name.', $settings['username'], $settings['password'], $settings['host'], $settings['port']); |
||
| 68 | |||
| 69 | $source->setEventDispatcher($eventDispatcher = new EventDispatcher()); |
||
| 70 | |||
| 71 | $source->fetch(); |
||
| 72 | } |
||
| 73 | } |