Skip to content

Commit

Permalink
Merge pull request #4 from gnahotelsolutions/develop
Browse files Browse the repository at this point in the history
Add tests
  • Loading branch information
Lloople authored Oct 11, 2022
2 parents 789a962 + b660353 commit 15d39af
Show file tree
Hide file tree
Showing 17 changed files with 76 additions and 224 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
fail-fast: true
matrix:
os: [ubuntu-latest, windows-latest]
php: [7.4, 8.0]
php: [7.2, 8.0]
laravel: [8.*]
stability: [prefer-lowest, prefer-stable]
include:
Expand Down
Empty file removed .idea/.gitignore
Empty file.
106 changes: 0 additions & 106 deletions .idea/blade.xml

This file was deleted.

12 changes: 0 additions & 12 deletions .idea/laravel-google-translate.iml

This file was deleted.

10 changes: 0 additions & 10 deletions .idea/laravel-idea-personal.xml

This file was deleted.

7 changes: 0 additions & 7 deletions .idea/laravel-idea.xml

This file was deleted.

8 changes: 0 additions & 8 deletions .idea/modules.xml

This file was deleted.

4 changes: 0 additions & 4 deletions .idea/php.xml

This file was deleted.

6 changes: 0 additions & 6 deletions .idea/vcs.xml

This file was deleted.

58 changes: 0 additions & 58 deletions .idea/workspace.xml

This file was deleted.

2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"GNAHotelSolutions\\LaravelGoogleTranslate\\GoogleTranslateServiceProvider"
],
"aliases": {
"GoogleTranslate": "GNAHotelSolutions\\LaravelGoogleTranslate\\GoogleTranslate"
"GoogleTranslate": "GNAHotelSolutions\\LaravelGoogleTranslate\\Facade\\GoogleTranslate"
}
}
}
Expand Down
29 changes: 29 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
verbose="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">src/</directory>
</whitelist>
</filter>
<logging>
<log type="tap" target="build/report.tap"/>
<log type="junit" target="build/report.junit.xml"/>
<log type="coverage-html" target="build/coverage"/>
<log type="coverage-text" target="build/coverage.txt"/>
<log type="coverage-clover" target="build/logs/clover.xml"/>
</logging>
</phpunit>
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?php

namespace GNAHotelSolutions\LaravelGoogleTranslate;
namespace GNAHotelSolutions\LaravelGoogleTranslate\Facade;

use Illuminate\Support\Facades\Facade;

/**
* @see \GNAHotelSolutions\LaravelGoogleTranslate\GoogleTranslate
*/
class GoogleTranslateFacade extends Facade
class GoogleTranslate extends Facade
{
/**
* Get the registered name of the component.
Expand Down
2 changes: 1 addition & 1 deletion src/GoogleTranslate.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function to(string $locale): self

public function translate(string $text): string
{
return (new TranslateClient(['key' => config('google-translate.key')]))
return app(TranslateClient::class)
->translate($text, [
'source' => $this->from,
'target' => $this->to
Expand Down
13 changes: 5 additions & 8 deletions src/GoogleTranslateServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@

namespace GNAHotelSolutions\LaravelGoogleTranslate;

use Google\Cloud\Translate\V2\TranslateClient;
use Illuminate\Support\ServiceProvider;

class GoogleTranslateServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*/
public function boot(): void
{
if ($this->app->runningInConsole()) {
Expand All @@ -19,17 +17,16 @@ public function boot(): void
}
}

/**
* Register the application services.
*/
public function register(): void
{
// Automatically apply the package configuration
$this->mergeConfigFrom(__DIR__.'/../config/config.php', 'google-translate');

// Register the main class to use with the facade
$this->app->singleton('google-translate', function () {
return new GoogleTranslate;
});

$this->app->singleton(TranslateClient::class, function () {
return new TranslateClient(['key' => config('google-translate.key')]);
});
}
}
23 changes: 23 additions & 0 deletions tests/GoogleTranslateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace GNAHotelSolutions\LaravelGoogleTranslate\Tests;

use GNAHotelSolutions\LaravelGoogleTranslate\Facade\GoogleTranslate;
use Google\Cloud\Translate\V2\TranslateClient;

class GoogleTranslateTest extends TestCase
{
public function test_it_calls_google_package(): void
{
app()->singleton(TranslateClient::class, function () {
return $this->mock(TranslateClient::class, function ($mock) {
$mock->shouldReceive('translate')
->withArgs(['Hello there!', ['source' => 'en', 'target' => 'es']])
->once()
->andReturn(['text' => 'Hola ahí!']);
});
});

$this->assertEquals('Hola ahí!', GoogleTranslate::from('en')->to('es')->translate('Hello there!'));
}
}
14 changes: 14 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace GNAHotelSolutions\LaravelGoogleTranslate\Tests;

use GNAHotelSolutions\LaravelGoogleTranslate\GoogleTranslateServiceProvider;
use Orchestra\Testbench\TestCase as OrchestraTestCase;

class TestCase extends OrchestraTestCase
{
protected function getPackageProviders($app): array
{
return [GoogleTranslateServiceProvider::class];
}
}

0 comments on commit 15d39af

Please sign in to comment.