|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace IBM\Watson\ToneAnalyzer\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\ToneAnalyzer\Api\ToneChat; |
|
11
|
|
|
use PHPUnit\Framework\TestCase; |
|
12
|
|
|
use Mockery as m; |
|
13
|
|
|
|
|
14
|
|
|
class ToneChatTest 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 function testToneChatAnalysis() |
|
28
|
|
|
{ |
|
29
|
|
|
$rawResponse = '{ |
|
30
|
|
|
"utterances_tone": [ |
|
31
|
|
|
{ |
|
32
|
|
|
"utterance_id": 0, |
|
33
|
|
|
"utterance_text": "If you\'re sending someone some Styrofoam, what do you pack it in?", |
|
34
|
|
|
"tones": [ |
|
35
|
|
|
{ |
|
36
|
|
|
"score": 0.711722, |
|
37
|
|
|
"tone_id": "polite", |
|
38
|
|
|
"tone_name": "polite" |
|
39
|
|
|
} |
|
40
|
|
|
] |
|
41
|
|
|
} |
|
42
|
|
|
] |
|
43
|
|
|
}'; |
|
44
|
|
|
$this->httpClient->shouldReceive('sendRequest')->once()->andReturnUsing(function () use ($rawResponse) { |
|
45
|
|
|
return new Response(200, ['Content-Type' => 'application/json'], $rawResponse); |
|
46
|
|
|
}); |
|
47
|
|
|
|
|
48
|
|
|
$toneChat = new ToneChat($this->httpClient, $this->hydrator, $this->requestBuilder); |
|
49
|
|
|
|
|
50
|
|
|
$response = $toneChat->analyze( |
|
51
|
|
|
'{"utterances": [{"text": "If you\'re sending someone some Styrofoam, what do you pack it in?", "user": "customer"}]}' |
|
52
|
|
|
); |
|
53
|
|
|
|
|
54
|
|
|
$utterances = $response->getUtterances(); |
|
55
|
|
|
|
|
56
|
|
|
$this->assertNotEmpty($utterances); |
|
57
|
|
|
|
|
58
|
|
|
$firstUtterance = $utterances[0]; |
|
59
|
|
|
|
|
60
|
|
|
$this->assertEquals(0, $firstUtterance->getId()); |
|
61
|
|
|
$this->assertEquals('If you\'re sending someone some Styrofoam, what do you pack it in?', $firstUtterance->getText()); |
|
62
|
|
|
$tones = $firstUtterance->getTones(); |
|
63
|
|
|
|
|
64
|
|
|
$this->assertNotEmpty($tones); |
|
65
|
|
|
|
|
66
|
|
|
$firstTone = $tones[0]; |
|
67
|
|
|
|
|
68
|
|
|
$this->assertEquals('polite', $firstTone->getId()); |
|
69
|
|
|
$this->assertEquals('polite', $firstTone->getName()); |
|
70
|
|
|
$this->assertEquals(0.711722, $firstTone->getScore()); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @expectedException \IBM\Watson\Common\Exception\UnknownErrorException |
|
75
|
|
|
*/ |
|
76
|
|
|
public function testToneChatErrors() |
|
77
|
|
|
{ |
|
78
|
|
|
$this->httpClient->shouldReceive('sendRequest')->once()->andReturnUsing(function () { |
|
79
|
|
|
return new Response(900, [], '{"error":"Unknown Error"}'); |
|
80
|
|
|
}); |
|
81
|
|
|
|
|
82
|
|
|
$toneChat = new ToneChat($this->httpClient, $this->hydrator, $this->requestBuilder); |
|
83
|
|
|
$toneChat->analyze('text'); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|