Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit eb6da9d

Browse files
add more tests with api v1
1 parent f71b037 commit eb6da9d

File tree

2 files changed

+78
-10
lines changed

2 files changed

+78
-10
lines changed

src/DeepL.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,27 @@ class DeepL
1212
/**
1313
* API v1 URL
1414
*/
15-
const API_URL_V1 = 'https://api.deepl.com/v1/translate';
15+
const API_URL_V1 = 'https://api.deepl.com/v1/translate';
1616

1717
/**
1818
* API v2 URL
1919
*/
20-
const API_URL_V2 = 'https://api.deepl.com/v2/translate';
20+
const API_URL_V2 = 'https://api.deepl.com/v2/translate';
2121

2222
/**
2323
* API URL: Parameter auth_key
2424
*/
25-
const API_URL_AUTH_KEY = 'auth_key=%s';
25+
const API_URL_AUTH_KEY = 'auth_key=%s';
2626

2727
/**
2828
* API URL: Parameter text
2929
*/
30-
const API_URL_TEXT = 'text=%s';
30+
const API_URL_TEXT = 'text=%s';
3131

3232
/**
3333
* API URL: Parameter source_lang
3434
*/
35-
const API_URL_SOURCE_LANG = 'source_lang=%s';
35+
const API_URL_SOURCE_LANG = 'source_lang=%s';
3636

3737
/**
3838
* API URL: Parameter target_lang
@@ -42,17 +42,17 @@ class DeepL
4242
/**
4343
* API URL: Parameter tag_handling
4444
*/
45-
const API_URL_TAG_HANDLING = 'tag_handling=%s';
45+
const API_URL_TAG_HANDLING = 'tag_handling=%s';
4646

4747
/**
4848
* API URL: Parameter ignore_tags
4949
*/
50-
const API_URL_IGNORE_TAGS = 'ignore_tags=%s';
50+
const API_URL_IGNORE_TAGS = 'ignore_tags=%s';
5151

5252
/**
5353
* API URL: Parameter formality
5454
*/
55-
const API_URL_FORMALITY = 'formality=%s';
55+
const API_URL_FORMALITY = 'formality=%s';
5656

5757
/**
5858
* DeepL HTTP error codes

tests/DeepLTest.php

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,30 @@ public function testBuildUrl()
113113
'formality' => 'default'
114114
));
115115

116-
$deepl = new DeepL($authKey);
116+
$deepl = new DeepL($authKey);
117+
118+
$buildUrl = self::getMethod('\BabyMarkt\DeepL\DeepL', 'buildUrl');
119+
120+
$return = $buildUrl->invokeArgs($deepl, array('de', 'en'));
121+
122+
$this->assertEquals($expectedString, $return);
123+
}
124+
125+
/**
126+
* Test buildUrl()
127+
*/
128+
public function testBuildUrlV1()
129+
{
130+
$authKey = '123456';
131+
132+
$expectedString = 'https://api.deepl.com/v1/translate?' . http_build_query(array(
133+
'auth_key' => $authKey,
134+
'source_lang' => 'de',
135+
'target_lang' => 'en',
136+
'formality' => 'default'
137+
));
138+
139+
$deepl = new DeepL($authKey, 1);
117140

118141
$buildUrl = self::getMethod('\BabyMarkt\DeepL\DeepL', 'buildUrl');
119142

@@ -137,7 +160,31 @@ public function testBuildUrlWithTags()
137160
'formality' => 'default'
138161
));
139162

140-
$deepl = new DeepL($authKey);
163+
$deepl = new DeepL($authKey);
164+
165+
$buildUrl = self::getMethod('\BabyMarkt\DeepL\DeepL', 'buildUrl');
166+
167+
$return = $buildUrl->invokeArgs($deepl, array('de', 'en', array('xml'), array('x')));
168+
169+
$this->assertEquals($expectedString, $return);
170+
}
171+
172+
/**
173+
* Test buildUrl()
174+
*/
175+
public function testBuildUrlWithTagsV1()
176+
{
177+
$authKey = '123456';
178+
$expectedString = 'https://api.deepl.com/v1/translate?' . http_build_query(array(
179+
'auth_key' => $authKey,
180+
'source_lang' => 'de',
181+
'target_lang' => 'en',
182+
'tag_handling' => 'xml',
183+
'ignore_tags' => 'x',
184+
'formality' => 'default'
185+
));
186+
187+
$deepl = new DeepL($authKey, 1);
141188

142189
$buildUrl = self::getMethod('\BabyMarkt\DeepL\DeepL', 'buildUrl');
143190

@@ -205,6 +252,27 @@ public function testTranslateV1Success()
205252
$this->assertEquals($expectedText, $translatedText);
206253
}
207254

255+
/**
256+
* Test translate() success with default v2 API
257+
*
258+
* TEST REQUIRES VALID DEEPL AUTH KEY!!
259+
*/
260+
public function testTranslateWrongVersionSuccess()
261+
{
262+
if (self::$authKey === false) {
263+
$this->markTestSkipped('DeepL Auth Key (DEEPL_AUTH_KEY) is not configured.');
264+
}
265+
266+
$deepl = new DeepL(self::$authKey, 3);
267+
268+
$germanText = 'Hallo Welt';
269+
$expectedText = 'Hello World';
270+
271+
$translatedText = $deepl->translate($germanText);
272+
273+
$this->assertEquals($expectedText, $translatedText);
274+
}
275+
208276
/**
209277
* Test translate() with tag handling success
210278
*

0 commit comments

Comments
 (0)