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

Commit

Permalink
Tests Completed for textTranslate and bulkTranslate
Browse files Browse the repository at this point in the history
  • Loading branch information
percymamedy committed Feb 8, 2016
1 parent 79c9b71 commit 3a705a8
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 14 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,9 @@ Set your correct credentials and default configuration for using your IBM Watson
## Usage

Read the [Docs](https://github.com/findbrok/laravel-watson-translate/wiki)
Read the [Docs](https://github.com/findbrok/laravel-watson-translate/wiki)

## TODO

- [ ] Create Translation Model
- [ ] Delete Translation Model
4 changes: 2 additions & 2 deletions src/Mocks/MockResponses.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ public function pretendBulkTranslateResponse()
['translation' => 'Lorem ipsum'],
['translation' => 'Lorem nam dolor'],
],
'word_count' => $this->faker->numberBetween(10, 100),
'character_count' => $this->faker->numberBetween(10, 100),
'word_count' => 100,
'character_count' => 200,
])->toJson());
}

Expand Down
123 changes: 112 additions & 11 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public function setUp()
$this->mockResponses = new MockResponses;
//Translator Class namespace
$this->translatorClass = 'FindBrok\WatsonTranslate\Translator';
//Create the mock client
$this->client = $this->getMockBuilder('GuzzleHttp\Client')->disableOriginalConstructor()->getMock();
}

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

/**
* Set response as a result of textTranslate
*/
public function fakeResponseForTextTranslate()
{
$this->client->method('send')->willReturn($this->mockResponses->pretendTextTranslateResponse());
}

/**
* Set response as a result of bulkTranslate
*/
public function fakeResponseForBulkTranslate()
{
$this->client->method('send')->willReturn($this->mockResponses->pretendBulkTranslateResponse());
}

/**
* Test if the getter really returns the property
* and that property is set
Expand All @@ -59,32 +77,65 @@ public function testPropertyInexistent_ReturnNull()
*/
public function testTextTranslate_WithGetTranslation_ReturnString()
{
$client = $this->getMockBuilder('GuzzleHttp\Client')->disableOriginalConstructor()->getMock();
$client->method('send')->willReturn($this->mockResponses->pretendTextTranslateResponse());
$this->fakeResponseForTextTranslate();

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

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

/**
* 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());
$this->fakeResponseForTextTranslate();

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

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

/**
* Test the textTranslate with arrayResults method returns array
*/
public function testTextTranslate_WithArrayResults_ReturnArray()
{
$this->fakeResponseForTextTranslate();

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

$this->assertEquals(
json_decode($this->mockResponses->pretendTextTranslateRaw(), true),
$translator->textTranslate('Lorem ipsum')->arrayResults()
);
}

/**
* Test textTranslate with collectResults method returns collection
*/
public function testTextTranslate_WithCollectionResults_ReturnCollection()
{
$this->fakeResponseForTextTranslate();

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

$this->assertEquals(
collect(json_decode($this->mockResponses->pretendTextTranslateRaw(), true)),
$translator->textTranslate('Lorem ipsum')->collectResults()
);
}

/**
* Test textTranslate throws \GuzzleHttp\Exception\ClientException with getTranslation returns null
*
Expand All @@ -107,13 +158,63 @@ public function testTextTranslate_WithGetTranslation_ThrowsClientException_Retur
*/
public function testBulkTranslate_WithGetTranslation_ReturnArray()
{
$client = $this->getMockBuilder('GuzzleHttp\Client')->disableOriginalConstructor()->getMock();
$client->method('send')->willReturn($this->mockResponses->pretendBulkTranslateResponse());
$this->fakeResponseForBulkTranslate();

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

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

/**
* Test the bulkTranslate method with rawResults method returns json
*/
public function testBulkTranslate_WithRawResults_ReturnJson()
{
$this->fakeResponseForBulkTranslate();

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

$this->assertJsonStringEqualsJsonString(
$this->mockResponses->pretendBulkTranslateRaw(),
$translator->bulkTranslate(['lorem', 'nam'])->rawResults()
);
}

/**
* Test the bulkTranslate method with arrayResults method returns array
*/
public function testBulkTranslate_WithArrayResults_ReturnArray()
{
$this->fakeResponseForBulkTranslate();

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

$this->assertEquals(
json_decode($this->mockResponses->pretendBulkTranslateRaw(), true),
$translator->bulkTranslate(['lorem', 'nam'])->arrayResults()
);
}

/**
* Test the bulkTranslate method with collectResults method returns collection
*/
public function testBulkTranslate_WithCollectionResults_ReturnCollection()
{
$this->fakeResponseForBulkTranslate();

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

$this->assertEquals(
collect(json_decode($this->mockResponses->pretendBulkTranslateRaw(), true)),
$translator->bulkTranslate(['lorem', 'nam'])->collectResults()
);
}

/**
Expand Down

0 comments on commit 3a705a8

Please sign in to comment.