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); |
|
|
|
|
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); |
|
|
|
|
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); |
|
|
|
|
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
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: