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