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 |
||
| 29 | class Base extends WebTestCase |
||
| 30 | { |
||
| 31 | protected static $isDatabaseCreated = false; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var ContainerInterface |
||
| 35 | */ |
||
| 36 | protected $container; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var Client |
||
| 40 | */ |
||
| 41 | protected $client; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Initialize an application with a container and a client. Create database if required. |
||
| 45 | * |
||
| 46 | * @param string $url |
||
| 47 | * @param null $method |
||
| 48 | * @param array $parameters |
||
| 49 | */ |
||
| 50 | public function initApplication($url = '/', $method = null, $parameters = []) |
||
| 51 | { |
||
| 52 | // creating kernel client |
||
| 53 | $this->client = Client::initClient(); |
||
| 54 | // test client initialization |
||
| 55 | $this->assertTrue($this->client != null, 'TestClient successfully initialized'); |
||
| 56 | |||
| 57 | // initialise database |
||
| 58 | if (!self::$isDatabaseCreated) { |
||
| 59 | // TODO remove database at the end of the tests |
||
| 60 | exec(__DIR__ . '/app/console doctrine:database:create --if-not-exists', $output); |
||
| 61 | exec(__DIR__ . '/app/console doctrine:schema:update --force', $output); |
||
| 62 | |||
| 63 | foreach ($output as $line) { |
||
| 64 | // TODO only in verbose mode |
||
| 65 | fwrite(STDOUT, $line . "\n"); |
||
| 66 | } |
||
| 67 | fwrite(STDOUT, "\n"); |
||
| 68 | self::$isDatabaseCreated = true; |
||
| 69 | } |
||
| 70 | // init a request |
||
| 71 | $request = Request::create($url, $method, $parameters); |
||
| 72 | // do request |
||
| 73 | $this->client->doRequest($request); |
||
| 74 | $this->container = $this->client->getContainer(); |
||
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Assert that an exception is raised in the given code. |
||
| 79 | * |
||
| 80 | * @param $exceptionClass |
||
| 81 | * @param Closure $closure |
||
| 82 | */ |
||
| 83 | protected function assertExceptionRaised($exceptionClass, Closure $closure) |
||
| 84 | { |
||
| 85 | $e = null; |
||
| 86 | $isClassValid = false; |
||
| 87 | |||
| 88 | try { |
||
| 89 | $closure(); |
||
| 90 | } catch (Exception $e) { |
||
| 91 | if (get_class($e) == $exceptionClass) { |
||
| 92 | $isClassValid = true; |
||
| 93 | } |
||
| 94 | } |
||
| 95 | $this->assertTrue($isClassValid, 'Expected ' . $exceptionClass . ', got ' . get_class($e)); |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Return Admin configurations samples |
||
| 100 | * |
||
| 101 | * @return array |
||
| 102 | */ |
||
| 103 | protected function getAdminsConfiguration() |
||
| 104 | { |
||
| 105 | return [ |
||
| 106 | 'minimal_configuration' => [ |
||
| 107 | 'entity' => 'Test\TestBundle\Entity\TestEntity', |
||
| 108 | 'form' => 'test' |
||
| 109 | ], |
||
| 110 | 'full_configuration' => [ |
||
| 111 | 'entity' => 'Test\TestBundle\Entity\TestEntity', |
||
| 112 | 'form' => 'test', |
||
| 113 | 'controller' => 'TestTestBundle:Test', |
||
| 114 | 'max_per_page' => 50, |
||
| 115 | 'actions' => [ |
||
| 116 | 'custom_list' => [], |
||
| 117 | 'custom_edit' => [], |
||
| 118 | ], |
||
| 119 | 'manager' => 'Test\TestBundle\Manager\TestManager', |
||
| 120 | 'routing_url_pattern' => 'lag.admin.{admin}', |
||
| 121 | 'routing_name_pattern' => 'lag.{admin}.{action}' |
||
| 122 | ] |
||
| 123 | ]; |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @param $name |
||
| 128 | * @param $configuration |
||
| 129 | * @return Admin |
||
| 130 | */ |
||
| 131 | protected function mockAdmin($name, $configuration) |
||
| 132 | { |
||
| 133 | return new Admin( |
||
| 134 | $name, |
||
| 135 | $this->mockDataProvider(), |
||
| 136 | $configuration, |
||
| 137 | $this->mockMessageHandler() |
||
| 138 | ); |
||
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @param $name |
||
| 143 | * @return ActionInterface|PHPUnit_Framework_MockObject_MockObject |
||
| 144 | */ |
||
| 145 | protected function mockAction($name) |
||
| 146 | { |
||
| 147 | $action = $this |
||
| 148 | ->getMockBuilder('LAG\AdminBundle\Admin\ActionInterface') |
||
| 149 | ->disableOriginalConstructor() |
||
| 150 | ->getMock(); |
||
| 151 | $action |
||
| 152 | ->method('getName') |
||
| 153 | ->willReturn($name); |
||
| 154 | $action |
||
| 155 | ->method('getConfiguration') |
||
| 156 | ->willReturn($this->mockActionConfiguration()); |
||
| 157 | $action |
||
| 158 | ->method('getPermissions') |
||
| 159 | ->willReturn([ |
||
| 160 | 'ROLE_ADMIN' |
||
| 161 | ]); |
||
| 162 | |||
| 163 | return $action; |
||
| 164 | } |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @return ActionConfiguration|PHPUnit_Framework_MockObject_MockObject |
||
| 168 | */ |
||
| 169 | protected function mockActionConfiguration() |
||
| 170 | { |
||
| 171 | $configuration = $this |
||
| 172 | ->getMockBuilder('LAG\AdminBundle\Admin\Configuration\ActionConfiguration') |
||
| 173 | ->disableOriginalConstructor() |
||
| 174 | ->getMock(); |
||
| 175 | $configuration |
||
| 176 | ->method('getLoadMethod') |
||
| 177 | ->willReturn(Admin::LOAD_STRATEGY_MULTIPLE); |
||
| 178 | $configuration |
||
| 179 | ->method('getCriteria') |
||
| 180 | ->willReturn([]); |
||
| 181 | |||
| 182 | return $configuration; |
||
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @param array $configuration |
||
| 187 | * @return AdminFactory |
||
| 188 | */ |
||
| 189 | protected function mockAdminFactory(array $configuration = []) |
||
| 190 | { |
||
| 191 | /** @var EventDispatcher $mockEventDispatcher */ |
||
| 192 | $mockEventDispatcher = $this |
||
| 193 | ->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcher') |
||
| 194 | ->getMock(); |
||
| 195 | |||
| 196 | return new AdminFactory( |
||
| 197 | $mockEventDispatcher, |
||
| 198 | $this->mockEntityManager(), |
||
| 199 | $this->mockApplicationConfiguration(), |
||
| 200 | $configuration, |
||
| 201 | $this->mockActionFactory(), |
||
| 202 | $this->mockMessageHandler() |
||
| 203 | ); |
||
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * @return Session|PHPUnit_Framework_MockObject_MockObject |
||
| 208 | */ |
||
| 209 | protected function mockSession() |
||
| 210 | { |
||
| 211 | if ($this->container) { |
||
| 212 | $session = $this |
||
| 213 | ->container |
||
| 214 | ->get('session'); |
||
| 215 | } else { |
||
| 216 | $session = $this |
||
| 217 | ->getMockBuilder('Symfony\Component\HttpFoundation\Session\Session') |
||
| 218 | ->disableOriginalConstructor() |
||
| 219 | ->getMock(); |
||
| 220 | } |
||
| 221 | return $session; |
||
| 222 | } |
||
| 223 | |||
| 224 | /** |
||
| 225 | * @return Logger|PHPUnit_Framework_MockObject_MockObject |
||
| 226 | */ |
||
| 227 | protected function mockLogger() |
||
| 228 | { |
||
| 229 | if ($this->container) { |
||
| 230 | $logger = $this |
||
| 231 | ->container |
||
| 232 | ->get('logger'); |
||
| 233 | } else { |
||
| 234 | $logger = $this |
||
| 235 | ->getMockBuilder('Monolog\Logger') |
||
| 236 | ->disableOriginalConstructor() |
||
| 237 | ->getMock(); |
||
| 238 | } |
||
| 239 | return $logger; |
||
| 240 | } |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Return a mock of an entity repository |
||
| 244 | * |
||
| 245 | * @return RepositoryInterface|PHPUnit_Framework_MockObject_MockObject |
||
| 246 | */ |
||
| 247 | protected function mockEntityRepository() |
||
| 248 | { |
||
| 249 | return $this |
||
| 250 | ->getMockBuilder('LAG\DoctrineRepositoryBundle\Repository\RepositoryInterface') |
||
| 251 | ->getMock(); |
||
| 252 | } |
||
| 253 | |||
| 254 | /** |
||
| 255 | * @return EntityManager|PHPUnit_Framework_MockObject_MockObject |
||
| 256 | */ |
||
| 257 | protected function mockEntityManager() |
||
| 258 | { |
||
| 259 | /** @var EntityManagerInterface|PHPUnit_Framework_MockObject_MockObject $entityManager */ |
||
| 260 | $entityManager = $this |
||
| 261 | ->getMockBuilder('Doctrine\ORM\EntityManager') |
||
| 262 | ->disableOriginalConstructor() |
||
| 263 | ->getMock(); |
||
| 264 | $repository = $this->mockEntityRepository(); |
||
| 265 | $repository |
||
| 266 | ->method('getEntityPersister') |
||
| 267 | ->willReturn( $this |
||
| 268 | ->getMockBuilder('Doctrine\ORM\Persisters\Entity\BasicEntityPersister') |
||
| 269 | ->disableOriginalConstructor() |
||
| 270 | ->getMock()); |
||
| 271 | |||
| 272 | $entityManager |
||
| 273 | ->method('getRepository') |
||
| 274 | ->willReturn($repository); |
||
| 275 | $entityManager |
||
| 276 | ->method('getClassMetadata') |
||
| 277 | ->willReturn(new ClassMetadata('LAG\AdminBundle\Tests\Entity\TestEntity')); |
||
| 278 | |||
| 279 | return $entityManager; |
||
| 280 | } |
||
| 281 | |||
| 282 | /** |
||
| 283 | * @return ActionFactory|PHPUnit_Framework_MockObject_MockObject |
||
| 284 | */ |
||
| 285 | protected function mockActionFactory() |
||
| 286 | { |
||
| 287 | $actionFactory = $this |
||
| 288 | ->getMockBuilder('LAG\AdminBundle\Admin\Factory\ActionFactory') |
||
| 289 | ->disableOriginalConstructor() |
||
| 290 | ->getMock(); |
||
| 291 | $actionFactory |
||
| 292 | ->method('create') |
||
| 293 | ->willReturn($this->mockAction('test')); |
||
| 294 | |||
| 295 | return $actionFactory; |
||
| 296 | } |
||
| 297 | |||
| 298 | /** |
||
| 299 | * @return TokenStorageInterface|PHPUnit_Framework_MockObject_MockObject |
||
| 300 | */ |
||
| 301 | protected function mockTokenStorage() |
||
| 302 | { |
||
| 303 | return $this |
||
| 304 | ->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface') |
||
| 305 | ->getMock(); |
||
| 306 | } |
||
| 307 | |||
| 308 | /** |
||
| 309 | * @return ApplicationConfiguration |
||
| 310 | */ |
||
| 311 | protected function mockApplicationConfiguration() |
||
| 312 | { |
||
| 313 | $applicationConfiguration = $this |
||
| 314 | ->getMockBuilder('LAG\AdminBundle\Admin\Configuration\ApplicationConfiguration') |
||
| 315 | ->disableOriginalConstructor() |
||
| 316 | ->getMock(); |
||
| 317 | $applicationConfiguration |
||
| 318 | ->method('getMaxPerPage') |
||
| 319 | ->willReturn(25); |
||
| 320 | |||
| 321 | return $applicationConfiguration; |
||
| 322 | } |
||
| 323 | |||
| 324 | /** |
||
| 325 | * @return MessageHandlerInterface|PHPUnit_Framework_MockObject_MockObject |
||
| 326 | */ |
||
| 327 | protected function mockMessageHandler() |
||
| 328 | { |
||
| 329 | $messageHandler = $this |
||
| 330 | ->getMockBuilder('LAG\AdminBundle\Message\MessageHandler') |
||
| 331 | ->disableOriginalConstructor() |
||
| 332 | ->getMock(); |
||
| 333 | |||
| 334 | return $messageHandler; |
||
| 335 | } |
||
| 336 | |||
| 337 | /** |
||
| 338 | * @return TranslatorInterface|PHPUnit_Framework_MockObject_MockObject |
||
| 339 | */ |
||
| 340 | protected function mockTranslator() |
||
| 341 | { |
||
| 342 | return $this |
||
| 343 | ->getMockBuilder('Symfony\Component\Translation\TranslatorInterface') |
||
| 344 | ->getMock(); |
||
| 345 | } |
||
| 346 | |||
| 347 | /** |
||
| 348 | * @return DataProviderInterface|PHPUnit_Framework_MockObject_MockObject |
||
| 349 | */ |
||
| 350 | protected function mockDataProvider() |
||
| 351 | { |
||
| 352 | return $this |
||
| 353 | ->getMockBuilder('LAG\AdminBundle\DataProvider\DataProviderInterface') |
||
| 354 | ->getMock(); |
||
| 355 | } |
||
| 356 | } |
||
| 357 |