Skip to content

Commit

Permalink
Tests prepared
Browse files Browse the repository at this point in the history
  • Loading branch information
andrey-helldar committed Jun 26, 2024
1 parent ace7c0f commit 5c3f272
Show file tree
Hide file tree
Showing 17 changed files with 207 additions and 74 deletions.
2 changes: 1 addition & 1 deletion src/Contracts/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ interface Translator
{
public function can(Locale|string $to): bool;

public function text(array|string $text, Locale|string $to, Locale|string|null $from = null): array|string;
public function translate(array|string $text, Locale|string $to, Locale|string|null $from = null): array|string;
}
8 changes: 4 additions & 4 deletions src/Facades/Translate.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
use LaravelLang\Translator\Services\Translate as TranslateService;

/**
* @method static array|string text(iterable|string $text, Locale|string $to, Locale|string|null $from)
* @method static array|string viaDeepl(iterable|string $text, Locale|string $to, Locale|string|null $from)
* @method static array|string viaGoogleFree(iterable|string $text, Locale|string $to, Locale|string|null $from)
* @method static array|string viaYandex(iterable|string $text, Locale|string $to, Locale|string|null $from)
* @method static array|string text(iterable|string $text, Locale|string $to, Locale|string|null $from = null)
* @method static array|string viaDeepl(iterable|string $text, Locale|string $to, Locale|string|null $from = null)
* @method static array|string viaGoogleFree(iterable|string $text, Locale|string $to, Locale|string|null $from = null)
* @method static array|string viaYandex(iterable|string $text, Locale|string $to, Locale|string|null $from = null)
*/
class Translate extends Facade
{
Expand Down
2 changes: 1 addition & 1 deletion src/Integrations/Deepl.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function __construct(
protected array $options
) {}

protected function translate(iterable|string $text, Locale|string $to, Locale|string|null $from): Collection
protected function request(iterable|string $text, Locale|string $to, Locale|string|null $from): Collection
{
return collect($this->translator->translateText($text, $this->lang($from), $this->lang($to), $this->options));
}
Expand Down
6 changes: 3 additions & 3 deletions src/Integrations/GoogleFree.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ class GoogleFree extends Integration

public function __construct(
protected GoogleTranslate $translator,
protected array $options,
protected bool|string $preserve
protected array $options = [],
protected bool|string $preserve = true
) {}

protected function translate(iterable|string $text, Locale|string $to, Locale|string|null $from): Collection
protected function request(iterable|string $text, Locale|string $to, Locale|string|null $from): Collection
{
return collect($text)->map(
fn (string $value) => $this->translator($to, $from)->translate($value)
Expand Down
8 changes: 4 additions & 4 deletions src/Integrations/Integration.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ abstract class Integration implements Translator
{
protected array $map = [];

abstract protected function translate(
abstract protected function request(
iterable|string $text,
Locale|string $to,
Locale|string|null $from
Expand All @@ -23,14 +23,14 @@ public function can(Locale|string $to): bool
return $this->lang($to) !== null;
}

public function text(
public function translate(
array|string $text,
Locale|string $to,
Locale|string|null $from = null
): array|string {
return is_array($text)
? $this->translate($text, $to, $from)->pluck('text')->all()
: $this->translate($text, $to, $from)->pluck('text')->first();
? $this->request($text, $to, $from)->pluck('text')->all()
: $this->request($text, $to, $from)->pluck('text')->first();
}

protected function lang(Locale|string|null $lang): ?string
Expand Down
2 changes: 1 addition & 1 deletion src/Integrations/Yandex.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function __construct(
protected YandexCloud $translator,
) {}

protected function translate(iterable|string $text, Locale|string $to, Locale|string|null $from): Collection
protected function request(iterable|string $text, Locale|string $to, Locale|string|null $from): Collection
{
return collect($this->translator->translate($text, $this->lang($to), $this->lang($from)));
}
Expand Down
15 changes: 9 additions & 6 deletions src/Services/Translate.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

class Translate
{
public function text(iterable|string $text, Locale|string $to, Locale|string|null $from): array|string
public function text(iterable|string $text, Locale|string $to, Locale|string|null $from = null): array|string
{
foreach ($this->translators() as $class) {
if ($translated = $this->translate($class, $text, $to, $from)) {
Expand All @@ -23,17 +23,20 @@ public function text(iterable|string $text, Locale|string $to, Locale|string|nul
return $text;
}

public function viaDeepl(iterable|string $text, Locale|string $to, Locale|string|null $from): array|string
public function viaDeepl(iterable|string $text, Locale|string $to, Locale|string|null $from = null): array|string
{
return $this->via(Deepl::class, $text, $to, $from);
}

public function viaGoogleFree(iterable|string $text, Locale|string $to, Locale|string|null $from): array|string
{
public function viaGoogleFree(
iterable|string $text,
Locale|string $to,
Locale|string|null $from = null
): array|string {
return $this->via(GoogleFree::class, $text, $to, $from);
}

public function viaYandex(iterable|string $text, Locale|string $to, Locale|string|null $from): array|string
public function viaYandex(iterable|string $text, Locale|string $to, Locale|string|null $from = null): array|string
{
return $this->via(Yandex::class, $text, $to, $from);
}
Expand All @@ -56,7 +59,7 @@ protected function translate(
$translator = $this->initialize($translator);

if ($translator->can($to)) {
return $translator->text($text, $to, $from);
return $translator->translate($text, $to, $from);
}

return null;
Expand Down
13 changes: 13 additions & 0 deletions tests/Concerns/Value.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Tests\Concerns;

class Value
{
public const Text1English = 'The :attribute must be accepted.';
public const Text1French = 'Le champ :attribute doit être accepté.';
public const Text2English = 'The :attribute must be a date after :date.';
public const Text2French = 'Le champ :attribute doit être une date postérieure au :date.';
}
13 changes: 13 additions & 0 deletions tests/Datasets/Translators.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

use LaravelLang\Translator\Integrations\Deepl;
use LaravelLang\Translator\Integrations\GoogleFree;
use LaravelLang\Translator\Integrations\Yandex;

dataset('translators', fn () => [
Deepl::class => [Deepl::class],
GoogleFree::class => [GoogleFree::class],
Yandex::class => [Yandex::class],
]);
5 changes: 0 additions & 5 deletions tests/Feature/ExampleTest.php

This file was deleted.

10 changes: 10 additions & 0 deletions tests/Helpers/Translators.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

use LaravelLang\Translator\Contracts\Translator;

function translator(string $class): Translator
{
return app($class);
}
44 changes: 0 additions & 44 deletions tests/Pest.php
Original file line number Diff line number Diff line change
@@ -1,45 +1 @@
<?php

/*
|--------------------------------------------------------------------------
| Test Case
|--------------------------------------------------------------------------
|
| The closure you provide to your test functions is always bound to a specific PHPUnit test
| case class. By default, that class is "PHPUnit\Framework\TestCase". Of course, you may
| need to change it using the "uses()" function to bind a different classes or traits.
|
*/

// uses(Tests\TestCase::class)->in('Feature');

/*
|--------------------------------------------------------------------------
| Expectations
|--------------------------------------------------------------------------
|
| When you're writing tests, you often need to check that values meet certain conditions. The
| "expect()" function gives you access to a set of "expectations" methods that you can use
| to assert different things. Of course, you may extend the Expectation API at any time.
|
*/

expect()->extend('toBeOne', function () {
return $this->toBe(1);
});

/*
|--------------------------------------------------------------------------
| Functions
|--------------------------------------------------------------------------
|
| While Pest is very powerful out-of-the-box, you may have some testing code specific to your
| project that you don't want to repeat in every file. Here you can also expose helpers as
| global functions to help you to reduce the number of lines of code in your test files.
|
*/

function something()
{
// ..
}
18 changes: 18 additions & 0 deletions tests/Unit/Can/IntegrationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

use LaravelLang\LocaleList\Locale;

test('can be translatable', function (string $translator) {
$translator = translator($translator);

expect($translator->can('fr'))->toBeTrue();
expect($translator->can(Locale::French))->toBeTrue();
})->with('translators');

test('cannot be translatable', function (string $translator) {
$translator = translator($translator);

expect($translator->can('qwerty'))->toBeTrue();
})->with('translators');
5 changes: 0 additions & 5 deletions tests/Unit/ExampleTest.php

This file was deleted.

37 changes: 37 additions & 0 deletions tests/Unit/Translate/FacadeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

use LaravelLang\LocaleList\Locale;
use LaravelLang\Translator\Facades\Translate;
use Tests\Concerns\Value;

test('translate', function () {
expect(Translate::text(Value::Text1English, Locale::French))->toBe(
Value::Text1French
);
});

test('cannot be translated', function () {
expect(Translate::text(Value::Text1English, 'qwerty'))->toBe(
Value::Text1English
);
});

test('via deepl', function () {
expect(Translate::viaDeepl(Value::Text1English, Locale::French))->toBe(
Value::Text1French
);
});

test('via google free', function () {
expect(Translate::viaGoogleFree(Value::Text1English, Locale::French))->toBe(
Value::Text1French
);
});

test('via yandex', function () {
expect(Translate::viaYandex(Value::Text1English, Locale::French))->toBe(
Value::Text1French
);
});
47 changes: 47 additions & 0 deletions tests/Unit/Translate/IntegrationCanTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

use LaravelLang\LocaleList\Locale;
use Tests\Concerns\Value;

test('as string', function (string $translator) {
$translator = translator($translator);

expect(
$translator->translate(Value::Text1English, Locale::French)
)->toBe(Value::Text1French);
})->with('translators');

test('as array without keys', function (string $translator) {
$translator = translator($translator);

expect($translator->translate([Value::Text1English, Value::Text2English], Locale::French))->toBe([
Value::Text1French,
Value::Text2French,
]);
})->with('translators');

test('as array with keys', function (string $translator) {
$translator = translator($translator);

expect($translator->translate([
'foo' => Value::Text1English,
'bar' => Value::Text2English,
], Locale::French))->toBe([
'foo' => Value::Text1French,
'bar' => Value::Text2French,
]);
})->with('translators');

test('as collection', function (string $translator) {
$translator = translator($translator);

expect($translator->translate(collect([
'foo' => Value::Text1English,
'bar' => Value::Text2English,
]), Locale::French))->toBe([
'foo' => Value::Text1French,
'bar' => Value::Text2French,
]);
})->with('translators');
46 changes: 46 additions & 0 deletions tests/Unit/Translate/IntegrationCannotTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

use Tests\Concerns\Value;

test('as string', function (string $translator) {
$translator = translator($translator);

expect(
$translator->translate(Value::Text1English, 'qwerty')
)->toBe(Value::Text1English);
})->with('translators');

test('as array without keys', function (string $translator) {
$translator = translator($translator);

expect($translator->translate([Value::Text1English, Value::Text2English], 'qwerty'))->toBe([
Value::Text1English,
Value::Text2English,
]);
})->with('translators');

test('as array with keys', function (string $translator) {
$translator = translator($translator);

expect($translator->translate([
'foo' => Value::Text1English,
'bar' => Value::Text2English,
], 'qwerty'))->toBe([
'foo' => Value::Text1English,
'bar' => Value::Text2English,
]);
})->with('translators');

test('as collection', function (string $translator) {
$translator = translator($translator);

expect($translator->translate(collect([
'foo' => Value::Text1English,
'bar' => Value::Text2English,
]), 'qwerty'))->toBe([
'foo' => Value::Text1English,
'bar' => Value::Text2English,
]);
})->with('translators');

0 comments on commit 5c3f272

Please sign in to comment.