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 |
||
| 20 | final class DiscountBuilderTest extends TestCase |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * @test |
||
| 24 | */ |
||
| 25 | public function buildFromJsonSingle() |
||
| 26 | { |
||
| 27 | $json = file_get_contents(dirname(__FILE__) . '/json-files/single-discount.json'); |
||
| 28 | $data = json_decode($json, true)[0]; |
||
| 29 | |||
| 30 | $imageBuilderMock = Mockery::mock(ImageBuilderInterface::class); |
||
| 31 | $imageBuilderMock->shouldReceive('buildFromArray')->with($data['imageLookupKeys'])->once()->andReturn(new ImageCollection()); |
||
| 32 | |||
| 33 | $builder = new DiscountBuilder($imageBuilderMock); |
||
| 34 | $discount = $builder->buildFromJson($json)[0]; |
||
| 35 | |||
| 36 | self::assertInstanceOf(Discount::class, $discount); |
||
| 37 | self::assertSame($data['uuid'], (string) $discount->getUuid()); |
||
| 38 | self::assertSame($data['name'], $discount->getName()); |
||
| 39 | self::assertSame($data['description'], $discount->getDescription()); |
||
| 40 | self::assertInstanceOf(ImageCollection::class, $discount->getImageCollection()); |
||
| 41 | self::assertSame($data['externalReference'], $discount->getExternalReference()); |
||
| 42 | self::assertSame($data['etag'], $discount->getEtag()); |
||
| 43 | self::assertEquals(new DateTime($data['updated']), $discount->getUpdatedAt()); |
||
| 44 | self::assertSame($data['updatedBy'], (string) $discount->getUpdatedBy()); |
||
| 45 | self::assertEquals(new DateTime($data['created']), $discount->getCreatedAt()); |
||
| 46 | |||
| 47 | if ($data['amount']) { |
||
| 48 | self::assertInstanceOf(Money::class, $discount->getAmount()); |
||
| 49 | self::assertSame((string) $data['amount']['amount'], $discount->getAmount()->getAmount()); |
||
| 50 | self::assertSame($data['amount']['currencyId'], $discount->getAmount()->getCurrency()->getCode()); |
||
| 51 | } else { |
||
| 52 | self::assertSame((float) $data['percentage'], $discount->getPercentage()); |
||
| 53 | } |
||
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @test |
||
| 58 | */ |
||
| 59 | public function buildFromJsonMultiple() |
||
| 60 | { |
||
| 61 | $json = file_get_contents(dirname(__FILE__) . '/json-files/multiple-discount.json'); |
||
| 62 | $data = json_decode($json, true); |
||
| 63 | |||
| 64 | $imageBuilderMock = Mockery::mock(ImageBuilderInterface::class); |
||
| 65 | $imageBuilderMock->shouldReceive('buildFromArray')->with($data[0]['imageLookupKeys'])->once()->andReturn(new ImageCollection()); |
||
| 66 | $imageBuilderMock->shouldReceive('buildFromArray')->with($data[1]['imageLookupKeys'])->once()->andReturn(new ImageCollection()); |
||
| 67 | |||
| 68 | $builder = new DiscountBuilder($imageBuilderMock); |
||
| 69 | $discounts = $builder->buildFromJson($json); |
||
| 70 | |||
| 71 | foreach ($discounts as $index => $discount) { |
||
| 72 | self::assertInstanceOf(Discount::class, $discount); |
||
| 73 | self::assertSame($data[$index]['uuid'], (string) $discount->getUuid()); |
||
| 74 | self::assertSame($data[$index]['name'], $discount->getName()); |
||
| 75 | self::assertSame($data[$index]['description'], $discount->getDescription()); |
||
| 76 | self::assertInstanceOf(ImageCollection::class, $discount->getImageCollection()); |
||
| 77 | self::assertSame($data[$index]['externalReference'], $discount->getExternalReference()); |
||
| 78 | self::assertSame($data[$index]['etag'], $discount->getEtag()); |
||
| 79 | self::assertEquals(new DateTime($data[$index]['updated']), $discount->getUpdatedAt()); |
||
| 80 | self::assertSame($data[$index]['updatedBy'], (string) $discount->getUpdatedBy()); |
||
| 81 | self::assertEquals(new DateTime($data[$index]['created']), $discount->getCreatedAt()); |
||
| 82 | |||
| 83 | if ($data[$index]['amount']) { |
||
| 84 | self::assertInstanceOf(Money::class, $discount->getAmount()); |
||
| 85 | self::assertSame((string) $data[$index]['amount']['amount'], $discount->getAmount()->getAmount()); |
||
| 86 | self::assertSame($data[$index]['amount']['currencyId'], $discount->getAmount()->getCurrency()->getCode()); |
||
| 87 | } else { |
||
| 88 | self::assertSame((float) $data[$index]['percentage'], $discount->getPercentage()); |
||
| 89 | } |
||
| 90 | } |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @test |
||
| 95 | */ |
||
| 96 | public function buildFromArray() |
||
| 97 | { |
||
| 98 | $json = file_get_contents(dirname(__FILE__) . '/json-files/multiple-discount.json'); |
||
| 99 | $data = json_decode($json, true); |
||
| 100 | |||
| 101 | $imageBuilderMock = Mockery::mock(ImageBuilderInterface::class); |
||
| 102 | $imageBuilderMock->shouldReceive('buildFromArray')->with($data[0]['imageLookupKeys'])->once()->andReturn(new ImageCollection()); |
||
| 103 | $imageBuilderMock->shouldReceive('buildFromArray')->with($data[1]['imageLookupKeys'])->once()->andReturn(new ImageCollection()); |
||
| 104 | |||
| 105 | $builder = new DiscountBuilder($imageBuilderMock); |
||
| 106 | $discounts = $builder->buildFromArray($data); |
||
| 107 | |||
| 108 | self::assertInstanceOf(DiscountCollection::class, $discounts); |
||
| 109 | |||
| 110 | foreach ($discounts->getAll() as $discount) { |
||
| 111 | self::assertInstanceOf(Discount::class, $discount); |
||
| 112 | } |
||
| 113 | } |
||
| 114 | } |
||
| 115 |