Skip to content

Commit

Permalink
Merge pull request #40 from resslinger/patch-4
Browse files Browse the repository at this point in the history
fix tests
  • Loading branch information
introwit authored Apr 20, 2022
2 parents e7f512b + 239e1b7 commit 60ceb66
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
6 changes: 4 additions & 2 deletions src/GoogleTranslate.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,18 +131,20 @@ public function getAvaliableTranslationsFor(string $languageCode): array
->getAvaliableTranslationsFor($languageCode);
}

public function unlessLanguageIs(string $languageCode, string $input, $to = null)
public function unlessLanguageIs(string $languageCode, string $input, $from = null, $to = null)
{
$translateFrom = $from ?? config('googletranslate.default_source_translation');
$translateTo = $to ?? config('googletranslate.default_target_translation');

$translateFrom = $this->sanitizeLanguageCode($translateFrom);
$translateTo = $this->sanitizeLanguageCode($translateTo);

$languageCode = $this->sanitizeLanguageCode($languageCode);

$languageMisMatch = $languageCode != $this->detectLanguage($input)['language_code'];

if ($languageMisMatch) {
return $this->translate($input, $translateTo);
return $this->translate($input, $translateFrom, $translateTo);
}

return $input;
Expand Down
24 changes: 10 additions & 14 deletions tests/GoogleTranslateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

namespace JoggApp\GoogleTranslate\Tests;

use Illuminate\Support\Facades\Config;
use JoggApp\GoogleTranslate\GoogleTranslate;
use JoggApp\GoogleTranslate\GoogleTranslateClient;
use Mockery;
use PHPUnit\Framework\TestCase;
use Orchestra\Testbench\TestCase as BaseTestCase;

class GoogleTranslateTest extends TestCase
class GoogleTranslateTest extends BaseTestCase
{
public $testString = 'A test string';
public $testHtmlString = '<p>A test string</p>';
Expand All @@ -16,20 +17,15 @@ class GoogleTranslateTest extends TestCase

private $translate;

public function setUp(): void
public function __construct()
{
parent::setUp();
parent::__construct();

$this->translateClient = Mockery::mock(GoogleTranslateClient::class);

$this->translate = new GoogleTranslate($this->translateClient);
}

public function tearDown(): void
{
Mockery::close();
}

/** @test */
public function it_can_detect_the_language_of_string_passed_to_it()
{
Expand Down Expand Up @@ -135,11 +131,11 @@ public function it_can_translate_an_array_of_strings_passed_to_it()
public function test_the_just_translate_method_returns_just_the_translated_string()
{
$this->translateClient
->shouldReceive('translate')->with($this->testString, 'en')
->shouldReceive('translate')->with($this->testString, 'en', 'hi')
->once()
->andReturn(['text' => 'A test string']);

$response = $this->translate->justTranslate($this->testString, 'en');
$response = $this->translate->justTranslate($this->testString, 'en', 'hi');

$this->assertEquals('A test string', $response);
}
Expand All @@ -152,7 +148,7 @@ public function test_the_unless_language_is_method_does_not_translate_the_langua
->once()
->andReturn(['languageCode' => 'en', 'confidence' => '']);

$response = $this->translate->unlessLanguageIs('en', $this->testString, 'hi');
$response = $this->translate->unlessLanguageIs('en', $this->testString, 'hi', 'en');

$this->assertEquals($this->testString, $response);
}
Expand All @@ -166,11 +162,11 @@ public function test_the_unless_language_is_method_translates_the_language_of_gi
->andReturn(['languageCode' => 'en', 'confidence' => '']);

$this->translateClient
->shouldReceive('translate')->with($this->testString, 'hi', 'text')
->shouldReceive('translate')->with($this->testString, 'hi', 'en', 'text')
->once()
->andReturn(['source' => 'en', 'text' => '']);

$response = $this->translate->unlessLanguageIs('hi', $this->testString, 'hi');
$response = $this->translate->unlessLanguageIs('hi', $this->testString, 'hi', 'en');

$this->assertIsArray($response);

Expand Down

0 comments on commit 60ceb66

Please sign in to comment.