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 CatalogFactoryTest extends PHPUnit_Framework_TestCase |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * @test |
||
| 14 | */ |
||
| 15 | public function getCatalog_IntegrationTest_CatalogJSONIsValid_CatalogIsReturned() |
||
| 16 | { |
||
| 17 | // arrange |
||
| 18 | $catalogFilePath = "catalog.json"; |
||
| 19 | $filesystem = new Filesystem(new MemoryAdapter()); |
||
| 20 | $catalogJSON = <<<JSON |
||
| 21 | [] |
||
| 22 | JSON; |
||
| 23 | $filesystem->write($catalogFilePath, $catalogJSON); |
||
| 24 | |||
| 25 | $catalogFactory = new CatalogFactory($filesystem, $catalogFilePath); |
||
| 26 | |||
| 27 | // act |
||
| 28 | $catalog = $catalogFactory->getCatalog(); |
||
| 29 | |||
| 30 | // assert |
||
| 31 | $this->assertNotNull($catalog, "The catalog should not be null"); |
||
| 32 | } |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @test |
||
| 36 | * @expectedException Wambo\Catalog\Error\CatalogException |
||
| 37 | */ |
||
| 38 | public function getCatalog_IntegrationTest_CatalogJSONIsInvalid_CatalogExceptionIsThrown() |
||
| 39 | { |
||
| 40 | // arrange |
||
| 41 | $catalogFilePath = "catalog.json"; |
||
| 42 | $filesystem = new Filesystem(new MemoryAdapter()); |
||
| 43 | $catalogJSON = <<<JSON |
||
| 44 | [ |
||
| 45 | { |
||
| 46 | "sku": "" |
||
| 47 | } |
||
| 48 | ] |
||
| 49 | JSON; |
||
| 50 | $filesystem->write($catalogFilePath, $catalogJSON); |
||
| 51 | |||
| 52 | $catalogFactory = new CatalogFactory($filesystem, $catalogFilePath); |
||
| 53 | |||
| 54 | // act |
||
| 55 | $catalogFactory->getCatalog(); |
||
| 56 | } |
||
| 57 | } |
||
| 58 |