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 |
||
| 11 | class LocatorTest extends \PHPUnit_Framework_TestCase |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * @var Credentials |
||
| 15 | */ |
||
| 16 | protected $credentials; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var Client|\PHPUnit_Framework_MockObject_MockObject |
||
| 20 | */ |
||
| 21 | protected $client; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var Decoder|\PHPUnit_Framework_MockObject_MockObject |
||
| 25 | */ |
||
| 26 | protected $decoder; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var Transaction|\PHPUnit_Framework_MockObject_MockObject |
||
| 30 | */ |
||
| 31 | private $transaction; |
||
| 32 | |||
| 33 | protected function setUp() |
||
| 34 | { |
||
| 35 | $environment = $this->getMockForAbstractClass(Environment::class); |
||
| 36 | |||
| 37 | $environment->expects($this->any()) |
||
| 38 | ->method('getHost') |
||
| 39 | ->willReturn('test.com'); |
||
| 40 | |||
| 41 | $environment->expects($this->any()) |
||
| 42 | ->method('getWsHost') |
||
| 43 | ->willReturn('ws.test.com'); |
||
| 44 | |||
| 45 | $this->credentials = new Credentials('[email protected]', 't', $environment); |
||
| 46 | |||
| 47 | $this->client = $this->createMock(Client::class); |
||
| 48 | $this->decoder = $this->createMock(Decoder::class); |
||
| 49 | $this->transaction = $this->createMock(Transaction::class); |
||
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @test |
||
| 54 | */ |
||
| 55 | public function getByCodeShouldDoAGetRequestAddingCredentialsData() |
||
| 56 | { |
||
| 57 | $xml = simplexml_load_string('<?xml version="1.0" encoding="UTF-8"?><data />'); |
||
| 58 | |||
| 59 | $this->client->expects($this->once()) |
||
| 60 | ->method('get') |
||
| 61 | ->with('https://ws.test.com/v2/transactions/1?email=a%40a.com&token=t') |
||
| 62 | ->willReturn($xml); |
||
| 63 | |||
| 64 | $this->decoder->expects($this->once()) |
||
| 65 | ->method('decode') |
||
| 66 | ->with($xml) |
||
| 67 | ->willReturn($this->transaction); |
||
| 68 | |||
| 69 | $service = new Locator($this->credentials, $this->client, $this->decoder); |
||
| 70 | |||
| 71 | $this->assertSame($this->transaction, $service->getByCode(1)); |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @test |
||
| 76 | */ |
||
| 77 | public function getByNotificationShouldDoAGetRequestAddingCredentialsData() |
||
| 78 | { |
||
| 79 | $xml = simplexml_load_string('<?xml version="1.0" encoding="UTF-8"?><data />'); |
||
| 80 | |||
| 81 | $this->client->expects($this->once()) |
||
| 82 | ->method('get') |
||
| 83 | ->with('https://ws.test.com/v2/transactions/notifications/1?email=a%40a.com&token=t') |
||
| 84 | ->willReturn($xml); |
||
| 85 | |||
| 86 | $this->decoder->expects($this->once()) |
||
| 87 | ->method('decode') |
||
| 88 | ->with($xml) |
||
| 89 | ->willReturn($this->transaction); |
||
| 90 | |||
| 91 | $service = new Locator($this->credentials, $this->client, $this->decoder); |
||
| 92 | |||
| 93 | $this->assertSame($this->transaction, $service->getByNotification(1)); |
||
| 94 | } |
||
| 95 | } |
||
| 96 |