Skip to content

Commit

Permalink
Merge pull request #10 from darrynten/dev
Browse files Browse the repository at this point in the history
100% Test Coverage
  • Loading branch information
darrynten authored Feb 18, 2017
2 parents 393190b + 43756f6 commit 628732d
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 88 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
168 changes: 80 additions & 88 deletions tests/GoogleNaturalLanguagePhp/GoogleNaturalLanguageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down Expand Up @@ -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();
}
}

0 comments on commit 628732d

Please sign in to comment.