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

Commit 42fa071

Browse files
committed
Breaking-Changes:
- Remove string-return of translate() - Add Exception when $tagHandling is send as array instead of String - Adjust and Add Testcases
1 parent 16eb956 commit 42fa071

File tree

3 files changed

+26
-15
lines changed

3 files changed

+26
-15
lines changed

src/DeepL.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ public function languages()
228228
* @param null $outlineDetection
229229
* @param array|null $splittingTags
230230
*
231-
* @return mixed
231+
* @return array
232232
* @throws DeepLException
233233
*
234234
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
@@ -246,6 +246,10 @@ public function translate(
246246
$outlineDetection = null,
247247
array $splittingTags = null
248248
) {
249+
if (is_array($tagHandling)) {
250+
throw new \InvalidArgumentException('$tagHandling must be of type String in V2 of DeepLLibrary');
251+
}
252+
249253
$paramsArray = array(
250254
'text' => $text,
251255
'source_lang' => $sourceLanguage,
@@ -269,13 +273,6 @@ public function translate(
269273

270274
// request the DeepL API
271275
$translationsArray = $this->request($url, $body);
272-
$translationsCount = count($translationsArray['translations']);
273-
274-
if ($translationsCount === 0) {
275-
throw new DeepLException('No translations found.');
276-
} elseif ($translationsCount === 1) {
277-
return $translationsArray['translations'][0]['text'];
278-
}
279276

280277
return $translationsArray['translations'];
281278
}

tests/integration/DeepLApiTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public function testTranslateSuccess()
7272

7373
$translatedText = $deepl->translate($germanText);
7474

75-
$this->assertEquals($expectedText, $translatedText);
75+
$this->assertEquals($expectedText, $translatedText[0]['text']);
7676
}
7777

7878
/**
@@ -91,7 +91,7 @@ public function testTranslateV1Success()
9191

9292
$translatedText = $deepl->translate($germanText);
9393

94-
$this->assertEquals($expectedText, $translatedText);
94+
$this->assertEquals($expectedText, $translatedText[0]['text']);
9595
}
9696

9797
/**
@@ -131,7 +131,7 @@ public function testTranslateTagHandlingSuccess()
131131
'xml'
132132
);
133133

134-
$this->assertEquals($expectedText, $translatedText);
134+
$this->assertEquals($expectedText, $translatedText[0]['text']);
135135
}
136136

137137
/**
@@ -156,7 +156,7 @@ public function testTranslateIgnoreTagsSuccess()
156156
array('strong')
157157
);
158158

159-
$this->assertEquals($expectedText, $translatedText);
159+
$this->assertEquals($expectedText, $translatedText[0]['text']);
160160
}
161161

162162
/**
@@ -221,7 +221,7 @@ public function testTranslateWithAllParams()
221221
array('p','br') //$splittingTags
222222
);
223223

224-
$this->assertEquals($expectedText, $translatedText);
224+
$this->assertEquals($expectedText, $translatedText[0]['text']);
225225
}
226226

227227
/**
@@ -246,7 +246,7 @@ public function testTranslateFormality()
246246
'less' //$formality
247247
);
248248

249-
$this->assertEquals($expectedText, $translatedText);
249+
$this->assertEquals($expectedText, $translatedText[0]['text']);
250250
}
251251

252252
/**

tests/unit/DeepLTest.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public function testBuildBaseUrlHost()
123123

124124

125125
/**
126-
* Test buildQuery with empty Arguemtns
126+
* Test buildQuery with empty Arguments
127127
*/
128128
public function testBuildQueryWithNulledArguments()
129129
{
@@ -481,4 +481,18 @@ public function testRemoveEmptyParamsAllArgumentsAndOutlineDetectionOne()
481481

482482
$this->assertEquals($expectation, $return);
483483
}
484+
485+
/**
486+
* Test behaviour of translate() when tagHandling is an array
487+
*/
488+
public function testTranslateExceptionTagHandling()
489+
{
490+
$authKey = '123456';
491+
$germanText = 'Hallo Welt';
492+
$deepl = new DeepL($authKey);
493+
494+
$this->setExpectedException('InvalidArgumentException');
495+
496+
$deepl->translate($germanText, 'de', 'en', array('xml'));
497+
}
484498
}

0 commit comments

Comments
 (0)