Completed
Push — develop ( c8d2e9...9b1d71 )
by Adam
03:00
created

ModelHydratorTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
4
namespace IBM\Watson\Common\Hydrator\ArrayHydratorTest;
5
6
7
use IBM\Watson\Common\Hydrator\ModelHydrator;
8
use IBM\Watson\Common\stubs\CreateableFromArrayModel;
9
use IBM\Watson\Common\stubs\Model;
10
use PHPUnit\Framework\TestCase;
11
use Mockery as m;
12
use Psr\Http\Message\ResponseInterface;
13
use Psr\Http\Message\StreamInterface;
14
15
class ModelHydratorTest extends TestCase
16
{
17
    private $response;
18
    private $stream;
19
    private $model;
20
21
    public function setUp()
22
    {
23
        $this->response = m::mock(ResponseInterface::class)->makePartial();
24
        $this->stream = m::mock(StreamInterface::class)->makePartial();
25
        $this->model = m::mock(CreateableFromArrayModel::class)->makePartial();
26
    }
27
28
    public function testHydrate()
29
    {
30
        $this->stream->shouldReceive('__toString')->andReturn('{"param":"value"}');
31
32
        $this->response->shouldReceive('getHeaderLine')->once()->andReturn('application/json');
33
        $this->response->shouldReceive('getBody')->once()->andReturn($this->stream);
34
35
        $hydrator = new ModelHydrator();
36
37
        $content = $hydrator->hydrate($this->response, get_class($this->model));
0 ignored issues
show
Documentation introduced by
$this->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...
38
        $this->assertInstanceOf(CreateableFromArrayModel::class, $content);
39
40
        $content = $hydrator->hydrate($this->response, Model::class);
0 ignored issues
show
Documentation introduced by
$this->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...
41
        $this->assertInstanceOf(Model::class, $content);
42
    }
43
44
    /**
45
     * @expectedException \IBM\Watson\Common\Exception\HydrationException
46
     * @expectedExceptionMessage The ModelHydrator requires a model class as the second parameter
47
     */
48
    public function testNoModelSuppliedException()
49
    {
50
        $hydrator = new ModelHydrator();
51
52
        $hydrator->hydrate($this->response);
0 ignored issues
show
Documentation introduced by
$this->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...
53
    }
54
55
    /**
56
     * @expectedException \IBM\Watson\Common\Exception\HydrationException
57
     * @expectedExceptionMessage The ModelHydrator cannot hydrate a response with Content-Type: text/plain
58
     */
59 View Code Duplication
    public function testNoneJsonExceptionIsThrown()
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...
60
    {
61
        $this->stream->shouldReceive('__toString')->once()->andReturn('Plain text response');
62
        $this->response->shouldReceive('getHeaderLine')->andReturn('text/plain');
0 ignored issues
show
Bug introduced by
The method andReturn does only exist in Mockery\CompositeExpectation, but not in Mockery\HigherOrderMessage.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
63
        $this->response->shouldReceive('getBody')->once()->andReturn($this->stream);
64
65
        $hydrator = new ModelHydrator();
66
        $hydrator->hydrate($this->response, get_class($this->model));
0 ignored issues
show
Documentation introduced by
$this->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...
67
    }
68
69
    /**
70
     * @expectedException \IBM\Watson\Common\Exception\HydrationException
71
     */
72 View Code Duplication
    public function testInvalidJsonExceptionIsThrown()
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...
73
    {
74
        $this->stream->shouldReceive('__toString')->once()->andReturn('{param:value}');
75
76
        $this->response->shouldReceive('getHeaderLine')->once()->andReturn('application/json');
77
        $this->response->shouldReceive('getBody')->once()->andReturn($this->stream);
78
79
        $hydrator = new ModelHydrator();
80
        $hydrator->hydrate($this->response, get_class($this->model));
0 ignored issues
show
Documentation introduced by
$this->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...
81
    }
82
}