Completed
Push — master ( f323bf...0da584 )
by Byron
02:55
created

TranslatorTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 95
Duplicated Lines 40 %

Coupling/Cohesion

Components 2
Dependencies 6
Metric Value
wmc 5
lcom 2
cbo 6
dl 38
loc 95
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A testConstructor() 0 9 1
A testAccountQuotaExceeded() 19 19 1
A testArgumentException() 19 19 1
A testExpiredToken() 0 15 1
A testUnhandledException() 0 19 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
use GuzzleHttp\Client;
4
use GuzzleHttp\Subscriber\Mock;
5
use GuzzleHttp\Message\Response;
6
use badams\MicrosoftTranslator\MicrosoftTranslator;
7
use GuzzleHttp\Stream\Stream;
8
9
class TranslatorTest extends \PHPUnit_Framework_TestCase
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
10
{
11
    protected $quotaExceededResponse = "<html><body><h1>TranslateApiException</h1><p>Method: Translate()</p><p>Message: The Azure Market Place Translator Subscription associated with the request credentials has zero balance.</p></body></html>";
12
13
    protected $argumentExceptionResponse = "<html><body><h1>Argument Exception</h1><p>Error Message Here</p></body></html>";
14
15
    protected $expiredTokenResponse = "<html><body><h1>Argument Exception</h1><p>Method: Translate()</p><p>Parameter: </p><p>Message: The incoming token has expired. Get a new access token from the Authorization Server.</p></body></html>";
16
17
18
    public function testConstructor()
19
    {
20
        $translator = new MicrosoftTranslator();
21
        $reflection = new ReflectionClass($translator);
22
        $client = $reflection->getProperty('http');
23
        $client->setAccessible(true);
24
        $this->assertNotNull($client->getValue($translator));
25
        $this->assertInstanceOf('GuzzleHttp\Client', $client->getValue($translator));
26
    }
27
28 View Code Duplication
    public function testAccountQuotaExceeded()
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...
29
    {
30
        $client = new Client();
31
32
        $mock = new Mock([
33
            new Response(200, [], Stream::factory('{"access_token":"valid"}')),
34
            new Response(400, [], Stream::factory($this->quotaExceededResponse))
35
        ]);
36
37
        $client->getEmitter()->attach($mock);
38
39
        $this->setExpectedException(
40
            '\badams\MicrosoftTranslator\Exceptions\QuotaExceededException',
41
            strip_tags($this->quotaExceededResponse)
42
        );
43
44
        $translator = new MicrosoftTranslator($client);
45
        $translator->translate('Hello', 'en', 'de');
46
    }
47
48 View Code Duplication
    public function testArgumentException()
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...
49
    {
50
        $client = new Client();
51
52
        $mock = new Mock([
53
            new Response(200, [], Stream::factory('{"access_token":"valid"}')),
54
            new Response(400, [], Stream::factory($this->argumentExceptionResponse))
55
        ]);
56
57
        $client->getEmitter()->attach($mock);
58
59
        $this->setExpectedException(
60
            '\badams\MicrosoftTranslator\Exceptions\ArgumentException',
61
            strip_tags($this->argumentExceptionResponse)
62
        );
63
64
        $translator = new MicrosoftTranslator($client);
65
        $translator->translate('Hello', 'en', 'de');
66
    }
67
68
    public function testExpiredToken()
69
    {
70
        $client = new Client();
71
72
        $mock = new Mock([
73
            new Response(200, [], Stream::factory('{"access_token":"valid"}')),
74
            new Response(400, [], Stream::factory($this->expiredTokenResponse)),
75
            new Response(200, [], Stream::factory('{"access_token":"valid"}')),
76
            new Response(200, [], Stream::factory("<string>Salut</string>")),
77
        ]);
78
79
        $client->getEmitter()->attach($mock);
80
        $translator = new MicrosoftTranslator($client);
81
        $this->assertEquals('Salut', $translator->translate('Hello', 'en', 'fr'));
82
    }
83
84
    public function testUnhandledException()
85
    {
86
        $client = new Client();
87
88
        $mock = new Mock([
89
            new Response(200, [], Stream::factory('{"access_token":"valid"}')),
90
            new Response(500, [], Stream::factory('Unknown Error'))
91
        ]);
92
93
        $client->getEmitter()->attach($mock);
94
95
        $this->setExpectedException(
96
            '\badams\MicrosoftTranslator\Exceptions\TranslatorException',
97
            'Unknown Error'
98
        );
99
100
        $translator = new MicrosoftTranslator($client);
101
        $translator->translate('Hello', 'en', 'de');
102
    }
103
}
104
105
106
107
108