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 |
||
| 14 | class AdminTestBase extends TestCase |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * Assert that the given service class is configured in the services.yaml |
||
| 18 | * |
||
| 19 | * @param string $serviceClass |
||
| 20 | */ |
||
| 21 | public function assertServiceExists(string $serviceClass) |
||
| 22 | { |
||
| 23 | $containerBuilder = new ContainerBuilder(); |
||
| 24 | $locator = new FileLocator([ |
||
| 25 | __DIR__.'/../../src/Resources/config', |
||
| 26 | ]); |
||
| 27 | $loader = new YamlFileLoader($containerBuilder, $locator); |
||
| 28 | $loader->load('services.yaml'); |
||
| 29 | $exists = false; |
||
| 30 | |||
| 31 | foreach ($containerBuilder->getDefinitions() as $definition) { |
||
| 32 | if ($serviceClass === $definition->getClass()) { |
||
| 33 | $exists = true; |
||
| 34 | } |
||
| 35 | } |
||
| 36 | $this->assertTrue($exists); |
||
| 37 | } |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Assert that an exception is raised in the given code. |
||
| 41 | * |
||
| 42 | * @param $exceptionClass |
||
| 43 | * @param Closure $closure |
||
| 44 | */ |
||
| 45 | protected function assertExceptionRaised($exceptionClass, Closure $closure) |
||
| 46 | { |
||
| 47 | $e = null; |
||
| 48 | $isClassValid = false; |
||
| 49 | $message = ''; |
||
| 50 | |||
| 51 | try { |
||
| 52 | $closure(); |
||
| 53 | } catch (Exception $e) { |
||
| 54 | if (get_class($e) == $exceptionClass) { |
||
| 55 | $isClassValid = true; |
||
| 56 | } |
||
| 57 | $message = $e->getMessage(); |
||
| 58 | } |
||
| 59 | $this->assertNotNull($e, 'No Exception was thrown'); |
||
| 60 | $this->assertTrue($isClassValid, sprintf('Expected %s, got %s (Exception message : "%s")', |
||
| 61 | $exceptionClass, |
||
| 62 | get_class($e), |
||
| 63 | $message |
||
| 64 | )); |
||
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @param $class |
||
| 69 | * |
||
| 70 | * @return PHPUnit_Framework_MockObject_MockObject|mixed |
||
| 71 | */ |
||
| 72 | protected function getMockWithoutConstructor($class) |
||
| 73 | { |
||
| 74 | return $this |
||
| 75 | ->getMockBuilder($class) |
||
| 76 | ->disableOriginalConstructor() |
||
| 77 | ->getMock() |
||
| 78 | ; |
||
| 79 | } |
||
| 80 | |||
| 81 | protected function setPrivateProperty($object, $property, $value) |
||
| 82 | { |
||
| 83 | $reflection = new ReflectionClass($object); |
||
| 84 | |||
| 85 | $property = $reflection->getProperty($property); |
||
| 86 | $property->setAccessible(true); |
||
| 87 | $property->setValue($object, $value); |
||
| 88 | } |
||
| 89 | |||
| 90 | protected function getPrivateProperty($object, $property) |
||
| 91 | { |
||
| 92 | $reflection = new ReflectionClass($object); |
||
| 93 | |||
| 94 | $property = $reflection->getProperty($property); |
||
| 95 | $property->setAccessible(true); |
||
| 96 | |||
| 97 | return $property->getValue($object); |
||
| 98 | } |
||
| 99 | } |
||
| 100 |