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

Commit 3a705a8

Browse files
committed
Tests Completed for textTranslate and bulkTranslate
1 parent 79c9b71 commit 3a705a8

File tree

3 files changed

+120
-14
lines changed

3 files changed

+120
-14
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,9 @@ Set your correct credentials and default configuration for using your IBM Watson
4343
4444
## Usage
4545

46-
Read the [Docs](https://github.com/findbrok/laravel-watson-translate/wiki)
46+
Read the [Docs](https://github.com/findbrok/laravel-watson-translate/wiki)
47+
48+
## TODO
49+
50+
- [ ] Create Translation Model
51+
- [ ] Delete Translation Model

src/Mocks/MockResponses.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ public function pretendBulkTranslateResponse()
8080
['translation' => 'Lorem ipsum'],
8181
['translation' => 'Lorem nam dolor'],
8282
],
83-
'word_count' => $this->faker->numberBetween(10, 100),
84-
'character_count' => $this->faker->numberBetween(10, 100),
83+
'word_count' => 100,
84+
'character_count' => 200,
8585
])->toJson());
8686
}
8787

tests/TestCase.php

Lines changed: 112 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ public function setUp()
2020
$this->mockResponses = new MockResponses;
2121
//Translator Class namespace
2222
$this->translatorClass = 'FindBrok\WatsonTranslate\Translator';
23+
//Create the mock client
24+
$this->client = $this->getMockBuilder('GuzzleHttp\Client')->disableOriginalConstructor()->getMock();
2325
}
2426

2527
/**
@@ -33,6 +35,22 @@ protected function getPackageProviders($app)
3335
return ['FindBrok\WatsonTranslate\WatsonTranslateServiceProvider'];
3436
}
3537

38+
/**
39+
* Set response as a result of textTranslate
40+
*/
41+
public function fakeResponseForTextTranslate()
42+
{
43+
$this->client->method('send')->willReturn($this->mockResponses->pretendTextTranslateResponse());
44+
}
45+
46+
/**
47+
* Set response as a result of bulkTranslate
48+
*/
49+
public function fakeResponseForBulkTranslate()
50+
{
51+
$this->client->method('send')->willReturn($this->mockResponses->pretendBulkTranslateResponse());
52+
}
53+
3654
/**
3755
* Test if the getter really returns the property
3856
* and that property is set
@@ -59,32 +77,65 @@ public function testPropertyInexistent_ReturnNull()
5977
*/
6078
public function testTextTranslate_WithGetTranslation_ReturnString()
6179
{
62-
$client = $this->getMockBuilder('GuzzleHttp\Client')->disableOriginalConstructor()->getMock();
63-
$client->method('send')->willReturn($this->mockResponses->pretendTextTranslateResponse());
80+
$this->fakeResponseForTextTranslate();
6481

6582
$translator = $this->getMock($this->translatorClass, ['getClient']);
66-
$translator->method('getClient')->willReturn($client);
83+
$translator->method('getClient')->willReturn($this->client);
6784

68-
$this->assertEquals('Lorem ipsum', $translator->textTranslate('Lorem ipsum')->getTranslation());
85+
$this->assertEquals(
86+
'Lorem ipsum',
87+
$translator->textTranslate('Lorem ipsum')->getTranslation()
88+
);
6989
}
7090

7191
/**
7292
* Test the textTranslate with rawResults method returns json
7393
*/
7494
public function testTextTranslate_WithRawResults_ReturnJson()
7595
{
76-
$client = $this->getMockBuilder('GuzzleHttp\Client')->disableOriginalConstructor()->getMock();
77-
$client->method('send')->willReturn($this->mockResponses->pretendTextTranslateResponse());
96+
$this->fakeResponseForTextTranslate();
7897

7998
$translator = $this->getMock($this->translatorClass, ['getClient']);
80-
$translator->method('getClient')->willReturn($client);
99+
$translator->method('getClient')->willReturn($this->client);
81100

82101
$this->assertJsonStringEqualsJsonString(
83102
$this->mockResponses->pretendTextTranslateRaw(),
84103
$translator->textTranslate('Lorem ipsum')->rawResults()
85104
);
86105
}
87106

107+
/**
108+
* Test the textTranslate with arrayResults method returns array
109+
*/
110+
public function testTextTranslate_WithArrayResults_ReturnArray()
111+
{
112+
$this->fakeResponseForTextTranslate();
113+
114+
$translator = $this->getMock($this->translatorClass, ['getClient']);
115+
$translator->method('getClient')->willReturn($this->client);
116+
117+
$this->assertEquals(
118+
json_decode($this->mockResponses->pretendTextTranslateRaw(), true),
119+
$translator->textTranslate('Lorem ipsum')->arrayResults()
120+
);
121+
}
122+
123+
/**
124+
* Test textTranslate with collectResults method returns collection
125+
*/
126+
public function testTextTranslate_WithCollectionResults_ReturnCollection()
127+
{
128+
$this->fakeResponseForTextTranslate();
129+
130+
$translator = $this->getMock($this->translatorClass, ['getClient']);
131+
$translator->method('getClient')->willReturn($this->client);
132+
133+
$this->assertEquals(
134+
collect(json_decode($this->mockResponses->pretendTextTranslateRaw(), true)),
135+
$translator->textTranslate('Lorem ipsum')->collectResults()
136+
);
137+
}
138+
88139
/**
89140
* Test textTranslate throws \GuzzleHttp\Exception\ClientException with getTranslation returns null
90141
*
@@ -107,13 +158,63 @@ public function testTextTranslate_WithGetTranslation_ThrowsClientException_Retur
107158
*/
108159
public function testBulkTranslate_WithGetTranslation_ReturnArray()
109160
{
110-
$client = $this->getMockBuilder('GuzzleHttp\Client')->disableOriginalConstructor()->getMock();
111-
$client->method('send')->willReturn($this->mockResponses->pretendBulkTranslateResponse());
161+
$this->fakeResponseForBulkTranslate();
112162

113163
$translator = $this->getMock($this->translatorClass, ['getClient']);
114-
$translator->method('getClient')->willReturn($client);
164+
$translator->method('getClient')->willReturn($this->client);
115165

116-
$this->assertSame(['Lorem ipsum', 'Lorem nam dolor'], $translator->bulkTranslate(['lorem', 'nam'])->getTranslation());
166+
$this->assertEquals(
167+
['Lorem ipsum', 'Lorem nam dolor'],
168+
$translator->bulkTranslate(['lorem', 'nam'])->getTranslation()
169+
);
170+
}
171+
172+
/**
173+
* Test the bulkTranslate method with rawResults method returns json
174+
*/
175+
public function testBulkTranslate_WithRawResults_ReturnJson()
176+
{
177+
$this->fakeResponseForBulkTranslate();
178+
179+
$translator = $this->getMock($this->translatorClass, ['getClient']);
180+
$translator->method('getClient')->willReturn($this->client);
181+
182+
$this->assertJsonStringEqualsJsonString(
183+
$this->mockResponses->pretendBulkTranslateRaw(),
184+
$translator->bulkTranslate(['lorem', 'nam'])->rawResults()
185+
);
186+
}
187+
188+
/**
189+
* Test the bulkTranslate method with arrayResults method returns array
190+
*/
191+
public function testBulkTranslate_WithArrayResults_ReturnArray()
192+
{
193+
$this->fakeResponseForBulkTranslate();
194+
195+
$translator = $this->getMock($this->translatorClass, ['getClient']);
196+
$translator->method('getClient')->willReturn($this->client);
197+
198+
$this->assertEquals(
199+
json_decode($this->mockResponses->pretendBulkTranslateRaw(), true),
200+
$translator->bulkTranslate(['lorem', 'nam'])->arrayResults()
201+
);
202+
}
203+
204+
/**
205+
* Test the bulkTranslate method with collectResults method returns collection
206+
*/
207+
public function testBulkTranslate_WithCollectionResults_ReturnCollection()
208+
{
209+
$this->fakeResponseForBulkTranslate();
210+
211+
$translator = $this->getMock($this->translatorClass, ['getClient']);
212+
$translator->method('getClient')->willReturn($this->client);
213+
214+
$this->assertEquals(
215+
collect(json_decode($this->mockResponses->pretendBulkTranslateRaw(), true)),
216+
$translator->bulkTranslate(['lorem', 'nam'])->collectResults()
217+
);
117218
}
118219

119220
/**

0 commit comments

Comments
 (0)