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 BuilderTest extends TestCase |
||
| 15 | { |
||
| 16 | private $httpClient; |
||
| 17 | private $messageFactory; |
||
| 18 | private $uriFactory; |
||
| 19 | |||
| 20 | public function setUp() |
||
| 21 | { |
||
| 22 | $this->httpClient = m::mock(HttpClient::class); |
||
| 23 | $this->messageFactory = m::mock(MessageFactory::class); |
||
| 24 | $this->uriFactory = m::mock(UriFactory::class); |
||
| 25 | } |
||
| 26 | |||
| 27 | public function testCreateConfiguredClient() |
||
| 28 | { |
||
| 29 | $client = (new Builder())->createConfiguredClient(); |
||
| 30 | |||
| 31 | $this->assertInstanceOf(PluginClient::class, $client); |
||
| 32 | } |
||
| 33 | |||
| 34 | public function testWithEndpoint() |
||
| 35 | { |
||
| 36 | $client = (new Builder())->withEndpoint('https://mockurl.test')->createConfiguredClient(); |
||
| 37 | |||
| 38 | $this->assertInstanceOf(PluginClient::class, $client); |
||
| 39 | } |
||
| 40 | |||
| 41 | public function testWithCredentials() |
||
| 42 | { |
||
| 43 | $client = (new Builder()) |
||
| 44 | ->withCredentials('adam', 'password') |
||
| 45 | ->createConfiguredClient(); |
||
| 46 | |||
| 47 | $this->assertInstanceOf(PluginClient::class, $client); |
||
| 48 | } |
||
| 49 | |||
| 50 | public function testAppendPlugin() |
||
| 51 | { |
||
| 52 | $plugin1 = m::mock(Plugin::class); |
||
| 53 | $plugin2 = m::mock(Plugin::class); |
||
| 54 | $plugin3 = m::mock(Plugin::class); |
||
| 55 | |||
| 56 | $client = (new Builder())->appendPlugin($plugin1, $plugin2, $plugin3)->createConfiguredClient(); |
||
| 57 | |||
| 58 | $this->assertInstanceOf(PluginClient::class, $client); |
||
| 59 | } |
||
| 60 | |||
| 61 | public function testPrependPlugin() |
||
| 62 | { |
||
| 63 | $plugin1 = m::mock(Plugin::class); |
||
| 64 | $plugin2 = m::mock(Plugin::class); |
||
| 65 | $plugin3 = m::mock(Plugin::class); |
||
| 66 | |||
| 67 | $client = (new Builder())->prependPlugin($plugin1, $plugin2, $plugin3)->createConfiguredClient(); |
||
| 68 | |||
| 69 | $this->assertInstanceOf(PluginClient::class, $client); |
||
| 70 | } |
||
| 71 | |||
| 72 | public function testWithLearningOptOut() |
||
| 73 | { |
||
| 74 | $client = (new Builder())->withLearningOptOut()->createConfiguredClient(); |
||
| 75 | |||
| 76 | $this->assertInstanceOf(PluginClient::class, $client); |
||
| 77 | } |
||
| 78 | |||
| 79 | public function testWithApiKey() |
||
| 80 | { |
||
| 81 | $client = (new Builder())->withApiKey('somekey')->createConfiguredClient(); |
||
| 82 | |||
| 83 | $this->assertInstanceOf(PluginClient::class, $client); |
||
| 84 | } |
||
| 85 | |||
| 86 | public function testWithVersion() |
||
| 87 | { |
||
| 88 | $client = (new Builder())->withVersion('123')->createConfiguredClient(); |
||
| 89 | |||
| 90 | $this->assertInstanceOf(PluginClient::class, $client); |
||
| 91 | } |
||
| 92 | } |
||
| 93 |