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 |
||
14 | class WriterClientTest extends \PHPUnit_Framework_TestCase |
||
15 | { |
||
16 | |||
17 | public function test_http_client_construction() |
||
18 | { |
||
19 | $factory = $this->prophesize(InfluxDbClientFactory::class); |
||
20 | $factory->buildHttpClient()->shouldBeCalledTimes(1); |
||
21 | |||
22 | $writer = new WriterClient($factory->reveal(), ClientInterface::HTTP_CLIENT); |
||
23 | |||
24 | $this->assertInstanceOf(WriterInterface::class, $writer); |
||
25 | } |
||
26 | |||
27 | public function test_udp_client_construction() |
||
28 | { |
||
29 | $factory = $this->prophesize(InfluxDbClientFactory::class); |
||
30 | $factory->buildUdpClient()->shouldBeCalledTimes(1); |
||
31 | |||
32 | $writer = new WriterClient($factory->reveal(), ClientInterface::UDP_CLIENT); |
||
33 | |||
34 | $this->assertInstanceOf(WriterInterface::class, $writer); |
||
35 | } |
||
36 | |||
37 | public function test_write() |
||
38 | { |
||
39 | $idbDatabase = $this->prophesize(Database::class); |
||
40 | $idbDatabase->writePoints(Argument::type('array'),Argument::type('string')) |
||
41 | ->shouldBeCalledTimes(1) |
||
42 | ->willReturn(true); |
||
43 | |||
44 | $factory = $this->prophesize(InfluxDbClientFactory::class); |
||
45 | $factory->buildHttpClient()->shouldBeCalledTimes(1)->willReturn($idbDatabase->reveal()); |
||
46 | |||
47 | $writer = new WriterClient($factory->reveal(), ClientInterface::HTTP_CLIENT); |
||
48 | $writer->write(new PointsCollection(),'test'); |
||
49 | } |
||
50 | |||
51 | } |
||
52 |