1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace IBM\Watson\ToneAnalyzer\tests\Api; |
4
|
|
|
|
5
|
|
|
use IBM\Watson\Common\Hydrator\ModelHydrator; |
6
|
|
|
use IBM\Watson\Common\tests\Api\AbstractTestCase; |
7
|
|
|
use IBM\Watson\ToneAnalyzer\Api\ToneChat; |
8
|
|
|
use IBM\Watson\ToneAnalyzer\Model\UtteranceAnalyses; |
9
|
|
|
use PHPUnit\Framework\Constraint\IsType; |
10
|
|
|
|
11
|
|
|
class ToneChatTest extends AbstractTestCase |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @dataProvider utteranceProvider |
15
|
|
|
* |
16
|
|
|
* @param array $utterances |
17
|
|
|
*/ |
18
|
|
|
public function testAnalyze($utterances) |
19
|
|
|
{ |
20
|
|
|
$response = $this->getMockResponse('ToneChatResponse.json'); |
21
|
|
|
$this->httpClient->shouldReceive('sendRequest')->once()->andReturn($response); |
22
|
|
|
$this->hydrator->shouldReceive('hydrate')->once()->andReturn( |
23
|
|
|
(new ModelHydrator())->hydrate($response, UtteranceAnalyses::class) |
24
|
|
|
); |
25
|
|
|
|
26
|
|
|
$api = new ToneChat($this->httpClient, $this->hydrator, $this->requestBuilder); |
27
|
|
|
$response = $api->analyze($utterances); |
28
|
|
|
|
29
|
|
|
$this->assertInstanceOf(UtteranceAnalyses::class, $response); |
30
|
|
|
$this->assertInternalType(IsType::TYPE_ARRAY, $response->getTones()); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @expectedException \IBM\Watson\Common\Exception\Domain\UnknownErrorException |
35
|
|
|
* @expectedExceptionMessage Input text exceeded API limit of 131,072 bytes |
36
|
|
|
*/ |
37
|
|
View Code Duplication |
public function testHydratorThrowsExceptions() |
|
|
|
|
38
|
|
|
{ |
39
|
|
|
$response = $this->getMockResponse('ErrorResponse.json', 500); |
40
|
|
|
$this->httpClient->shouldReceive('sendRequest')->once()->andReturn($response); |
41
|
|
|
|
42
|
|
|
$api = new ToneChat($this->httpClient, $this->hydrator, $this->requestBuilder); |
43
|
|
|
$api->analyze([]); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public static function utteranceProvider() |
47
|
|
|
{ |
48
|
|
|
return [ |
49
|
|
|
[ |
50
|
|
|
[ |
51
|
|
|
[ |
52
|
|
|
'text' => 'Hello, I\'m having a problem with your product.', |
53
|
|
|
'user' => 'customer', |
54
|
|
|
], |
55
|
|
|
[ |
56
|
|
|
'text' => 'OK, let me know what\'s going on, please.', |
57
|
|
|
'user' => 'agent', |
58
|
|
|
], |
59
|
|
|
[ |
60
|
|
|
'text' => 'Well, nothing is working :(', |
61
|
|
|
'user' => 'customer', |
62
|
|
|
], |
63
|
|
|
[ |
64
|
|
|
'text' => 'Sorry to hear that', |
65
|
|
|
'user' => 'agent', |
66
|
|
|
], |
67
|
|
|
], |
68
|
|
|
], |
69
|
|
|
]; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
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.