|
| 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