Skip to content
This repository has been archived by the owner on May 28, 2023. It is now read-only.

Commit

Permalink
New tests, added Orchestra testbench and configured functions to alwa…
Browse files Browse the repository at this point in the history
…ys return translator object even on failure
  • Loading branch information
percymamedy committed Feb 6, 2016
1 parent 63f176c commit 6f778c2
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 45 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"require-dev": {
"fzaninotto/faker": "~1.4",
"phpunit/phpunit": "~4.0",
"mockery/mockery": "0.9.*"
"mockery/mockery": "0.9.*",
"orchestra/testbench": "~3.0"
},
"autoload": {
"classmap": [
Expand Down
24 changes: 24 additions & 0 deletions src/AbstractTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ abstract class AbstractTranslator
*/
protected $response = null;

/**
* Error message if any
*
* @var string
*/
protected $error = null;

/**
* The results from the request
*
Expand Down Expand Up @@ -82,6 +89,23 @@ public function __construct()
$this->setClient();
}

/**
* Getting attributes
*
* @param $variable
* @return mixed
*/
public function __get($attribute)
{
//Attributes exists
if(property_exists($this, $attribute)) {
//return the attribute value
return $this->$attribute;
}
//We return null
return null;
}

/**
* Creates the http client
*
Expand Down
86 changes: 49 additions & 37 deletions src/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ class Translator extends AbstractTranslator implements TranslatorInterface
* Translates the input text from the source language to the target language
*
* @param string $text
* @return self|null
* @return self
*/
public function textTranslate($text = '')
{
//No text input
if($text == '') {
//We return
return $this;
}
try {
//No text input
if($text == '') {
//We return null
return null;
}
//Perform get request on client and return results
$this->request('GET', 'v2/translate')->send([
'query' => collect([
Expand All @@ -38,29 +38,31 @@ public function textTranslate($text = '')
return $item == null || $item == '';
})->all()
]);
//Return the object
return $this;
} catch (ClientException $e) {
//Unexpected client error
return null;
//Set response to null
$this->response = null;
//Set error message
$this->error = $e->getMessage();
}
//Return translator object
return $this;
}

/**
* Translate a large text from the source language to the target language.
* Also used to translate multiple paragraphs or multiple inputs
*
* @param string|array $text
* @return self|null
* @return self
*/
public function bulkTranslate($text = null)
{
//No text input
if($text == null) {
//We return
return $this;
}
try {
//No text input
if($text == null) {
//We return null
return null;
}
//Perform a Post request on client and return results
$this->request('POST', 'v2/translate')->send([
'json' => collect([
Expand All @@ -72,12 +74,14 @@ public function bulkTranslate($text = null)
return $item == null || $item == '';
})->all()
]);
//Return the object
return $this;
} catch (ClientException $e) {
//Unexpected client error
return null;
//Set response to null
$this->response = null;
//Set error message
$this->error = $e->getMessage();
}
//Return translator object
return $this;
}

/**
Expand All @@ -90,20 +94,22 @@ public function listLanguages()
try {
//Perform a Get request on client and return results
$this->request('GET', 'v2/identifiable_languages')->send();
//Return the object
return $this;
} catch (ClientException $e) {
//Unexpected client error
return null;
//Set response to null
$this->response = null;
//Set error message
$this->error = $e->getMessage();
}
//Return translator object
return $this;
}

/**
* Identify the language in which the text is written
* with a certain level of confidence
*
* @param string $text
* @return self|null
* @return self
*/
public function identifyLanguage($text = '')
{
Expand All @@ -114,12 +120,14 @@ public function identifyLanguage($text = '')
'text' => $text
])->all()
]);
//Return the object
return $this;
} catch (ClientException $e) {
//Unexpected client error
return null;
//Set response to null
$this->response = null;
//Set error message
$this->error = $e->getMessage();
}
//Return translator object
return $this;
}

/**
Expand All @@ -143,12 +151,14 @@ public function listModels($defaultOnly = null, $sourceFilter = null, $targetFil
return $item == null || $item == '';
})->all()
]);
//Return the object
return $this;
} catch (ClientException $e) {
//Unexpected client error
return null;
//Set response to null
$this->response = null;
//Set error message
$this->error = $e->getMessage();
}
//Return translator object
return $this;
}

/**
Expand All @@ -161,12 +171,14 @@ public function getModelDetails()
try {
//Perform a get Request to get the model's Details and return it
$this->request('GET', 'v2/models/'.$this->modelId)->send();
//Return the object
return $this;
} catch (ClientException $e) {
//Unexpected client error
return null;
//Set response to null
$this->response = null;
//Set error message
$this->error = $e->getMessage();
}
//Return translator object
return $this;
}

/**
Expand Down
46 changes: 39 additions & 7 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,51 @@
<?php

use FindBrok\WatsonTranslate\Mocks\MockResponses as Response;
use FindBrok\WatsonTranslate\Translator;
use Orchestra\Testbench\TestCase as TestBenchTestCase;

/**
* Class TestCase
*/
class TestCase extends PHPUnit_Framework_TestCase
class TestCase extends TestBenchTestCase
{
/**
* Testing text translate method with sucessfull set of from
* attribute
* Setup
*/
public function testTranslator_SetFrom_ReturnTranslator()
public function setUp()
{

parent::setUp();
//Create translator class
$this->translator = app()->make('FindBrok\WatsonTranslate\Contracts\TranslatorInterface');
}

/**
* Test if the getter really returns the property
* and that property is set
*/
public function testSetterGetter()
{
$this->translator->from('en')->to('fr')->usingModel('default');
$this->assertEquals($this->translator->from, 'en');
$this->assertEquals($this->translator->to, 'fr');
$this->assertEquals($this->translator->modelId, config('watson-translate.models.default'));
}

/**
* Test that when a property does not exists getter
* returns null
*/
public function testPropertyInexistent_ReturnNull()
{
$this->assertEquals($this->translator->foo, null);
}

/**
* Get package providers.
*
* @param \Illuminate\Foundation\Application $app
* @return array
*/
protected function getPackageProviders($app)
{
return ['FindBrok\WatsonTranslate\WatsonTranslateServiceProvider'];
}
}

0 comments on commit 6f778c2

Please sign in to comment.