1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace IBM\Watson\Common\tests\Api; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Psr7\Response; |
6
|
|
|
use IBM\Watson\Common\stubs\Api; |
7
|
|
|
|
8
|
|
|
class ApiTest extends AbstractTestCase |
9
|
|
|
{ |
10
|
|
|
public function setup200Response() |
11
|
|
|
{ |
12
|
|
|
$this->httpClient->shouldReceive('sendRequest')->once()->andReturnUsing(function () { |
13
|
|
|
return new Response(200, []); |
14
|
|
|
}); |
15
|
|
|
} |
16
|
|
|
|
17
|
|
View Code Duplication |
public function testGet() |
|
|
|
|
18
|
|
|
{ |
19
|
|
|
$this->setup200Response(); |
20
|
|
|
|
21
|
|
|
$api = new Api($this->httpClient, $this->hydrator, $this->requestBuilder); |
22
|
|
|
$response = $api->getMethod(); |
23
|
|
|
|
24
|
|
|
$this->assertEquals(200, $response->getStatusCode()); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
View Code Duplication |
public function testPost() |
|
|
|
|
28
|
|
|
{ |
29
|
|
|
$this->setup200Response(); |
30
|
|
|
|
31
|
|
|
$api = new Api($this->httpClient, $this->hydrator, $this->requestBuilder); |
32
|
|
|
$response = $api->postMethod(); |
33
|
|
|
|
34
|
|
|
$this->assertEquals(200, $response->getStatusCode()); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
View Code Duplication |
public function testPostRaw() |
|
|
|
|
38
|
|
|
{ |
39
|
|
|
$this->setup200Response(); |
40
|
|
|
|
41
|
|
|
$api = new Api($this->httpClient, $this->hydrator, $this->requestBuilder); |
42
|
|
|
$response = $api->postRawMethod(); |
43
|
|
|
|
44
|
|
|
$this->assertEquals(200, $response->getStatusCode()); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
View Code Duplication |
public function testPut() |
|
|
|
|
48
|
|
|
{ |
49
|
|
|
$this->setup200Response(); |
50
|
|
|
|
51
|
|
|
$api = new Api($this->httpClient, $this->hydrator, $this->requestBuilder); |
52
|
|
|
$response = $api->putMethod(); |
53
|
|
|
|
54
|
|
|
$this->assertEquals(200, $response->getStatusCode()); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
View Code Duplication |
public function testPatch() |
|
|
|
|
58
|
|
|
{ |
59
|
|
|
$this->setup200Response(); |
60
|
|
|
|
61
|
|
|
$api = new Api($this->httpClient, $this->hydrator, $this->requestBuilder); |
62
|
|
|
$response = $api->patchMethod(); |
63
|
|
|
|
64
|
|
|
$this->assertEquals(200, $response->getStatusCode()); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
View Code Duplication |
public function testDelete() |
|
|
|
|
68
|
|
|
{ |
69
|
|
|
$this->setup200Response(); |
70
|
|
|
|
71
|
|
|
$api = new Api($this->httpClient, $this->hydrator, $this->requestBuilder); |
72
|
|
|
$response = $api->deleteMethod(); |
73
|
|
|
|
74
|
|
|
$this->assertEquals(200, $response->getStatusCode()); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @expectedException \IBM\Watson\Common\Exception\Domain\InsufficientPrivilegesException |
79
|
|
|
* @expectedExceptionMessage Not Authorized |
80
|
|
|
*/ |
81
|
|
|
public function testUnauthorizedExceptionIsThrown() |
82
|
|
|
{ |
83
|
|
|
parent::setUpUnauthorizedResponse(); |
|
|
|
|
84
|
|
|
|
85
|
|
|
$api = new Api($this->httpClient, $this->hydrator, $this->requestBuilder); |
86
|
|
|
$api->getMethod(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @expectedException \IBM\Watson\Common\Exception\Domain\NotFoundException |
91
|
|
|
* @expectedExceptionMessage Not Found |
92
|
|
|
*/ |
93
|
|
|
public function testNotFoundExceptionIsThrown() |
94
|
|
|
{ |
95
|
|
|
parent::setUpNotFoundResponse(); |
|
|
|
|
96
|
|
|
|
97
|
|
|
$api = new Api($this->httpClient, $this->hydrator, $this->requestBuilder); |
98
|
|
|
$api->getMethod(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @expectedException \IBM\Watson\Common\Exception\Domain\UnknownErrorException |
103
|
|
|
* @expectedExceptionMessage The service encountered an internal error |
104
|
|
|
*/ |
105
|
|
|
public function testWatsonUnknownErrorIsThrown() |
106
|
|
|
{ |
107
|
|
|
parent::setupWatsonServiceError(); |
|
|
|
|
108
|
|
|
|
109
|
|
|
$api = new Api($this->httpClient, $this->hydrator, $this->requestBuilder); |
110
|
|
|
$api->getMethod(); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @expectedException \IBM\Watson\Common\Exception\Domain\UnknownErrorException |
115
|
|
|
* @expectedExceptionMessage The service encountered an internal error |
116
|
|
|
*/ |
117
|
|
|
public function testDefaultUnknownErrorIsThrown() |
118
|
|
|
{ |
119
|
|
|
$this->httpClient->shouldReceive('sendRequest')->once()->andReturnUsing(function () { |
120
|
|
|
return new Response(999, [], '{"error":"The service encountered an internal error"}'); |
121
|
|
|
}); |
122
|
|
|
|
123
|
|
|
$api = new Api($this->httpClient, $this->hydrator, $this->requestBuilder); |
124
|
|
|
$api->getMethod(); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
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.