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 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 | public function testToneChatErrors() |
||
| 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 |