10
10
class DeepLTest extends \PHPUnit_Framework_TestCase
11
11
{
12
12
/**
13
- * DeepL Auth Key (Set if you want to test all methods)
13
+ * DeepL Auth Key.
14
14
*
15
- * @var string
15
+ * @var bool| string
16
16
*/
17
- private $ authKey = '' ;
17
+ protected static $ authKey = false ;
18
+
19
+ /**
20
+ * Setup DeepL Auth Key.
21
+ */
22
+ public static function setUpBeforeClass ()
23
+ {
24
+ parent ::setUpBeforeClass ();
25
+
26
+ if (($ authKey = getenv ('DEEPL_AUTH_KEY ' )) === false ) {
27
+ return ;
28
+ }
29
+
30
+ self ::$ authKey = $ authKey ;
31
+ }
18
32
19
33
/**
20
34
* Get protected method
@@ -98,32 +112,37 @@ public function testBuildUrl()
98
112
}
99
113
100
114
/**
101
- * Test buildBody ()
115
+ * Test buildUrl ()
102
116
*/
103
- public function testBuildBody ()
117
+ public function testBuildUrlWithTags ()
104
118
{
105
- $ expectedString = 'text=Hallo%20Welt ' ;
119
+ $ expectedString = 'https://api.deepl.com/v1/translate?auth_key=123456&source_lang=de&target_lang=en&tag_handling=xml&ignore_tags=x ' ;
106
120
107
121
$ authKey = '123456 ' ;
108
122
$ deepl = new DeepL ($ authKey );
109
123
110
- $ buildUrl = self ::getMethod ('\BabyMarkt\DeepL\DeepL ' , 'buildBody ' );
124
+ $ buildUrl = self ::getMethod ('\BabyMarkt\DeepL\DeepL ' , 'buildUrl ' );
111
125
112
- $ return = $ buildUrl ->invokeArgs ($ deepl , array ('Hallo Welt ' ));
126
+ $ return = $ buildUrl ->invokeArgs ($ deepl , array ('de ' , ' en ' , array ( ' xml ' ), array ( ' x ' ) ));
113
127
114
128
$ this ->assertEquals ($ expectedString , $ return );
115
129
}
116
130
117
131
/**
118
- * Test translate() calls
132
+ * Test buildBody()
119
133
*/
120
- public function testTranslate ()
134
+ public function testBuildBody ()
121
135
{
122
- return ;
136
+ $ expectedString = ' text=Hallo%20Welt ' ;
123
137
124
- $ mock = $ this ->getMockBuilder ('\BabyMarkt\DeepL\DeepL ' );
138
+ $ authKey = '123456 ' ;
139
+ $ deepl = new DeepL ($ authKey );
140
+
141
+ $ buildUrl = self ::getMethod ('\BabyMarkt\DeepL\DeepL ' , 'buildBody ' );
125
142
126
- // TODO: test if translate methods calls correct methods
143
+ $ return = $ buildUrl ->invokeArgs ($ deepl , array ('Hallo Welt ' ));
144
+
145
+ $ this ->assertEquals ($ expectedString , $ return );
127
146
}
128
147
129
148
/**
@@ -133,11 +152,11 @@ public function testTranslate()
133
152
*/
134
153
public function testTranslateSuccess ()
135
154
{
136
- if (! $ this -> authKey ) {
137
- return ;
155
+ if (self :: $ authKey === false ) {
156
+ $ this -> markTestSkipped ( ' DeepL Auth Key (DEEPL_AUTH_KEY) is not configured. ' ) ;
138
157
}
139
158
140
- $ deepl = new DeepL ($ this -> authKey );
159
+ $ deepl = new DeepL (self :: $ authKey );
141
160
142
161
$ germanText = 'Hallo Welt ' ;
143
162
$ expectedText = 'Hello World ' ;
@@ -154,11 +173,11 @@ public function testTranslateSuccess()
154
173
*/
155
174
public function testTranslateV2Success ()
156
175
{
157
- if (! $ this -> authKey ) {
158
- return ;
176
+ if (self :: $ authKey === false ) {
177
+ $ this -> markTestSkipped ( ' DeepL Auth Key (DEEPL_AUTH_KEY) is not configured. ' ) ;
159
178
}
160
179
161
- $ deepl = new DeepL ($ this -> authKey , 2 );
180
+ $ deepl = new DeepL (self :: $ authKey , 2 );
162
181
163
182
$ germanText = 'Hallo Welt ' ;
164
183
$ expectedText = 'Hello World ' ;
@@ -175,11 +194,11 @@ public function testTranslateV2Success()
175
194
*/
176
195
public function testTranslateTagHandlingSuccess ()
177
196
{
178
- if (! $ this -> authKey ) {
179
- return ;
197
+ if (self :: $ authKey === false ) {
198
+ $ this -> markTestSkipped ( ' DeepL Auth Key (DEEPL_AUTH_KEY) is not configured. ' ) ;
180
199
}
181
200
182
- $ deepl = new DeepL ($ this -> authKey );
201
+ $ deepl = new DeepL (self :: $ authKey );
183
202
184
203
$ englishText = '<strong>text to translate</strong> ' ;
185
204
$ expectedText = '<strong>zu übersetzender Text</strong> ' ;
@@ -201,14 +220,14 @@ public function testTranslateTagHandlingSuccess()
201
220
*/
202
221
public function testTranslateIgnoreTagsSuccess ()
203
222
{
204
- if (! $ this -> authKey ) {
205
- return ;
223
+ if (self :: $ authKey === false ) {
224
+ $ this -> markTestSkipped ( ' DeepL Auth Key (DEEPL_AUTH_KEY) is not configured. ' ) ;
206
225
}
207
226
208
- $ deepl = new DeepL ($ this -> authKey );
227
+ $ deepl = new DeepL (self :: $ authKey );
209
228
210
229
$ englishText = '<strong>text to do not translate</strong><p>text to translate</p> ' ;
211
- $ expectedText = '<strong>Text, der nicht übersetzt werden soll </strong><p>zu übersetzender Text</p> ' ;
230
+ $ expectedText = '<strong>text to do not translate </strong><p>zu übersetzender Text</p> ' ;
212
231
213
232
$ translatedText = $ deepl ->translate (
214
233
$ englishText ,
0 commit comments