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 |
||
20 | class SWPBridgeExtensionTest extends \PHPUnit_Framework_TestCase |
||
21 | { |
||
22 | /** |
||
23 | * @covers SWP\Bundle\BridgeBundle\SWPBridgeBundle |
||
24 | * @covers SWP\Bundle\BridgeBundle\DependencyInjection\SWPBridgeExtension::load |
||
25 | * @covers SWP\Bundle\BridgeBundle\DependencyInjection\Configuration::getConfigTreeBuilder |
||
26 | */ |
||
27 | public function testLoad() |
||
28 | { |
||
29 | $data = array( |
||
30 | 'swp_bridge.api.host' => 'example.com', |
||
31 | 'swp_bridge.api.port' => 8000, |
||
32 | 'swp_bridge.api.protocol' => 'http', |
||
33 | 'swp_bridge.auth.client_id' => 'my_client_id', |
||
34 | 'swp_bridge.auth.username' => 'my_username', |
||
35 | 'swp_bridge.auth.password' => 'my_password', |
||
36 | 'swp_bridge.options' => array('curl' => 'dummy'), |
||
37 | ); |
||
38 | |||
39 | $container = $this->createContainer($data); |
||
40 | $loader = $this->createLoader(); |
||
41 | $config = $this->getConfig(); |
||
42 | |||
43 | $loader->load(array($config), $container); |
||
44 | |||
45 | foreach ($data as $key => $value) { |
||
46 | $this->assertEquals($value, $container->getParameter($key)); |
||
47 | } |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * @expectedException Symfony\Component\Config\Definition\Exception\InvalidConfigurationException |
||
52 | */ |
||
53 | public function testLoadWhenHostIsRequiredAndCannotBeEmpty() |
||
54 | { |
||
55 | $container = $this->createContainer(); |
||
56 | $loader = $this->createLoader(); |
||
57 | |||
58 | $config = array( |
||
59 | 'swp_bridge.api.host' => '', |
||
60 | ); |
||
61 | |||
62 | $loader->load(array($config), $container); |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * @expectedException Symfony\Component\Config\Definition\Exception\InvalidConfigurationException |
||
67 | */ |
||
68 | public function testLoadWhenClientIdIsRequiredAndCannotBeEmpty() |
||
69 | { |
||
70 | $container = $this->createContainer(); |
||
71 | $loader = $this->createLoader(); |
||
72 | |||
73 | $config = array( |
||
74 | 'swp_bridge.auth.client_id' => '', |
||
75 | ); |
||
76 | |||
77 | $loader->load(array($config), $container); |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * @expectedException Symfony\Component\Config\Definition\Exception\InvalidConfigurationException |
||
82 | */ |
||
83 | public function testLoadWhenUsernameIsRequiredAndCannotBeEmpty() |
||
84 | { |
||
85 | $container = $this->createContainer(); |
||
86 | $loader = $this->createLoader(); |
||
87 | |||
88 | $config = array( |
||
89 | 'swp_bridge.auth.username' => '', |
||
90 | ); |
||
91 | |||
92 | $loader->load(array($config), $container); |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * @expectedException Symfony\Component\Config\Definition\Exception\InvalidConfigurationException |
||
97 | */ |
||
98 | public function testLoadWhenPasswordIsRequiredAndCannotBeEmpty() |
||
99 | { |
||
100 | $container = $this->createContainer(); |
||
101 | $loader = $this->createLoader(); |
||
102 | |||
103 | $config = array( |
||
104 | 'swp_bridge.auth.password' => '', |
||
105 | ); |
||
106 | |||
107 | $loader->load(array($config), $container); |
||
108 | } |
||
109 | |||
110 | protected function createLoader() |
||
111 | { |
||
112 | return new SWPBridgeExtension(); |
||
113 | } |
||
114 | |||
115 | protected function getConfig() |
||
116 | { |
||
117 | return array( |
||
118 | 'api' => array( |
||
119 | 'host' => 'example.com', |
||
120 | 'port' => 8000, |
||
121 | 'protocol' => 'http', |
||
122 | ), |
||
123 | 'auth' => array( |
||
124 | 'client_id' => 'my_client_id', |
||
125 | 'username' => 'my_username', |
||
126 | 'password' => 'my_password', |
||
127 | ), |
||
128 | 'options' => array( |
||
129 | 'curl' => 'dummy', |
||
130 | ), |
||
131 | ); |
||
132 | } |
||
133 | |||
134 | protected function createContainer(array $data = array()) |
||
135 | { |
||
136 | return new ContainerBuilder(new ParameterBag($data)); |
||
137 | } |
||
138 | } |
||
139 |