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 ArrayHydratorTest extends TestCase |
||
11 | { |
||
12 | private $response; |
||
13 | private $hydrator; |
||
14 | |||
15 | public function setUp() |
||
16 | { |
||
17 | $this->response = m::mock(Response::class); |
||
18 | |||
19 | $this->hydrator = new ArrayHydrator(); |
||
20 | } |
||
21 | |||
22 | public function testSuccessfulHydration() |
||
23 | { |
||
24 | $response = m::mock(Response::class, [ |
||
25 | 200, |
||
26 | ['Content-Type' => 'application/json'], |
||
27 | '{"document_tone": {}}' |
||
28 | ])->makePartial(); |
||
29 | |||
30 | |||
31 | $result = $this->hydrator->hydrate($response); |
||
32 | |||
33 | $this->assertArrayHasKey('document_tone', $result); |
||
34 | } |
||
35 | |||
36 | /** |
||
37 | * @expectedException \IBM\Watson\Common\Exception\HydrationException |
||
38 | * @expectedExceptionMessage The ArrayHydrator cannot hydrate response with Content-Type: text/plain |
||
39 | */ |
||
40 | public function testHydrateFailsForNoneJsonResponses() |
||
41 | { |
||
42 | $response = m::mock(Response::class, [200, ['Content-Type' => 'text/plain']])->makePartial(); |
||
43 | |||
44 | $this->hydrator->hydrate($response); |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * @expectedException \IBM\Watson\Common\Exception\HydrationException |
||
49 | */ |
||
50 | public function testHydrateFailsWhenJsonDecodeFails() |
||
51 | { |
||
52 | $response = m::mock(Response::class, [200, ['Content-Type' => 'application/json'], '{"brokenJson}']) |
||
53 | ->makePartial(); |
||
54 | |||
55 | $this->hydrator->hydrate($response); |
||
56 | } |
||
57 | } |
||
58 |