php tests for wordnik.com api client

This commit is contained in:
Russell Horton
2012-12-20 14:32:51 -08:00
parent da5c7a77c3
commit 3cd48e17d2
6 changed files with 395 additions and 0 deletions

View File

@@ -0,0 +1,51 @@
<?php
require_once 'BaseApiTest.php';
class AccountApiTest extends BaseApiTest {
public function setUp() {
parent::setUp();
$this->authToken = $this->accountApi->authenticate($this->username,
$this->password)->token;
date_default_timezone_set('America/Los_Angeles');
}
public function testAuthenticate() {
$res = $this->accountApi->authenticate($this->username,
$this->password);
$this->assertObjectHasAttribute('token', $res);
$this->assertNotEquals(0, $res->userId);
$this->assertObjectHasAttribute('userSignature', $res);
}
public function testAuthenticatePost() {
$res = $this->accountApi->authenticatePost($this->username,
$this->password);
$this->assertObjectHasAttribute('token', $res);
$this->assertNotEquals(0, $res->userId);
$this->assertObjectHasAttribute('userSignature', $res);
}
public function testGetWordListsForLoggedInUser() {
$res = $this->accountApi->getWordListsForLoggedInUser($this->authToken);
$this->assertNotEquals(0, count($res));
}
public function testGetApiTokenStatus() {
$res = $this->accountApi->getApiTokenStatus();
$this->assertObjectHasAttribute('valid', $res);
$this->assertNotEquals(0, count($res->remainingCalls));
}
public function testGetLoggedInUser() {
$res = $this->accountApi->getLoggedInUser($this->authToken);
$this->assertNotEquals(0, $res->id);
$this->assertEquals($this->username, $res->username);
$this->assertEquals(0, $res->status);
$this->assertNotEquals(null, $res->email);
}
}
?>

View File

@@ -0,0 +1,57 @@
<?php
// Unit tests for PHP Wordnik API client.
//
// Requires you to set four environment varibales:
// WN_APIKEY your API key
// WN_APIURL the API base url
// WN_USERNAME the username of a user
// WN_PASSWORD the user's password
// Run all tests:
//
// phpunit tests/AccountApiTest.php
// phpunit tests/WordApiTest.php
// phpunit tests/WordsApiTest.php
// phpunit tests/WordListApiTest.php
// phpunit tests/WordListsApiTest.php
// If you require PHPUnit to be installed, first get PEAR:
//
// $ wget http://pear.php.net/go-pear.phar
// $ php -d detect_unicode=0 go-pear.phar
//
// Then install PHPUnit:
//
// $ pear config-set auto_discover
// $ pear install pear.phpunit.de/PHPUnit
require_once 'wordnik/Swagger.php';
// This used to be required, but now gives an error:
// Cannot redeclare phpunit_autoload()
// require_once '/usr/lib/php/PHPUnit/Autoload.php';
class BaseApiTest extends PHPUnit_Framework_TestCase {
public function setUp() {
$this->apiUrl = 'http://api.wordnik.com/v4';
$this->apiKey = getenv('API_KEY');
$this->username = getenv('USER_NAME');
$this->password = getenv('PASSWORD');
$this->client = new APIClient($this->apiKey, $this->apiUrl);
$this->accountApi = new AccountApi($this->client);
$this->wordApi = new WordApi($this->client);
$this->wordListApi = new WordListApi($this->client);
$this->wordListsApi = new WordListsApi($this->client);
$this->wordsApi = new WordsApi($this->client);
}
public function tearDown() {
unset($this->client);
}
}
?>

View File

@@ -0,0 +1,95 @@
<?php
require_once 'BaseApiTest.php';
date_default_timezone_set('America/Los_Angeles');
class WordApiTest extends BaseApiTest {
public function testWordApis() {
$ch = curl_init("http://api.wordnik.com/v4/word.json");
if (! $ch) {
die("No php curl handle");
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
$doc = json_decode($data);
$this->assertEquals(12, count($doc->apis));
}
public function testGetWord() {
$res = $this->wordApi->getWord('cat');
$this->assertEquals('cat', $res->word);
}
public function testGetWordWithSuggestions() {
$res = $this->wordApi->getWord('cAt', $includeSuggestions=true);
$this->assertEquals('cAt', $res->word);
}
public function testGetWordWithCanonicalForm() {
$res = $this->wordApi->getWord('cAt', $useCanonical='true');
$this->assertEquals('cat', $res->word);
}
public function testGetDefinitions() {
$res = $this->wordApi->getDefinitions('cat');
$this->assertEquals(15, count($res));
}
public function testGetDefinitionsWithSpacesInWord() {
$res = $this->wordApi->getDefinitions('bon vivant');
$this->assertEquals(1, count($res));
}
public function testGetExamples() {
$res = $this->wordApi->getExamples('cat', $limit=5);
$this->assertEquals(5, count($res->examples));
}
public function testGetTopExample() {
$res = $this->wordApi->getTopExample('cat', $limit=5);
$this->assertEquals('cat', $res->word);
}
public function testGetHyphenation() {
$res = $this->wordApi->getHyphenation('catalog', $useCanonical=null, $sourceDictionary=null, $limit=1);
$this->assertEquals(1, count($res));
}
public function testGetWordFrequency() {
$res = $this->wordApi->getWordFrequency('catalog');
$this->assertFalse($res->totalCount == 0);
}
public function testGetPhrases() {
$res = $this->wordApi->getPhrases('money');
$this->assertFalse(count($res) == 0);
}
public function testGetRelatedWords() {
$res = $this->wordApi->getRelatedWords('cat');
foreach ($res as $related) {
$this->assertLessThan(11, count($related->words));
}
}
public function testGetAudio() {
$res = $this->wordApi->getAudio('cat', $useCanonical=True, $limit=2);
$this->assertEquals(2, count($res));
}
public function testGetScrabbleScore() {
$res = $this->wordApi->getScrabbleScore('quixotry');
$this->assertEquals(27, $res->value);
}
public function testGetEtymologies() {
$res = $this->wordApi->getEtymologies('butter');
$this->assertFalse(strpos($res[0], 'of Scythian origin') === false);
}
}
?>

View File

@@ -0,0 +1,115 @@
<?php
require_once 'BaseApiTest.php';
date_default_timezone_set('America/Los_Angeles');
class WordListApiTest extends BaseApiTest {
public function setUp() {
parent::setUp();
$this->authToken = $this->accountApi->authenticate($this->username,
$this->password)->token;
$lists = $this->accountApi->getWordListsForLoggedInUser($this->authToken, $skip=null, $limit=1);
$this->existingList = $lists[0];
$this->wordList = new WordList();
$this->wordList->name = "my test list";
$this->wordList->type = "PUBLIC";
$this->wordList->description = "some words I want to play with";
}
public function testGetWordListByPermalink() {
$res = $this->wordListApi->getWordListByPermalink($this->existingList->permalink,
$this->authToken);
$this->assertNotEquals(null, $res);
}
public function testUpdateWordList() {
$description = 'list updated at ' . time();
$this->existingList->description = $description;
$this->wordListApi->updateWordList($this->existingList->permalink,
$body=$this->existingList,
$this->authToken);
$res = $this->wordListApi->getWordListByPermalink($this->existingList->permalink, $this->authToken);
$this->assertEquals($description, $res->description);
}
public function testAddWordsToWordList() {
$wordsToAdd = array();
$word1 = new StringValue();
$word1->word = "delicious";
$wordsToAdd[] = $word1;
$word2 = new StringValue();
$word2->word = "tasty";
$wordsToAdd[] = $word2;
$word3 = new StringValue();
$word3->word = "scrumptious";
$wordsToAdd[] = $word3;
$word4 = new StringValue();
$word4->word = "not to be deleted";
$wordsToAdd[] = $word4;
$this->wordListApi->addWordsToWordList($this->existingList->permalink,
$body=$wordsToAdd,
$this->authToken);
$res = $this->wordListApi->getWordListWords($this->existingList->permalink, $this->authToken, $sortBy=null, $sortOrder=null, $skip=null, $limit=null);
$returnedWords = array();
foreach ($res as $wordListWord) {
$returnedWords[] = $wordListWord->word;
}
$intersection = array();
foreach ($wordsToAdd as $addedWord) {
if (in_array($addedWord->word, $returnedWords)) {
$intersection[] = $addedWord->word;
}
}
$this->assertEquals(4, count($intersection));
}
public function testDeleteWordsFromList() {
$wordsToRemove = array();
$word1 = new StringValue();
$word1->word = "delicious";
$wordsToRemove[] = $word1;
$word2 = new StringValue();
$word2->word = "tasty";
$wordsToRemove[] = $word2;
$word3 = new StringValue();
$word3->word = "scrumptious";
$wordsToRemove[] = $word3;
$res = $this->wordListApi->getWordListWords($this->existingList->permalink, $this->authToken, $sortBy=null, $sortOrder=null, $skip=null, $limit=null);
$this->wordListApi->deleteWordsFromWordList($this->existingList->permalink,
$body=$wordsToRemove,
$this->authToken);
$res = $this->wordListApi->getWordListWords($this->existingList->permalink, $this->authToken, $sortBy=null, $sortOrder=null, $skip=null, $limit=null);
$returnedWords = array();
foreach ($res as $wordListWord) {
$returnedWords[] = $wordListWord->word;
}
$intersection = array();
foreach ($wordsToRemove as $removedWord) {
if (in_array($removedWord->word, $returnedWords)) {
$intersection[] = $removedWord->word;
}
}
$this->assertEquals(0, count($intersection));
}
}
?>

View File

@@ -0,0 +1,40 @@
<?php
require_once 'BaseApiTest.php';
date_default_timezone_set('America/Los_Angeles');
class WordListsApiTest extends BaseApiTest {
public function setUp() {
parent::setUp();
$this->authToken = $this->accountApi->authenticate($this->username,
$this->password)->token;
}
public function testCreateWordList() {
$wordList = new WordList();
$wordList->name = "my test list";
$wordList->type = "PUBLIC";
$wordList->description = "some words I want to play with";
$res = $this->wordListsApi->createWordList($body=$wordList,
$this->authToken);
$this->assertEquals($wordList->description, $res->description);
$wordsToAdd = array();
$word1 = new StringValue();
$word1->word = "foo";
$wordsToAdd[] = $word1;
$this->wordListApi->addWordsToWordList($res->permalink,
$body=$wordsToAdd,
$this->authToken);
}
}
?>

View File

@@ -0,0 +1,37 @@
<?php
require_once 'BaseApiTest.php';
date_default_timezone_set('America/Los_Angeles');
class WordsApiTest extends BaseApiTest {
public function testSearchWords() {
$res = $this->wordsApi->searchWords('tree');
$this->assertEquals('tree', $res->searchResults[0]->word);
$this->assertNotEquals(0, $res->totalResults);
}
public function testGetWordOfTheDay() {
$res = $this->wordsApi->getWordOfTheDay();
$this->assertNotEquals(null, $res);
}
public function testReverseDictionary() {
$res = $this->wordsApi->reverseDictionary('hairy');
$this->assertNotEquals(0, $res->totalResults);
$this->assertNotEquals(0, count($res->results));
}
public function testGetRandomWords() {
$res = $this->wordsApi->getRandomWords();
$this->assertEquals(10, count($res));
}
public function testGetRandomWord() {
$res = $this->wordsApi->getRandomWord();
$this->assertNotEquals(null, $res);
}
}
?>