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 |
||
| 13 | use stdClass; |
||
| 14 | use Symfony\Component\HttpFoundation\Request; |
||
| 15 | use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
||
| 16 | use Symfony\Component\Security\Core\User\User; |
||
| 17 | |||
| 18 | class AdminTest extends Base |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * Test if configuration is properly set. |
||
| 22 | */ |
||
| 23 | public function testAdmin() |
||
| 24 | { |
||
| 25 | $configurations = $this->getFakeAdminsConfiguration(); |
||
| 26 | |||
| 27 | foreach ($configurations as $adminName => $configuration) { |
||
| 28 | $adminConfiguration = new AdminConfiguration($configuration); |
||
| 29 | $admin = $this->mockAdmin($adminName, $adminConfiguration); |
||
| 30 | $this->doTestAdmin($admin, $configuration, $adminName); |
||
| 31 | } |
||
| 32 | } |
||
| 33 | |||
| 34 | /** |
||
| 35 | * handleRequest method SHOULD throw an exception if the action is not valid. |
||
| 36 | * handleRequest method SHOULD throw an exception if the action is not valid. |
||
| 37 | */ |
||
| 38 | public function testHandleRequest() |
||
| 39 | { |
||
| 40 | $configurations = $this->getFakeAdminsConfiguration(); |
||
| 41 | |||
| 42 | foreach ($configurations as $adminName => $configuration) { |
||
| 43 | $adminConfiguration = new AdminConfiguration($configuration); |
||
| 44 | $admin = $this->mockAdmin($adminName, $adminConfiguration); |
||
| 45 | $this->doTestAdmin($admin, $configuration, $adminName); |
||
| 46 | |||
| 47 | |||
| 48 | // with no action, handleRequest method SHOULD throw an exception |
||
| 49 | $this->assertExceptionRaised('Exception', function () use ($admin) { |
||
| 50 | $request = new Request(); |
||
| 51 | $admin->handleRequest($request); |
||
| 52 | }); |
||
| 53 | |||
| 54 | // with a wrong action, handleRequest method SHOULD throw an exception |
||
| 55 | $this->assertExceptionRaised('Exception', function () use ($admin) { |
||
| 56 | $request = new Request([], [], [ |
||
| 57 | '_route_params' => [ |
||
| 58 | '_action' => 'bad_action' |
||
| 59 | ] |
||
| 60 | ]); |
||
| 61 | $admin->handleRequest($request); |
||
| 62 | }); |
||
| 63 | |||
| 64 | // with an existing action, handleRequest method SHOULD NOT throwing an exception |
||
| 65 | $request = new Request([], [], [ |
||
| 66 | '_route_params' => [ |
||
| 67 | '_action' => 'custom_list' |
||
| 68 | ] |
||
| 69 | ]); |
||
| 70 | $admin->handleRequest($request); |
||
| 71 | } |
||
| 72 | |||
| 73 | // test pagerfanta filter |
||
| 74 | $configurations = $this->getFakeAdminsConfiguration(); |
||
| 75 | $configuration = $configurations['full_entity']; |
||
| 76 | |||
| 77 | $adminConfiguration = new AdminConfiguration($configuration); |
||
| 78 | $admin = $this->mockAdmin('full_entity', $adminConfiguration); |
||
| 79 | |||
| 80 | $admin->addAction(new Action('custom_list', [ |
||
| 81 | 'title' => 'Test action', |
||
| 82 | 'permissions' => [ |
||
| 83 | 'ROLE_ADMIN' |
||
| 84 | ], |
||
| 85 | 'submit_actions' => [], |
||
| 86 | 'batch' => [], |
||
| 87 | ], new ActionConfiguration([ |
||
| 88 | 'load_strategy' => '', |
||
| 89 | 'route' => '', |
||
| 90 | 'parameters' => '', |
||
| 91 | 'export' => '', |
||
| 92 | 'order' => '', |
||
| 93 | 'target' => '', |
||
| 94 | 'icon' => '', |
||
| 95 | 'batch' => '', |
||
| 96 | 'pager' => 'pagerfanta', |
||
| 97 | 'criteria' => [], |
||
| 98 | ]) |
||
| 99 | )); |
||
| 100 | $request = new Request([], [], [ |
||
| 101 | '_route_params' => [ |
||
| 102 | '_action' => 'custom_list' |
||
| 103 | ] |
||
| 104 | ]); |
||
| 105 | $admin->handleRequest($request); |
||
| 106 | |||
| 107 | // test load stregy none |
||
| 108 | $configurations = $this->getFakeAdminsConfiguration(); |
||
| 109 | $configuration = $configurations['full_entity']; |
||
| 110 | |||
| 111 | $adminConfiguration = new AdminConfiguration($configuration); |
||
| 112 | $admin = $this->mockAdmin('full_entity', $adminConfiguration); |
||
| 113 | |||
| 114 | $admin->addAction(new Action('custom_list', [ |
||
| 115 | 'title' => 'Test action', |
||
| 116 | 'permissions' => [ |
||
| 117 | 'ROLE_ADMIN' |
||
| 118 | ], |
||
| 119 | 'submit_actions' => [], |
||
| 120 | 'batch' => [], |
||
| 121 | ], new ActionConfiguration([ |
||
| 122 | 'load_strategy' => Admin::LOAD_STRATEGY_NONE, |
||
| 123 | 'route' => '', |
||
| 124 | 'parameters' => '', |
||
| 125 | 'export' => '', |
||
| 126 | 'order' => '', |
||
| 127 | 'target' => '', |
||
| 128 | 'icon' => '', |
||
| 129 | 'batch' => '', |
||
| 130 | 'pager' => 'pagerfanta', |
||
| 131 | 'criteria' => [], |
||
| 132 | ]) |
||
| 133 | )); |
||
| 134 | $request = new Request([], [], [ |
||
| 135 | '_route_params' => [ |
||
| 136 | '_action' => 'custom_list' |
||
| 137 | ] |
||
| 138 | ]); |
||
| 139 | $admin->handleRequest($request); |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * checkPermissions method SHOULd throw an exception if the permissions are invalid. |
||
| 144 | */ |
||
| 145 | public function testCheckPermissions() |
||
| 146 | { |
||
| 147 | $configurations = $this->getFakeAdminsConfiguration(); |
||
| 148 | |||
| 149 | foreach ($configurations as $adminName => $configuration) { |
||
| 150 | $adminConfiguration = new AdminConfiguration($configuration); |
||
| 151 | $admin = $this->mockAdmin($adminName, $adminConfiguration); |
||
| 152 | $this->doTestAdmin($admin, $configuration, $adminName); |
||
| 153 | |||
| 154 | // with a current action unset, checkPermissions method SHOULD throw an exception |
||
| 155 | $this->assertExceptionRaised('Exception', function () use ($admin) { |
||
| 156 | $user = new User('JohnKrovitch', 'john1234'); |
||
| 157 | $admin->checkPermissions($user); |
||
| 158 | }); |
||
| 159 | |||
| 160 | // with the wrong roles, checkPermissions method SHOULD throw an exception |
||
| 161 | $this->assertExceptionRaised(NotFoundHttpException::class, function () use ($admin) { |
||
| 162 | $request = new Request([], [], [ |
||
| 163 | '_route_params' => [ |
||
| 164 | '_action' => 'custom_list' |
||
| 165 | ] |
||
| 166 | ]); |
||
| 167 | $user = new User('JohnKrovitch', 'john1234'); |
||
| 168 | $admin->handleRequest($request); |
||
| 169 | $admin->checkPermissions($user); |
||
| 170 | }); |
||
| 171 | |||
| 172 | // with the wrong roles, checkPermissions method SHOULD throw an exception |
||
| 173 | $this->assertExceptionRaised(NotFoundHttpException::class, function () use ($admin) { |
||
| 174 | $request = new Request([], [], [ |
||
| 363 |