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 |
||
7 | class PermissionManagerTest extends AbstractManagerTest |
||
8 | { |
||
9 | private $permissions; |
||
10 | private $permissionManager; |
||
11 | |||
12 | public function setUp() |
||
13 | { |
||
14 | parent::setUp(); |
||
15 | |||
16 | $this->permissions = $this->prophesize('RabbitMq\ManagementApi\Api\Permission'); |
||
17 | $this->client->permissions()->willReturn($this->permissions->reveal()); |
||
18 | $this->configuration->isDeleteAllowed()->willReturn(false); |
||
19 | |||
20 | $this->permissionManager = new PermissionManager(); |
||
21 | } |
||
22 | |||
23 | public function test_define_create() |
||
24 | { |
||
25 | $this->configuration->getConfiguration('permissions')->willReturn(array( |
||
26 | 'foo' => array(), |
||
27 | )); |
||
28 | |||
29 | $exception = $this->get404Exception(); |
||
30 | $this->permissions->get('vhost', 'foo')->willThrow($exception->reveal()); |
||
31 | |||
32 | $this->permissions->create('vhost', 'foo', array())->shouldBeCalled(); |
||
33 | |||
34 | $this->permissionManager->define($this->configuration->reveal()); |
||
35 | } |
||
36 | |||
37 | public function test_define_exist() |
||
38 | { |
||
39 | $this->configuration->getConfiguration('permissions')->willReturn(array( |
||
40 | 'foo' => array(), |
||
41 | )); |
||
42 | |||
43 | $this->permissions->get('vhost', 'foo')->willReturn(array()); |
||
44 | |||
45 | $this->permissions->create('vhost', 'foo', array())->shouldNotBeCalled(); |
||
46 | |||
47 | $this->permissionManager->define($this->configuration->reveal()); |
||
48 | } |
||
49 | |||
50 | public function test_define_update() |
||
51 | { |
||
52 | $this->configuration->getConfiguration('permissions')->willReturn(array( |
||
53 | 'foo' => array('durable' => true), |
||
54 | 'bar' => array('durable' => true) |
||
55 | )); |
||
56 | |||
57 | $this->configuration->isDeleteAllowed()->willReturn(true); |
||
58 | |||
59 | $this->permissions->get('vhost', 'foo')->willReturn(array('durable' => true)); |
||
60 | $this->permissions->get('vhost', 'bar')->willReturn(array('durable' => false)); |
||
61 | |||
62 | $this->permissions->create('vhost', 'foo', array('durable' => true))->shouldNotBeCalled(); |
||
63 | $this->permissions->delete('vhost', 'bar')->shouldBeCalled(); |
||
64 | $this->permissions->create('vhost', 'bar', array('durable' => true))->shouldBeCalled(); |
||
65 | |||
66 | $this->permissionManager->define($this->configuration->reveal()); |
||
67 | } |
||
68 | } |
||
69 |