From 6f778c2d469731020f3b676efa07e4b2e7bea895 Mon Sep 17 00:00:00 2001 From: Percy Mamedy Date: Sat, 6 Feb 2016 13:05:01 +0400 Subject: [PATCH] New tests, added Orchestra testbench and configured functions to always return translator object even on failure --- composer.json | 3 +- src/AbstractTranslator.php | 24 +++++++++++ src/Translator.php | 86 ++++++++++++++++++++++---------------- tests/TestCase.php | 46 ++++++++++++++++---- 4 files changed, 114 insertions(+), 45 deletions(-) diff --git a/composer.json b/composer.json index 54e257b..8c53c43 100644 --- a/composer.json +++ b/composer.json @@ -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": [ diff --git a/src/AbstractTranslator.php b/src/AbstractTranslator.php index 07b4c67..15bb543 100644 --- a/src/AbstractTranslator.php +++ b/src/AbstractTranslator.php @@ -45,6 +45,13 @@ abstract class AbstractTranslator */ protected $response = null; + /** + * Error message if any + * + * @var string + */ + protected $error = null; + /** * The results from the request * @@ -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 * diff --git a/src/Translator.php b/src/Translator.php index e0e8384..a3a752b 100644 --- a/src/Translator.php +++ b/src/Translator.php @@ -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([ @@ -38,12 +38,14 @@ 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; } /** @@ -51,16 +53,16 @@ public function textTranslate($text = '') * 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([ @@ -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; } /** @@ -90,12 +94,14 @@ 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; } /** @@ -103,7 +109,7 @@ public function listLanguages() * with a certain level of confidence * * @param string $text - * @return self|null + * @return self */ public function identifyLanguage($text = '') { @@ -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; } /** @@ -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; } /** @@ -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; } /** diff --git a/tests/TestCase.php b/tests/TestCase.php index c38c053..d8f147c 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -1,19 +1,51 @@ 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']; } } \ No newline at end of file