|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* BitFeed 2015 |
|
5
|
|
|
* @Author Chiarillo Massimo |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace Yandex\TranslatorBundle\Tests\Service; |
|
9
|
|
|
|
|
10
|
|
|
use Yandex\TranslatorBundle\Model\Key; |
|
11
|
|
|
use Yandex\TranslatorBundle\Service\Client; |
|
12
|
|
|
use Yandex\TranslatorBundle\Service\Translator; |
|
13
|
|
|
use Guzzle\Http\Exception\RequestException; |
|
14
|
|
|
|
|
15
|
|
|
class TranslatorTest extends \PHPUnit_Framework_TestCase |
|
16
|
|
|
{ |
|
17
|
|
|
public function testTransaltorOk() |
|
18
|
|
|
{ |
|
19
|
|
|
$translator = new Translator( |
|
20
|
|
|
$this->getClientMock(true) |
|
21
|
|
|
); |
|
22
|
|
|
|
|
23
|
|
|
$key = (new Key()) |
|
24
|
|
|
->setValue('myYandexKey'); |
|
25
|
|
|
|
|
26
|
|
|
$response = $this->createRequest($translator, $key); |
|
27
|
|
|
|
|
28
|
|
|
$this->assertEquals('Ciao Max', $response->getText()); |
|
29
|
|
|
$this->assertEquals(200, $response->getCode()); |
|
30
|
|
|
$this->assertEquals('en', $response->getFrom()); |
|
31
|
|
|
$this->assertEquals('it', $response->getTo()); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function testTranslatorKo() |
|
35
|
|
|
{ |
|
36
|
|
|
$translator = new Translator( |
|
37
|
|
|
$this->getClientMock(false) |
|
38
|
|
|
); |
|
39
|
|
|
|
|
40
|
|
|
$key = (new Key()) |
|
41
|
|
|
->setValue('myYandexKey'); |
|
42
|
|
|
|
|
43
|
|
|
$response = $this->createRequest($translator, $key); |
|
44
|
|
|
|
|
45
|
|
|
//Yandex return invalid response |
|
46
|
|
|
$this->assertFalse($response); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
private function createRequest(Translator $translator, Key $key) |
|
50
|
|
|
{ |
|
51
|
|
|
return $translator->createRequest() |
|
52
|
|
|
->setKey($key->getValue()) |
|
53
|
|
|
->setText('Hello Max') |
|
54
|
|
|
->setFrom('en') |
|
55
|
|
|
->setTo('it') |
|
56
|
|
|
->send(); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
private function getClientMock($sendValidRequest = true) |
|
60
|
|
|
{ |
|
61
|
|
|
$clientMock = $this->getMockBuilder('Yandex\TranslatorBundle\Service\Client') |
|
62
|
|
|
->disableOriginalConstructor() |
|
63
|
|
|
->setMethods(['get']) |
|
64
|
|
|
->getMock(); |
|
65
|
|
|
|
|
66
|
|
|
$clientMock->expects($this->any()) |
|
67
|
|
|
->method('get') |
|
68
|
|
|
->willReturn( |
|
69
|
|
|
$this->getRequestMock($sendValidRequest) |
|
70
|
|
|
); |
|
71
|
|
|
|
|
72
|
|
|
return $clientMock; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
private function getRequestMock($isValidRequest = true) |
|
76
|
|
|
{ |
|
77
|
|
|
$requestMock = $this->getMockBuilder('Guzzle\Http\Message\Request') |
|
78
|
|
|
->setMethods(['send']) |
|
79
|
|
|
->disableOriginalConstructor() |
|
80
|
|
|
->getMock(); |
|
81
|
|
|
|
|
82
|
|
|
$sendMethodMock = $requestMock->expects( |
|
83
|
|
|
$this->once() |
|
84
|
|
|
)->method('send'); |
|
85
|
|
|
|
|
86
|
|
|
if ($isValidRequest) { |
|
87
|
|
|
$sendMethodMock->willReturn( |
|
88
|
|
|
$this->getResponseMock() |
|
89
|
|
|
); |
|
90
|
|
|
|
|
91
|
|
|
return $requestMock; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
$sendMethodMock->will( |
|
95
|
|
|
$this->throwException( |
|
96
|
|
|
new RequestException() |
|
97
|
|
|
) |
|
98
|
|
|
); |
|
99
|
|
|
|
|
100
|
|
|
return $requestMock; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
protected function getResponseMock() |
|
104
|
|
|
{ |
|
105
|
|
|
$responseMock = $this->getMockBuilder('Guzzle\Http\Message\Response') |
|
106
|
|
|
->setMethods(['getBody']) |
|
107
|
|
|
->getMock(); |
|
108
|
|
|
|
|
109
|
|
|
$responseMock->expects($this->any()) |
|
110
|
|
|
->method('getBody') |
|
111
|
|
|
->willReturn(json_encode([ |
|
112
|
|
|
'code' => 200, |
|
113
|
|
|
'lang' => 'en-it', |
|
114
|
|
|
'text' => [ |
|
115
|
|
|
'Ciao Max' |
|
116
|
|
|
] |
|
117
|
|
|
])); |
|
118
|
|
|
|
|
119
|
|
|
return $responseMock; |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|