These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace IBM\Watson\VisualRecognition\tests\Api; |
||
4 | |||
5 | use GuzzleHttp\Psr7\Response; |
||
6 | use Http\Client\HttpClient; |
||
7 | use IBM\Watson\Common\Hydrator\HydratorInterface; |
||
8 | use IBM\Watson\Common\Hydrator\ModelHydrator; |
||
9 | use IBM\Watson\Common\RequestBuilder; |
||
10 | use IBM\Watson\VisualRecognition\Api\Classify; |
||
11 | use PHPUnit\Framework\TestCase; |
||
12 | use Mockery as m; |
||
13 | |||
14 | class ClassifyTest extends TestCase |
||
15 | { |
||
16 | private $httpClient; |
||
17 | private $hydrator; |
||
18 | private $requestBuilder; |
||
19 | |||
20 | public function setUp() |
||
21 | { |
||
22 | $this->httpClient = m::mock(HttpClient::class); |
||
23 | $this->hydrator = m::mock(new ModelHydrator, HydratorInterface::class); |
||
24 | $this->requestBuilder = new RequestBuilder(); |
||
25 | } |
||
26 | |||
27 | public static function successResponse() |
||
28 | { |
||
29 | return '{ |
||
30 | "custom_classes": 0, |
||
31 | "images": [ |
||
32 | { |
||
33 | "classifiers": [ |
||
34 | { |
||
35 | "classes": [ |
||
36 | { |
||
37 | "class": "elder statesman", |
||
38 | "score": 0.529, |
||
39 | "type_hierarchy": "/person/adult/elder statesman" |
||
40 | }, |
||
41 | { |
||
42 | "class": "adult", |
||
43 | "score": 0.579 |
||
44 | }, |
||
45 | { |
||
46 | "class": "person", |
||
47 | "score": 0.73 |
||
48 | }, |
||
49 | { |
||
50 | "class": "people", |
||
51 | "score": 0.507, |
||
52 | "type_hierarchy": "/person/people" |
||
53 | }, |
||
54 | { |
||
55 | "class": "male official", |
||
56 | "score": 0.502, |
||
57 | "type_hierarchy": "/person/male official" |
||
58 | }, |
||
59 | { |
||
60 | "class": "leader", |
||
61 | "score": 0.501, |
||
62 | "type_hierarchy": "/adult/person/leader" |
||
63 | }, |
||
64 | { |
||
65 | "class": "sociologist", |
||
66 | "score": 0.5, |
||
67 | "type_hierarchy": "/person/sociologist" |
||
68 | } |
||
69 | ], |
||
70 | "classifier_id": "default", |
||
71 | "name": "default" |
||
72 | } |
||
73 | ], |
||
74 | "resolved_url": "http://www.fillmurray.com/200/500.jpg", |
||
75 | "source_url": "http://www.fillmurray.com/200/500.jpg" |
||
76 | } |
||
77 | ], |
||
78 | "images_processed": 1 |
||
79 | } |
||
80 | '; |
||
81 | } |
||
82 | |||
83 | public static function errorResponse() |
||
84 | { |
||
85 | return '{"error": "Too many images in collection", "code": 400}'; |
||
86 | } |
||
87 | |||
88 | public function testClassifyImageViaUrl() |
||
89 | { |
||
90 | $rawResponse = self::successResponse(); |
||
91 | |||
92 | $this->httpClient->shouldReceive('sendRequest')->once()->andReturnUsing(function () use ($rawResponse) { |
||
93 | return new Response(200, ['Content-Type' => 'application/json'], $rawResponse); |
||
94 | }); |
||
95 | |||
96 | $classifier = new Classify($this->httpClient, $this->hydrator, $this->requestBuilder); |
||
97 | |||
98 | $response = $classifier->classify('http://www.fillmurray.com/200/500.jpg'); |
||
99 | |||
100 | $this->assertEquals(0, $response->getCustomClassesCount()); |
||
101 | $this->assertEquals(1, $response->getImagesProcessedCount()); |
||
102 | $images = $response->getImages(); |
||
103 | $this->assertNotEmpty($images); |
||
104 | $firstImage = $images[0]; |
||
105 | |||
106 | $this->assertNotEmpty($firstImage->getClassifiers()); |
||
107 | $this->assertEquals('http://www.fillmurray.com/200/500.jpg', $firstImage->getResolvedUrl()); |
||
108 | $this->assertEquals('http://www.fillmurray.com/200/500.jpg', $firstImage->getSourceUrl()); |
||
109 | $classifiers = $firstImage->getClassifiers(); |
||
110 | $this->assertNotEmpty($classifiers); |
||
111 | |||
112 | $firstClassifier = $classifiers[0]; |
||
113 | $classes = $firstClassifier->getClasses(); |
||
114 | $this->assertNotEmpty($classes); |
||
115 | $this->assertEquals('default', $firstClassifier->getClassifierId()); |
||
116 | $this->assertEquals('default', $firstClassifier->getName()); |
||
117 | |||
118 | $firstClass = $classes[0]; |
||
119 | |||
120 | $this->assertEquals('elder statesman', $firstClass->getClass()); |
||
121 | $this->assertEquals(0.529, $firstClass->getScore()); |
||
122 | } |
||
123 | |||
124 | public function testClassifyFilePath() |
||
125 | { |
||
126 | $rawResponse = self::successResponse(); |
||
127 | |||
128 | $this->httpClient->shouldReceive('sendRequest')->once()->andReturnUsing(function () use ($rawResponse) { |
||
129 | return new Response(200, ['Content-Type' => 'application/json'], $rawResponse); |
||
130 | }); |
||
131 | |||
132 | $classifier = new Classify($this->httpClient, $this->hydrator, $this->requestBuilder); |
||
133 | |||
134 | $response = $classifier->classify('src/VisualRecognition/tests/images/flower.jpg'); |
||
135 | |||
136 | $this->assertEquals(0, $response->getCustomClassesCount()); |
||
137 | $this->assertEquals(1, $response->getImagesProcessedCount()); |
||
138 | $this->assertNotEmpty($response->getImages()); |
||
139 | } |
||
140 | |||
141 | /** |
||
142 | * @expectedException \IBM\Watson\Common\Exception\InvalidRequestException |
||
143 | */ |
||
144 | View Code Duplication | public function testErrors() |
|
0 ignored issues
–
show
|
|||
145 | { |
||
146 | $rawResponse = self::errorResponse(); |
||
147 | |||
148 | $this->httpClient->shouldReceive('sendRequest')->once()->andReturnUsing(function () use ($rawResponse) { |
||
149 | return new Response(400, ['Content-Type' => 'application/json'], $rawResponse); |
||
150 | }); |
||
151 | |||
152 | $classifier = new Classify($this->httpClient, $this->hydrator, $this->requestBuilder); |
||
153 | |||
154 | $classifier->classify('src/VisualRecognition/tests/images/flower.jpg'); |
||
155 | $classifier->classify('http://someurl.com/image.jpg'); |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * @expectedException \IBM\Watson\Common\Exception\InvalidRequestException |
||
160 | */ |
||
161 | View Code Duplication | public function testUrlErrors() |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
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.
Loading history...
|
|||
162 | { |
||
163 | $rawResponse = self::errorResponse(); |
||
164 | |||
165 | $this->httpClient->shouldReceive('sendRequest')->once()->andReturnUsing(function () use ($rawResponse) { |
||
166 | return new Response(400, ['Content-Type' => 'application/json'], $rawResponse); |
||
167 | }); |
||
168 | |||
169 | $classifier = new Classify($this->httpClient, $this->hydrator, $this->requestBuilder); |
||
170 | |||
171 | $classifier->classify('http://someurl.com/image.jpg'); |
||
172 | } |
||
173 | } |
||
174 |
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.