Completed
Push — develop ( a1f82e...6f4353 )
by Adam
15:09 queued 12s
created

ToneTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 163
Duplicated Lines 17.18 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
lcom 1
cbo 8
dl 28
loc 163
rs 10
c 1
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 6 1
A testUnauthorized() 10 10 1
A testNotFound() 9 9 1
A testUnknownError() 9 9 1
B testDocumentToneAnalysis() 0 25 1
B testSentenceToneAnaysis() 0 80 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\Tone;
11
use PHPUnit\Framework\TestCase;
12
use Mockery as m;
13
14
class ToneTest 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
    /**
28
     * @expectedException \IBM\Watson\Common\Exception\InsufficientPrivilegesException
29
     * @expectedExceptionMessage Not Authorized
30
     */
31 View Code Duplication
    public function testUnauthorized()
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...
32
    {
33
        $this->httpClient->shouldReceive('sendRequest')->once()->andReturnUsing(function () {
34
            return new Response(401, [], '{"error":"Not Authorized"}');
35
        });
36
37
        $tone = new Tone($this->httpClient, $this->hydrator, $this->requestBuilder);
38
39
        $tone->analyze('text');
40
    }
41
42
    /**
43
     * @expectedException \IBM\Watson\Common\Exception\NotFoundException
44
     * @expectedExceptionMessage Not Found
45
     */
46 View Code Duplication
    public function testNotFound()
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...
47
    {
48
        $this->httpClient->shouldReceive('sendRequest')->once()->andReturnUsing(function () {
49
            return new Response(404, [], '{"error":"Not Found"}');
50
        });
51
52
        $tone = new Tone($this->httpClient, $this->hydrator, $this->requestBuilder);
53
        $tone->analyze('text');
54
    }
55
56
    /**
57
     * @expectedException \IBM\Watson\Common\Exception\UnknownErrorException
58
     * @expectedExceptionMessage Unknown Error
59
     */
60 View Code Duplication
    public function testUnknownError()
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...
61
    {
62
        $this->httpClient->shouldReceive('sendRequest')->once()->andReturnUsing(function () {
63
            return new Response(900, [], '{"error":"Unknown Error"}');
64
        });
65
66
        $tone = new Tone($this->httpClient, $this->hydrator, $this->requestBuilder);
67
        $tone->analyze('text');
68
    }
69
70
    public function testDocumentToneAnalysis()
71
    {
72
        $rawResponse = '{"document_tone": {"tones": [{"score": 0.6165,"tone_id": "sadness","tone_name": "Sadness"}]}}';
73
74
        $this->httpClient->shouldReceive('sendRequest')->once()->andReturnUsing(function () use ($rawResponse) {
75
            return new Response(200, ['Content-Type' => 'application/json'], $rawResponse);
76
        });
77
78
        $tone = new Tone($this->httpClient, $this->hydrator, $this->requestBuilder);
79
80
        $response = $tone->analyze(
81
            'text',
82
            ['content_language' => 'en', 'accept_language' => 'en', 'learning_opt_out' => true]
83
        );
84
85
        $tones = $response->getDocumentAnalysis()->getTones();
86
87
        $this->assertNotEmpty($tones);
88
89
        $firstTone = $tones[0];
90
91
        $this->assertEquals('sadness', $firstTone->getId());
92
        $this->assertEquals('Sadness', $firstTone->getName());
93
        $this->assertEquals(0.6165, $firstTone->getScore());
94
    }
95
96
    public function testSentenceToneAnaysis()
97
    {
98
        $rawResponse = '{
99
  "document_tone": {
100
    "tones": [
101
      {
102
        "score": 0.6165,
103
        "tone_id": "sadness",
104
        "tone_name": "Sadness"
105
      },
106
      {
107
        "score": 0.829888,
108
        "tone_id": "analytical",
109
        "tone_name": "Analytical"
110
      }
111
    ]
112
  },
113
  "sentences_tone": [
114
    {
115
      "sentence_id": 0,
116
      "text": "Team, I know that times are tough!",
117
      "tones": [
118
        {
119
          "score": 0.801827,
120
          "tone_id": "analytical",
121
          "tone_name": "Analytical"
122
        }
123
      ]
124
    },
125
    {
126
      "sentence_id": 1,
127
      "text": "Product sales have been disappointing for the past three quarters.",
128
      "tones": [
129
        {
130
          "score": 0.771241,
131
          "tone_id": "sadness",
132
          "tone_name": "Sadness"
133
        },
134
        {
135
          "score": 0.687768,
136
          "tone_id": "analytical",
137
          "tone_name": "Analytical"
138
        }
139
      ]
140
    },
141
    {
142
      "sentence_id": 2,
143
      "text": "We have a competitive product, but we need to do a better job of selling it!",
144
      "tones": [
145
        {
146
          "score": 0.506763,
147
          "tone_id": "analytical",
148
          "tone_name": "Analytical"
149
        }
150
      ]
151
    }
152
  ]
153
}';
154
155
        $this->httpClient->shouldReceive('sendRequest')->once()->andReturnUsing(function () use ($rawResponse) {
156
            return new Response(200, ['Content-Type' => 'application/json'], $rawResponse);
157
        });
158
159
        $tone = new Tone($this->httpClient, $this->hydrator, $this->requestBuilder);
160
161
        $response = $tone->analyze('text', ['content_language' => 'en', 'accept_language' => 'en']);
162
163
        $sentences = $response->getSentenceAnalysis()->getSentences();
164
165
        $this->assertNotEmpty($sentences);
166
167
        $firstSentence = $sentences[0];
168
169
        $this->assertEquals(0, $firstSentence->getId());
170
        $this->assertEquals('Team, I know that times are tough!', $firstSentence->getText());
171
172
        $tones = $firstSentence->getTones();
173
174
        $this->assertNotEmpty($tones);
175
    }
176
}
177