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
|
|
|
public function testGetData() |
35
|
|
|
{ |
36
|
|
|
$data = ['username' => 'adam', 'password' => '01234']; |
37
|
|
|
$this->request->initialize($data); |
|
|
|
|
38
|
|
|
|
39
|
|
|
$this->assertSame($data, $this->request->getData()); |
|
|
|
|
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @expectedException \IBM\Watson\Common\Exception\RuntimeException |
44
|
|
|
* @expectedExceptionMessage Request cannot be modified after it has been sent! |
45
|
|
|
*/ |
46
|
|
|
public function testInitializeAfterRequestSent() |
47
|
|
|
{ |
48
|
|
|
$this->request = new AbstractRequestTest_MockAbstractRequest(new Client(), new Request()); |
49
|
|
|
$this->request->send(); |
50
|
|
|
|
51
|
|
|
$this->request->initialize(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @expectedException \IBM\Watson\Common\Exception\RuntimeException |
56
|
|
|
* @expectedExceptionMessage Request cannot be modified after it has been sent! |
57
|
|
|
*/ |
58
|
|
|
public function testSetParameterAfterRequestSent() |
59
|
|
|
{ |
60
|
|
|
$this->request = new AbstractRequestTest_MockAbstractRequest(new Client(), new Request()); |
61
|
|
|
$this->request->send(); |
62
|
|
|
|
63
|
|
|
$this->request->setUsername('adam'); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @expectedException \IBM\Watson\Common\Exception\RuntimeException |
68
|
|
|
* @expectedExceptionMessage You must call send() before accessing the Response! |
69
|
|
|
*/ |
70
|
|
|
public function testGetResponseBeforeRequestSent() |
71
|
|
|
{ |
72
|
|
|
$this->request = new AbstractRequestTest_MockAbstractRequest(new Client(), new Request()); |
73
|
|
|
$this->request->getResponse(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function testGetResponseAfterRequestSent() |
77
|
|
|
{ |
78
|
|
|
$this->request = new AbstractRequestTest_MockAbstractRequest(new Client(), new Request()); |
79
|
|
|
$this->request->send(); |
80
|
|
|
|
81
|
|
|
$response = $this->request->getResponse(); |
82
|
|
|
$this->assertInstanceOf('\IBM\Watson\Common\Message\ResponseInterface', $response); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
class AbstractRequestTest_MockAbstractRequest extends AbstractRequest |
|
|
|
|
87
|
|
|
{ |
88
|
|
|
public function sendData($data) |
89
|
|
|
{ |
90
|
|
|
$this->response = m::mock('\IBM\Watson\Common\Message\AbstractResponse'); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
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: