Completed
Push — develop ( 68a985...a1f82e )
by Adam
10s
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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 6 1
A testSuccessfulHydration() 0 13 1
A testHydrateFailsForNoneJsonResponses() 0 6 1
A testHydrateFailsWhenJsonDecodeFails() 7 7 1

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);
0 ignored issues
show
Documentation introduced by
$response is of type object<Mockery\Mock>, but the function expects a object<Psr\Http\Message\ResponseInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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);
0 ignored issues
show
Documentation introduced by
$response is of type object<Mockery\Mock>, but the function expects a object<Psr\Http\Message\ResponseInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
45
    }
46
47
    /**
48
     * @expectedException \IBM\Watson\Common\Exception\HydrationException
49
     */
50 View Code Duplication
    public function testHydrateFailsWhenJsonDecodeFails()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
51
    {
52
        $response = m::mock(Response::class, [200, ['Content-Type' => 'application/json'], '{"brokenJson}'])
53
            ->makePartial();
54
55
        $this->hydrator->hydrate($response);
0 ignored issues
show
Documentation introduced by
$response is of type object<Mockery\Mock>, but the function expects a object<Psr\Http\Message\ResponseInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
56
    }
57
}
58