|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bobbyshaw\WatsonVisualRecognition\Tests\Message; |
|
4
|
|
|
|
|
5
|
|
|
use Bobbyshaw\WatsonVisualRecognition\Client; |
|
6
|
|
|
use Bobbyshaw\WatsonVisualRecognition\Message\CreateClassifierRequest; |
|
7
|
|
|
use Bobbyshaw\WatsonVisualRecognition\Message\RequestInterface; |
|
8
|
|
|
use Bobbyshaw\WatsonVisualRecognition\Tests\Base; |
|
9
|
|
|
use GuzzleHttp\Psr7\Request; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class CreateClassifierRequestTest |
|
13
|
|
|
* @package Bobbyshaw\WatsonVisualRecognition\Tests\Message |
|
14
|
|
|
*/ |
|
15
|
|
|
class CreateClassifierRequestTest extends Base |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var RequestInterface |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $request; |
|
21
|
|
|
|
|
22
|
|
|
/** @var [] $container */ |
|
|
|
|
|
|
23
|
|
|
protected $container = []; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Create a mock Guzzle Client with example response |
|
27
|
|
|
*/ |
|
28
|
|
View Code Duplication |
public function setUp() |
|
|
|
|
|
|
29
|
|
|
{ |
|
30
|
|
|
parent::setUp(); |
|
31
|
|
|
|
|
32
|
|
|
$request = $this->getMockHttpResponse('CreateClassifierSuccess.txt'); |
|
33
|
|
|
$httpClient = $this->getMockHttpClientWithHistoryAndResponses($this->container, [$request]); |
|
34
|
|
|
|
|
35
|
|
|
$this->request = new CreateClassifierRequest($httpClient); |
|
36
|
|
|
|
|
37
|
|
|
$this->request->initialize( |
|
38
|
|
|
[ |
|
39
|
|
|
'positive_examples' => 'Tests/images/butterfly-positive.zip', |
|
40
|
|
|
'negative_examples' => 'Tests/images/butterfly-negative.zip', |
|
41
|
|
|
'name' => 'butterfly' |
|
42
|
|
|
] |
|
43
|
|
|
); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Check for expected data |
|
48
|
|
|
*/ |
|
49
|
|
|
public function testGetData() |
|
50
|
|
|
{ |
|
51
|
|
|
$data = $this->request->getData(); |
|
52
|
|
|
|
|
53
|
|
|
$this->assertSame('Tests/images/butterfly-positive.zip', $data['positive_examples']); |
|
54
|
|
|
$this->assertSame('Tests/images/butterfly-negative.zip', $data['negative_examples']); |
|
55
|
|
|
$this->assertSame('butterfly', $data['name']); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Test that the Classifier POST HTTP Request is sent |
|
60
|
|
|
* and contains the correct data |
|
61
|
|
|
*/ |
|
62
|
|
|
public function testSendData() |
|
63
|
|
|
{ |
|
64
|
|
|
$this->request->send(); |
|
65
|
|
|
|
|
66
|
|
|
// Count number of requests sent |
|
67
|
|
|
$this->assertEquals(1, count($this->container)); |
|
68
|
|
|
|
|
69
|
|
|
$transaction = $this->container[0]; |
|
70
|
|
|
|
|
71
|
|
|
/** @var Request $httpRequest */ |
|
72
|
|
|
$httpRequest = $transaction['request']; |
|
73
|
|
|
|
|
74
|
|
|
$uri = $httpRequest->getUri(); |
|
75
|
|
|
|
|
76
|
|
|
$this->assertEquals('POST', $httpRequest->getMethod()); |
|
77
|
|
|
|
|
78
|
|
|
// Check path |
|
79
|
|
|
$this->assertEquals('/visual-recognition-beta/api/v2/classifiers/', $uri->getPath()); |
|
80
|
|
|
|
|
81
|
|
|
// Check post data |
|
82
|
|
|
$this->assertStringStartsWith('multipart/form-data', $httpRequest->getHeaderLine('Content-Type')); |
|
83
|
|
|
|
|
84
|
|
|
$body = $httpRequest->getBody()->getContents(); |
|
85
|
|
|
|
|
86
|
|
|
$positiveData = 'Content-Disposition: form-data; name="positive_examples"; filename="butterfly-positive.zip"'; |
|
87
|
|
|
$this->assertContains($positiveData, $body); |
|
88
|
|
|
|
|
89
|
|
|
$negativeData = 'Content-Disposition: form-data; name="negative_examples"; filename="butterfly-negative.zip"'; |
|
90
|
|
|
$this->assertContains($negativeData, $body); |
|
91
|
|
|
|
|
92
|
|
|
$nameData = '/\bbutterfly\b/'; |
|
93
|
|
|
|
|
94
|
|
|
$this->assertRegExp($nameData, $body); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Test createClassifier failed auth is handled appropriately |
|
100
|
|
|
* |
|
101
|
|
|
* @expectedException \Bobbyshaw\WatsonVisualRecognition\Exceptions\AuthException |
|
102
|
|
|
*/ |
|
103
|
|
View Code Duplication |
public function testCreateClassifierFailedAuthResponse() |
|
|
|
|
|
|
104
|
|
|
{ |
|
105
|
|
|
$container = []; |
|
106
|
|
|
$response = $this->getMockHttpResponse('FailedAuth.txt', 401); |
|
107
|
|
|
$httpClient = $this->getMockHttpClientWithHistoryAndResponses($container, [$response]); |
|
108
|
|
|
|
|
109
|
|
|
$client = new Client($httpClient); |
|
110
|
|
|
$client->initialize(['username' => 'test', 'password' => 'test']); |
|
111
|
|
|
|
|
112
|
|
|
/** @var CreateClassifierRequest $request */ |
|
113
|
|
|
$request = $client->createClassifier($this->request->getData()); |
|
114
|
|
|
$request->send(); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Pass incorrect file types to request to |
|
119
|
|
|
* |
|
120
|
|
|
* @expectedException \InvalidArgumentException |
|
121
|
|
|
*/ |
|
122
|
|
|
public function testInvalidData() |
|
123
|
|
|
{ |
|
124
|
|
|
$client = new Client(); |
|
125
|
|
|
$client->initialize(['username' => 'test', 'password' => 'test']); |
|
126
|
|
|
|
|
127
|
|
|
/** @var CreateClassifierRequest $request */ |
|
128
|
|
|
$request = $client->createClassifier( |
|
129
|
|
|
[ |
|
130
|
|
|
'positive_examples' => 'test.log', |
|
131
|
|
|
'negative_examples' => 'test.log', |
|
132
|
|
|
'name' => 'test', |
|
133
|
|
|
] |
|
134
|
|
|
); |
|
135
|
|
|
|
|
136
|
|
|
$request->send(); |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.