Skip to content

Commit

Permalink
Added dictionaryLookup tests
Browse files Browse the repository at this point in the history
  • Loading branch information
arraintxo committed Sep 24, 2021
1 parent a2b7761 commit bd1a69f
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 17 deletions.
26 changes: 12 additions & 14 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="./tests/bootstrap.php" colors="true">
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="./tests/bootstrap.php" colors="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage>
<include>
<directory>./</directory>
</include>
<exclude>
<directory suffix="Interface.php">./</directory>
<directory>./Resources</directory>
<directory>./tests</directory>
<directory>./vendor</directory>
</exclude>
</coverage>
<testsuites>
<testsuite name="microsoft-translator test suite">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory>./</directory>
<exclude>
<directory>./Resources</directory>
<directory>./tests</directory>
<directory>./vendor</directory>
</exclude>
</whitelist>
</filter>
<!--
<php>
<env name="MICROSOFT_AZURE_KEY" value="www"></env>
<env name="MICROSOFT_OAUTH_CLIENT_ID" value="xxx"></env>
<env name="MICROSOFT_OAUTH_CLIENT_SECRET" value="yyy"></env>
<env name="MICROSOFT_TRANSLATOR_SAVE_AUDIO_TO" value="./" />
</php>
-->
</phpunit>
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function __construct($text, $to, $from = null)

public function getApiMethodName()
{
return '/dictionary/lookup';
return 'dictionary/lookup';
}

public function getHttpMethod()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,6 @@ public function getLanguageNames(array $languageCodes, $locale)
* @param string $text
* @param string $to
* @param string|null $from
* @param string $contentType
* @param string|null $category
* @return string
*/
public function dictionaryLookup(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace MatthiasNoback\Tests\MicrosoftTranslator\ApiCall;

use PHPUnit\Framework\TestCase;
use MatthiasNoback\MicrosoftTranslator\ApiCall;

class DictionaryLookupTest extends TestCase
{
public function testValidatesLengthOfText()
{
$text = str_repeat('t', 10001);

$this->expectException('\InvalidArgumentException');

new ApiCall\DictionaryLookup($text, 'nl');
}

public function testGetRequestToDictionaryLookupCallContent()
{
$apiCall = new ApiCall\DictionaryLookup('text', 'nl');

$this->assertSame('dictionary/lookup', $apiCall->getApiMethodName());
$this->assertSame('POST', $apiCall->getHttpMethod());
$this->assertSame([['Text' => 'text']], $apiCall->getRequestContent());
}

public function testQueryParameters()
{
$text = 'text';
$from = 'from';
$to = 'to';

$apiCall = new ApiCall\DictionaryLookup($text, $to, $from);
$this->assertEquals(array(
'from' => $from,
'to' => $to
), $apiCall->getQueryParameters());
}

public function testParseResponse()
{
$apiCall = new ApiCall\DictionaryLookup('peter', 'eu');

$response = json_encode([
['translations' => [
['text' => 'Dit is een test'],
]]
]);

$this->assertSame([['text' => 'Dit is een test']], $apiCall->parseResponse($response));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,17 @@ private function getEnvironmentVariable($name)

return getenv($name);
}

public function testDictionaryLookup()
{
$translated = $this->translator->dictionaryLookup('fly', 'es', 'en');

$this->assertGreaterThanOrEqual(2, $translated);
$this->assertSame('volar', $translated[0]['normalizedTarget']);
$this->assertSame('volar', $translated[0]['displayTarget']);
$this->assertSame('VERB', $translated[0]['posTag']);
$this->assertGreaterThanOrEqual(2, $translated[0]['backTranslations']);
}


}

0 comments on commit bd1a69f

Please sign in to comment.