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 |
||
9 | class MetableTest extends AbstractTestCase |
||
10 | { |
||
11 | public function testMeta() |
||
12 | { |
||
13 | $m = $this->getMetableStub(); |
||
14 | |||
15 | $this->assertInstanceOf(HasMeta::class, $m->meta()); |
||
16 | } |
||
17 | |||
18 | public function testIsDirtyParent() |
||
19 | { |
||
20 | $m = $this->getMetableStub(); |
||
21 | |||
22 | $m->foo = 'bar'; |
||
23 | |||
24 | $this->assertTrue($m->isDirty()); |
||
25 | |||
26 | $this->assertTrue($m->isDirty('foo')); |
||
27 | } |
||
28 | |||
29 | public function testIsDirtyMeta() |
||
30 | { |
||
31 | $m = $this->getMetableStub(); |
||
32 | |||
33 | $item = new MetaItem(['key' => 'foo', 'value' => 'bar']); |
||
34 | |||
35 | $m->meta->add($item); |
||
36 | |||
37 | $this->assertTrue($m->isDirty()); |
||
38 | |||
39 | $this->assertTrue($m->isDirty('value')); |
||
40 | } |
||
41 | |||
42 | public function testGetMetaItemClassNameDefault() |
||
43 | { |
||
44 | $m = $this->getMetableStub(); |
||
45 | |||
46 | $this->assertEquals(MetaItem::class, $m->getMetaItemClassName()); |
||
47 | } |
||
48 | |||
49 | public function testGetMetaItemClassNameCustom() |
||
50 | { |
||
51 | $m = $this->getMetableStub(); |
||
52 | |||
53 | $m->setProperty('metaItemClassname', 'FooClass'); |
||
54 | |||
55 | $this->assertEquals('FooClass', $m->getMetaItemClassName()); |
||
56 | } |
||
57 | |||
58 | public function testGetMetaItemInstance() |
||
59 | { |
||
60 | $m = $this->getMetableStub(); |
||
61 | |||
62 | $this->assertInstanceOf(MetaItem::class, $m->getMetaItemInstance()); |
||
63 | } |
||
64 | |||
65 | public function testHasMeta() |
||
66 | { |
||
67 | $m = $this->getMetableStub(); |
||
68 | |||
69 | $has_meta = $m->hasMeta($m->getMetaItemClassName(), 'model', $m->getMetaItemClassName(), 'id', 'id'); |
||
70 | |||
71 | $this->assertInstanceOf(HasMeta::class, $has_meta); |
||
72 | } |
||
73 | |||
74 | public function testObserveSaveAndCascadeNewItems() |
||
75 | { |
||
76 | $m = $this->createMetableStub(); |
||
77 | |||
78 | $this->assertSame('bar', $m->meta->foo); |
||
79 | } |
||
80 | |||
81 | public function testObserveSaveAndCascadeExistingItems() |
||
82 | { |
||
83 | $m = $this->createMetableStub(); |
||
84 | |||
85 | $m->meta->foo = 'baz'; |
||
86 | |||
87 | $m->save(); |
||
88 | |||
89 | $m = $m::first(); |
||
90 | |||
91 | $this->assertSame('baz', $m->meta->foo); |
||
92 | } |
||
93 | |||
94 | public function testObserveSaveAndCascadeRemoveItems() |
||
95 | { |
||
96 | $m = $this->createMetableStub(); |
||
97 | |||
98 | unset($m->meta[0]); |
||
99 | |||
100 | $m->save(); |
||
101 | |||
102 | $m = $m::first(); |
||
103 | |||
104 | $this->assertCount(0, $m->meta); |
||
105 | } |
||
106 | |||
107 | public function testObserveDeleteAndCascade() |
||
108 | { |
||
109 | $m = $this->createMetableStub(); |
||
110 | |||
111 | $m->delete(); |
||
112 | |||
113 | $this->assertNull(MetaItem::first()); |
||
114 | } |
||
115 | |||
116 | protected function createMetableStub() |
||
117 | { |
||
118 | $m = $this->getMetableStub(); |
||
119 | |||
120 | $item = new MetaItem(['key' => 'foo', 'value' => 'bar']); |
||
121 | |||
122 | $m->meta->add($item); |
||
123 | |||
124 | $m->save(); |
||
125 | |||
126 | return $m::first(); |
||
127 | } |
||
128 | |||
129 | protected function getMetableStub() |
||
130 | { |
||
131 | return new MetableModel; |
||
132 | } |
||
133 | } |
||
134 |