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

Commit 1770d25

Browse files
authored
Merge pull request #14 from arkadiusjonczek/use-api-v2-as-default
use DeepL API v2 as default
2 parents 64c4a0f + eb6da9d commit 1770d25

File tree

2 files changed

+86
-18
lines changed

2 files changed

+86
-18
lines changed

src/DeepL.php

Lines changed: 10 additions & 10 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
@@ -132,7 +132,7 @@ class DeepL
132132
* @param string $authKey
133133
* @param integer $apiVersion
134134
*/
135-
public function __construct($authKey, $apiVersion = 1)
135+
public function __construct($authKey, $apiVersion = 2)
136136
{
137137
$this->authKey = $authKey;
138138
$this->apiVersion = $apiVersion;
@@ -251,7 +251,7 @@ protected function buildUrl(
251251
$url = DeepL::API_URL_V2;
252252
break;
253253
default:
254-
$url = DeepL::API_URL_V1;
254+
$url = DeepL::API_URL_V2;
255255
}
256256

257257
$url .= '?' . sprintf(DeepL::API_URL_AUTH_KEY, $this->authKey);

tests/DeepLTest.php

Lines changed: 76 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,14 @@ public function testBuildUrl()
106106
{
107107
$authKey = '123456';
108108

109-
$expectedString = 'https://api.deepl.com/v1/translate?' . http_build_query(array(
109+
$expectedString = 'https://api.deepl.com/v2/translate?' . http_build_query(array(
110110
'auth_key' => $authKey,
111111
'source_lang' => 'de',
112112
'target_lang' => 'en',
113113
'formality' => 'default'
114114
));
115115

116-
$deepl = new DeepL($authKey);
116+
$deepl = new DeepL($authKey);
117117

118118
$buildUrl = self::getMethod('\BabyMarkt\DeepL\DeepL', 'buildUrl');
119119

@@ -125,10 +125,33 @@ public function testBuildUrl()
125125
/**
126126
* Test buildUrl()
127127
*/
128-
public function testBuildUrlWithTags()
128+
public function testBuildUrlV1()
129129
{
130130
$authKey = '123456';
131+
131132
$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);
140+
141+
$buildUrl = self::getMethod('\BabyMarkt\DeepL\DeepL', 'buildUrl');
142+
143+
$return = $buildUrl->invokeArgs($deepl, array('de', 'en'));
144+
145+
$this->assertEquals($expectedString, $return);
146+
}
147+
148+
/**
149+
* Test buildUrl()
150+
*/
151+
public function testBuildUrlWithTags()
152+
{
153+
$authKey = '123456';
154+
$expectedString = 'https://api.deepl.com/v2/translate?' . http_build_query(array(
132155
'auth_key' => $authKey,
133156
'source_lang' => 'de',
134157
'target_lang' => 'en',
@@ -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

@@ -164,7 +211,7 @@ public function testBuildBody()
164211
}
165212

166213
/**
167-
* Test translate() success
214+
* Test translate() success with v2 API
168215
*
169216
* TEST REQUIRES VALID DEEPL AUTH KEY!!
170217
*/
@@ -185,17 +232,38 @@ public function testTranslateSuccess()
185232
}
186233

187234
/**
188-
* Test translate() success with v2 API
235+
* Test translate() success with v1 API
236+
*
237+
* TEST REQUIRES VALID DEEPL AUTH KEY!!
238+
*/
239+
public function testTranslateV1Success()
240+
{
241+
if (self::$authKey === false) {
242+
$this->markTestSkipped('DeepL Auth Key (DEEPL_AUTH_KEY) is not configured.');
243+
}
244+
245+
$deepl = new DeepL(self::$authKey, 1);
246+
247+
$germanText = 'Hallo Welt';
248+
$expectedText = 'Hello World';
249+
250+
$translatedText = $deepl->translate($germanText);
251+
252+
$this->assertEquals($expectedText, $translatedText);
253+
}
254+
255+
/**
256+
* Test translate() success with default v2 API
189257
*
190258
* TEST REQUIRES VALID DEEPL AUTH KEY!!
191259
*/
192-
public function testTranslateV2Success()
260+
public function testTranslateWrongVersionSuccess()
193261
{
194262
if (self::$authKey === false) {
195263
$this->markTestSkipped('DeepL Auth Key (DEEPL_AUTH_KEY) is not configured.');
196264
}
197265

198-
$deepl = new DeepL(self::$authKey, 2);
266+
$deepl = new DeepL(self::$authKey, 3);
199267

200268
$germanText = 'Hallo Welt';
201269
$expectedText = 'Hello World';

0 commit comments

Comments
 (0)