diff --git a/README.md b/README.md index 324501f..0876a0c 100644 --- a/README.md +++ b/README.md @@ -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) \ No newline at end of file +Read the [Docs](https://github.com/findbrok/laravel-watson-translate/wiki) + +## TODO + +- [ ] Create Translation Model +- [ ] Delete Translation Model \ No newline at end of file diff --git a/src/Mocks/MockResponses.php b/src/Mocks/MockResponses.php index 8b07bc7..064dd45 100644 --- a/src/Mocks/MockResponses.php +++ b/src/Mocks/MockResponses.php @@ -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()); } diff --git a/tests/TestCase.php b/tests/TestCase.php index ba61a28..f958aef 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -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(); } /** @@ -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 @@ -59,13 +77,15 @@ 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() + ); } /** @@ -73,11 +93,10 @@ public function testTextTranslate_WithGetTranslation_ReturnString() */ 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(), @@ -85,6 +104,38 @@ public function testTextTranslate_WithRawResults_ReturnJson() ); } + /** + * 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 * @@ -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() + ); } /**