Completed
Push — develop ( 5fc40e...dcac96 )
by Adam
13s
created

ToneChatTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 72
Duplicated Lines 12.5 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 8
dl 9
loc 72
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 6 1
B testToneChatAnalysis() 0 45 1
A testToneChatErrors() 9 9 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 View Code Duplication
    public function testToneChatErrors()
0 ignored issues
show
Duplication introduced by
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...
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