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 DataProviderCompilerPassTest extends \PHPUnit_Framework_TestCase |
||
17 | { |
||
18 | private $testedInstance; |
||
19 | |||
20 | private $container; |
||
21 | |||
22 | private $registryDefinition; |
||
23 | |||
24 | public function setUp() |
||
25 | { |
||
26 | $this->testedInstance = new DataProviderCompilerPass(); |
||
27 | $this->container = new ContainerBuilder(); |
||
28 | $this->registryDefinition = new Definition(); |
||
29 | } |
||
30 | |||
31 | public function testShouldRegisterTaggedDataProviders() |
||
32 | { |
||
33 | $this->container->setDefinition( |
||
34 | 'gbprod.elasticsearch_dataprovider.registry', |
||
35 | $this->registryDefinition |
||
36 | ); |
||
37 | |||
38 | $this->container->setDefinition( |
||
39 | 'data_provider.foo.bar', |
||
40 | $this->newDataProviderDefinition('foo', 'bar') |
||
41 | ); |
||
42 | |||
43 | $this->container->setDefinition( |
||
44 | 'data_provider.bar.foo', |
||
45 | $this->newDataProviderDefinition('fizz', 'buzz') |
||
46 | ); |
||
47 | |||
48 | $this->testedInstance->process($this->container); |
||
49 | |||
50 | $calls = $this->registryDefinition->getMethodCalls(); |
||
51 | |||
52 | $this->assertEquals('add', $calls[0][0]); |
||
53 | $this->assertInstanceOf(Definition::class, $calls[0][1][0]); |
||
54 | $this->assertInstanceOf(Reference::class, $calls[0][1][0]->getArgument(0)); |
||
55 | $this->assertEquals('data_provider.foo.bar', $calls[0][1][0]->getArgument(0)->__toString()); |
||
56 | $this->assertEquals('foo', $calls[0][1][0]->getArgument(1)); |
||
57 | $this->assertEquals('bar', $calls[0][1][0]->getArgument(2)); |
||
58 | |||
59 | $this->assertEquals('add', $calls[1][0]); |
||
60 | $this->assertInstanceOf(Definition::class, $calls[1][1][0]); |
||
61 | $this->assertEquals('data_provider.bar.foo', $calls[1][1][0]->getArgument(0)->__toString()); |
||
62 | $this->assertEquals('fizz', $calls[1][1][0]->getArgument(1)); |
||
63 | $this->assertEquals('buzz', $calls[1][1][0]->getArgument(2)); |
||
64 | } |
||
65 | |||
66 | private function newDataProviderDefinition($index, $type) |
||
67 | { |
||
68 | $tag = ['index' => $index, 'type' => $type]; |
||
69 | |||
70 | $definition = new Definition(DataProvider::class); |
||
71 | $definition->addTag('elasticsearch.dataprovider', $tag); |
||
72 | |||
73 | return $definition; |
||
74 | } |
||
75 | |||
76 | public function testThrowsExceptionIfNotDataProvider() |
||
77 | { |
||
78 | $this->container->setDefinition( |
||
79 | 'gbprod.elasticsearch_dataprovider.registry', |
||
80 | $this->registryDefinition |
||
81 | ); |
||
82 | |||
83 | $definition = new Definition(\stdClass::class); |
||
84 | $definition->addTag( |
||
85 | 'elasticsearch.dataprovider', |
||
86 | ['index' => 'foo', 'type' => 'bar'] |
||
87 | ); |
||
88 | |||
89 | $this->container->setDefinition( |
||
90 | 'data_provider.foo.bar', |
||
91 | $definition |
||
92 | ); |
||
93 | |||
94 | $this->setExpectedException(\InvalidArgumentException::class); |
||
95 | |||
96 | $this->testedInstance->process($this->container); |
||
97 | } |
||
98 | |||
99 | public function testThrowsExceptionIfBadTag() |
||
100 | { |
||
101 | $this->container->setDefinition( |
||
102 | 'gbprod.elasticsearch_dataprovider.registry', |
||
103 | $this->registryDefinition |
||
104 | ); |
||
105 | |||
106 | $definition = new Definition(DataProvider::class); |
||
107 | $definition->addTag( |
||
108 | 'elasticsearch.dataprovider', |
||
109 | ['type' => 'my-type'] |
||
110 | ); |
||
111 | |||
112 | $this->container->setDefinition( |
||
113 | 'data_provider.foo.bar', |
||
114 | $definition |
||
115 | ); |
||
116 | |||
117 | $this->setExpectedException(\InvalidArgumentException::class); |
||
118 | |||
119 | $this->testedInstance->process($this->container); |
||
120 | } |
||
121 | |||
122 | public function testNeverCallGetDefinitionIfServiceNotSet() |
||
123 | { |
||
124 | $container = $this->getMock(ContainerBuilder::class); |
||
125 | |||
126 | $container |
||
127 | ->expects($this->any()) |
||
128 | ->method('hasDefinition') |
||
129 | ->with('gbprod.elasticsearch_dataprovider.registry') |
||
130 | ->willReturn(false) |
||
131 | ; |
||
132 | |||
133 | $container |
||
134 | ->expects($this->never()) |
||
135 | ->method('getDefinition') |
||
136 | ->with('gbprod.elasticsearch_dataprovider.registry') |
||
137 | ; |
||
138 | |||
139 | $this->testedInstance->process($container); |
||
140 | } |
||
141 | } |
||
142 |