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