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

testInvalidJsonExceptionIsThrown()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace IBM\Watson\Common\Hydrator\ArrayHydratorTest;
4
5
use IBM\Watson\Common\Hydrator\ArrayHydrator;
6
use PHPUnit\Framework\TestCase;
7
use Mockery as m;
8
use Psr\Http\Message\ResponseInterface;
9
use Psr\Http\Message\StreamInterface;
10
11
class ArrayHydratorTest extends TestCase
12
{
13
    private $response;
14
    private $stream;
15
16
    public function setUp()
17
    {
18
        $this->response =  m::mock(ResponseInterface::class)->makePartial();
19
        $this->stream = m::mock(StreamInterface::class)->makePartial();
20
    }
21
22
    public function testHydrate()
23
    {
24
        $this->stream->shouldReceive('__toString')->andReturn('{"param": "value","param2": 99}');
25
26
        $this->response->shouldReceive('getHeaderLine')->once()->andReturn('application/json');
27
        $this->response->shouldReceive('getBody')->once()->andReturn($this->stream);
28
29
        $hydrator = new ArrayHydrator();
30
        $content = $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...
31
32
        $this->assertArrayHasKey('param', $content);
33
        $this->assertArrayHasKey('param2', $content);
34
        $this->assertEquals('value', $content['param']);
35
        $this->assertEquals(99, $content['param2']);
36
    }
37
38
    /**
39
     * @expectedException \IBM\Watson\Common\Exception\HydrationException
40
     * @expectedExceptionMessage The ArrayHydrator cannot hydrate a response with Content-Type: text/plain
41
     */
42
    public function testNoneJsonExceptionIsThrown()
43
    {
44
        $this->response->shouldReceive('getHeaderLine')->once()->andReturn('text/plain');
45
46
        $hydrator = new ArrayHydrator();
47
        $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...
48
    }
49
50
    /**
51
     * @expectedException \IBM\Watson\Common\Exception\HydrationException
52
     * @expectedExceptionMessage Error (4) when trying to json_decode response: Syntax error
53
     */
54
    public function testInvalidJsonExceptionIsThrown()
55
    {
56
        $this->stream->shouldReceive('__toString')->once()->andReturn('{param:value}');
57
58
        $this->response->shouldReceive('getHeaderLine')->once()->andReturn('application/json');
59
        $this->response->shouldReceive('getBody')->once()->andReturn($this->stream);
60
61
        $hydrator = new ArrayHydrator();
62
        $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...
63
    }
64
}
65