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 |
||
| 21 | class AddressClientTest extends \PHPUnit_Framework_TestCase |
||
| 22 | { |
||
| 23 | /** @var AddressClient */ |
||
| 24 | private $addressClient; |
||
| 25 | |||
| 26 | /** @var \PHPUnit_Framework_MockObject_MockObject|ClientInterface */ |
||
| 27 | private $guzzle; |
||
| 28 | |||
| 29 | /** @var \PHPUnit_Framework_MockObject_MockObject|ResponseInterface */ |
||
| 30 | private $response; |
||
| 31 | |||
| 32 | /** @var \PHPUnit_Framework_MockObject_MockObject|StreamInterface */ |
||
| 33 | private $stream; |
||
| 34 | |||
| 35 | public function setUp() |
||
| 36 | { |
||
| 37 | $this->guzzle = $this->createClientInterfaceMock(); |
||
| 38 | $this->response = $this->createResponseInterfaceMock(); |
||
| 39 | $this->stream = $this->createStreamInterfaceMock(); |
||
| 40 | |||
| 41 | $this->addressClient = new AddressClient($this->guzzle, 'secret-key'); |
||
| 42 | } |
||
| 43 | |||
| 44 | public function testGetAddress() |
||
| 45 | { |
||
| 46 | $this->guzzle->expects($this->once()) |
||
| 47 | ->method('send') |
||
| 48 | ->willReturn($this->response); |
||
| 49 | |||
| 50 | $this->response->expects($this->once()) |
||
| 51 | ->method('getStatusCode') |
||
| 52 | ->willReturn(200); |
||
| 53 | |||
| 54 | $this->response->expects($this->once()) |
||
| 55 | ->method('getBody') |
||
| 56 | ->willReturn($this->stream); |
||
| 57 | |||
| 58 | $this->stream->expects($this->once()) |
||
| 59 | ->method('getContents') |
||
| 60 | ->willReturn(file_get_contents(__DIR__ . '/response.json')); |
||
| 61 | |||
| 62 | $this->assertInstanceOf('Usoft\PostcodeBundle\Model\Address', $this->addressClient->getAddress('1010AB', 18)); |
||
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @expectedException \Usoft\PostcodeBundle\Exceptions\InvalidPostcodeException |
||
| 67 | */ |
||
| 68 | public function testGetAddressWithInvalidPostcode() |
||
| 69 | { |
||
| 70 | $this->guzzle->expects($this->never()) |
||
| 71 | ->method('send'); |
||
| 72 | |||
| 73 | $this->addressClient->getAddress('*@NXNI@', 18); |
||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @expectedException \Usoft\PostcodeBundle\Exceptions\InvalidApiResponseException |
||
| 78 | */ |
||
| 79 | public function testGetAddressWithInvalidResponse() |
||
| 80 | { |
||
| 81 | $this->guzzle->expects($this->once()) |
||
| 82 | ->method('send') |
||
| 83 | ->willReturn($this->response); |
||
| 84 | |||
| 85 | $this->response->expects($this->once()) |
||
| 86 | ->method('getStatusCode') |
||
| 87 | ->willReturn(401); |
||
| 88 | |||
| 89 | $this->response->expects($this->never()) |
||
| 90 | ->method('getBody'); |
||
| 91 | |||
| 92 | $this->addressClient->getAddress('1010AB', 18); |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @expectedException \Usoft\PostcodeBundle\Exceptions\InvalidApiResponseException |
||
| 97 | */ |
||
| 98 | public function testGetAddressWithInvalidJsonResponse() |
||
| 99 | { |
||
| 100 | $this->guzzle->expects($this->once()) |
||
| 101 | ->method('send') |
||
| 102 | ->willReturn($this->response); |
||
| 103 | |||
| 104 | $this->response->expects($this->once()) |
||
| 105 | ->method('getStatusCode') |
||
| 106 | ->willReturn(200); |
||
| 107 | |||
| 108 | $this->response->expects($this->once()) |
||
| 109 | ->method('getBody') |
||
| 110 | ->willReturn($this->stream); |
||
| 111 | |||
| 112 | $this->stream->expects($this->once()) |
||
| 113 | ->method('getContents') |
||
| 114 | ->willReturn(file_get_contents(__DIR__ . '/invalid.json')); |
||
| 115 | |||
| 116 | $this->addressClient->getAddress('1010AB', 18); |
||
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @return \PHPUnit_Framework_MockObject_MockObject|ClientInterface |
||
| 121 | */ |
||
| 122 | private function createClientInterfaceMock() |
||
| 123 | { |
||
| 124 | return $this->getMock('GuzzleHttp\ClientInterface'); |
||
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @return \PHPUnit_Framework_MockObject_MockObject|ResponseInterface |
||
| 129 | */ |
||
| 130 | private function createResponseInterfaceMock() |
||
| 131 | { |
||
| 132 | return $this->getMock('Psr\Http\Message\ResponseInterface'); |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @return \PHPUnit_Framework_MockObject_MockObject|StreamInterface |
||
| 137 | */ |
||
| 138 | private function createStreamInterfaceMock() |
||
| 139 | { |
||
| 140 | return $this->getMock('Psr\Http\Message\StreamInterface'); |
||
| 141 | } |
||
| 142 | } |
||
| 143 |