Completed
Pull Request — develop (#5)
by Adam
01:26
created

ToneTest::testSentenceToneAnaysis()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 80
Code Lines 13

Duplication

Lines 80
Ratio 100 %

Importance

Changes 0
Metric Value
dl 80
loc 80
rs 8.8387
c 0
b 0
f 0
cc 1
eloc 13
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 View Code Duplication
    public function testDocumentToneAnalysis()
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...
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('text', ['content_language' => 'en', 'accept_language' => 'en']);
81
82
        $tones = $response->getDocumentAnalysis()->getTones();
83
84
        $this->assertNotEmpty($tones);
85
86
        $firstTone = $tones[0];
87
88
        $this->assertEquals('sadness', $firstTone->getId());
89
        $this->assertEquals('Sadness', $firstTone->getName());
90
        $this->assertEquals(0.6165, $firstTone->getScore());
91
    }
92
93 View Code Duplication
    public function testSentenceToneAnaysis()
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...
94
    {
95
        $rawResponse = '{
96
  "document_tone": {
97
    "tones": [
98
      {
99
        "score": 0.6165,
100
        "tone_id": "sadness",
101
        "tone_name": "Sadness"
102
      },
103
      {
104
        "score": 0.829888,
105
        "tone_id": "analytical",
106
        "tone_name": "Analytical"
107
      }
108
    ]
109
  },
110
  "sentences_tone": [
111
    {
112
      "sentence_id": 0,
113
      "text": "Team, I know that times are tough!",
114
      "tones": [
115
        {
116
          "score": 0.801827,
117
          "tone_id": "analytical",
118
          "tone_name": "Analytical"
119
        }
120
      ]
121
    },
122
    {
123
      "sentence_id": 1,
124
      "text": "Product sales have been disappointing for the past three quarters.",
125
      "tones": [
126
        {
127
          "score": 0.771241,
128
          "tone_id": "sadness",
129
          "tone_name": "Sadness"
130
        },
131
        {
132
          "score": 0.687768,
133
          "tone_id": "analytical",
134
          "tone_name": "Analytical"
135
        }
136
      ]
137
    },
138
    {
139
      "sentence_id": 2,
140
      "text": "We have a competitive product, but we need to do a better job of selling it!",
141
      "tones": [
142
        {
143
          "score": 0.506763,
144
          "tone_id": "analytical",
145
          "tone_name": "Analytical"
146
        }
147
      ]
148
    }
149
  ]
150
}';
151
152
        $this->httpClient->shouldReceive('sendRequest')->once()->andReturnUsing(function () use ($rawResponse) {
153
            return new Response(200, ['Content-Type' => 'application/json'], $rawResponse);
154
        });
155
156
        $tone = new Tone($this->httpClient, $this->hydrator, $this->requestBuilder);
157
158
        $response = $tone->analyze('text', ['content_language' => 'en', 'accept_language' => 'en']);
159
160
        $sentences = $response->getSentenceAnalysis()->getSentences();
161
162
        $this->assertNotEmpty($sentences);
163
164
        $firstSentence = $sentences[0];
165
166
        $this->assertEquals(0, $firstSentence->getId());
167
        $this->assertEquals('Team, I know that times are tough!', $firstSentence->getText());
168
169
        $tones = $firstSentence->getTones();
170
171
        $this->assertNotEmpty($tones);
172
    }
173
}
174