Completed
Pull Request — develop (#5)
by Adam
01:26
created

ModelHydratorTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace IBM\Watson\Common\tests;
4
5
use GuzzleHttp\Psr7\Response;
6
use IBM\Watson\Common\Hydrator\ModelHydrator;
7
use IBM\Watson\Common\Model\CreateableFromArray;
8
use PHPUnit\Framework\TestCase;
9
use Mockery as m;
10
11
class ModelHydratorTest extends TestCase
12
{
13
    private $hydrator;
14
    private $model;
15
16
    public function setUp()
17
    {
18
        $this->hydrator = new ModelHydrator();
19
        $this->model = m::mock('alias:'.CreateableFromArray::class);
20
    }
21
22
    public function testSuccessfulHydration()
23
    {
24
        $response = m::mock(
25
            Response::class,
26
            [
27
                200,
28
                ['Content-Type' => 'application/json'],
29
                '{"document_tone": {}}'
30
            ]
31
        );
32
33
        $response->makePartial();
34
35
        $this->model->shouldReceive('createFromArray')->andReturn(['document_tone' => []]);
36
37
        $result = $this->hydrator->hydrate($response, $this->model);
38
39
        $this->assertInstanceOf(CreateableFromArray::class, $result);
40
    }
41
42
    /**
43
     * @expectedException \IBM\Watson\Common\Exception\HydrationException
44
     * @expectedExceptionMessage The ModelHydrator requires a model class as the second parameter
45
     */
46
    public function testHydrateFailsWithoutClass()
47
    {
48
        $response = m::mock(Response::class, [200]);
49
50
        $this->hydrator->hydrate($response);
51
    }
52
53
    /**
54
     * @expectedException \IBM\Watson\Common\Exception\HydrationException
55
     * @expectedExceptionMessage The ModelHydrator cannot hydrate response with Content-Type: text/plain
56
     */
57
    public function testHydrateFailsForNoneJsonResponses()
58
    {
59
        $response = m::mock(Response::class, [200, ['Content-Type' => 'text/plain']])->makePartial();
60
61
        $this->hydrator->hydrate($response, \stdClass::class);
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...
62
    }
63
64
    /**
65
     * @expectedException \IBM\Watson\Common\Exception\HydrationException
66
     */
67 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...
68
    {
69
        $response = m::mock(Response::class, [200, ['Content-Type' => 'application/json'], '{"brokenJson}'])
70
            ->makePartial();
71
72
        $this->hydrator->hydrate($response, \stdClass::class);
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...
73
    }
74
}
75