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 | ||
| 25 | class StepActionTest extends AbstractGeneratorControllerTest | ||
| 26 | { | ||
| 27 | /** | ||
| 28 | * @expectedException \RuntimeException | ||
| 29 | * @expectedExceptionMessage Session is mandatory when using the character generator. | ||
| 30 | */ | ||
| 31 | public function test step action needs session() | ||
| 32 |     { | ||
| 33 | $controller = $this->createController(); | ||
| 34 | $request = new Request(); | ||
| 35 | |||
| 36 | $controller->stepAction($request, ''); | ||
| 37 | } | ||
| 38 | |||
| 39 | /** | ||
| 40 | * @expectedException \Symfony\Component\HttpKernel\Exception\NotFoundHttpException | ||
| 41 | * @expectedExceptionMessage Step not found. | ||
| 42 | */ | ||
| 43 | public function test step action with non existent name() | ||
| 44 |     { | ||
| 45 | $resolver = new StepResolver([ | ||
| 46 |             'manager_one' => $this->createManagerConfiguration('manager_one'), | ||
| 47 | ]); | ||
| 48 | |||
| 49 | $controller = $this->createController($resolver); | ||
| 50 | |||
| 51 | $controller->stepAction($this->createRequest(), 'non_existent_step'); | ||
| 52 | } | ||
| 53 | |||
| 54 | public function test step action should execute action class() | ||
| 55 |     { | ||
| 56 | $resolver = new StepResolver([ | ||
| 57 |             'test_manager' => $this->createManagerConfiguration('test_manager', [ | ||
| 58 | 'test_step' => [ | ||
| 59 | 'name' => 'test_step', | ||
| 60 | ], | ||
| 61 | ]), | ||
| 62 | ]); | ||
| 63 | |||
| 64 | $step = StepStub::createStub(); | ||
| 65 | |||
| 66 | $action = new ConcreteAbstractActionStub(); | ||
| 67 | $action->configure($step->getManagerName(), $step->getName(), CharacterStub::class, $resolver); | ||
| 68 | |||
| 69 | $registry = new ActionsRegistry(); | ||
| 70 |         $registry->addStepAction('test_manager', $action); | ||
| 71 | |||
| 72 | $controller = $this->createController($resolver, null, null, $registry); | ||
| 73 | |||
| 74 | $response = $controller->stepAction($this->createRequest(), 'test_step'); | ||
| 75 | |||
| 76 |         static::assertSame('Stub response based on abstract class', $response->getContent()); | ||
| 77 | } | ||
| 78 | |||
| 79 | public function test step action should check for dependencies() | ||
| 80 |     { | ||
| 81 | $resolver = new StepResolver([ | ||
| 82 |             'test_manager' => $this->createManagerConfiguration('test_manager', [ | ||
| 83 | 'test_step' => [ | ||
| 84 | 'name' => 'test_step', | ||
| 85 | 'label' => 'Test step one' | ||
| 86 | ], | ||
| 87 | 'second_step' => [ | ||
| 88 | 'name' => 'second_step', | ||
| 89 | 'label' => 'Test step two', | ||
| 90 | 'number' => 2, | ||
| 91 | 'dependencies' => ['test_step'], | ||
| 92 | ], | ||
| 93 | ]), | ||
| 94 | ]); | ||
| 95 | $router = $this->createMock(RouterInterface::class); | ||
| 96 | $router->expects(self::once()) | ||
| 97 |             ->method('generate') | ||
| 98 |             ->with('pierstoval_character_generator_index') | ||
| 99 |             ->willReturn('/generate/manager_one/test_step') | ||
| 100 | ; | ||
| 101 | $translator = $this->createMock(TranslatorInterface::class); | ||
| 102 | $translator->expects(self::once()) | ||
| 103 |             ->method('trans') | ||
| 104 |             ->with('steps.dependency_not_set', [ | ||
| 105 | '%current_step%' => 'Test step two', | ||
| 106 | '%dependency%' => 'Test step one', | ||
| 107 | ], 'PierstovalCharacterManager') | ||
| 108 |             ->willReturn('Translated error message') | ||
| 109 | ; | ||
| 110 | |||
| 111 |         $step2 = $resolver->resolve('second_step'); | ||
| 112 | |||
| 113 | $action = new ConcreteAbstractActionStub(); | ||
| 114 | $action->configure($step2->getManagerName(), $step2->getName(), CharacterStub::class, $resolver); | ||
| 115 | |||
| 116 | $registry = new ActionsRegistry(); | ||
| 117 |         $registry->addStepAction('test_manager', $action); | ||
| 118 | |||
| 119 | $request = $this->createRequest(); | ||
| 120 | $controller = $this->createController($resolver, $router, $translator, $registry); | ||
| 121 | $response = $controller->stepAction($request, 'second_step'); | ||
| 122 | |||
| 123 | static::assertInstanceOf(RedirectResponse::class, $response); | ||
| 124 |         static::assertTrue($response->isRedirect('/generate/manager_one/test_step')); | ||
| 125 |         static::assertSame(['Translated error message'], $request->getSession()->getFlashBag()->get('error')); | ||
| 126 | } | ||
| 127 | } | ||
| 128 |