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

Commit 8f74d00

Browse files
committed
- remove $errorCodes-Array and add Messages from DeepL-Response to DeepLException
- fix conversion from array to url-param - change behaviour for $outlineDetection since DeepL-Api only needs it when it is disabled - fix some missing Doc-Blocks - Split Unit-(=local) and Integration(=api)-Tests - add some Testcases discovered while manual testing //I really want to refactor the Tests using Dataproviders!
1 parent d68ee9d commit 8f74d00

File tree

3 files changed

+334
-210
lines changed

3 files changed

+334
-210
lines changed

src/DeepL.php

Lines changed: 23 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ class DeepL
3131
*/
3232
const API_URL_RESOURCE_LANGUAGES = 'languages';
3333

34-
3534
/**
3635
* API URL: Parameter text
3736
*/
@@ -87,19 +86,6 @@ class DeepL
8786
*/
8887
const API_URL_SPLITTING_TAGS = 'splitting_tags=%s';
8988

90-
/**
91-
* DeepL HTTP error codes
92-
*
93-
* @var array
94-
*/
95-
protected $errorCodes = array(
96-
400 => 'Wrong request, please check error message and your parameters.',
97-
403 => 'Authorization failed. Please supply a valid auth_key parameter.',
98-
413 => 'Request Entity Too Large. The request size exceeds the current limit.',
99-
429 => 'Too many requests. Please wait and send your request once again.',
100-
456 => 'Quota exceeded. The character limit has been reached.',
101-
);
102-
10389
/**
10490
* Supported translation source languages
10591
*
@@ -297,12 +283,17 @@ protected function checkLanguages($sourceLanguage, $destinationLanguage)
297283
/**
298284
* Build the URL for the DeepL API request
299285
*
300-
* @param string $sourceLanguage
301-
* @param string $destinationLanguage
302-
* @param array $tagHandling
303-
* @param array $ignoreTags
304-
* @param string $formality
305-
* @param string $resource
286+
* @param string $sourceLanguage
287+
* @param string $destinationLanguage
288+
* @param string $tagHandling
289+
* @param array|null $ignoreTags
290+
* @param string $formality
291+
* @param string $resource
292+
* @param null $splitSentences
293+
* @param null $preserveFormatting
294+
* @param array|null $nonSplittingTags
295+
* @param null $outlineDetection
296+
* @param array|null $splittingTags
306297
*
307298
* @return string
308299
*
@@ -316,11 +307,11 @@ protected function buildUrl(
316307
$tagHandling = null,
317308
array $ignoreTags = null,
318309
$formality = 'default',
319-
$resource = 'translate',
310+
$resource = self::API_URL_RESOURCE_TRANSLATE,
320311
$splitSentences = null,
321312
$preserveFormatting = null,
322313
array $nonSplittingTags = null,
323-
$outlineDetection = null,
314+
$outlineDetection = 1,
324315
array $splittingTags = null
325316
) {
326317
$url = $this->buildBaseUrl($resource);
@@ -338,7 +329,7 @@ protected function buildUrl(
338329
}
339330

340331
if (false === empty($ignoreTags)) {
341-
$url .= '&'.sprintf(self::API_URL_IGNORE_TAGS, implode(',', $ignoreTags));
332+
$url .= '&'.sprintf(self::API_URL_IGNORE_TAGS, urlencode(implode(',', $ignoreTags)));
342333
}
343334

344335
if (false === empty($formality)) {
@@ -354,15 +345,15 @@ protected function buildUrl(
354345
}
355346

356347
if (false === empty($nonSplittingTags)) {
357-
$url .= '&'.sprintf(self::API_URL_NON_SPLITTING_TAGS, implode(',', $nonSplittingTags));
348+
$url .= '&'.sprintf(self::API_URL_NON_SPLITTING_TAGS, urlencode(implode(',', $nonSplittingTags)));
358349
}
359350

360-
if (false === empty($outlineDetection)) {
351+
if (0 === $outlineDetection) {
361352
$url .= '&'.sprintf(self::API_URL_OUTLINE_DETECTION, $outlineDetection);
362353
}
363354

364355
if (false === empty($splittingTags)) {
365-
$url .= '&'.sprintf(self::API_URL_SPLITTING_TAGS, implode(',', $splittingTags));
356+
$url .= '&'.sprintf(self::API_URL_SPLITTING_TAGS, urlencode(implode(',', $splittingTags)));
366357
}
367358

368359
return $url;
@@ -437,20 +428,18 @@ protected function request($url, $body)
437428
if (curl_errno($this->curl)) {
438429
throw new DeepLException('There was a cURL Request Error.');
439430
}
431+
$httpCode = curl_getinfo($this->curl, CURLINFO_HTTP_CODE);
432+
$responseArray = json_decode($response, true);
440433

441-
$httpCode = curl_getinfo($this->curl, CURLINFO_HTTP_CODE);
442-
443-
if ($httpCode != 200 && array_key_exists($httpCode, $this->errorCodes)) {
444-
throw new DeepLException($this->errorCodes[$httpCode], $httpCode);
434+
if ($httpCode != 200) {
435+
throw new DeepLException($responseArray['message'], $httpCode);
445436
}
446437

447-
$translationsArray = json_decode($response, true);
448-
449-
if (!$translationsArray) {
438+
if (false === is_array($responseArray)) {
450439
throw new DeepLException('The Response seems to not be valid JSON.');
451440
}
452441

453-
return $translationsArray;
442+
return $responseArray;
454443
}
455444

456445
/**

tests/integration/DeepLApiTest.php

Lines changed: 276 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,276 @@
1+
<?php
2+
3+
namespace BabyMarkt\DeepL\integration;
4+
5+
use BabyMarkt\DeepL\DeepL;
6+
use PHPUnit_Framework_TestCase;
7+
use ReflectionClass;
8+
9+
/**
10+
* Class DeepLTest
11+
*
12+
* @package BabyMarkt\DeepL
13+
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
14+
*/
15+
class DeepLApiTest extends PHPUnit_Framework_TestCase
16+
{
17+
/**
18+
* DeepL Auth Key.
19+
*
20+
* @var bool|string
21+
*/
22+
protected static $authKey = false;
23+
24+
/**
25+
* Setup DeepL Auth Key.
26+
*/
27+
public static function setUpBeforeClass()
28+
{
29+
parent::setUpBeforeClass();
30+
31+
$authKey = getenv('DEEPL_AUTH_KEY');
32+
33+
if ($authKey === false) {
34+
return;
35+
}
36+
37+
self::$authKey = $authKey;
38+
}
39+
40+
/**
41+
* Get protected method
42+
*
43+
* @param $className
44+
* @param $methodName
45+
*
46+
* @throws \ReflectionException
47+
*
48+
* @return \ReflectionMethod
49+
*/
50+
protected static function getMethod($className, $methodName)
51+
{
52+
$class = new ReflectionClass($className);
53+
$method = $class->getMethod($methodName);
54+
$method->setAccessible(true);
55+
56+
return $method;
57+
}
58+
59+
/**
60+
* Test translate() success with v2 API
61+
*/
62+
public function testTranslateSuccess()
63+
{
64+
if (self::$authKey === false) {
65+
$this->markTestSkipped('DeepL Auth Key (DEEPL_AUTH_KEY) is not configured.');
66+
}
67+
68+
$deepl = new DeepL(self::$authKey);
69+
70+
$germanText = 'Hallo Welt';
71+
$expectedText = 'Hello World';
72+
73+
$translatedText = $deepl->translate($germanText);
74+
75+
$this->assertEquals($expectedText, $translatedText);
76+
}
77+
78+
/**
79+
* Test translate() success with v1 API
80+
*/
81+
public function testTranslateV1Success()
82+
{
83+
if (self::$authKey === false) {
84+
$this->markTestSkipped('DeepL Auth Key (DEEPL_AUTH_KEY) is not configured.');
85+
}
86+
87+
$deepl = new DeepL(self::$authKey, 1);
88+
89+
$germanText = 'Hallo Welt';
90+
$expectedText = 'Hello World';
91+
92+
$translatedText = $deepl->translate($germanText);
93+
94+
$this->assertEquals($expectedText, $translatedText);
95+
}
96+
97+
/**
98+
* Test translate() success with default v2 API
99+
*/
100+
public function testTranslateWrongVersion()
101+
{
102+
if (self::$authKey === false) {
103+
$this->markTestSkipped('DeepL Auth Key (DEEPL_AUTH_KEY) is not configured.');
104+
}
105+
$germanText = 'Hallo Welt';
106+
$deepl = new DeepL(self::$authKey, 3);
107+
108+
$this->setExpectedException('\BabyMarkt\DeepL\DeepLException');
109+
110+
$deepl->translate($germanText);
111+
}
112+
113+
/**
114+
* Test translate() with tag handling success
115+
*/
116+
public function testTranslateTagHandlingSuccess()
117+
{
118+
if (self::$authKey === false) {
119+
$this->markTestSkipped('DeepL Auth Key (DEEPL_AUTH_KEY) is not configured.');
120+
}
121+
122+
$deepl = new DeepL(self::$authKey);
123+
124+
$englishText = '<strong>text to translate</strong>';
125+
$expectedText = '<strong>zu übersetzender Text</strong>';
126+
127+
$translatedText = $deepl->translate(
128+
$englishText,
129+
'en',
130+
'de',
131+
'xml'
132+
);
133+
134+
$this->assertEquals($expectedText, $translatedText);
135+
}
136+
137+
/**
138+
* Test translate() with tag ignored success
139+
*/
140+
public function testTranslateIgnoreTagsSuccess()
141+
{
142+
if (self::$authKey === false) {
143+
$this->markTestSkipped('DeepL Auth Key (DEEPL_AUTH_KEY) is not configured.');
144+
}
145+
146+
$deepl = new DeepL(self::$authKey);
147+
148+
$englishText = '<strong>text to do not translate</strong><p>text to translate</p>';
149+
$expectedText = '<strong>text to do not translate</strong><p>zu übersetzender Text</p>';
150+
151+
$translatedText = $deepl->translate(
152+
$englishText,
153+
'en',
154+
'de',
155+
'xml',
156+
array('strong')
157+
);
158+
159+
$this->assertEquals($expectedText, $translatedText);
160+
}
161+
162+
/**
163+
* Test usage() has certain array-keys
164+
*/
165+
public function testUsage()
166+
{
167+
if (self::$authKey === false) {
168+
$this->markTestSkipped('DeepL Auth Key (DEEPL_AUTH_KEY) is not configured.');
169+
}
170+
171+
$deepl = new DeepL(self::$authKey);
172+
$response = $deepl->usage();
173+
174+
$this->assertArrayHasKey('character_count', $response);
175+
$this->assertArrayHasKey('character_limit', $response);
176+
}
177+
178+
/**
179+
* Test languages() has certain array-keys
180+
*/
181+
public function testLanguages()
182+
{
183+
if (self::$authKey === false) {
184+
$this->markTestSkipped('DeepL Auth Key (DEEPL_AUTH_KEY) is not configured.');
185+
}
186+
187+
$deepl = new DeepL(self::$authKey);
188+
$response = $deepl->languages();
189+
190+
foreach ($response as $language) {
191+
$this->assertArrayHasKey('language', $language);
192+
$this->assertArrayHasKey('name', $language);
193+
}
194+
}
195+
196+
/**
197+
* Test translate() with all Params
198+
*/
199+
public function testTranslateWithAllParams()
200+
{
201+
if (self::$authKey === false) {
202+
$this->markTestSkipped('DeepL Auth Key (DEEPL_AUTH_KEY) is not configured.');
203+
}
204+
205+
$deepl = new DeepL(self::$authKey);
206+
207+
$englishText = '<strong>text to do not translate</strong><p>please translate this text</p>';
208+
$expectedText = '<strong>text to do not translate</strong><p>bitte übersetzen Sie diesen Text</p>';
209+
210+
$translatedText = $deepl->translate(
211+
$englishText,
212+
'en', //$sourceLanguage
213+
'de', //$destinationLanguage
214+
'xml', //$tagHandling
215+
array('html','html5','html6'), //$ignoreTags
216+
'more', //$formality
217+
'translate', //$resource
218+
'nonewlines', //$splitSentences
219+
1, //$preserveFormatting
220+
array('href','href2'), //$nonSplittingTags
221+
0, //$outlineDetection
222+
array('p','br') //$splittingTags
223+
);
224+
225+
$this->assertEquals($expectedText, $translatedText);
226+
}
227+
228+
/**
229+
* Test translate() $formality
230+
*/
231+
public function testTranslateFormality()
232+
{
233+
if (self::$authKey === false) {
234+
$this->markTestSkipped('DeepL Auth Key (DEEPL_AUTH_KEY) is not configured.');
235+
}
236+
237+
$deepl = new DeepL(self::$authKey);
238+
239+
$englishText = '<strong>text to do not translate</strong><p>please translate this text</p>';
240+
$expectedText = '<stark>nicht zu übersetzender Text</stark><p>bitte diesen Text übersetzen</p>';
241+
$translatedText = $deepl->translate(
242+
$englishText,
243+
'en', //$sourceLanguage
244+
'de', //$destinationLanguage
245+
null, //$tagHandling
246+
null, //$ignoreTags
247+
'less' //$formality
248+
);
249+
250+
$this->assertEquals($expectedText, $translatedText);
251+
}
252+
253+
/**
254+
* Test translate() $formality
255+
*/
256+
public function testTranslateFormalityFail()
257+
{
258+
if (self::$authKey === false) {
259+
$this->markTestSkipped('DeepL Auth Key (DEEPL_AUTH_KEY) is not configured.');
260+
}
261+
262+
$deepl = new DeepL(self::$authKey);
263+
$englishText = '<strong>text to do not translate</strong><p>please translate this text</p>';
264+
265+
$this->setExpectedException('\BabyMarkt\DeepL\DeepLException');
266+
267+
$deepl->translate(
268+
$englishText,
269+
'en', //$sourceLanguage
270+
'es', //$destinationLanguage
271+
null, //$tagHandling
272+
null, //$ignoreTags
273+
'more' //$formality
274+
);
275+
}
276+
}

0 commit comments

Comments
 (0)