Completed
Branch develop (3cde25)
by Adam
14:50
created

ArrayHydratorTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 48
Duplicated Lines 14.58 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 4
dl 7
loc 48
rs 10
c 0
b 0
f 0

How to fix   Duplicated Code   

Duplicated Code

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
2
3
namespace IBM\Watson\Common\tests;
4
5
use GuzzleHttp\Psr7\Response;
6
use IBM\Watson\Common\Hydrator\ArrayHydrator;
7
use PHPUnit\Framework\TestCase;
8
use Mockery as m;
9
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