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 |
||
12 | class ActionsPassTest extends AbstractCompilerPassTestCase |
||
13 | { |
||
14 | public function testCompilerPassCollectsValidServices() |
||
15 | { |
||
16 | $actionsRegistry = new Definition(); |
||
17 | $this->setDefinition('netgen_information_collection.action.registry', $actionsRegistry); |
||
18 | |||
19 | $action = new Definition(); |
||
20 | $action->addTag('netgen_information_collection.action', array('alias' => 'custom_action', 'priority' => 100)); |
||
21 | $this->setDefinition('my_action', $action); |
||
22 | |||
23 | $this->compile(); |
||
24 | |||
25 | $this->assertContainerBuilderHasServiceDefinitionWithMethodCall( |
||
26 | 'netgen_information_collection.action.registry', |
||
27 | 'addAction', |
||
28 | array( |
||
29 | 'custom_action', |
||
30 | new Reference('my_action'), |
||
31 | 100, |
||
32 | ) |
||
33 | ); |
||
34 | } |
||
35 | |||
36 | /** |
||
37 | * @expectedException \Symfony\Component\DependencyInjection\Exception\LogicException |
||
38 | * @expectedExceptionMessage 'netgen_information_collection.action' service tag needs an 'alias' attribute to identify the action. None given. |
||
39 | */ |
||
40 | public function testCompilerPassMustThrowExceptionIfActionServiceHasntGotAlias() |
||
41 | { |
||
42 | $actionsRegistry = new Definition(); |
||
43 | $this->setDefinition('netgen_information_collection.action.registry', $actionsRegistry); |
||
44 | |||
45 | $action = new Definition(); |
||
46 | $action->addTag('netgen_information_collection.action'); |
||
47 | $this->setDefinition('my_action', $action); |
||
48 | |||
49 | $this->compile(); |
||
50 | |||
51 | $this->assertContainerBuilderHasServiceDefinitionWithMethodCall( |
||
52 | 'netgen_information_collection.action.registry', |
||
53 | 'addAction', |
||
54 | array( |
||
55 | new Reference('my_action'), |
||
56 | ) |
||
57 | ); |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * @expectedException \Symfony\Component\DependencyInjection\Exception\LogicException |
||
62 | * @expectedExceptionMessage Service my_action uses priority less than allowed. Priority must be greater than or equal to -255. |
||
63 | */ |
||
64 | public function testCompilerWithServicePriorityLessThanAllowed() |
||
65 | { |
||
66 | $actionsRegistry = new Definition(); |
||
67 | $this->setDefinition('netgen_information_collection.action.registry', $actionsRegistry); |
||
68 | $priority = Priority::MIN_PRIORITY - 1; |
||
69 | |||
70 | $action = new Definition(); |
||
71 | $action->addTag('netgen_information_collection.action', array('alias' => 'custom_action', 'priority' => $priority)); |
||
72 | $this->setDefinition('my_action', $action); |
||
73 | |||
74 | $this->compile(); |
||
75 | |||
76 | $this->assertContainerBuilderHasServiceDefinitionWithMethodCall( |
||
77 | 'netgen_information_collection.action.registry', |
||
78 | 'addAction', |
||
79 | array( |
||
80 | 'custom_action', |
||
81 | new Reference('my_action'), |
||
82 | $priority, |
||
83 | ) |
||
84 | ); |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * @expectedException \Symfony\Component\DependencyInjection\Exception\LogicException |
||
89 | * @expectedExceptionMessage Service my_action uses priority greater than allowed. Priority must be lower than or equal to 255. |
||
90 | */ |
||
91 | public function testCompilerWithServicePriorityGreaterThanAllowed() |
||
92 | { |
||
93 | $actionsRegistry = new Definition(); |
||
94 | $this->setDefinition('netgen_information_collection.action.registry', $actionsRegistry); |
||
95 | $priority = Priority::MAX_PRIORITY + 1; |
||
96 | |||
97 | $action = new Definition(); |
||
98 | $action->addTag('netgen_information_collection.action', array('alias' => 'custom_action', 'priority' => $priority)); |
||
99 | $this->setDefinition('my_action', $action); |
||
100 | |||
101 | $this->compile(); |
||
102 | |||
103 | $this->assertContainerBuilderHasServiceDefinitionWithMethodCall( |
||
104 | 'netgen_information_collection.action.registry', |
||
105 | 'addAction', |
||
106 | array( |
||
107 | 'custom_action', |
||
108 | new Reference('my_action'), |
||
109 | $priority, |
||
110 | ) |
||
111 | ); |
||
112 | } |
||
113 | |||
114 | public function testCompilerWithServiceThatIsMissingPriority() |
||
115 | { |
||
116 | $actionsRegistry = new Definition(); |
||
117 | $this->setDefinition('netgen_information_collection.action.registry', $actionsRegistry); |
||
118 | |||
119 | $action = new Definition(); |
||
120 | $action->addTag('netgen_information_collection.action', array('alias' => 'custom_action')); |
||
121 | $this->setDefinition('my_action', $action); |
||
122 | |||
123 | $this->compile(); |
||
124 | |||
125 | $this->assertContainerBuilderHasServiceDefinitionWithMethodCall( |
||
126 | 'netgen_information_collection.action.registry', |
||
127 | 'addAction', |
||
128 | array( |
||
129 | 'custom_action', |
||
130 | new Reference('my_action'), |
||
131 | Priority::DEFAULT_PRIORITY, |
||
132 | ) |
||
133 | ); |
||
134 | } |
||
135 | |||
136 | public function testCompilerWithDatabasePriority() |
||
137 | { |
||
138 | $actionsRegistry = new Definition(); |
||
139 | $this->setDefinition('netgen_information_collection.action.registry', $actionsRegistry); |
||
140 | |||
141 | $action = new Definition(); |
||
142 | $action->addTag('netgen_information_collection.action', array('alias' => 'database', 'priority' => 300)); |
||
143 | $this->setDefinition('my_action', $action); |
||
144 | |||
145 | $this->compile(); |
||
146 | |||
147 | $this->assertContainerBuilderHasServiceDefinitionWithMethodCall( |
||
148 | 'netgen_information_collection.action.registry', |
||
149 | 'addAction', |
||
150 | array( |
||
151 | 'database', |
||
152 | new Reference('my_action'), |
||
153 | 300, |
||
154 | ) |
||
155 | ); |
||
156 | } |
||
157 | |||
158 | protected function registerCompilerPass(ContainerBuilder $container) |
||
159 | { |
||
160 | $container->addCompilerPass(new ActionsPass()); |
||
161 | } |
||
162 | } |
||
163 |