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)); |
|
|
|
|
38
|
|
|
$this->assertInstanceOf(CreateableFromArrayModel::class, $content); |
39
|
|
|
|
40
|
|
|
$content = $hydrator->hydrate($this->response, Model::class); |
|
|
|
|
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); |
|
|
|
|
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() |
|
|
|
|
60
|
|
|
{ |
61
|
|
|
$this->stream->shouldReceive('__toString')->once()->andReturn('Plain text response'); |
62
|
|
|
$this->response->shouldReceive('getHeaderLine')->andReturn('text/plain'); |
|
|
|
|
63
|
|
|
$this->response->shouldReceive('getBody')->once()->andReturn($this->stream); |
64
|
|
|
|
65
|
|
|
$hydrator = new ModelHydrator(); |
66
|
|
|
$hydrator->hydrate($this->response, get_class($this->model)); |
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @expectedException \IBM\Watson\Common\Exception\HydrationException |
71
|
|
|
*/ |
72
|
|
View Code Duplication |
public function testInvalidJsonExceptionIsThrown() |
|
|
|
|
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)); |
|
|
|
|
81
|
|
|
} |
82
|
|
|
} |
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: