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

Commit

Permalink
More testing
Browse files Browse the repository at this point in the history
  • Loading branch information
percymamedy committed Feb 8, 2016
1 parent 419623f commit 79c9b71
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/AbstractTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ public function request($method = 'GET', $uri = null, $options = [])
public function send($options = [])
{
//Send request with client and return response
$this->response = $this->client->send($this->request, collect($this->getAuth())->merge($this->getHeaders())->merge($options)->all());
$this->response = $this->getClient()->send($this->request, collect($this->getAuth())->merge($this->getHeaders())->merge($options)->all());
//Add results to class
$this->results = $this->response->getBody()->getContents();
//return the translator
Expand Down
17 changes: 14 additions & 3 deletions src/Mocks/MockResponses.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,21 +70,32 @@ public function pretendTextTranslateRaw()
*
* @return GuzzleHttp\Psr7\Response
*/
public function pretendBulkTranslate()
public function pretendBulkTranslateResponse()
{
//Build a new successful response for bulk translate
return new Response(202, [
'Content-Type' => 'application/json'
], collect([
'translations' => [
['translation' => $this->faker->sentence(rand(6, 10))],
['translation' => $this->faker->sentence(rand(6, 10))],
['translation' => 'Lorem ipsum'],
['translation' => 'Lorem nam dolor'],
],
'word_count' => $this->faker->numberBetween(10, 100),
'character_count' => $this->faker->numberBetween(10, 100),
])->toJson());
}

/**
* Mock a raw json results for Bulk translate
*
* @return string
*/
public function pretendBulkTranslateRaw()
{
//Return content of pretended response
return $this->pretendBulkTranslateResponse()->getBody()->getContents();
}

/**
* Mock a response list languages
*
Expand Down
92 changes: 80 additions & 12 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@ public function setUp()
$this->translator = app()->make('FindBrok\WatsonTranslate\Contracts\TranslatorInterface');
//Create mock responses
$this->mockResponses = new MockResponses;
//Translator Class namespace
$this->translatorClass = 'FindBrok\WatsonTranslate\Translator';
}

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

/**
Expand All @@ -42,26 +55,81 @@ public function testPropertyInexistent_ReturnNull()
}

/**
* Test text translate with getTranslation method returns string
* Test textTranslate with getTranslation method returns string
*/
public function testTextTranslate_GetTranslation_ReturnString()
public function testTextTranslate_WithGetTranslation_ReturnString()
{
$translator = $this->getMock('FindBrok\WatsonTranslate\Translator', ['request', 'send', 'getResponse', 'rawResults']);
$translator->method('request')->willReturnSelf();
$translator->method('send')->willReturnSelf();
$translator->method('getResponse')->willReturn($this->mockResponses->pretendTextTranslateResponse());
$translator->method('rawResults')->willReturn($this->mockResponses->pretendTextTranslateRaw());
$client = $this->getMockBuilder('GuzzleHttp\Client')->disableOriginalConstructor()->getMock();
$client->method('send')->willReturn($this->mockResponses->pretendTextTranslateResponse());

$translator = $this->getMock($this->translatorClass, ['getClient']);
$translator->method('getClient')->willReturn($client);

$this->assertEquals('Lorem ipsum', $translator->textTranslate('Lorem ipsum')->getTranslation());
}

/**
* Get package providers.
* Test the textTranslate with rawResults method returns json
*/
public function testTextTranslate_WithRawResults_ReturnJson()
{
$client = $this->getMockBuilder('GuzzleHttp\Client')->disableOriginalConstructor()->getMock();
$client->method('send')->willReturn($this->mockResponses->pretendTextTranslateResponse());

$translator = $this->getMock($this->translatorClass, ['getClient']);
$translator->method('getClient')->willReturn($client);

$this->assertJsonStringEqualsJsonString(
$this->mockResponses->pretendTextTranslateRaw(),
$translator->textTranslate('Lorem ipsum')->rawResults()
);
}

/**
* Test textTranslate throws \GuzzleHttp\Exception\ClientException with getTranslation returns null
*
* @param \Illuminate\Foundation\Application $app
* @return array
* @expectedException
*/
protected function getPackageProviders($app)
public function testTextTranslate_WithGetTranslation_ThrowsClientException_ReturnsNull()
{
return ['FindBrok\WatsonTranslate\WatsonTranslateServiceProvider'];
$translator = $this->getMock($this->translatorClass, ['send']);
$translator->method('send')->willThrowException(
Mockery::mock('GuzzleHttp\Exception\ClientException')
->shouldReceive(['getMessage', 'getCode'])
->andReturn(['Bad request', 400])
->getMock()
);
$this->assertNull($translator->textTranslate('lorem ipsum')->getTranslation());
}

/**
* Test the bulkTranslate method with getTranslation method returns string
*/
public function testBulkTranslate_WithGetTranslation_ReturnArray()
{
$client = $this->getMockBuilder('GuzzleHttp\Client')->disableOriginalConstructor()->getMock();
$client->method('send')->willReturn($this->mockResponses->pretendBulkTranslateResponse());

$translator = $this->getMock($this->translatorClass, ['getClient']);
$translator->method('getClient')->willReturn($client);

$this->assertSame(['Lorem ipsum', 'Lorem nam dolor'], $translator->bulkTranslate(['lorem', 'nam'])->getTranslation());
}

/**
* Test the bulkTranslate method throws \GuzzleHttp\Exception\ClientException with getTranslation method returns null
*
* @expectedException
*/
public function testBulkTranslate_WithGetTranslation_ThrowsClientException_ReturnsNull()
{
$translator = $this->getMock($this->translatorClass, ['send']);
$translator->method('send')->willThrowException(
Mockery::mock('GuzzleHttp\Exception\ClientException')
->shouldReceive(['getMessage', 'getCode'])
->andReturn(['Bad request', 400])
->getMock()
);
$this->assertNull($translator->bulkTranslate(['lorem', 'nam'])->getTranslation());
}
}

0 comments on commit 79c9b71

Please sign in to comment.