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