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

Commit 6f778c2

Browse files
committed
New tests, added Orchestra testbench and configured functions to always return translator object even on failure
1 parent 63f176c commit 6f778c2

File tree

4 files changed

+114
-45
lines changed

4 files changed

+114
-45
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
"require-dev": {
1818
"fzaninotto/faker": "~1.4",
1919
"phpunit/phpunit": "~4.0",
20-
"mockery/mockery": "0.9.*"
20+
"mockery/mockery": "0.9.*",
21+
"orchestra/testbench": "~3.0"
2122
},
2223
"autoload": {
2324
"classmap": [

src/AbstractTranslator.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ abstract class AbstractTranslator
4545
*/
4646
protected $response = null;
4747

48+
/**
49+
* Error message if any
50+
*
51+
* @var string
52+
*/
53+
protected $error = null;
54+
4855
/**
4956
* The results from the request
5057
*
@@ -82,6 +89,23 @@ public function __construct()
8289
$this->setClient();
8390
}
8491

92+
/**
93+
* Getting attributes
94+
*
95+
* @param $variable
96+
* @return mixed
97+
*/
98+
public function __get($attribute)
99+
{
100+
//Attributes exists
101+
if(property_exists($this, $attribute)) {
102+
//return the attribute value
103+
return $this->$attribute;
104+
}
105+
//We return null
106+
return null;
107+
}
108+
85109
/**
86110
* Creates the http client
87111
*

src/Translator.php

Lines changed: 49 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@ class Translator extends AbstractTranslator implements TranslatorInterface
1717
* Translates the input text from the source language to the target language
1818
*
1919
* @param string $text
20-
* @return self|null
20+
* @return self
2121
*/
2222
public function textTranslate($text = '')
2323
{
24+
//No text input
25+
if($text == '') {
26+
//We return
27+
return $this;
28+
}
2429
try {
25-
//No text input
26-
if($text == '') {
27-
//We return null
28-
return null;
29-
}
3030
//Perform get request on client and return results
3131
$this->request('GET', 'v2/translate')->send([
3232
'query' => collect([
@@ -38,29 +38,31 @@ public function textTranslate($text = '')
3838
return $item == null || $item == '';
3939
})->all()
4040
]);
41-
//Return the object
42-
return $this;
4341
} catch (ClientException $e) {
44-
//Unexpected client error
45-
return null;
42+
//Set response to null
43+
$this->response = null;
44+
//Set error message
45+
$this->error = $e->getMessage();
4646
}
47+
//Return translator object
48+
return $this;
4749
}
4850

4951
/**
5052
* Translate a large text from the source language to the target language.
5153
* Also used to translate multiple paragraphs or multiple inputs
5254
*
5355
* @param string|array $text
54-
* @return self|null
56+
* @return self
5557
*/
5658
public function bulkTranslate($text = null)
5759
{
60+
//No text input
61+
if($text == null) {
62+
//We return
63+
return $this;
64+
}
5865
try {
59-
//No text input
60-
if($text == null) {
61-
//We return null
62-
return null;
63-
}
6466
//Perform a Post request on client and return results
6567
$this->request('POST', 'v2/translate')->send([
6668
'json' => collect([
@@ -72,12 +74,14 @@ public function bulkTranslate($text = null)
7274
return $item == null || $item == '';
7375
})->all()
7476
]);
75-
//Return the object
76-
return $this;
7777
} catch (ClientException $e) {
78-
//Unexpected client error
79-
return null;
78+
//Set response to null
79+
$this->response = null;
80+
//Set error message
81+
$this->error = $e->getMessage();
8082
}
83+
//Return translator object
84+
return $this;
8185
}
8286

8387
/**
@@ -90,20 +94,22 @@ public function listLanguages()
9094
try {
9195
//Perform a Get request on client and return results
9296
$this->request('GET', 'v2/identifiable_languages')->send();
93-
//Return the object
94-
return $this;
9597
} catch (ClientException $e) {
96-
//Unexpected client error
97-
return null;
98+
//Set response to null
99+
$this->response = null;
100+
//Set error message
101+
$this->error = $e->getMessage();
98102
}
103+
//Return translator object
104+
return $this;
99105
}
100106

101107
/**
102108
* Identify the language in which the text is written
103109
* with a certain level of confidence
104110
*
105111
* @param string $text
106-
* @return self|null
112+
* @return self
107113
*/
108114
public function identifyLanguage($text = '')
109115
{
@@ -114,12 +120,14 @@ public function identifyLanguage($text = '')
114120
'text' => $text
115121
])->all()
116122
]);
117-
//Return the object
118-
return $this;
119123
} catch (ClientException $e) {
120-
//Unexpected client error
121-
return null;
124+
//Set response to null
125+
$this->response = null;
126+
//Set error message
127+
$this->error = $e->getMessage();
122128
}
129+
//Return translator object
130+
return $this;
123131
}
124132

125133
/**
@@ -143,12 +151,14 @@ public function listModels($defaultOnly = null, $sourceFilter = null, $targetFil
143151
return $item == null || $item == '';
144152
})->all()
145153
]);
146-
//Return the object
147-
return $this;
148154
} catch (ClientException $e) {
149-
//Unexpected client error
150-
return null;
155+
//Set response to null
156+
$this->response = null;
157+
//Set error message
158+
$this->error = $e->getMessage();
151159
}
160+
//Return translator object
161+
return $this;
152162
}
153163

154164
/**
@@ -161,12 +171,14 @@ public function getModelDetails()
161171
try {
162172
//Perform a get Request to get the model's Details and return it
163173
$this->request('GET', 'v2/models/'.$this->modelId)->send();
164-
//Return the object
165-
return $this;
166174
} catch (ClientException $e) {
167-
//Unexpected client error
168-
return null;
175+
//Set response to null
176+
$this->response = null;
177+
//Set error message
178+
$this->error = $e->getMessage();
169179
}
180+
//Return translator object
181+
return $this;
170182
}
171183

172184
/**

tests/TestCase.php

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,51 @@
11
<?php
22

3-
use FindBrok\WatsonTranslate\Mocks\MockResponses as Response;
4-
use FindBrok\WatsonTranslate\Translator;
3+
use Orchestra\Testbench\TestCase as TestBenchTestCase;
54

65
/**
76
* Class TestCase
87
*/
9-
class TestCase extends PHPUnit_Framework_TestCase
8+
class TestCase extends TestBenchTestCase
109
{
1110
/**
12-
* Testing text translate method with sucessfull set of from
13-
* attribute
11+
* Setup
1412
*/
15-
public function testTranslator_SetFrom_ReturnTranslator()
13+
public function setUp()
1614
{
17-
15+
parent::setUp();
16+
//Create translator class
17+
$this->translator = app()->make('FindBrok\WatsonTranslate\Contracts\TranslatorInterface');
18+
}
19+
20+
/**
21+
* Test if the getter really returns the property
22+
* and that property is set
23+
*/
24+
public function testSetterGetter()
25+
{
26+
$this->translator->from('en')->to('fr')->usingModel('default');
27+
$this->assertEquals($this->translator->from, 'en');
28+
$this->assertEquals($this->translator->to, 'fr');
29+
$this->assertEquals($this->translator->modelId, config('watson-translate.models.default'));
30+
}
31+
32+
/**
33+
* Test that when a property does not exists getter
34+
* returns null
35+
*/
36+
public function testPropertyInexistent_ReturnNull()
37+
{
38+
$this->assertEquals($this->translator->foo, null);
39+
}
40+
41+
/**
42+
* Get package providers.
43+
*
44+
* @param \Illuminate\Foundation\Application $app
45+
* @return array
46+
*/
47+
protected function getPackageProviders($app)
48+
{
49+
return ['FindBrok\WatsonTranslate\WatsonTranslateServiceProvider'];
1850
}
1951
}

0 commit comments

Comments
 (0)