@@ -20,6 +20,8 @@ public function setUp()
20
20
$ this ->mockResponses = new MockResponses ;
21
21
//Translator Class namespace
22
22
$ this ->translatorClass = 'FindBrok\WatsonTranslate\Translator ' ;
23
+ //Create the mock client
24
+ $ this ->client = $ this ->getMockBuilder ('GuzzleHttp\Client ' )->disableOriginalConstructor ()->getMock ();
23
25
}
24
26
25
27
/**
@@ -33,6 +35,22 @@ protected function getPackageProviders($app)
33
35
return ['FindBrok\WatsonTranslate\WatsonTranslateServiceProvider ' ];
34
36
}
35
37
38
+ /**
39
+ * Set response as a result of textTranslate
40
+ */
41
+ public function fakeResponseForTextTranslate ()
42
+ {
43
+ $ this ->client ->method ('send ' )->willReturn ($ this ->mockResponses ->pretendTextTranslateResponse ());
44
+ }
45
+
46
+ /**
47
+ * Set response as a result of bulkTranslate
48
+ */
49
+ public function fakeResponseForBulkTranslate ()
50
+ {
51
+ $ this ->client ->method ('send ' )->willReturn ($ this ->mockResponses ->pretendBulkTranslateResponse ());
52
+ }
53
+
36
54
/**
37
55
* Test if the getter really returns the property
38
56
* and that property is set
@@ -59,32 +77,65 @@ public function testPropertyInexistent_ReturnNull()
59
77
*/
60
78
public function testTextTranslate_WithGetTranslation_ReturnString ()
61
79
{
62
- $ client = $ this ->getMockBuilder ('GuzzleHttp\Client ' )->disableOriginalConstructor ()->getMock ();
63
- $ client ->method ('send ' )->willReturn ($ this ->mockResponses ->pretendTextTranslateResponse ());
80
+ $ this ->fakeResponseForTextTranslate ();
64
81
65
82
$ translator = $ this ->getMock ($ this ->translatorClass , ['getClient ' ]);
66
- $ translator ->method ('getClient ' )->willReturn ($ client );
83
+ $ translator ->method ('getClient ' )->willReturn ($ this -> client );
67
84
68
- $ this ->assertEquals ('Lorem ipsum ' , $ translator ->textTranslate ('Lorem ipsum ' )->getTranslation ());
85
+ $ this ->assertEquals (
86
+ 'Lorem ipsum ' ,
87
+ $ translator ->textTranslate ('Lorem ipsum ' )->getTranslation ()
88
+ );
69
89
}
70
90
71
91
/**
72
92
* Test the textTranslate with rawResults method returns json
73
93
*/
74
94
public function testTextTranslate_WithRawResults_ReturnJson ()
75
95
{
76
- $ client = $ this ->getMockBuilder ('GuzzleHttp\Client ' )->disableOriginalConstructor ()->getMock ();
77
- $ client ->method ('send ' )->willReturn ($ this ->mockResponses ->pretendTextTranslateResponse ());
96
+ $ this ->fakeResponseForTextTranslate ();
78
97
79
98
$ translator = $ this ->getMock ($ this ->translatorClass , ['getClient ' ]);
80
- $ translator ->method ('getClient ' )->willReturn ($ client );
99
+ $ translator ->method ('getClient ' )->willReturn ($ this -> client );
81
100
82
101
$ this ->assertJsonStringEqualsJsonString (
83
102
$ this ->mockResponses ->pretendTextTranslateRaw (),
84
103
$ translator ->textTranslate ('Lorem ipsum ' )->rawResults ()
85
104
);
86
105
}
87
106
107
+ /**
108
+ * Test the textTranslate with arrayResults method returns array
109
+ */
110
+ public function testTextTranslate_WithArrayResults_ReturnArray ()
111
+ {
112
+ $ this ->fakeResponseForTextTranslate ();
113
+
114
+ $ translator = $ this ->getMock ($ this ->translatorClass , ['getClient ' ]);
115
+ $ translator ->method ('getClient ' )->willReturn ($ this ->client );
116
+
117
+ $ this ->assertEquals (
118
+ json_decode ($ this ->mockResponses ->pretendTextTranslateRaw (), true ),
119
+ $ translator ->textTranslate ('Lorem ipsum ' )->arrayResults ()
120
+ );
121
+ }
122
+
123
+ /**
124
+ * Test textTranslate with collectResults method returns collection
125
+ */
126
+ public function testTextTranslate_WithCollectionResults_ReturnCollection ()
127
+ {
128
+ $ this ->fakeResponseForTextTranslate ();
129
+
130
+ $ translator = $ this ->getMock ($ this ->translatorClass , ['getClient ' ]);
131
+ $ translator ->method ('getClient ' )->willReturn ($ this ->client );
132
+
133
+ $ this ->assertEquals (
134
+ collect (json_decode ($ this ->mockResponses ->pretendTextTranslateRaw (), true )),
135
+ $ translator ->textTranslate ('Lorem ipsum ' )->collectResults ()
136
+ );
137
+ }
138
+
88
139
/**
89
140
* Test textTranslate throws \GuzzleHttp\Exception\ClientException with getTranslation returns null
90
141
*
@@ -107,13 +158,63 @@ public function testTextTranslate_WithGetTranslation_ThrowsClientException_Retur
107
158
*/
108
159
public function testBulkTranslate_WithGetTranslation_ReturnArray ()
109
160
{
110
- $ client = $ this ->getMockBuilder ('GuzzleHttp\Client ' )->disableOriginalConstructor ()->getMock ();
111
- $ client ->method ('send ' )->willReturn ($ this ->mockResponses ->pretendBulkTranslateResponse ());
161
+ $ this ->fakeResponseForBulkTranslate ();
112
162
113
163
$ translator = $ this ->getMock ($ this ->translatorClass , ['getClient ' ]);
114
- $ translator ->method ('getClient ' )->willReturn ($ client );
164
+ $ translator ->method ('getClient ' )->willReturn ($ this -> client );
115
165
116
- $ this ->assertSame (['Lorem ipsum ' , 'Lorem nam dolor ' ], $ translator ->bulkTranslate (['lorem ' , 'nam ' ])->getTranslation ());
166
+ $ this ->assertEquals (
167
+ ['Lorem ipsum ' , 'Lorem nam dolor ' ],
168
+ $ translator ->bulkTranslate (['lorem ' , 'nam ' ])->getTranslation ()
169
+ );
170
+ }
171
+
172
+ /**
173
+ * Test the bulkTranslate method with rawResults method returns json
174
+ */
175
+ public function testBulkTranslate_WithRawResults_ReturnJson ()
176
+ {
177
+ $ this ->fakeResponseForBulkTranslate ();
178
+
179
+ $ translator = $ this ->getMock ($ this ->translatorClass , ['getClient ' ]);
180
+ $ translator ->method ('getClient ' )->willReturn ($ this ->client );
181
+
182
+ $ this ->assertJsonStringEqualsJsonString (
183
+ $ this ->mockResponses ->pretendBulkTranslateRaw (),
184
+ $ translator ->bulkTranslate (['lorem ' , 'nam ' ])->rawResults ()
185
+ );
186
+ }
187
+
188
+ /**
189
+ * Test the bulkTranslate method with arrayResults method returns array
190
+ */
191
+ public function testBulkTranslate_WithArrayResults_ReturnArray ()
192
+ {
193
+ $ this ->fakeResponseForBulkTranslate ();
194
+
195
+ $ translator = $ this ->getMock ($ this ->translatorClass , ['getClient ' ]);
196
+ $ translator ->method ('getClient ' )->willReturn ($ this ->client );
197
+
198
+ $ this ->assertEquals (
199
+ json_decode ($ this ->mockResponses ->pretendBulkTranslateRaw (), true ),
200
+ $ translator ->bulkTranslate (['lorem ' , 'nam ' ])->arrayResults ()
201
+ );
202
+ }
203
+
204
+ /**
205
+ * Test the bulkTranslate method with collectResults method returns collection
206
+ */
207
+ public function testBulkTranslate_WithCollectionResults_ReturnCollection ()
208
+ {
209
+ $ this ->fakeResponseForBulkTranslate ();
210
+
211
+ $ translator = $ this ->getMock ($ this ->translatorClass , ['getClient ' ]);
212
+ $ translator ->method ('getClient ' )->willReturn ($ this ->client );
213
+
214
+ $ this ->assertEquals (
215
+ collect (json_decode ($ this ->mockResponses ->pretendBulkTranslateRaw (), true )),
216
+ $ translator ->bulkTranslate (['lorem ' , 'nam ' ])->collectResults ()
217
+ );
117
218
}
118
219
119
220
/**
0 commit comments