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 EntityFactoryTest extends AbstractEntityManagerTest |
||
11 | { |
||
12 | 3 | protected function getClientNames() |
|
13 | { |
||
14 | 3 | return array_merge(parent::getClientNames(), ['test-reference-client']); |
|
15 | } |
||
16 | |||
17 | |||
18 | 1 | public function testEntityLoading() |
|
19 | { |
||
20 | 1 | $repository = $this->getManager()->getRepository(TestEntity::class); |
|
21 | |||
22 | 1 | $this->getResponseMock()->append( |
|
23 | 1 | new Response( |
|
24 | 1 | 200, |
|
25 | 1 | [], |
|
26 | 1 | json_encode( |
|
27 | [ |
||
28 | 1 | 'jsonrpc' => '2.0', |
|
29 | 1 | 'id' => 'test', |
|
30 | 'result' => [ |
||
31 | 1 | 'id' => '1', |
|
32 | 1 | 'payload' => 'test-payload', |
|
33 | 1 | ], |
|
34 | ] |
||
35 | 1 | ) |
|
36 | 1 | ) |
|
37 | 1 | ); |
|
38 | |||
39 | /** @var TestEntity $entity */ |
||
40 | 1 | $entity = $repository->find(1); |
|
41 | |||
42 | 1 | self::assertInstanceOf(TestEntity::class, $entity); |
|
43 | 1 | self::assertEquals(1, $entity->getId()); |
|
44 | 1 | self::assertInternalType('int', $entity->getId()); |
|
45 | 1 | self::assertEquals('test-payload', $entity->getPayload()); |
|
46 | 1 | } |
|
47 | |||
48 | 1 | public function testInheritanceLoading() |
|
49 | { |
||
50 | 1 | $repository = $this->getManager()->getRepository(SubEntity::class); |
|
51 | 1 | $this->getResponseMock()->append( |
|
52 | 1 | new Response( |
|
53 | 1 | 200, |
|
54 | 1 | [], |
|
55 | 1 | json_encode( |
|
56 | [ |
||
57 | 1 | 'jsonrpc' => '2.0', |
|
58 | 1 | 'id' => 'test', |
|
59 | 'result' => [ |
||
60 | 1 | 'id' => 2, |
|
61 | 1 | 'payload' => 'test-payload', |
|
62 | 1 | 'sub-payload' => 'sub-payload', |
|
63 | 1 | ], |
|
64 | ] |
||
65 | 1 | ) |
|
66 | 1 | ) |
|
67 | 1 | ); |
|
68 | |||
69 | /** @var SubEntity $entity */ |
||
70 | 1 | $entity = $repository->find(2); |
|
71 | |||
72 | 1 | self::assertInstanceOf(SubEntity::class, $entity); |
|
73 | 1 | self::assertEquals(2, $entity->getId()); |
|
74 | 1 | self::assertEquals('test-payload', $entity->getPayload()); |
|
75 | 1 | self::assertEquals('sub-payload', $entity->getSubPayload()); |
|
76 | 1 | } |
|
77 | |||
78 | 1 | public function testCompositeKeyLoading() |
|
79 | { |
||
80 | 1 | $repository = $this->getManager()->getRepository(CompositeKeyEntity::class); |
|
81 | 1 | $this->getResponseMock()->append( |
|
82 | 1 | new Response( |
|
83 | 1 | 200, |
|
84 | 1 | [], |
|
85 | 1 | json_encode( |
|
86 | [ |
||
87 | 1 | 'jsonrpc' => '2.0', |
|
88 | 1 | 'id' => 'test', |
|
89 | 'result' => [ |
||
90 | 1 | 'first_key' => 2, |
|
91 | 1 | 'second_key' => 'test', |
|
92 | 1 | 'payload' => 'test-payload', |
|
93 | 1 | ], |
|
94 | ] |
||
95 | 1 | ) |
|
96 | 1 | ) |
|
97 | 1 | ); |
|
98 | |||
99 | /** @var CompositeKeyEntity $entity */ |
||
100 | 1 | $entity = $repository->find(['firstKey' => 2, 'secondKey' => 'test']); |
|
101 | |||
102 | 1 | self::assertInstanceOf(CompositeKeyEntity::class, $entity); |
|
103 | 1 | self::assertEquals(2, $entity->getFirstKey()); |
|
104 | 1 | self::assertEquals('test', $entity->getSecondKey()); |
|
105 | 1 | self::assertEquals('test-payload', $entity->getPayload()); |
|
106 | 1 | } |
|
107 | } |
||
108 |