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 |
||
| 22 | class AdminTest extends AdminTestBase |
||
| 23 | { |
||
| 24 | public function testHandleRequest() |
||
| 25 | { |
||
| 26 | $resource = $this->getMockWithoutConstructor(AdminResource::class); |
||
| 27 | $resource |
||
| 28 | ->expects($this->once()) |
||
| 29 | ->method('getName') |
||
| 30 | ->willReturn('admin_test') |
||
| 31 | ; |
||
| 32 | $configuration = $this->getMockWithoutConstructor(AdminConfiguration::class); |
||
| 33 | $action = $this->getMockWithoutConstructor(ActionInterface::class); |
||
| 34 | $form = $this->getMockWithoutConstructor(FormInterface::class); |
||
| 35 | $form |
||
| 36 | ->expects($this->once()) |
||
| 37 | ->method('isValid') |
||
| 38 | ->willReturn(true) |
||
| 39 | ; |
||
| 40 | $form |
||
| 41 | ->expects($this->once()) |
||
| 42 | ->method('isSubmitted') |
||
| 43 | ->willReturn(true) |
||
| 44 | ; |
||
| 45 | |||
| 46 | $eventDispatcher = $this->getMockWithoutConstructor(EventDispatcher::class); |
||
| 47 | $eventDispatcher |
||
| 48 | ->expects($this->exactly(5)) |
||
| 49 | ->method('dispatch') |
||
| 50 | ->willReturnCallback(function ($eventName, $event) use ($action, $form) { |
||
| 51 | if (AdminEvents::HANDLE_REQUEST === $eventName) { |
||
| 52 | /** @var AdminEvent $event */ |
||
| 53 | $this->assertInstanceOf(AdminEvent::class, $event); |
||
| 54 | $event->setAction($action); |
||
| 55 | } |
||
| 56 | |||
| 57 | if (AdminEvents::ENTITY_LOAD === $eventName) { |
||
| 58 | /** @var EntityEvent $event */ |
||
| 59 | $this->assertInstanceOf(EntityEvent::class, $event); |
||
| 60 | $event->setEntities(new ArrayCollection([ |
||
| 61 | 'test', |
||
| 62 | ])); |
||
| 63 | } |
||
| 64 | |||
| 65 | if (AdminEvents::HANDLE_FORM === $eventName) { |
||
| 66 | /** @var FormEvent $event */ |
||
| 67 | $this->assertInstanceOf(FormEvent::class, $event); |
||
| 68 | $event->addForm($form, 'entity'); |
||
| 69 | } |
||
| 70 | }) |
||
| 71 | ; |
||
| 72 | |||
| 73 | $admin = new Admin( |
||
| 74 | $resource, |
||
| 75 | $configuration, |
||
| 76 | $eventDispatcher |
||
| 77 | ); |
||
| 78 | $request = new Request(); |
||
| 79 | |||
| 80 | $admin->handleRequest($request); |
||
| 81 | |||
| 82 | $this->assertEquals('admin_test', $admin->getName()); |
||
| 83 | $this->assertEquals($resource, $admin->getResource()); |
||
| 84 | $this->assertEquals($eventDispatcher, $admin->getEventDispatcher()); |
||
| 85 | $this->assertEquals($configuration, $admin->getConfiguration()); |
||
| 86 | $this->assertCount(1, $admin->getEntities()); |
||
| 87 | $this->assertEquals($admin->getEntities()[0], 'test'); |
||
| 88 | $this->assertCount(1, $admin->getForms()); |
||
| 89 | $this->assertEquals($form, $admin->getForms()['entity']); |
||
| 90 | } |
||
| 91 | |||
| 92 | public function testHandleRequestWithoutAction() |
||
| 93 | { |
||
| 94 | $resource = $this->getMockWithoutConstructor(AdminResource::class); |
||
| 95 | $resource |
||
| 96 | ->expects($this->once()) |
||
| 97 | ->method('getName') |
||
| 98 | ->willReturn('admin_test') |
||
| 99 | ; |
||
| 100 | $configuration = $this->getMockWithoutConstructor(AdminConfiguration::class); |
||
| 101 | |||
| 102 | $eventDispatcher = $this->getMockWithoutConstructor(EventDispatcher::class); |
||
| 103 | $eventDispatcher |
||
| 104 | ->expects($this->exactly(1)) |
||
| 105 | ->method('dispatch') |
||
| 106 | ; |
||
| 107 | |||
| 108 | $admin = new Admin( |
||
| 109 | $resource, |
||
| 110 | $configuration, |
||
| 111 | $eventDispatcher |
||
| 112 | ); |
||
| 113 | $request = new Request(); |
||
| 114 | |||
| 115 | $this->assertExceptionRaised(Exception::class, function () use ($admin, $request) { |
||
| 116 | $admin->handleRequest($request); |
||
| 117 | }); |
||
| 118 | } |
||
| 119 | |||
| 120 | public function testCreateView() |
||
| 121 | { |
||
| 122 | $resource = $this->getMockWithoutConstructor(AdminResource::class); |
||
| 123 | $configuration = $this->getMockWithoutConstructor(AdminConfiguration::class); |
||
| 124 | $view = $this->getMockWithoutConstructor(ViewInterface::class); |
||
| 125 | |||
| 126 | $eventDispatcher = $this->getMockWithoutConstructor(EventDispatcher::class); |
||
| 127 | $eventDispatcher |
||
| 128 | ->expects($this->once()) |
||
| 129 | ->method('dispatch') |
||
| 130 | ->willReturnCallback(function ($eventName, $event) use ($view) { |
||
| 131 | $this->assertEquals(AdminEvents::VIEW, $eventName); |
||
| 132 | /** @var ViewEvent $event */ |
||
| 133 | $this->assertInstanceOf(ViewEvent::class, $event); |
||
| 134 | |||
| 135 | $event->setView($view); |
||
| 136 | }) |
||
| 137 | ; |
||
| 138 | |||
| 139 | $admin = new Admin( |
||
| 140 | $resource, |
||
| 141 | $configuration, |
||
| 142 | $eventDispatcher |
||
| 143 | ); |
||
| 144 | $request = new Request(); |
||
| 145 | $this->setPrivateProperty($admin, 'request', $request); |
||
| 146 | |||
| 147 | $createdView = $admin->createView(); |
||
| 148 | $this->assertInstanceOf(ViewInterface::class, $createdView); |
||
| 149 | $this->assertEquals($view, $createdView); |
||
| 150 | } |
||
| 151 | |||
| 152 | public function testGetAction() |
||
| 153 | { |
||
| 154 | $resource = $this->getMockWithoutConstructor(AdminResource::class); |
||
| 155 | $configuration = $this->getMockWithoutConstructor(AdminConfiguration::class); |
||
| 156 | $eventDispatcher = $this->getMockWithoutConstructor(EventDispatcher::class); |
||
| 157 | |||
| 158 | $admin = new Admin( |
||
| 159 | $resource, |
||
| 160 | $configuration, |
||
| 161 | $eventDispatcher |
||
| 162 | ); |
||
| 163 | $action = $this->getMockWithoutConstructor(ActionInterface::class); |
||
| 164 | $this->setPrivateProperty($admin, 'action', $action); |
||
| 165 | |||
| 166 | $this->assertEquals($action, $admin->getAction()); |
||
| 167 | $this->assertTrue($admin->hasAction()); |
||
| 168 | } |
||
| 169 | |||
| 170 | public function testHandleFormWithoutForm() |
||
| 171 | { |
||
| 172 | $resource = $this->getMockWithoutConstructor(AdminResource::class); |
||
| 173 | $configuration = $this->getMockWithoutConstructor(AdminConfiguration::class); |
||
| 174 | $action = $this->getMockWithoutConstructor(ActionInterface::class); |
||
| 175 | $form = $this->getMockWithoutConstructor(FormInterface::class); |
||
| 176 | |||
| 177 | $eventDispatcher = $this->getMockWithoutConstructor(EventDispatcher::class); |
||
| 178 | $eventDispatcher |
||
| 179 | ->expects($this->exactly(4)) |
||
| 180 | ->method('dispatch') |
||
| 181 | ->willReturnCallback(function ($eventName, $event) use ($action, $form) { |
||
| 182 | if (AdminEvents::HANDLE_REQUEST === $eventName) { |
||
| 183 | /** @var AdminEvent $event */ |
||
| 184 | $this->assertInstanceOf(AdminEvent::class, $event); |
||
| 185 | $event->setAction($action); |
||
| 186 | } |
||
| 187 | }) |
||
| 188 | ; |
||
| 189 | |||
| 190 | $admin = new Admin( |
||
| 191 | $resource, |
||
| 192 | $configuration, |
||
| 193 | $eventDispatcher |
||
| 194 | ); |
||
| 195 | $request = new Request(); |
||
| 196 | |||
| 197 | $admin->handleRequest($request); |
||
| 198 | } |
||
| 199 | |||
| 200 | public function testHandleFormWithoutEntities() |
||
| 201 | { |
||
| 202 | $resource = $this->getMockWithoutConstructor(AdminResource::class); |
||
| 203 | $configuration = $this->getMockWithoutConstructor(AdminConfiguration::class); |
||
| 204 | $action = $this->getMockWithoutConstructor(ActionInterface::class); |
||
| 205 | $form = $this->getMockWithoutConstructor(FormInterface::class); |
||
| 206 | |||
| 207 | $eventDispatcher = $this->getMockWithoutConstructor(EventDispatcher::class); |
||
| 208 | $eventDispatcher |
||
| 209 | ->expects($this->exactly(4)) |
||
| 210 | ->method('dispatch') |
||
| 211 | ->willReturnCallback(function ($eventName, $event) use ($action, $form) { |
||
| 212 | if (AdminEvents::HANDLE_REQUEST === $eventName) { |
||
| 213 | /** @var AdminEvent $event */ |
||
| 214 | $this->assertInstanceOf(AdminEvent::class, $event); |
||
| 215 | $event->setAction($action); |
||
| 216 | } |
||
| 217 | |||
| 218 | if (AdminEvents::ENTITY_LOAD === $eventName) { |
||
| 219 | /** @var EntityEvent $event */ |
||
| 220 | $this->assertInstanceOf(EntityEvent::class, $event); |
||
| 221 | $event->setEntities(new ArrayCollection()); |
||
| 222 | } |
||
| 223 | |||
| 224 | if (AdminEvents::HANDLE_FORM === $eventName) { |
||
| 225 | /** @var FormEvent $event */ |
||
| 226 | $this->assertInstanceOf(FormEvent::class, $event); |
||
| 227 | $event->addForm($form, 'entity'); |
||
| 228 | } |
||
| 229 | }) |
||
| 230 | ; |
||
| 231 | |||
| 232 | $admin = new Admin( |
||
| 233 | $resource, |
||
| 234 | $configuration, |
||
| 235 | $eventDispatcher |
||
| 236 | ); |
||
| 237 | $request = new Request(); |
||
| 238 | |||
| 239 | $admin->handleRequest($request); |
||
| 240 | } |
||
| 241 | } |
||
| 242 |