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 EnvironmentTest extends \PHPUnit_Framework_TestCase |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * @var Environment |
||
| 14 | */ |
||
| 15 | private $environment; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * {@inheritdoc} |
||
| 19 | */ |
||
| 20 | protected function setUp() |
||
| 21 | { |
||
| 22 | $this->environment = $this->getMockForAbstractClass(Environment::class); |
||
| 23 | |||
| 24 | $this->environment->expects($this->any()) |
||
| 25 | ->method('getHost') |
||
| 26 | ->willReturn('test.com'); |
||
| 27 | |||
| 28 | $this->environment->expects($this->any()) |
||
| 29 | ->method('getWsHost') |
||
| 30 | ->willReturn('ws.test.com'); |
||
| 31 | } |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @test |
||
| 35 | */ |
||
| 36 | public function isValidShouldReturnTrueWhenHostIsProduction() |
||
| 37 | { |
||
| 38 | $this->assertTrue(Environment::isValid(Production::WS_HOST)); |
||
| 39 | } |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @test |
||
| 43 | */ |
||
| 44 | public function isValidShouldReturnFalseWhenHostIsSandbox() |
||
| 45 | { |
||
| 46 | $this->assertTrue(Environment::isValid(Sandbox::WS_HOST)); |
||
| 47 | } |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @test |
||
| 51 | */ |
||
| 52 | public function isValidShouldReturnFalseWhenHostNotProductionOrSandbox() |
||
| 53 | { |
||
| 54 | $this->assertFalse(Environment::isValid('example.org')); |
||
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @test |
||
| 59 | */ |
||
| 60 | public function getWsUrlShouldAppendProtocolAndWsHostToResource() |
||
| 61 | { |
||
| 62 | $this->assertEquals('https://ws.test.com/test', $this->environment->getWsUrl('/test')); |
||
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @test |
||
| 67 | */ |
||
| 68 | public function getUrlShouldAppendProtocolAndHostToResource() |
||
| 69 | { |
||
| 70 | $this->assertEquals('https://test.com/test', $this->environment->getUrl('/test')); |
||
| 71 | } |
||
| 72 | } |
||
| 73 |