1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bobbyshaw\WatsonVisualRecognition\Tests\Message; |
4
|
|
|
|
5
|
|
|
use Bobbyshaw\WatsonVisualRecognition\Client; |
6
|
|
|
use Bobbyshaw\WatsonVisualRecognition\Message\ClassifierResponse; |
7
|
|
|
use Bobbyshaw\WatsonVisualRecognition\Message\GetClassifierRequest; |
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 GetClassifierRequestTest 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
|
|
|
private $classifierId = 'Magenta'; |
25
|
|
|
|
26
|
|
View Code Duplication |
public function setUp() |
|
|
|
|
27
|
|
|
{ |
28
|
|
|
$this->config = array( |
29
|
|
|
'username' => $this->username, |
30
|
|
|
'password' => $this->password, |
31
|
|
|
'version' => $this->version |
32
|
|
|
); |
33
|
|
|
|
34
|
|
|
$this->client = new Client(); |
35
|
|
|
$this->client->initialize($this->config); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Test that params are as expected |
40
|
|
|
*/ |
41
|
|
|
public function testGetData() |
42
|
|
|
{ |
43
|
|
|
/** @var GetClassifierRequest $request */ |
44
|
|
|
$request = $this->client->getClassifier(['classifier_id' => $this->classifierId]); |
45
|
|
|
|
46
|
|
|
$this->assertEquals(array_merge($this->config, ['classifier_id' => $this->classifierId]), $request->getData()); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Test the getClassifier function HTTP request |
51
|
|
|
*/ |
52
|
|
|
public function testGetClassifier() |
53
|
|
|
{ |
54
|
|
|
$container = []; |
55
|
|
|
$guzzle = $this->getMockHttpClientWithHistoryAndResponses( |
56
|
|
|
$container, |
57
|
|
|
[$this->getMockHttpResponse('GetClassifierSuccess.txt')] |
58
|
|
|
); |
59
|
|
|
|
60
|
|
|
$this->client = new Client($guzzle); |
61
|
|
|
$this->client->initialize(['username' => $this->username, 'password' => $this->password]); |
62
|
|
|
|
63
|
|
|
/** @var GetClassifierRequest $request */ |
64
|
|
|
$classifierRequest = $this->client->getClassifier(['classifier_id' => $this->classifierId]); |
65
|
|
|
|
66
|
|
|
/** @var ClassifierResponse $response */ |
67
|
|
|
$response = $classifierRequest->send(); |
68
|
|
|
$response->getClassifier(); |
69
|
|
|
|
70
|
|
|
|
71
|
|
|
// One request should be sent |
72
|
|
|
$this->assertCount(1, $container); |
73
|
|
|
|
74
|
|
|
$transaction = $container[0]; |
75
|
|
|
|
76
|
|
|
/** @var Request $request */ |
77
|
|
|
$request = $transaction['request']; |
78
|
|
|
|
79
|
|
|
/** @var Uri $uri */ |
80
|
|
|
$uri = $request->getUri(); |
81
|
|
|
|
82
|
|
|
// Check data is passed in as query parameter and not as a post |
83
|
|
|
$this->assertEquals('GET', $request->getMethod()); |
84
|
|
|
|
85
|
|
|
// Check path |
86
|
|
|
$this->assertEquals('/visual-recognition-beta/api/v2/classifiers/' . $this->classifierId, $uri->getPath()); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Test getClassifier failed auth is handled appropriately |
91
|
|
|
* |
92
|
|
|
* @expectedException \Bobbyshaw\WatsonVisualRecognition\Exceptions\AuthException |
93
|
|
|
*/ |
94
|
|
View Code Duplication |
public function testGetClassifierFailedAuthResponse() |
|
|
|
|
95
|
|
|
{ |
96
|
|
|
$container = []; |
97
|
|
|
$response = $this->getMockHttpResponse('FailedAuth.txt', 401); |
98
|
|
|
$httpClient = $this->getMockHttpClientWithHistoryAndResponses($container, [$response]); |
99
|
|
|
|
100
|
|
|
$this->client = new Client($httpClient); |
101
|
|
|
$this->client->initialize(['username' => $this->username, 'password' => $this->password]); |
102
|
|
|
|
103
|
|
|
/** @var GetClassifierRequest $request */ |
104
|
|
|
$request = $this->client->getClassifier(['classifier_id' => $this->classifierId]); |
105
|
|
|
$request->send(); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
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.