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 |
||
16 | class InnmindNeo4jExtensionTest extends \PHPUnit_Framework_TestCase |
||
17 | { |
||
18 | private $c; |
||
19 | private $e; |
||
20 | |||
21 | public function setUp() |
||
22 | { |
||
23 | $this->c = new ContainerBuilder; |
||
24 | $this->e = new InnmindNeo4jExtension; |
||
25 | $config = [ |
||
26 | 'innmind_neo4j' => [ |
||
27 | 'connection' => [ |
||
28 | 'scheme' => 'http', |
||
29 | 'host' => 'docker', |
||
30 | 'port' => 1337, |
||
31 | 'timeout' => 42, |
||
32 | 'user' => 'neo4j', |
||
33 | 'password' => 'ci', |
||
34 | ], |
||
35 | 'types' => ['foo', 'bar'], |
||
36 | 'persister' => 'another_service', |
||
37 | 'metadata_configuration' => 'config', |
||
38 | ], |
||
39 | ]; |
||
40 | |||
41 | $this->e->load($config, $this->c); |
||
42 | } |
||
43 | |||
44 | public function testPersister() |
||
45 | { |
||
46 | $def = $this->c->getDefinition('innmind_neo4j.unit_of_work'); |
||
47 | |||
48 | $this->assertInstanceOf(Reference::class, $def->getArgument(5)); |
||
49 | $this->assertSame('another_service', (string) $def->getArgument(5)); |
||
50 | } |
||
51 | |||
52 | public function testConnection() |
||
53 | { |
||
54 | $def = $this->c->getDefinition('innmind_neo4j.connection.server'); |
||
55 | $this->assertSame('http', $def->getArgument(0)); |
||
56 | $this->assertSame('docker', $def->getArgument(1)); |
||
57 | $this->assertSame(1337, $def->getArgument(2)); |
||
58 | |||
59 | $def = $this->c->getDefinition('innmind_neo4j.connection.authentication'); |
||
60 | $this->assertSame('neo4j', $def->getArgument(0)); |
||
61 | $this->assertSame('ci', $def->getArgument(1)); |
||
62 | |||
63 | $def = $this->c->getDefinition('innmind_neo4j.connection.transactions'); |
||
64 | $this->assertSame(42, $def->getArgument(2)); |
||
65 | |||
66 | $transport = $this->c->getDefinition('innmind_neo4j.connection.transport'); |
||
67 | $this->assertSame(42, $transport->getArgument(4)); |
||
68 | } |
||
69 | |||
70 | public function testTypes() |
||
71 | { |
||
72 | $def = $this->c->getDefinition('innmind_neo4j.types'); |
||
73 | $calls = $def->getMethodCalls(); |
||
74 | |||
75 | $this->assertSame(2, count($calls)); |
||
76 | $this->assertSame('register', $calls[0][0]); |
||
77 | $this->assertSame('foo', $calls[0][1][0]); |
||
78 | $this->assertSame('register', $calls[1][0]); |
||
79 | $this->assertSame('bar', $calls[1][1][0]); |
||
80 | } |
||
81 | |||
82 | public function testMetadataConfiguration() |
||
83 | { |
||
84 | $def = $this->c->getDefinition('innmind_neo4j.metadata_builder'); |
||
85 | |||
86 | $this->assertInstanceOf(Reference::class, $def->getArgument(2)); |
||
87 | $this->assertSame('config', (string) $def->getArgument(2)); |
||
88 | } |
||
89 | |||
90 | public function testDefaultPersister() |
||
91 | { |
||
92 | $c = new ContainerBuilder; |
||
93 | $this->e->load([], $c); |
||
94 | $def = $c->getDefinition('innmind_neo4j.unit_of_work'); |
||
95 | |||
96 | $this->assertInstanceOf(Reference::class, $def->getArgument(5)); |
||
97 | $this->assertSame( |
||
98 | 'innmind_neo4j.persister.delegation', |
||
99 | (string) $def->getArgument(5) |
||
100 | ); |
||
101 | } |
||
102 | |||
103 | public function testDefaultConnection() |
||
104 | { |
||
105 | $c = new ContainerBuilder; |
||
106 | $this->e->load([], $c); |
||
107 | |||
108 | $def = $this->c->getDefinition('innmind_neo4j.connection.server'); |
||
109 | $this->assertSame('http', $def->getArgument(0)); |
||
110 | $this->assertSame('docker', $def->getArgument(1)); |
||
111 | $this->assertSame(1337, $def->getArgument(2)); |
||
112 | |||
113 | $def = $this->c->getDefinition('innmind_neo4j.connection.authentication'); |
||
114 | $this->assertSame('neo4j', $def->getArgument(0)); |
||
115 | $this->assertSame('ci', $def->getArgument(1)); |
||
116 | |||
117 | $def = $c->getDefinition('innmind_neo4j.connection.transactions'); |
||
118 | $this->assertSame(60, $def->getArgument(2)); |
||
119 | |||
120 | $transport = $c->getDefinition('innmind_neo4j.connection.transport'); |
||
121 | $this->assertSame(60, $transport->getArgument(4)); |
||
122 | } |
||
123 | |||
124 | public function testDefaultTypes() |
||
125 | { |
||
126 | $c = new ContainerBuilder; |
||
127 | $this->e->load([], $c); |
||
128 | $def = $c->getDefinition('innmind_neo4j.types'); |
||
129 | $calls = $def->getMethodCalls(); |
||
130 | |||
131 | $this->assertSame(0, count($calls)); |
||
132 | } |
||
133 | |||
134 | public function testDefaultMetadataConfiguration() |
||
135 | { |
||
136 | $c = new ContainerBuilder; |
||
137 | $this->e->load([], $c); |
||
138 | $def = $c->getDefinition('innmind_neo4j.metadata_builder'); |
||
139 | |||
140 | $this->assertInstanceOf(Reference::class, $def->getArgument(2)); |
||
141 | $this->assertSame('innmind_neo4j.metadata_builder.configuration', (string) $def->getArgument(2)); |
||
142 | } |
||
143 | } |
||
144 |