1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bobbyshaw\WatsonVisualRecognition\Tests\Message; |
4
|
|
|
|
5
|
|
|
use Bobbyshaw\WatsonVisualRecognition\Client; |
6
|
|
|
use Bobbyshaw\WatsonVisualRecognition\Message\ClassifiersResponse; |
7
|
|
|
use Bobbyshaw\WatsonVisualRecognition\Message\GetClassifiersRequest; |
8
|
|
|
use Bobbyshaw\WatsonVisualRecognition\Tests\Base; |
9
|
|
|
use GuzzleHttp\Psr7\Request; |
10
|
|
|
use GuzzleHttp\Psr7\Uri; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class GetClassifiersRequestTest |
14
|
|
|
* @package Bobbyshaw\WatsonVisualRecognition\Tests\Message |
15
|
|
|
*/ |
16
|
|
|
class GetClassifiersRequestTest extends Base |
17
|
|
|
{ |
18
|
|
|
/** @var Client */ |
19
|
|
|
private $client; |
20
|
|
|
private $config; |
21
|
|
|
private $username = 'username'; |
22
|
|
|
private $password = 'password'; |
23
|
|
|
private $version = '2015-12-02'; |
24
|
|
|
|
25
|
|
View Code Duplication |
public function setUp() |
26
|
|
|
{ |
27
|
|
|
$this->config = array( |
28
|
|
|
'username' => $this->username, |
29
|
|
|
'password' => $this->password, |
30
|
|
|
'version' => $this->version |
31
|
|
|
); |
32
|
|
|
|
33
|
|
|
$this->client = new Client(); |
34
|
|
|
$this->client->initialize($this->config); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Test that params are as expected |
39
|
|
|
*/ |
40
|
|
|
public function testGetData() |
41
|
|
|
{ |
42
|
|
|
/** @var GetClassifiersRequest $request */ |
43
|
|
|
$request = $this->client->getClassifiers(); |
44
|
|
|
|
45
|
|
|
$this->assertEquals($this->config, $request->getData()); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Test the getClassifiers function HTTP request and response |
50
|
|
|
*/ |
51
|
|
|
public function testGetClassifiers() |
52
|
|
|
{ |
53
|
|
|
$container = []; |
54
|
|
|
$guzzle = $this->getMockHttpClientWithHistoryAndResponses( |
55
|
|
|
$container, |
56
|
|
|
[$this->getMockHttpResponse('GetClassifiersSuccess.txt')] |
57
|
|
|
); |
58
|
|
|
|
59
|
|
|
$this->client = new Client($guzzle); |
60
|
|
|
$this->client->initialize(['username' => $this->username, 'password' => $this->password]); |
61
|
|
|
|
62
|
|
|
/** @var GetClassifiersRequest $request */ |
63
|
|
|
$classifiersRequest = $this->client->getClassifiers(); |
64
|
|
|
|
65
|
|
|
$response = $classifiersRequest->send(); |
66
|
|
|
$response->getClassifiers(); |
67
|
|
|
|
68
|
|
|
|
69
|
|
|
// One request should be sent |
70
|
|
|
$this->assertCount(1, $container); |
71
|
|
|
|
72
|
|
|
$transaction = $container[0]; |
73
|
|
|
|
74
|
|
|
/** @var Request $request */ |
75
|
|
|
$request = $transaction['request']; |
76
|
|
|
|
77
|
|
|
/** @var Uri $uri */ |
78
|
|
|
$uri = $request->getUri(); |
79
|
|
|
|
80
|
|
|
// Check method |
81
|
|
|
$this->assertEquals('GET', $request->getMethod()); |
82
|
|
|
|
83
|
|
|
// Check we're talking to the right host |
84
|
|
|
$this->assertEquals('https', $uri->getScheme()); |
85
|
|
|
$this->assertEquals('gateway.watsonplatform.net', $uri->getHost()); |
86
|
|
|
|
87
|
|
|
// Check version query parameter |
88
|
|
|
$query = \GuzzleHttp\Psr7\parse_query($uri->getQuery()); |
89
|
|
|
$this->assertArrayHasKey('version', $query); |
90
|
|
|
|
91
|
|
|
// Check path |
92
|
|
|
$this->assertEquals('/visual-recognition-beta/api/v2/classifiers/', $uri->getPath()); |
93
|
|
|
|
94
|
|
|
// Check basic auth |
95
|
|
|
$auth = $request->getHeader('Authorization'); |
96
|
|
|
$this->assertEquals('Basic ' . base64_encode($this->username . ':' . $this->password), $auth[0]); |
97
|
|
|
|
98
|
|
|
$this->assertEquals($response, $classifiersRequest->getResponse()); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Test getClassifiers failed auth is handled appropriately |
103
|
|
|
* |
104
|
|
|
* @expectedException \Bobbyshaw\WatsonVisualRecognition\Exceptions\AuthException |
105
|
|
|
*/ |
106
|
|
View Code Duplication |
public function testGetClassifiersFailedAuthResponse() |
107
|
|
|
{ |
108
|
|
|
$container = []; |
109
|
|
|
$response = $this->getMockHttpResponse('FailedAuth.txt', 401); |
110
|
|
|
$httpClient = $this->getMockHttpClientWithHistoryAndResponses($container, [$response]); |
111
|
|
|
|
112
|
|
|
$this->client = new Client($httpClient); |
113
|
|
|
$this->client->initialize(['username' => $this->username, 'password' => $this->password]); |
114
|
|
|
|
115
|
|
|
/** @var GetClassifiersRequest $request */ |
116
|
|
|
$request = $this->client->getClassifiers(); |
117
|
|
|
$request->send(); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function testGetClassifiersVerbose() |
121
|
|
|
{ |
122
|
|
|
$container = []; |
123
|
|
|
$guzzle = $this->getMockHttpClientWithHistoryAndResponses( |
124
|
|
|
$container, |
125
|
|
|
[$this->getMockHttpResponse('GetClassifiersSuccess.txt')] |
126
|
|
|
); |
127
|
|
|
|
128
|
|
|
$this->client = new Client($guzzle); |
129
|
|
|
$this->client->initialize(['username' => $this->username, 'password' => $this->password]); |
130
|
|
|
|
131
|
|
|
/** @var GetClassifiersRequest $request */ |
132
|
|
|
$classifiersRequest = $this->client->getClassifiers(['verbose' => 'true']); |
133
|
|
|
|
134
|
|
|
/** @var ClassifiersResponse $response */ |
135
|
|
|
$response = $classifiersRequest->send(); |
136
|
|
|
$response->getClassifiers(); |
137
|
|
|
|
138
|
|
|
$transaction = $container[0]; |
139
|
|
|
|
140
|
|
|
/** @var Request $request */ |
141
|
|
|
$request = $transaction['request']; |
142
|
|
|
|
143
|
|
|
/** @var Uri $uri */ |
144
|
|
|
$uri = $request->getUri(); |
145
|
|
|
|
146
|
|
|
// Check verbose query parameter |
147
|
|
|
$query = \GuzzleHttp\Psr7\parse_query($uri->getQuery()); |
148
|
|
|
|
149
|
|
|
$this->assertArrayHasKey('verbose', $query); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|