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 |
||
| 16 | class ActionFactoryTest extends AdminTestBase |
||
| 17 | { |
||
| 18 | public function testCreate() |
||
| 19 | { |
||
| 20 | $resourceCollection = $this->getMockWithoutConstructor(ResourceCollection::class); |
||
| 21 | $eventDispatcher = $this->getMockWithoutConstructor(EventDispatcherInterface::class); |
||
| 22 | |||
| 23 | $adminConfiguration = $this->getMockWithoutConstructor(AdminConfiguration::class); |
||
| 24 | $adminConfiguration |
||
| 25 | ->expects($this->atLeastOnce()) |
||
| 26 | ->method('getParameter') |
||
| 27 | ->willReturnMap([ |
||
| 28 | ['actions', [ |
||
| 29 | 'list' => [], |
||
| 30 | ]], |
||
| 31 | ['entity', 'MyLittleTauntaun'], |
||
| 32 | ['class', EntityFixture::class], |
||
| 33 | ]) |
||
| 34 | ; |
||
| 35 | |||
| 36 | $actionConfiguration = $this->getMockWithoutConstructor(ActionConfiguration::class); |
||
| 37 | $actionConfiguration |
||
| 38 | ->expects($this->once()) |
||
| 39 | ->method('getParameter') |
||
| 40 | ->willReturnMap([ |
||
| 41 | ['class', ActionFixture::class], |
||
| 42 | ]) |
||
| 43 | ; |
||
| 44 | |||
| 45 | $configurationFactory = $this->getMockWithoutConstructor(ConfigurationFactory::class); |
||
| 46 | $configurationFactory |
||
| 47 | ->expects($this->once()) |
||
| 48 | ->method('createActionConfiguration') |
||
| 49 | ->with('list', [], 'tauntaun', $adminConfiguration) |
||
| 50 | ->willReturn($actionConfiguration) |
||
| 51 | ; |
||
| 52 | |||
| 53 | $factory = new ActionFactory( |
||
| 54 | $resourceCollection, |
||
| 55 | $eventDispatcher, |
||
| 56 | $configurationFactory |
||
| 57 | ); |
||
| 58 | |||
| 59 | $factory->create('list', 'tauntaun', $adminConfiguration); |
||
| 60 | } |
||
| 61 | |||
| 62 | public function testCreateWithMissingAction() |
||
| 63 | { |
||
| 64 | $resourceCollection = $this->getMockWithoutConstructor(ResourceCollection::class); |
||
| 65 | $eventDispatcher = $this->getMockWithoutConstructor(EventDispatcherInterface::class); |
||
| 66 | $configurationFactory = $this->getMockWithoutConstructor(ConfigurationFactory::class); |
||
| 67 | |||
| 68 | $factory = new ActionFactory( |
||
| 69 | $resourceCollection, |
||
| 70 | $eventDispatcher, |
||
| 71 | $configurationFactory |
||
| 72 | ); |
||
| 73 | |||
| 74 | $adminConfiguration = $this->getMockWithoutConstructor(AdminConfiguration::class); |
||
| 75 | $adminConfiguration |
||
| 76 | ->expects($this->atLeastOnce()) |
||
| 77 | ->method('getParameter') |
||
| 78 | ->willReturnMap([ |
||
| 79 | ['actions', []], |
||
| 80 | ['entity', 'MyLittleTauntaun'], |
||
| 81 | ]) |
||
| 82 | ; |
||
| 83 | |||
| 84 | $this->assertExceptionRaised(Exception::class, function () use ($factory, $adminConfiguration) { |
||
| 85 | $factory->create('list', 'tauntaun', $adminConfiguration); |
||
| 86 | }); |
||
| 87 | } |
||
| 88 | |||
| 89 | public function testCreateWithWrongActionClass() |
||
| 90 | { |
||
| 91 | $resourceCollection = $this->getMockWithoutConstructor(ResourceCollection::class); |
||
| 92 | $eventDispatcher = $this->getMockWithoutConstructor(EventDispatcherInterface::class); |
||
| 93 | |||
| 94 | $adminConfiguration = $this->getMockWithoutConstructor(AdminConfiguration::class); |
||
| 95 | $adminConfiguration |
||
| 96 | ->expects($this->atLeastOnce()) |
||
| 97 | ->method('getParameter') |
||
| 98 | ->willReturnMap([ |
||
| 99 | ['actions', [ |
||
| 100 | 'list' => [], |
||
| 101 | ]], |
||
| 102 | ['entity', 'MyLittleTauntaun'], |
||
| 103 | ['class', EntityFixture::class], |
||
| 104 | ]) |
||
| 105 | ; |
||
| 106 | |||
| 107 | $actionConfiguration = $this->getMockWithoutConstructor(ActionConfiguration::class); |
||
| 108 | $actionConfiguration |
||
| 109 | ->expects($this->once()) |
||
| 110 | ->method('getParameter') |
||
| 111 | ->willReturnMap([ |
||
| 112 | ['class', EntityFixture::class], |
||
| 113 | ]) |
||
| 114 | ; |
||
| 115 | |||
| 116 | $configurationFactory = $this->getMockWithoutConstructor(ConfigurationFactory::class); |
||
| 117 | $configurationFactory |
||
| 118 | ->expects($this->once()) |
||
| 119 | ->method('createActionConfiguration') |
||
| 120 | ->with('list', [], 'tauntaun', $adminConfiguration) |
||
| 121 | ->willReturn($actionConfiguration) |
||
| 122 | ; |
||
| 123 | |||
| 124 | $factory = new ActionFactory( |
||
| 125 | $resourceCollection, |
||
| 126 | $eventDispatcher, |
||
| 127 | $configurationFactory |
||
| 128 | ); |
||
| 129 | |||
| 130 | $this->assertExceptionRaised(Exception::class, function () use ($factory, $adminConfiguration) { |
||
| 131 | $factory->create('list', 'tauntaun', $adminConfiguration); |
||
| 132 | }); |
||
| 133 | } |
||
| 134 | } |
||
| 135 |