1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace IBM\Watson\Common\Tests\Message; |
5
|
|
|
|
6
|
|
|
use GuzzleHttp\Client; |
7
|
|
|
use IBM\Watson\Common\Message\AbstractRequest; |
8
|
|
|
use Mockery as m; |
9
|
|
|
use Symfony\Component\HttpFoundation\Request; |
10
|
|
|
|
11
|
|
|
class AbstractRequestTest extends \PHPUnit_Framework_TestCase |
12
|
|
|
{ |
13
|
|
|
protected $request; |
14
|
|
|
|
15
|
|
|
public function setUp() |
16
|
|
|
{ |
17
|
|
|
$this->request = m::mock('\IBM\Watson\Common\Message\AbstractRequest')->makePartial(); |
18
|
|
|
$this->request->initialize(); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function testConstruct() |
22
|
|
|
{ |
23
|
|
|
$this->request = new AbstractRequestTest_MockAbstractRequest(new Client(), new Request()); |
24
|
|
|
$this->assertSame([], $this->request->getParameters()); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function testInitializeWithParameters() |
28
|
|
|
{ |
29
|
|
|
$this->assertSame($this->request, $this->request->initialize(['username' => 'adam', 'password' => '01234'])); |
|
|
|
|
30
|
|
|
$this->assertSame('adam', $this->request->getUsername()); |
|
|
|
|
31
|
|
|
$this->assertSame('01234', $this->request->getPassword()); |
|
|
|
|
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @expectedException \IBM\Watson\Common\Exception\RuntimeException |
36
|
|
|
* @expectedExceptionMessage Request cannot be modified after it has been sent! |
37
|
|
|
*/ |
38
|
|
|
public function testInitializeAfterRequestSent() |
39
|
|
|
{ |
40
|
|
|
$this->request = new AbstractRequestTest_MockAbstractRequest(new Client(), new Request()); |
41
|
|
|
$this->request->send(); |
42
|
|
|
|
43
|
|
|
$this->request->initialize(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @expectedException \IBM\Watson\Common\Exception\RuntimeException |
48
|
|
|
* @expectedExceptionMessage Request cannot be modified after it has been sent! |
49
|
|
|
*/ |
50
|
|
|
public function testSetParameterAfterRequestSent() |
51
|
|
|
{ |
52
|
|
|
$this->request = new AbstractRequestTest_MockAbstractRequest(new Client(), new Request()); |
53
|
|
|
$this->request->send(); |
54
|
|
|
|
55
|
|
|
$this->request->setUsername('adam'); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @expectedException \IBM\Watson\Common\Exception\RuntimeException |
60
|
|
|
* @expectedExceptionMessage You must call send() before accessing the Response! |
61
|
|
|
*/ |
62
|
|
|
public function testGetResponseBeforeRequestSent() |
63
|
|
|
{ |
64
|
|
|
$this->request = new AbstractRequestTest_MockAbstractRequest(new Client(), new Request()); |
65
|
|
|
$this->request->getResponse(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function testGetResponseAfterRequestSent() |
69
|
|
|
{ |
70
|
|
|
$this->request = new AbstractRequestTest_MockAbstractRequest(new Client(), new Request()); |
71
|
|
|
$this->request->send(); |
72
|
|
|
|
73
|
|
|
$response = $this->request->getResponse(); |
74
|
|
|
$this->assertInstanceOf('\IBM\Watson\Common\Message\ResponseInterface', $response); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
class AbstractRequestTest_MockAbstractRequest extends AbstractRequest |
|
|
|
|
79
|
|
|
{ |
80
|
|
|
public function getData() |
81
|
|
|
{ |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function sendData($data) |
85
|
|
|
{ |
86
|
|
|
$this->response = m::mock('\IBM\Watson\Common\Message\AbstractResponse'); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
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:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: