1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Fab\Clarifai\Tests; |
4
|
|
|
|
5
|
|
|
use Fab\Clarifai\Client; |
6
|
|
|
use Fab\Clarifai\Models; |
7
|
|
|
use GuzzleHttp\Client as HttpClient; |
8
|
|
|
use Mockery; |
9
|
|
|
|
10
|
|
|
class ClientTest extends TestCase |
11
|
|
|
{ |
12
|
|
|
/** @test */ |
13
|
|
|
function can_instantiate_client() |
|
|
|
|
14
|
|
|
{ |
15
|
|
|
$httpClient = $this->getHttpClientMock(); |
16
|
|
|
|
17
|
|
|
$client = new Client($httpClient, 'client-id', 'client-secret'); |
18
|
|
|
|
19
|
|
|
$this->assertInstanceOf(Client::class, $client); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** @test */ |
23
|
|
|
function can_retrieve_an_access_token() |
24
|
|
|
{ |
25
|
|
|
$httpClient = $this->getHttpClientMock(); |
26
|
|
|
$httpClient |
27
|
|
|
->shouldReceive('request') |
28
|
|
|
->with('POST', 'https://api.clarifai.com/v2/token', [ |
29
|
|
|
'headers' => [ |
30
|
|
|
'Accept' => 'application/json', |
31
|
|
|
'Content-Type' => 'application/json', |
32
|
|
|
], |
33
|
|
|
'auth' => ['client-id', 'client-secret'], |
34
|
|
|
]) |
35
|
|
|
->andReturn($httpClient); |
36
|
|
|
|
37
|
|
|
$httpClient |
38
|
|
|
->shouldReceive('getBody') |
39
|
|
|
->andReturn(json_encode(['foo' => 'bar'])); |
40
|
|
|
|
41
|
|
|
$client = new Client($httpClient, 'client-id', 'client-secret'); |
42
|
|
|
|
43
|
|
|
$response = $client->accessToken(); |
44
|
|
|
|
45
|
|
|
$this->assertEquals(['foo' => 'bar'], $response); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** @test */ |
49
|
|
|
function can_predict_the_content_of_an_image_by_passing_an_url() |
50
|
|
|
{ |
51
|
|
|
$httpClient = $this->sendRequest( |
52
|
|
|
$this->getHttpClientMock(), |
53
|
|
|
'https://api.clarifai.com/v2/models/aaa03c23b3724a16a56b629203edc62c/outputs', |
54
|
|
|
[ |
55
|
|
|
[ |
56
|
|
|
'data' => [ |
57
|
|
|
'image' => [ |
58
|
|
|
'url' => 'https://samples.clarifai.com/metro-north.jpg', |
59
|
|
|
] |
60
|
|
|
] |
61
|
|
|
] |
62
|
|
|
] |
63
|
|
|
); |
64
|
|
|
|
65
|
|
|
$client = new Client($httpClient, 'client-id', 'client-secret'); |
66
|
|
|
|
67
|
|
|
$response = $client->withAccessToken('access-token')->predict([ |
68
|
|
|
'https://samples.clarifai.com/metro-north.jpg', |
69
|
|
|
]); |
70
|
|
|
|
71
|
|
|
$this->assertTrue(is_array($response)); |
72
|
|
|
$this->assertEquals([ |
73
|
|
|
'foo' => 'bar' |
74
|
|
|
], $response); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** @test */ |
78
|
|
View Code Duplication |
function use_general_model_by_default_when_predicting_the_content_of_an_image() |
|
|
|
|
79
|
|
|
{ |
80
|
|
|
$httpClient = $this->sendRequest( |
81
|
|
|
$this->getHttpClientMock(), |
82
|
|
|
'https://api.clarifai.com/v2/models/'.Models::GENERAL.'/outputs', |
83
|
|
|
[ |
84
|
|
|
[ |
85
|
|
|
'data' => [ |
86
|
|
|
'image' => [ |
87
|
|
|
'url' => 'https://samples.clarifai.com/metro-north.jpg', |
88
|
|
|
] |
89
|
|
|
] |
90
|
|
|
] |
91
|
|
|
] |
92
|
|
|
); |
93
|
|
|
|
94
|
|
|
$client = new Client($httpClient, 'client-id', 'client-secret'); |
95
|
|
|
|
96
|
|
|
$response = $client->withAccessToken('access-token')->predict([ |
97
|
|
|
'https://samples.clarifai.com/metro-north.jpg', |
98
|
|
|
]); |
99
|
|
|
|
100
|
|
|
$this->assertTrue(is_array($response)); |
101
|
|
|
$this->assertEquals([ |
102
|
|
|
'foo' => 'bar' |
103
|
|
|
], $response); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** @test */ |
107
|
|
View Code Duplication |
function can_predict_the_content_of_an_image_by_passing_an_url_and_a_specific_model() |
|
|
|
|
108
|
|
|
{ |
109
|
|
|
$httpClient = $this->sendRequest( |
110
|
|
|
$this->getHttpClientMock(), |
111
|
|
|
'https://api.clarifai.com/v2/models/'.Models::FOOD.'/outputs', |
112
|
|
|
[ |
113
|
|
|
[ |
114
|
|
|
'data' => [ |
115
|
|
|
'image' => [ |
116
|
|
|
'url' => 'https://samples.clarifai.com/metro-north.jpg', |
117
|
|
|
] |
118
|
|
|
] |
119
|
|
|
] |
120
|
|
|
] |
121
|
|
|
); |
122
|
|
|
|
123
|
|
|
$client = new Client($httpClient, 'client-id', 'client-secret'); |
124
|
|
|
|
125
|
|
|
$response = $client->withAccessToken('access-token')->predict([ |
126
|
|
|
'https://samples.clarifai.com/metro-north.jpg', |
127
|
|
|
], Models::FOOD); |
128
|
|
|
|
129
|
|
|
$this->assertTrue(is_array($response)); |
130
|
|
|
$this->assertEquals([ |
131
|
|
|
'foo' => 'bar' |
132
|
|
|
], $response); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
private function getHttpClientMock() |
136
|
|
|
{ |
137
|
|
|
return Mockery::mock(HttpClient::class); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
private function sendRequest($httpClient, $url, $inputs) |
141
|
|
|
{ |
142
|
|
|
$httpClient |
143
|
|
|
->shouldReceive('request') |
144
|
|
|
->with('POST', $url, [ |
145
|
|
|
'headers' => [ |
146
|
|
|
'Accept' => 'application/json', |
147
|
|
|
'Content-Type' => 'application/json', |
148
|
|
|
'Authorization' => 'Bearer access-token', |
149
|
|
|
], |
150
|
|
|
'json' => [ |
151
|
|
|
'inputs' => $inputs |
152
|
|
|
] |
153
|
|
|
]) |
154
|
|
|
->andReturn($httpClient); |
155
|
|
|
|
156
|
|
|
$httpClient |
157
|
|
|
->shouldReceive('getBody') |
158
|
|
|
->andReturn(json_encode(['foo' => 'bar'])); |
159
|
|
|
|
160
|
|
|
return $httpClient; |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.