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) diff --git a/tests/GoogleNaturalLanguagePhp/GoogleNaturalLanguageTest.php b/tests/GoogleNaturalLanguagePhp/GoogleNaturalLanguageTest.php index a8d94e7..c23f7fe 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(); } }