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 |
||
| 10 | class AdminFactoryTest extends Base |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * Init method should create Admin object according to given configuration. |
||
| 14 | */ |
||
| 15 | public function testInit() |
||
| 16 | { |
||
| 17 | // admin factory should work without configuration |
||
| 18 | $this->mockAdminFactory(); |
||
| 19 | // test admin creation |
||
| 20 | $configuration = $this->getAdminsConfiguration(); |
||
| 21 | $adminFactory = $this->mockAdminFactory($configuration); |
||
| 22 | $adminFactory->init(); |
||
| 23 | |||
| 24 | foreach ($configuration as $name => $adminConfiguration) { |
||
| 25 | $admin = $adminFactory->getAdmin($name); |
||
| 26 | $this->doTestAdmin($admin, $adminConfiguration, $name); |
||
| 27 | } |
||
| 28 | } |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Create method should return an Admin according to given configuration. |
||
| 32 | * |
||
| 33 | * @throws Exception |
||
| 34 | */ |
||
| 35 | public function testCreate() |
||
| 36 | { |
||
| 37 | // test minimal configuration |
||
| 38 | $adminConfiguration = $this->getAdminsConfiguration()['minimal_configuration']; |
||
| 39 | // mock admin factory with empty configuration |
||
| 40 | $adminFactory = $this->mockAdminFactory(); |
||
| 41 | $admin = $adminFactory->create('admin_test', $adminConfiguration); |
||
| 42 | $this->doTestAdmin($admin, $adminConfiguration, 'admin_test'); |
||
| 43 | |||
| 44 | // test full configuration |
||
| 45 | $adminConfiguration = $this->getAdminsConfiguration()['full_configuration']; |
||
| 46 | $admin = $adminFactory->create('admin_test2', $adminConfiguration); |
||
| 47 | $this->doTestAdmin($admin, $adminConfiguration, 'admin_test2'); |
||
| 48 | } |
||
| 49 | |||
| 50 | /** |
||
| 51 | * GetAdminFromRequest method should return a configured Admin from request parameters. |
||
| 52 | * |
||
| 53 | * @throws Exception |
||
| 54 | */ |
||
| 55 | public function testGetAdminFromRequest() |
||
| 56 | { |
||
| 57 | $adminConfiguration = $this->getAdminsConfiguration(); |
||
| 58 | $adminFactory = $this->mockAdminFactory($adminConfiguration); |
||
| 59 | $adminFactory->init(); |
||
| 60 | |||
| 61 | foreach ($adminConfiguration as $name => $configuration) { |
||
| 62 | $request = new Request([], [], [ |
||
| 63 | '_route_params' => [ |
||
| 64 | '_admin' => $name, |
||
| 65 | // see Base->mockActionFactory |
||
| 66 | '_action' => 'test' |
||
| 67 | ] |
||
| 68 | ]); |
||
| 69 | $admin = $adminFactory->getAdminFromRequest($request); |
||
| 70 | $this->doTestAdmin($admin, $configuration, $name); |
||
| 71 | } |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * GetAdmin method should return an configured Admin by its name. |
||
| 76 | * |
||
| 77 | * @throws Exception |
||
| 78 | */ |
||
| 79 | public function testGetAdmin() |
||
| 80 | { |
||
| 81 | // test with no configuration |
||
| 82 | $adminFactory = $this->mockAdminFactory(); |
||
| 83 | // unknow admin not exists, it should throw an exception |
||
| 84 | $this->assertExceptionRaised('Exception', function () use ($adminFactory) { |
||
| 85 | $adminFactory->getAdmin('unknown_admin'); |
||
| 86 | }); |
||
| 87 | // test with configurations samples |
||
| 88 | $adminsConfiguration = $this->getAdminsConfiguration(); |
||
| 89 | $adminFactory = $this->mockAdminFactory($adminsConfiguration); |
||
| 90 | $adminFactory->init(); |
||
| 91 | |||
| 92 | foreach ($adminsConfiguration as $name => $configuration) { |
||
| 93 | $admin = $adminFactory->getAdmin($name); |
||
| 94 | $this->doTestAdmin($admin, $configuration, $name); |
||
| 95 | } |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * GetAdmins method should return all configured Admin. |
||
| 100 | * |
||
| 101 | * @throws Exception |
||
| 102 | */ |
||
| 103 | public function testGetAdmins() |
||
| 104 | { |
||
| 105 | // test with no configuration |
||
| 106 | $adminFactory = $this->mockAdminFactory(); |
||
| 107 | // unknow admin not exists, it should throw an exception |
||
| 108 | $this->assertExceptionRaised('Exception', function () use ($adminFactory) { |
||
| 109 | $adminFactory->getAdmin('unknown_admin'); |
||
| 110 | }); |
||
| 111 | // test with configurations samples |
||
| 112 | $adminsConfiguration = $this->getAdminsConfiguration(); |
||
| 113 | $adminFactory = $this->mockAdminFactory($adminsConfiguration); |
||
| 114 | $adminFactory->init(); |
||
| 115 | |||
| 116 | $admins = $adminFactory->getAdmins(); |
||
| 117 | |||
| 118 | foreach ($admins as $admin) { |
||
| 119 | $this->assertArrayHasKey($admin->getName(), $adminsConfiguration); |
||
| 120 | $this->doTestAdmin($admin, $adminsConfiguration[$admin->getName()], $admin->getName()); |
||
| 121 | } |
||
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * AddDataProvider method must add a DataProviderInterface to the Admin. |
||
| 126 | */ |
||
| 127 | public function testAddDataProvider() |
||
| 128 | { |
||
| 129 | // test with no configuration |
||
| 130 | $adminFactory = $this->mockAdminFactory(); |
||
| 131 | // unknow admin not exists, it should throw an exception |
||
| 132 | $this->assertExceptionRaised('Exception', function () use ($adminFactory) { |
||
| 133 | $adminFactory->getAdmin('unknown_admin'); |
||
| 134 | }); |
||
| 135 | $dataProvider = $this->mockDataProvider(); |
||
| 136 | $adminFactory->addDataProvider('test', $dataProvider); |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @param AdminInterface $admin |
||
| 141 | * @param array $configuration |
||
| 142 | * @param $adminName |
||
| 143 | */ |
||
| 144 | protected function doTestAdmin(AdminInterface $admin, array $configuration, $adminName) |
||
| 145 | { |
||
| 146 | $this->assertEquals($admin->getName(), $adminName); |
||
| 147 | $this->assertEquals($admin->getConfiguration()->getFormType(), $configuration['form']); |
||
| 148 | $this->assertEquals($admin->getConfiguration()->getEntityName(), $configuration['entity']); |
||
| 149 | |||
| 150 | if (array_key_exists('controller', $configuration)) { |
||
| 151 | $this->assertEquals($admin->getConfiguration()->getControllerName(), $configuration['controller']); |
||
| 152 | } |
||
| 153 | if (array_key_exists('max_per_page', $configuration)) { |
||
| 154 | $this->assertEquals($admin->getConfiguration()->getMaxPerPage(), $configuration['max_per_page']); |
||
| 155 | } else { |
||
| 156 | $this->assertEquals($admin->getConfiguration()->getMaxPerPage(), 25); |
||
| 157 | } |
||
| 158 | } |
||
| 159 | } |
||
| 160 |