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 |
||
| 19 | class AdminFactoryTest extends AdminTestBase |
||
| 20 | { |
||
| 21 | public function testCreateFromRequest() |
||
| 22 | { |
||
| 23 | $resource = $this->getMockWithoutConstructor(AdminResource::class); |
||
| 24 | $resource |
||
| 25 | ->expects($this->atLeastOnce()) |
||
| 26 | ->method('getName') |
||
| 27 | ->willReturn('MyLittleTaunTaun') |
||
| 28 | ; |
||
| 29 | |||
| 30 | $resourceCollection = $this->getMockWithoutConstructor(ResourceCollection::class); |
||
| 31 | $resourceCollection |
||
| 32 | ->expects($this->once()) |
||
| 33 | ->method('has') |
||
| 34 | ->with('MyLittleTaunTaun') |
||
| 35 | ->willReturn(true) |
||
| 36 | ; |
||
| 37 | $resourceCollection |
||
| 38 | ->expects($this->once()) |
||
| 39 | ->method('get') |
||
| 40 | ->with('MyLittleTaunTaun') |
||
| 41 | ->willReturn($resource) |
||
| 42 | ; |
||
| 43 | |||
| 44 | $adminConfiguration = $this->getMockWithoutConstructor(AdminConfiguration::class); |
||
| 45 | $adminConfiguration |
||
| 46 | ->expects($this->once()) |
||
| 47 | ->method('getParameter') |
||
| 48 | ->with('class') |
||
| 49 | ->willReturn(Admin::class) |
||
| 50 | ; |
||
| 51 | |||
| 52 | $configurationFactory = $this->getMockWithoutConstructor(ConfigurationFactory::class); |
||
| 53 | $configurationFactory |
||
| 54 | ->expects($this->once()) |
||
| 55 | ->method('createAdminConfiguration') |
||
| 56 | ->with('MyLittleTaunTaun') |
||
| 57 | ->willReturn($adminConfiguration) |
||
| 58 | ; |
||
| 59 | |||
| 60 | $eventDispatcher = $this->getMockWithoutConstructor(EventDispatcherInterface::class); |
||
| 61 | |||
| 62 | $applicationConfiguration = new ApplicationConfiguration(); |
||
| 63 | $applicationConfigurationStorage = $this->getMockWithoutConstructor(ApplicationConfigurationStorage::class); |
||
| 64 | $applicationConfigurationStorage |
||
| 65 | ->expects($this->once()) |
||
| 66 | ->method('getConfiguration') |
||
| 67 | ->willReturn($applicationConfiguration) |
||
| 68 | ; |
||
| 69 | |||
| 70 | $request = new Request([ |
||
| 71 | '_admin' => 'MyLittleTaunTaun', |
||
| 72 | '_route_params' => [ |
||
| 73 | '_admin' => 'MyLittleTaunTaun', |
||
| 74 | '_action' => 'jump', |
||
| 75 | ], |
||
| 76 | ]); |
||
| 77 | |||
| 78 | $adminFactory = new AdminFactory( |
||
| 79 | $resourceCollection, |
||
| 80 | $eventDispatcher, |
||
| 81 | $configurationFactory, |
||
| 82 | $applicationConfigurationStorage |
||
| 83 | ); |
||
| 84 | |||
| 85 | $adminFactory->createFromRequest($request); |
||
| 86 | } |
||
| 87 | |||
| 88 | public function testCreateFromRequestWithoutRouteParams() |
||
| 89 | { |
||
| 90 | $resourceCollection = $this->getMockWithoutConstructor(ResourceCollection::class); |
||
| 91 | $eventDispatcher = $this->getMockWithoutConstructor(EventDispatcherInterface::class); |
||
| 92 | $configurationFactory = $this->getMockWithoutConstructor(ConfigurationFactory::class); |
||
| 93 | $applicationConfigurationStorage = $this->getMockWithoutConstructor(ApplicationConfigurationStorage::class); |
||
| 94 | |||
| 95 | $request = new Request(); |
||
| 96 | |||
| 97 | $adminFactory = new AdminFactory( |
||
| 98 | $resourceCollection, |
||
| 99 | $eventDispatcher, |
||
| 100 | $configurationFactory, |
||
| 101 | $applicationConfigurationStorage |
||
| 102 | ); |
||
| 103 | |||
| 104 | $this->expectException(Exception::class); |
||
| 105 | $adminFactory->createFromRequest($request); |
||
| 106 | } |
||
| 107 | |||
| 108 | public function testCreateFromRequestWithoutAdminParams() |
||
| 109 | { |
||
| 110 | $resourceCollection = $this->getMockWithoutConstructor(ResourceCollection::class); |
||
| 111 | $eventDispatcher = $this->getMockWithoutConstructor(EventDispatcherInterface::class); |
||
| 112 | $configurationFactory = $this->getMockWithoutConstructor(ConfigurationFactory::class); |
||
| 113 | $applicationConfigurationStorage = $this->getMockWithoutConstructor(ApplicationConfigurationStorage::class); |
||
| 114 | |||
| 115 | $request = new Request([ |
||
| 116 | '_route_params' => [], |
||
| 117 | ]); |
||
| 118 | |||
| 119 | $adminFactory = new AdminFactory( |
||
| 120 | $resourceCollection, |
||
| 121 | $eventDispatcher, |
||
| 122 | $configurationFactory, |
||
| 123 | $applicationConfigurationStorage |
||
| 124 | ); |
||
| 125 | |||
| 126 | $this->expectException(Exception::class); |
||
| 127 | $adminFactory->createFromRequest($request); |
||
| 128 | } |
||
| 129 | |||
| 130 | public function testCreateFromRequestWithoutExistingAdmin() |
||
| 131 | { |
||
| 132 | $resourceCollection = $this->getMockWithoutConstructor(ResourceCollection::class); |
||
| 133 | $eventDispatcher = $this->getMockWithoutConstructor(EventDispatcherInterface::class); |
||
| 134 | $configurationFactory = $this->getMockWithoutConstructor(ConfigurationFactory::class); |
||
| 135 | $applicationConfigurationStorage = $this->getMockWithoutConstructor(ApplicationConfigurationStorage::class); |
||
| 136 | |||
| 137 | $request = new Request([ |
||
| 138 | '_route_params' => [ |
||
| 139 | '_admin' => 'MyLittleTaunTaun', |
||
| 140 | '_action' => 'jump', |
||
| 141 | ], |
||
| 142 | ]); |
||
| 143 | |||
| 144 | $adminFactory = new AdminFactory( |
||
| 145 | $resourceCollection, |
||
| 146 | $eventDispatcher, |
||
| 147 | $configurationFactory, |
||
| 148 | $applicationConfigurationStorage |
||
| 149 | ); |
||
| 150 | |||
| 151 | $this->expectException(Exception::class); |
||
| 152 | $adminFactory->createFromRequest($request); |
||
| 153 | } |
||
| 154 | |||
| 155 | public function testCreateFromRequestWithInvalidAdminClass() |
||
| 156 | { |
||
| 157 | $resource = $this->getMockWithoutConstructor(AdminResource::class); |
||
| 158 | $resource |
||
| 159 | ->expects($this->atLeastOnce()) |
||
| 160 | ->method('getName') |
||
| 161 | ->willReturn('MyLittleTaunTaun') |
||
| 162 | ; |
||
| 163 | |||
| 164 | $resourceCollection = $this->getMockWithoutConstructor(ResourceCollection::class); |
||
| 165 | $resourceCollection |
||
| 166 | ->expects($this->once()) |
||
| 167 | ->method('has') |
||
| 168 | ->with('MyLittleTaunTaun') |
||
| 169 | ->willReturn(true) |
||
| 170 | ; |
||
| 171 | $resourceCollection |
||
| 172 | ->expects($this->once()) |
||
| 173 | ->method('get') |
||
| 174 | ->with('MyLittleTaunTaun') |
||
| 175 | ->willReturn($resource) |
||
| 176 | ; |
||
| 177 | |||
| 178 | $adminConfiguration = $this->getMockWithoutConstructor(AdminConfiguration::class); |
||
| 179 | $adminConfiguration |
||
| 180 | ->expects($this->once()) |
||
| 181 | ->method('getParameter') |
||
| 182 | ->with('class') |
||
| 183 | ->willReturn(FakeAdmin::class) |
||
| 184 | ; |
||
| 185 | |||
| 186 | $configurationFactory = $this->getMockWithoutConstructor(ConfigurationFactory::class); |
||
| 187 | $configurationFactory |
||
| 188 | ->expects($this->once()) |
||
| 189 | ->method('createAdminConfiguration') |
||
| 190 | ->with('MyLittleTaunTaun') |
||
| 191 | ->willReturn($adminConfiguration) |
||
| 192 | ; |
||
| 193 | |||
| 194 | $eventDispatcher = $this->getMockWithoutConstructor(EventDispatcherInterface::class); |
||
| 195 | |||
| 196 | $applicationConfiguration = new ApplicationConfiguration(); |
||
| 197 | $applicationConfigurationStorage = $this->getMockWithoutConstructor(ApplicationConfigurationStorage::class); |
||
| 198 | $applicationConfigurationStorage |
||
| 199 | ->expects($this->once()) |
||
| 200 | ->method('getConfiguration') |
||
| 201 | ->willReturn($applicationConfiguration) |
||
| 202 | ; |
||
| 203 | |||
| 204 | $request = new Request([ |
||
| 205 | '_admin' => 'MyLittleTaunTaun', |
||
| 206 | '_route_params' => [ |
||
| 207 | '_admin' => 'MyLittleTaunTaun', |
||
| 208 | '_action' => 'jump', |
||
| 209 | ], |
||
| 210 | ]); |
||
| 211 | |||
| 212 | $adminFactory = new AdminFactory( |
||
| 213 | $resourceCollection, |
||
| 214 | $eventDispatcher, |
||
| 215 | $configurationFactory, |
||
| 216 | $applicationConfigurationStorage |
||
| 217 | ); |
||
| 218 | |||
| 219 | $this->expectException(Exception::class); |
||
| 220 | $adminFactory->createFromRequest($request); |
||
| 221 | } |
||
| 222 | } |
||
| 223 |