1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bobbyshaw\WatsonVisualRecognition\Tests\Message; |
4
|
|
|
|
5
|
|
|
use Bobbyshaw\WatsonVisualRecognition\Client; |
6
|
|
|
use Bobbyshaw\WatsonVisualRecognition\Message\ClassifyRequest; |
7
|
|
|
use Bobbyshaw\WatsonVisualRecognition\Message\RequestInterface; |
8
|
|
|
use Bobbyshaw\WatsonVisualRecognition\Tests\Base; |
9
|
|
|
use GuzzleHttp\Psr7\Request; |
10
|
|
|
use GuzzleHttp\Psr7\Response; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class ClassifyRequestTest |
14
|
|
|
* @package Bobbyshaw\WatsonVisualRecognition\Tests\Message |
15
|
|
|
*/ |
16
|
|
|
class ClassifyRequestTest extends Base |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var RequestInterface |
20
|
|
|
*/ |
21
|
|
|
protected $request; |
22
|
|
|
|
23
|
|
|
/** @var array $container */ |
24
|
|
|
protected $container = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Create a mock Guzzle Client with example response |
28
|
|
|
*/ |
29
|
|
View Code Duplication |
public function setUp() |
30
|
|
|
{ |
31
|
|
|
parent::setUp(); |
32
|
|
|
|
33
|
|
|
$request = $this->getMockHttpResponse('ClassifySuccess.txt'); |
34
|
|
|
$httpClient = $this->getMockHttpClientWithHistoryAndResponses($this->container, [$request]); |
35
|
|
|
|
36
|
|
|
$this->request = new ClassifyRequest($httpClient); |
37
|
|
|
|
38
|
|
|
$this->request->initialize( |
39
|
|
|
[ |
40
|
|
|
'images_file' => 'Tests/images/hummingbird-1047836_640.jpg', |
41
|
|
|
'classifier_ids' => [1, 2, 3], |
42
|
|
|
] |
43
|
|
|
); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Check for expected image and classifier IDs |
48
|
|
|
*/ |
49
|
|
|
public function testGetData() |
50
|
|
|
{ |
51
|
|
|
$data = $this->request->getData(); |
52
|
|
|
|
53
|
|
|
$this->assertSame('Tests/images/hummingbird-1047836_640.jpg', $data['images_file']); |
54
|
|
|
$this->assertSame([1, 2, 3], $data['classifier_ids']); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Test that the Classify POST HTTP Request is sent |
59
|
|
|
* and contains the correct data |
60
|
|
|
*/ |
61
|
|
|
public function testSendData() |
62
|
|
|
{ |
63
|
|
|
$this->request->send(); |
64
|
|
|
|
65
|
|
|
// Count number of requests sent |
66
|
|
|
$this->assertEquals(1, count($this->container)); |
67
|
|
|
|
68
|
|
|
$transaction = $this->container[0]; |
69
|
|
|
|
70
|
|
|
/** @var Request $httpRequest */ |
71
|
|
|
$httpRequest = $transaction['request']; |
72
|
|
|
|
73
|
|
|
$uri = $httpRequest->getUri(); |
74
|
|
|
|
75
|
|
|
$this->assertEquals('POST', $httpRequest->getMethod()); |
76
|
|
|
|
77
|
|
|
// Check path |
78
|
|
|
$this->assertEquals('/visual-recognition-beta/api/v2/classify/', $uri->getPath()); |
79
|
|
|
|
80
|
|
|
// Check post data |
81
|
|
|
$this->assertStringStartsWith('multipart/form-data', $httpRequest->getHeaderLine('Content-Type')); |
82
|
|
|
|
83
|
|
|
$body = $httpRequest->getBody()->getContents(); |
84
|
|
|
|
85
|
|
|
$imageData = "Content-Disposition: form-data; name=\"images_file\"; filename=\"hummingbird-1047836_640.jpg\""; |
86
|
|
|
$this->assertContains($imageData, $body); |
87
|
|
|
|
88
|
|
|
$classifierIdsData = "{\"classifiers\":[{\"classifier_id\":1},{\"classifier_id\":2},{\"classifier_id\":3}]}"; |
89
|
|
|
$this->assertContains($classifierIdsData, $body); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Check that exception is thrown if incorrect file type provided |
94
|
|
|
* |
95
|
|
|
* @expectedException \InvalidArgumentException |
96
|
|
|
*/ |
97
|
|
|
public function testDisallowedFileType() |
98
|
|
|
{ |
99
|
|
|
$request = $this->getMockHttpResponse('ClassifySuccess.txt'); |
100
|
|
|
$httpClient = $this->getMockHttpClientWithHistoryAndResponses($this->container, [$request]); |
101
|
|
|
|
102
|
|
|
$this->request = new ClassifyRequest($httpClient); |
103
|
|
|
|
104
|
|
|
$this->request->initialize(['images_file' => 'Tests/images/hummingbird-1047836_640.tom']); |
105
|
|
|
|
106
|
|
|
$this->request->send(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Test getClassifiers failed auth is handled appropriately |
111
|
|
|
* |
112
|
|
|
* @expectedException \InvalidArgumentException |
113
|
|
|
*/ |
114
|
|
View Code Duplication |
public function testIncorrectFileApiResponse() |
|
|
|
|
115
|
|
|
{ |
116
|
|
|
$container = []; |
117
|
|
|
$response = new Response(415, [], '{"code":415,"error":"Unsupported image type"}'); |
118
|
|
|
$httpClient = $this->getMockHttpClientWithHistoryAndResponses($container, [$response]); |
119
|
|
|
|
120
|
|
|
$client = new Client($httpClient); |
121
|
|
|
$client->initialize(['username' => 'test', 'password' => 'test']); |
122
|
|
|
|
123
|
|
|
/** @var ClassifyRequest $request */ |
124
|
|
|
$request = $client->classify(['images_file' => 'Tests/images/hummingbird-1047836_640.jpg']); |
125
|
|
|
$request->send(); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Test failed auth is handled appropriately |
130
|
|
|
* |
131
|
|
|
* @expectedException \Bobbyshaw\WatsonVisualRecognition\Exceptions\AuthException |
132
|
|
|
*/ |
133
|
|
View Code Duplication |
public function testFailedAuth() |
|
|
|
|
134
|
|
|
{ |
135
|
|
|
$container = []; |
136
|
|
|
$response = $this->getMockHttpResponse('FailedAuth.txt', 401); |
137
|
|
|
$httpClient = $this->getMockHttpClientWithHistoryAndResponses($container, [$response]); |
138
|
|
|
|
139
|
|
|
$client = new Client($httpClient); |
140
|
|
|
$client->initialize(['username' => 'test', 'password' => 'test']); |
141
|
|
|
|
142
|
|
|
/** @var ClassifyRequest $request */ |
143
|
|
|
$request = $client->classify(['images_file' => 'Tests/images/hummingbird-1047836_640.jpg']); |
144
|
|
|
$request->send(); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.