From b687ebdf00c982cd83e60be1c1a967c9903c87c1 Mon Sep 17 00:00:00 2001 From: Darryn Ten Date: Sat, 18 Feb 2017 11:43:24 +0200 Subject: [PATCH 1/3] Mocks for the NaturalLanguageClient and 100% Coverage --- .../GoogleNaturalLanguageTest.php | 168 +++++++++--------- 1 file changed, 80 insertions(+), 88 deletions(-) diff --git a/tests/GoogleNaturalLanguagePhp/GoogleNaturalLanguageTest.php b/tests/GoogleNaturalLanguagePhp/GoogleNaturalLanguageTest.php index a8d94e7..255bd71 100644 --- a/tests/GoogleNaturalLanguagePhp/GoogleNaturalLanguageTest.php +++ b/tests/GoogleNaturalLanguagePhp/GoogleNaturalLanguageTest.php @@ -3,11 +3,18 @@ namespace DarrynTen\GoogleNaturalLanguagePhp\Tests\GoogleNaturalLanguagePhp; use PHPUnit_Framework_TestCase; +use Mockery as m; +use ReflectionClass; use DarrynTen\GoogleNaturalLanguagePhp\GoogleNaturalLanguage; class GoogleNaturalLanguageTest extends PHPUnit_Framework_TestCase { + public function tearDown() + { + m::close(); + } + public function testConstruct() { $config = [ @@ -61,120 +68,105 @@ public function testSet() public function testGetEntities() { - if (getenv('DO_LIVE_API_TESTS') == "true") { - $config = [ - 'projectId' => 'project-id', - 'cheapskate' => true, - 'cache' => true, - ]; - - $instance = new GoogleNaturalLanguage($config); - - $instance->setText('A duck and a cat in a field at night'); + $config = [ + 'projectId' => 'project-id', + 'cheapskate' => true, + 'cache' => true, + ]; - $entities = $instance->getEntities(); + $client = m::mock(NaturalLanguageClient::class); - $retrievedEntities = $entities->entities(); - $this->assertInternalType('array', $retrievedEntities); + $client->shouldReceive('analyzeEntities') + ->once() + ->andReturn(); - $this->assertEquals('duck', $retrievedEntities[0]['name']); - $this->assertEquals('OTHER', $retrievedEntities[0]['type']); + $instance = new GoogleNaturalLanguage($config); - $this->assertEquals('cat', $retrievedEntities[1]['name']); - $this->assertEquals('OTHER', $retrievedEntities[1]['type']); + // Need to inject mock to a private property + $reflection = new ReflectionClass($instance); + $reflectedClient = $reflection->getProperty('languageClient'); + $reflectedClient->setAccessible(true); + $reflectedClient->setValue($instance, $client); - $this->assertEquals('field', $retrievedEntities[2]['name']); - $this->assertEquals('LOCATION', $retrievedEntities[2]['type']); - } + $instance->setText('A duck and a cat in a field at night'); + $entities = $instance->getEntities(); } public function testGetSyntax() { - if (getenv('DO_LIVE_API_TESTS') == "true") { - $config = [ - 'projectId' => 'project-id', - 'cheapskate' => false, - 'cache' => true, - ]; - - $instance = new GoogleNaturalLanguage($config); - - $instance->setText('A duck and a cat in a field at night'); - - $syntax = $instance->getSyntax(); + $config = [ + 'projectId' => 'project-id', + 'cheapskate' => true, + 'cache' => true, + ]; - $sentences = $syntax->sentences(); - $tokens = $syntax->tokens(); + $client = m::mock(NaturalLanguageClient::class); - $this->assertInternalType('array', $sentences); - $this->assertInternalType('array', $tokens); + $client->shouldReceive('analyzeSyntax') + ->once() + ->andReturn(); - $this->assertEquals('A duck and a cat in a field at night', $sentences[0]['text']['content']); + $instance = new GoogleNaturalLanguage($config); - $this->assertEquals(0, $sentences[0]['beginOffset']); + // Need to inject mock to a private property + $reflection = new ReflectionClass($instance); + $reflectedClient = $reflection->getProperty('languageClient'); + $reflectedClient->setAccessible(true); + $reflectedClient->setValue($instance, $client); - $this->assertEquals('duck', $tokens[1]['text']['content']); - $this->assertEquals('NOUN', $tokens[1]['partOfSpeech']['tag']); - $this->assertEquals('SINGULAR', $tokens[1]['partOfSpeech']['number']); - } + $instance->setText('A duck and a cat in a field at night'); + $entities = $instance->getSyntax(); } public function testGetSentiment() { - if (getenv('DO_LIVE_API_TESTS') == "true") { - $config = [ - 'projectId' => 'project-id', - 'cheapskate' => true, - 'cache' => true, - ]; - - $instance = new GoogleNaturalLanguage($config); + $config = [ + 'projectId' => 'project-id', + 'cheapskate' => true, + 'cache' => true, + ]; - $instance->setText('A duck and a cat in a field at night'); + $client = m::mock(NaturalLanguageClient::class); - $sentiment = $instance->getSentiment(); + $client->shouldReceive('analyzeSentiment') + ->once() + ->andReturn(); - $this->assertInternalType('array', $sentiment->documentSentiment()); + $instance = new GoogleNaturalLanguage($config); - $documentSentiment = $sentiment->documentSentiment(); + // Need to inject mock to a private property + $reflection = new ReflectionClass($instance); + $reflectedClient = $reflection->getProperty('languageClient'); + $reflectedClient->setAccessible(true); + $reflectedClient->setValue($instance, $client); - $this->assertInternalType('double', $documentSentiment['magnitude']); - $this->assertInternalType('double', $documentSentiment['score']); - } + $instance->setText('A duck and a cat in a field at night'); + $entities = $instance->getSentiment(); } public function testGetAll() { - if (getenv('DO_LIVE_API_TESTS') == "true") { - $config = [ - 'projectId' => 'project-id', - 'cheapskate' => true, - 'cache' => true, - ]; - - $instance = new GoogleNaturalLanguage($config); - - $instance->setText('A duck and a cat in a field at night'); - - $all = $instance->getAll(); - - $entities = $all->entities(); - $sentences = $all->sentences(); - $tokens = $all->tokens(); - $sentiment = $all->documentSentiment(); - $language = $all->language(); - - $this->assertInternalType('array', $entities); - $this->assertInternalType('array', $entities[0]); - $this->assertInternalType('array', $sentences); - $this->assertInternalType('array', $sentences[0]); - $this->assertInternalType('array', $tokens); - $this->assertInternalType('array', $tokens[0]); - $this->assertInternalType('array', $sentiment); - $this->assertInternalType('double', $sentiment['magnitude']); - $this->assertInternalType('string', $language); - - $this->assertEquals('en', $language); - } + $config = [ + 'projectId' => 'project-id', + 'cheapskate' => false, + 'cache' => true, + ]; + + $client = m::mock(NaturalLanguageClient::class); + + $client->shouldReceive('annotateText') + ->once() + ->andReturn(); + + $instance = new GoogleNaturalLanguage($config); + + // Need to inject mock to a private property + $reflection = new ReflectionClass($instance); + $reflectedClient = $reflection->getProperty('languageClient'); + $reflectedClient->setAccessible(true); + $reflectedClient->setValue($instance, $client); + + $instance->setText('A duck and a cat in a field at night'); + $entities = $instance->getAll(); } } From c6dcd63c788f7148ade6ea073fe7208c65791ec6 Mon Sep 17 00:00:00 2001 From: Darryn Ten Date: Sat, 18 Feb 2017 11:45:05 +0200 Subject: [PATCH 2/3] Add badge to README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index b01fe58..d0dd7f0 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,7 @@ ![Travis Build Status](https://travis-ci.org/darrynten/google-natural-language-php.svg?branch=master) ![StyleCI Status](https://styleci.io/repos/81687310/shield?branch=master) +[![codecov](https://codecov.io/gh/darrynten/google-natural-language-php/branch/master/graph/badge.svg)](https://codecov.io/gh/darrynten/google-natural-language-php) ![Packagist Version](https://img.shields.io/packagist/v/darrynten/google-natural-language-php.svg) ![MIT License](https://img.shields.io/github/license/darrynten/google-natural-language-php.svg) From ed3dac8a126c7f2df20b9dd214d7e345dc2f7603 Mon Sep 17 00:00:00 2001 From: Darryn Ten Date: Sat, 18 Feb 2017 11:48:36 +0200 Subject: [PATCH 3/3] Indentation --- .../GoogleNaturalLanguageTest.php | 40 +++++++++---------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/tests/GoogleNaturalLanguagePhp/GoogleNaturalLanguageTest.php b/tests/GoogleNaturalLanguagePhp/GoogleNaturalLanguageTest.php index 255bd71..c23f7fe 100644 --- a/tests/GoogleNaturalLanguagePhp/GoogleNaturalLanguageTest.php +++ b/tests/GoogleNaturalLanguagePhp/GoogleNaturalLanguageTest.php @@ -69,16 +69,16 @@ public function testSet() public function testGetEntities() { $config = [ - 'projectId' => 'project-id', - 'cheapskate' => true, - 'cache' => true, + 'projectId' => 'project-id', + 'cheapskate' => true, + 'cache' => true, ]; $client = m::mock(NaturalLanguageClient::class); $client->shouldReceive('analyzeEntities') - ->once() - ->andReturn(); + ->once() + ->andReturn(); $instance = new GoogleNaturalLanguage($config); @@ -95,16 +95,16 @@ public function testGetEntities() public function testGetSyntax() { $config = [ - 'projectId' => 'project-id', - 'cheapskate' => true, - 'cache' => true, + 'projectId' => 'project-id', + 'cheapskate' => true, + 'cache' => true, ]; $client = m::mock(NaturalLanguageClient::class); $client->shouldReceive('analyzeSyntax') - ->once() - ->andReturn(); + ->once() + ->andReturn(); $instance = new GoogleNaturalLanguage($config); @@ -121,16 +121,16 @@ public function testGetSyntax() public function testGetSentiment() { $config = [ - 'projectId' => 'project-id', - 'cheapskate' => true, - 'cache' => true, + 'projectId' => 'project-id', + 'cheapskate' => true, + 'cache' => true, ]; $client = m::mock(NaturalLanguageClient::class); $client->shouldReceive('analyzeSentiment') - ->once() - ->andReturn(); + ->once() + ->andReturn(); $instance = new GoogleNaturalLanguage($config); @@ -147,16 +147,16 @@ public function testGetSentiment() public function testGetAll() { $config = [ - 'projectId' => 'project-id', - 'cheapskate' => false, - 'cache' => true, + 'projectId' => 'project-id', + 'cheapskate' => false, + 'cache' => true, ]; $client = m::mock(NaturalLanguageClient::class); $client->shouldReceive('annotateText') - ->once() - ->andReturn(); + ->once() + ->andReturn(); $instance = new GoogleNaturalLanguage($config);