Skip to content

Commit 459fdaf

Browse files
author
Alex Belenky
committed
Add analyze unit tests
1 parent 2e6d13d commit 459fdaf

File tree

3 files changed

+60
-21
lines changed

3 files changed

+60
-21
lines changed

src/CommentsClient.php

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php namespace PerspectiveApi;
22

33
use GuzzleHttp\Client;
4+
use GuzzleHttp\Exception\ClientException;
45

56
class CommentsClient {
67
const API_URL = 'https://commentanalyzer.googleapis.com/v1alpha1';
@@ -19,8 +20,20 @@ public function __construct(string $token) {
1920
$this->token = $token;
2021
}
2122

22-
public function analyze(array $fields): CommentsResponse {
23-
return $this->request('analyze', []);
23+
public function analyze(): CommentsResponse {
24+
$data = [];
25+
$fields = [
26+
'comment', 'languages', 'requestedAttributes', 'context', 'spanAnnotations', 'doNotStore', 'clientToken',
27+
'sessionId'
28+
];
29+
30+
foreach ($fields AS $field) {
31+
if (isset($this->{$field})) {
32+
$data[$field] = $this->{$field};
33+
}
34+
}
35+
36+
return $this->request('analyze', $data);
2437
}
2538

2639
public function suggestScore() {
@@ -68,21 +81,21 @@ public function communityId() {
6881
}
6982

7083
protected function request(string $method, array $data): CommentsResponse {
71-
$fields = 'attributeScores,detectedLanguages,languages';
7284
$client = new Client(['defaults' => [
7385
'headers' => ['content-type' => 'application/json', 'Accept' => 'application/json'],
7486
]]);
75-
$response = $client->post(self::API_URL."/comments:{$method}?key={$this->token}&fields=".$fields, [
76-
'json' => [
77-
'comment' => $this->comment,
78-
'languages' => $this->languages,
79-
'requestedAttributes' => $this->requestedAttributes,
80-
'context' => $this->context,
81-
'spanAnnotations' => $this->spanAnnotations,
82-
'doNotStore' => $this->doNotStore,
83-
'clientToken' => $this->clientToken,
84-
'sessionId' => $this->sessionId
85-
]]);
87+
88+
try {
89+
$response = $client->post(self::API_URL."/comments:{$method}?key={$this->token}", ['json' => $data]);
90+
} catch (ClientException $e) {
91+
$error = json_decode($e->getResponse()->getBody(), true);
92+
93+
if (isset($error['error'])) {
94+
throw new CommentsException($error['error']['message'], $error['error']['code']);
95+
} else {
96+
throw $e;
97+
}
98+
}
8699

87100
$result = json_decode($response->getBody(), true);
88101

src/CommentsException.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php namespace PerspectiveApi;
22

3-
class CommentsException {
3+
use Exception;
44

5-
}
5+
class CommentsException extends Exception {}

tests/CommentsClientAnalyzeTest.php

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
<?php namespace PerspectiveApi\Test;
22

33
use PerspectiveApi\CommentsClient;
4+
use PerspectiveApi\CommentsException;
45
use PerspectiveApi\CommentsResponse;
56
use PHPUnit\Framework\TestCase;
67

78
class CommentsClientAnalyzeTest extends TestCase {
9+
/** @var CommentsResponse */
810
protected static $response;
911

1012
public static function setUpBeforeClass(): void {
@@ -15,18 +17,42 @@ public static function setUpBeforeClass(): void {
1517
$commentsClient->requestedAttributes(['TOXICITY' => ['scoreType' => 'PROBABILITY', 'scoreThreshold' => 0]]);
1618
$commentsClient->spanAnnotations(true);
1719
$commentsClient->doNotStore(true);
18-
$commentsClient->clientToken('some token');
20+
$commentsClient->clientToken('some-token');
1921
$commentsClient->sessionId('ses1');
2022

21-
self::$response = $commentsClient->analyze(['attributeScores', 'detectedLanguages', 'languages']);
23+
self::$response = $commentsClient->analyze();
2224
}
2325

2426
public function testHasCommentsResponseInstance() {
2527
$this->assertInstanceOf(CommentsResponse::class, self::$response);
2628
}
2729

28-
public function testAnalyzeResponse(): void {
29-
$this->assertIsArray(self::$response->attributeScores());
30-
$this->assertIsArray(self::$response->languages());
30+
public function testAttributeScores(): void {
31+
$attributeScores = self::$response->attributeScores();
32+
33+
$this->assertIsArray($attributeScores);
34+
$this->arrayHasKey('TOXICITY');
35+
$this->assertGreaterThan(0.9, $attributeScores['TOXICITY']['summaryScore']['value']);
36+
}
37+
38+
public function testLanguages() {
39+
$languages = self::$response->languages();
40+
41+
$this->assertIsArray($languages);
42+
$this->assertContains('en', $languages);
43+
}
44+
45+
public function testClientToken() {
46+
$clientToken = self::$response->clientToken();
47+
48+
$this->assertEquals('some-token', $clientToken);
49+
}
50+
51+
public function testCommentsException() {
52+
$this->expectException(CommentsException::class);
53+
$this->expectExceptionMessage('The request is missing a valid API key.');
54+
55+
$commentsClient = new CommentsClient('');
56+
$commentsClient->analyze();
3157
}
3258
}

0 commit comments

Comments
 (0)