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

Commit

Permalink
Merge pull request #14 from arkadiusjonczek/use-api-v2-as-default
Browse files Browse the repository at this point in the history
use DeepL API v2 as default
  • Loading branch information
VimS authored Aug 7, 2020
2 parents 64c4a0f + eb6da9d commit 1770d25
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 18 deletions.
20 changes: 10 additions & 10 deletions src/DeepL.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,27 @@ class DeepL
/**
* API v1 URL
*/
const API_URL_V1 = 'https://api.deepl.com/v1/translate';
const API_URL_V1 = 'https://api.deepl.com/v1/translate';

/**
* API v2 URL
*/
const API_URL_V2 = 'https://api.deepl.com/v2/translate';
const API_URL_V2 = 'https://api.deepl.com/v2/translate';

/**
* API URL: Parameter auth_key
*/
const API_URL_AUTH_KEY = 'auth_key=%s';
const API_URL_AUTH_KEY = 'auth_key=%s';

/**
* API URL: Parameter text
*/
const API_URL_TEXT = 'text=%s';
const API_URL_TEXT = 'text=%s';

/**
* API URL: Parameter source_lang
*/
const API_URL_SOURCE_LANG = 'source_lang=%s';
const API_URL_SOURCE_LANG = 'source_lang=%s';

/**
* API URL: Parameter target_lang
Expand All @@ -42,17 +42,17 @@ class DeepL
/**
* API URL: Parameter tag_handling
*/
const API_URL_TAG_HANDLING = 'tag_handling=%s';
const API_URL_TAG_HANDLING = 'tag_handling=%s';

/**
* API URL: Parameter ignore_tags
*/
const API_URL_IGNORE_TAGS = 'ignore_tags=%s';
const API_URL_IGNORE_TAGS = 'ignore_tags=%s';

/**
* API URL: Parameter formality
*/
const API_URL_FORMALITY = 'formality=%s';
const API_URL_FORMALITY = 'formality=%s';

/**
* DeepL HTTP error codes
Expand Down Expand Up @@ -132,7 +132,7 @@ class DeepL
* @param string $authKey
* @param integer $apiVersion
*/
public function __construct($authKey, $apiVersion = 1)
public function __construct($authKey, $apiVersion = 2)
{
$this->authKey = $authKey;
$this->apiVersion = $apiVersion;
Expand Down Expand Up @@ -251,7 +251,7 @@ protected function buildUrl(
$url = DeepL::API_URL_V2;
break;
default:
$url = DeepL::API_URL_V1;
$url = DeepL::API_URL_V2;
}

$url .= '?' . sprintf(DeepL::API_URL_AUTH_KEY, $this->authKey);
Expand Down
84 changes: 76 additions & 8 deletions tests/DeepLTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,14 @@ public function testBuildUrl()
{
$authKey = '123456';

$expectedString = 'https://api.deepl.com/v1/translate?' . http_build_query(array(
$expectedString = 'https://api.deepl.com/v2/translate?' . http_build_query(array(
'auth_key' => $authKey,
'source_lang' => 'de',
'target_lang' => 'en',
'formality' => 'default'
));

$deepl = new DeepL($authKey);
$deepl = new DeepL($authKey);

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

Expand All @@ -125,10 +125,33 @@ public function testBuildUrl()
/**
* Test buildUrl()
*/
public function testBuildUrlWithTags()
public function testBuildUrlV1()
{
$authKey = '123456';

$expectedString = 'https://api.deepl.com/v1/translate?' . http_build_query(array(
'auth_key' => $authKey,
'source_lang' => 'de',
'target_lang' => 'en',
'formality' => 'default'
));

$deepl = new DeepL($authKey, 1);

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

$return = $buildUrl->invokeArgs($deepl, array('de', 'en'));

$this->assertEquals($expectedString, $return);
}

/**
* Test buildUrl()
*/
public function testBuildUrlWithTags()
{
$authKey = '123456';
$expectedString = 'https://api.deepl.com/v2/translate?' . http_build_query(array(
'auth_key' => $authKey,
'source_lang' => 'de',
'target_lang' => 'en',
Expand All @@ -137,7 +160,31 @@ public function testBuildUrlWithTags()
'formality' => 'default'
));

$deepl = new DeepL($authKey);
$deepl = new DeepL($authKey);

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

$return = $buildUrl->invokeArgs($deepl, array('de', 'en', array('xml'), array('x')));

$this->assertEquals($expectedString, $return);
}

/**
* Test buildUrl()
*/
public function testBuildUrlWithTagsV1()
{
$authKey = '123456';
$expectedString = 'https://api.deepl.com/v1/translate?' . http_build_query(array(
'auth_key' => $authKey,
'source_lang' => 'de',
'target_lang' => 'en',
'tag_handling' => 'xml',
'ignore_tags' => 'x',
'formality' => 'default'
));

$deepl = new DeepL($authKey, 1);

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

Expand All @@ -164,7 +211,7 @@ public function testBuildBody()
}

/**
* Test translate() success
* Test translate() success with v2 API
*
* TEST REQUIRES VALID DEEPL AUTH KEY!!
*/
Expand All @@ -185,17 +232,38 @@ public function testTranslateSuccess()
}

/**
* Test translate() success with v2 API
* Test translate() success with v1 API
*
* TEST REQUIRES VALID DEEPL AUTH KEY!!
*/
public function testTranslateV1Success()
{
if (self::$authKey === false) {
$this->markTestSkipped('DeepL Auth Key (DEEPL_AUTH_KEY) is not configured.');
}

$deepl = new DeepL(self::$authKey, 1);

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

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

$this->assertEquals($expectedText, $translatedText);
}

/**
* Test translate() success with default v2 API
*
* TEST REQUIRES VALID DEEPL AUTH KEY!!
*/
public function testTranslateV2Success()
public function testTranslateWrongVersionSuccess()
{
if (self::$authKey === false) {
$this->markTestSkipped('DeepL Auth Key (DEEPL_AUTH_KEY) is not configured.');
}

$deepl = new DeepL(self::$authKey, 2);
$deepl = new DeepL(self::$authKey, 3);

$germanText = 'Hallo Welt';
$expectedText = 'Hello World';
Expand Down

0 comments on commit 1770d25

Please sign in to comment.