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

Commit

Permalink
add more tests with api v1
Browse files Browse the repository at this point in the history
  • Loading branch information
arkadiusjonczek committed Jul 19, 2020
1 parent f71b037 commit eb6da9d
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 10 deletions.
16 changes: 8 additions & 8 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
72 changes: 70 additions & 2 deletions tests/DeepLTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,30 @@ public function testBuildUrl()
'formality' => 'default'
));

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

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

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

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

/**
* Test buildUrl()
*/
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');

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 Down Expand Up @@ -205,6 +252,27 @@ public function testTranslateV1Success()
$this->assertEquals($expectedText, $translatedText);
}

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

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

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

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

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

/**
* Test translate() with tag handling success
*
Expand Down

0 comments on commit eb6da9d

Please sign in to comment.