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 |
||
| 10 | class CsvTwigTest extends BaseTwigTest |
||
| 11 | { |
||
| 12 | protected static $TEMP_PATH = '/../../tmp/csv/'; |
||
| 13 | |||
| 14 | // |
||
| 15 | // PhpUnit |
||
| 16 | // |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @return array |
||
| 20 | */ |
||
| 21 | public function formatProvider() |
||
| 22 | { |
||
| 23 | return [['csv']]; |
||
| 24 | } |
||
| 25 | |||
| 26 | // |
||
| 27 | // Tests |
||
| 28 | // |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @param string $format |
||
| 32 | * |
||
| 33 | * @throws \Exception |
||
| 34 | * |
||
| 35 | * @dataProvider formatProvider |
||
| 36 | */ |
||
| 37 | public function testBasic($format) |
||
| 38 | { |
||
| 39 | $path = $this->getDocument('documentSimple', $format); |
||
| 40 | |||
| 41 | static::assertFileExists($path, 'File does not exist'); |
||
| 42 | static::assertGreaterThan(0, filesize($path), 'File is empty'); |
||
| 43 | static::assertStringEqualsFile($path, '"Foo","Bar"'.PHP_EOL.'"Hello","World"'.PHP_EOL, 'Unexpected content'); |
||
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @param string $format |
||
| 48 | * |
||
| 49 | * @throws \Exception |
||
| 50 | * |
||
| 51 | * @dataProvider formatProvider |
||
| 52 | */ |
||
| 53 | public function testDocumentTemplate($format) |
||
| 54 | { |
||
| 55 | $path = $this->getDocument('documentTemplate.csv', $format); |
||
| 56 | |||
| 57 | static::assertFileExists($path, 'File does not exist'); |
||
| 58 | static::assertGreaterThan(0, filesize($path), 'File is empty'); |
||
| 59 | static::assertStringEqualsFile($path, '"Hello2","World"'.PHP_EOL.'"Foo","Bar2"'.PHP_EOL, 'Unexpected content'); |
||
| 60 | } |
||
| 61 | } |
||
| 62 |