From 3cd48e17d297911346d5b092caf46e6803cb892e Mon Sep 17 00:00:00 2001 From: Russell Horton Date: Thu, 20 Dec 2012 14:32:51 -0800 Subject: [PATCH] php tests for wordnik.com api client --- .../wordnik-api/php/tests/AccountApiTest.php | 51 ++++++++ .../wordnik-api/php/tests/BaseApiTest.php | 57 +++++++++ .../wordnik-api/php/tests/WordApiTest.php | 95 +++++++++++++++ .../wordnik-api/php/tests/WordListApiTest.php | 115 ++++++++++++++++++ .../php/tests/WordListsApiTest.php | 40 ++++++ .../wordnik-api/php/tests/WordsApiTest.php | 37 ++++++ 6 files changed, 395 insertions(+) create mode 100644 samples/client/wordnik-api/php/tests/AccountApiTest.php create mode 100644 samples/client/wordnik-api/php/tests/BaseApiTest.php create mode 100644 samples/client/wordnik-api/php/tests/WordApiTest.php create mode 100644 samples/client/wordnik-api/php/tests/WordListApiTest.php create mode 100644 samples/client/wordnik-api/php/tests/WordListsApiTest.php create mode 100644 samples/client/wordnik-api/php/tests/WordsApiTest.php diff --git a/samples/client/wordnik-api/php/tests/AccountApiTest.php b/samples/client/wordnik-api/php/tests/AccountApiTest.php new file mode 100644 index 000000000000..7f77ffc113c4 --- /dev/null +++ b/samples/client/wordnik-api/php/tests/AccountApiTest.php @@ -0,0 +1,51 @@ +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); + } + + +} +?> diff --git a/samples/client/wordnik-api/php/tests/BaseApiTest.php b/samples/client/wordnik-api/php/tests/BaseApiTest.php new file mode 100644 index 000000000000..594ca202e0a1 --- /dev/null +++ b/samples/client/wordnik-api/php/tests/BaseApiTest.php @@ -0,0 +1,57 @@ +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); + } + +} + + +?> diff --git a/samples/client/wordnik-api/php/tests/WordApiTest.php b/samples/client/wordnik-api/php/tests/WordApiTest.php new file mode 100644 index 000000000000..e8738093d8ab --- /dev/null +++ b/samples/client/wordnik-api/php/tests/WordApiTest.php @@ -0,0 +1,95 @@ +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); + } + +} +?> diff --git a/samples/client/wordnik-api/php/tests/WordListApiTest.php b/samples/client/wordnik-api/php/tests/WordListApiTest.php new file mode 100644 index 000000000000..126368f16c0e --- /dev/null +++ b/samples/client/wordnik-api/php/tests/WordListApiTest.php @@ -0,0 +1,115 @@ +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)); + } + + +} +?> diff --git a/samples/client/wordnik-api/php/tests/WordListsApiTest.php b/samples/client/wordnik-api/php/tests/WordListsApiTest.php new file mode 100644 index 000000000000..5692402993ef --- /dev/null +++ b/samples/client/wordnik-api/php/tests/WordListsApiTest.php @@ -0,0 +1,40 @@ +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); + + + + } + +} +?> diff --git a/samples/client/wordnik-api/php/tests/WordsApiTest.php b/samples/client/wordnik-api/php/tests/WordsApiTest.php new file mode 100644 index 000000000000..f5fdcc7d074f --- /dev/null +++ b/samples/client/wordnik-api/php/tests/WordsApiTest.php @@ -0,0 +1,37 @@ +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); + } + +} +?>