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 |
||
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 | public function testUnauthorized() |
||
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 | public function testNotFound() |
||
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 | public function testUnknownError() |
||
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 | public function testAnalyzeHtml() |
||
178 | { |
||
179 | $rawResponse = '{"document_tone": {"tones": [{"score": 0.6165,"tone_id": "sadness","tone_name": "Sadness"}]}}'; |
||
180 | |||
181 | $this->httpClient->shouldReceive('sendRequest')->once()->andReturnUsing(function () use ($rawResponse) { |
||
182 | return new Response(200, ['Content-Type' => 'application/json'], $rawResponse); |
||
183 | }); |
||
184 | |||
185 | $tone = new Tone($this->httpClient, $this->hydrator, $this->requestBuilder); |
||
186 | |||
187 | $response = $tone->analyze('<div>Some HTML to test</div>', ['is_html' => true]); |
||
188 | |||
189 | $tones = $response->getDocumentAnalysis()->getTones(); |
||
190 | |||
191 | $this->assertNotEmpty($tones); |
||
192 | } |
||
193 | |||
194 | public function testAnalyzeJson() |
||
195 | { |
||
196 | $rawResponse = '{"document_tone": {"tones": [{"score": 0.6165,"tone_id": "sadness","tone_name": "Sadness"}]}}'; |
||
197 | |||
198 | $this->httpClient->shouldReceive('sendRequest')->once()->andReturnUsing(function () use ($rawResponse) { |
||
199 | return new Response(200, ['Content-Type' => 'application/json'], $rawResponse); |
||
200 | }); |
||
201 | |||
202 | $tone = new Tone($this->httpClient, $this->hydrator, $this->requestBuilder); |
||
203 | |||
204 | $response = $tone->analyze('{"text": "Some text to analyze"}'); |
||
205 | |||
206 | $tones = $response->getDocumentAnalysis()->getTones(); |
||
207 | |||
208 | $this->assertNotEmpty($tones); |
||
209 | } |
||
210 | } |
||
211 |