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->assertEquals(10.31, $shipping->getCost()); |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * @test |
||
82 | */ |
||
83 | public function xmlSerializeMustAppendShippingData() |
||
84 | { |
||
85 | $this->markTestSkipped(); |
||
86 | |||
87 | $xml = simplexml_load_string('<?xml version="1.0" encoding="UTF-8"?><test />'); |
||
88 | |||
89 | $address = $this->createMock(Address::class); |
||
90 | $shipping = new Shipping(Type::TYPE_PAC, $address, '10.31'); |
||
91 | |||
92 | $address->expects($this->once()) |
||
93 | ->method('xmlSerialize') |
||
94 | ->with($this->isInstanceOf('SimpleXMLElement')); |
||
95 | |||
96 | $shipping->xmlSerialize($xml); |
||
97 | |||
98 | $this->assertEquals(1, (string) $xml->shipping->type); |
||
99 | $this->assertEquals(10.31, (string) $xml->shipping->cost); |
||
100 | } |
||
101 | } |
||
102 |