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 |
||
| 6 | class ShippingTest extends \PHPUnit_Framework_TestCase |
||
| 7 | { |
||
| 8 | /** |
||
| 9 | * @test |
||
| 10 | * @expectedException \InvalidArgumentException |
||
| 11 | */ |
||
| 12 | public function constructorMustRaiseExceptionWhenTypeIsInvalid() |
||
| 13 | { |
||
| 14 | new Shipping(-10); |
||
| 15 | } |
||
| 16 | |||
| 17 | /** |
||
| 18 | * @test |
||
| 19 | */ |
||
| 20 | public function constructorMustBeAbleToReceiveTypeOnly() |
||
| 21 | { |
||
| 22 | $shipping = new Shipping(Type::TYPE_PAC); |
||
| 23 | |||
| 24 | $this->assertAttributeEquals(Type::TYPE_PAC, 'type', $shipping); |
||
| 25 | $this->assertAttributeEquals(null, 'address', $shipping); |
||
| 26 | $this->assertAttributeEquals(null, 'cost', $shipping); |
||
| 27 | } |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @test |
||
| 31 | */ |
||
| 32 | public function constructorMustBeAbleToReceiveTypeAndCost() |
||
| 33 | { |
||
| 34 | $shipping = new Shipping(Type::TYPE_PAC, null, '10.31'); |
||
| 35 | |||
| 36 | $this->assertAttributeEquals(Type::TYPE_PAC, 'type', $shipping); |
||
| 37 | $this->assertAttributeEquals(null, 'address', $shipping); |
||
| 38 | $this->assertAttributeEquals(10.31, 'cost', $shipping); |
||
| 39 | } |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @test |
||
| 43 | */ |
||
| 44 | public function constructorMustBeAbleToReceiveTypeAndAddress() |
||
| 45 | { |
||
| 46 | $address = $this->createMock(Address::class); |
||
| 47 | $shipping = new Shipping(Type::TYPE_PAC, $address); |
||
| 48 | |||
| 49 | $this->assertAttributeEquals(Type::TYPE_PAC, 'type', $shipping); |
||
| 50 | $this->assertAttributeSame($address, 'address', $shipping); |
||
| 51 | $this->assertAttributeEquals(null, 'cost', $shipping); |
||
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @test |
||
| 56 | */ |
||
| 57 | public function constructorMustBeAbleToReceiveAllArguments() |
||
| 58 | { |
||
| 59 | $address = $this->createMock(Address::class); |
||
| 60 | $shipping = new Shipping(Type::TYPE_PAC, $address, '10.31'); |
||
| 61 | |||
| 62 | $this->assertAttributeEquals(Type::TYPE_PAC, 'type', $shipping); |
||
| 63 | $this->assertAttributeSame($address, 'address', $shipping); |
||
| 64 | $this->assertAttributeEquals(10.31, 'cost', $shipping); |
||
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @test |
||
| 69 | */ |
||
| 70 | public function getterShouldReturnConfiguredData() |
||
| 71 | { |
||
| 72 | $address = $this->createMock(Address::class); |
||
| 73 | $shipping = new Shipping(Type::TYPE_PAC, $address, '10.31'); |
||
| 74 | |||
| 75 | $this->assertEquals(Type::TYPE_PAC, $shipping->getType()); |
||
| 76 | $this->assertSame($address, $shipping->getAddress()); |
||
| 77 | $this->assertSame('10.31', $shipping->getCost()); |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @test |
||
| 82 | */ |
||
| 83 | public function xmlSerializeMustAppendFormattedShippingData() |
||
| 84 | { |
||
| 85 | $data = simplexml_load_string('<?xml version="1.0" encoding="UTF-8"?><data />'); |
||
| 86 | |||
| 87 | $address = new Address('BA', 'Salvador', '40999-999', 'Red River', 'Beco Sem Nome', 25, 'Buteco do França'); |
||
| 88 | $shipping = new Shipping(Type::TYPE_PAC, $address, '10.31'); |
||
| 89 | |||
| 90 | $xml = $shipping->xmlSerialize($data); |
||
| 91 | |||
| 92 | $this->assertSame('1', (string) $xml->shipping->type); |
||
| 93 | $this->assertSame('10.31', (string) $xml->shipping->cost); |
||
| 94 | $this->assertSame('BRA', (string) $xml->shipping->address->country); |
||
| 95 | $this->assertSame('BA', (string) $xml->shipping->address->state); |
||
| 96 | $this->assertSame('Salvador', (string) $xml->shipping->address->city); |
||
| 97 | $this->assertSame('40999999', (string) $xml->shipping->address->postalCode); |
||
| 98 | $this->assertSame('Red River', (string) $xml->shipping->address->district); |
||
| 99 | $this->assertSame('Beco Sem Nome', (string) $xml->shipping->address->street); |
||
| 100 | $this->assertSame('25', (string) $xml->shipping->address->number); |
||
| 101 | $this->assertSame('Buteco do França', (string) $xml->shipping->address->complement); |
||
| 102 | } |
||
| 104 |