Skip to content

Commit

Permalink
Expanding the test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
wtfzdotnet committed Jan 28, 2014
1 parent 31e605e commit f73fc2a
Show file tree
Hide file tree
Showing 13 changed files with 377 additions and 21 deletions.
21 changes: 16 additions & 5 deletions lib/Tmdb/Api/Genres.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,7 @@ public function getGenre($id, array $options = array(), array $headers = array()
$response = $this->getGenres($options, $headers);

if (array_key_exists('genres', $response)) {
foreach($response['genres'] as $genre) {
if ($id == $genre['id']) {
return $genre;
}
}
return $this->extractGenreByIdFromResponse($id, $response['genres']);
}

return null;
Expand Down Expand Up @@ -62,4 +58,19 @@ public function getMovies($genre_id, array $options = array(), array $headers =
{
return $this->get('genre/' . $genre_id . '/movies', $options, $headers);
}

/**
* @param $id
* @param array $data
* @return mixed
*/
private function extractGenreByIdFromResponse($id, array $data = array())
{
foreach($data as $genre) {
if ($id == $genre['id'])
return $genre;
}

return null;
}
}
2 changes: 1 addition & 1 deletion lib/Tmdb/Model/Tv.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public function __construct()
{
$this->createdBy = new Images();
$this->episodeRunTime = new GenericCollection();
$this->genres = new GenericCollection();
$this->genres = new Genres();
$this->languages = new GenericCollection();
$this->networks = new GenericCollection();
$this->originCountry = new GenericCollection();
Expand Down
41 changes: 40 additions & 1 deletion test/Tmdb/Tests/Api/GenresTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,46 @@ public function shouldGetMovies()
$api->getMovies(self::GENRE_ID);
}

/**
* @test
*/
public function shouldGetGenreAndReturnOne()
{
$api = $this->getApiMock(array('getGenres'));

$api->expects($this->once())
->method('getGenres')
->will($this->returnCallback(function(){
return array('genres' => array(array('id' => 28, 'name' => 'Action')));
}))
;

$genre = $api->getGenre(self::GENRE_ID);

$this->assertEquals(28, $genre['id']);
$this->assertEquals('Action', $genre['name']);
}

/**
* @test
*/
public function shouldReturnNullWithNoData()
{
$api = $this->getApiMock(array('getGenres'));

$api->expects($this->once())
->method('getGenres')
->will($this->returnCallback(function(){
return array('genres' => array());
}))
;

$genre = $api->getGenre(self::GENRE_ID);

$this->assertEquals(null, $genre);
}

protected function getApiClass() {
return 'Tmdb\Api\Genres';
}
}
}
29 changes: 23 additions & 6 deletions test/Tmdb/Tests/Api/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,30 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase

abstract protected function getApiClass();

protected function getApiMock()
protected function getApiMock(array $methods = array())
{
if ($this->_api) {
return $this->_api;
}

$client = $this->getClientWithMockedHttpClient();

return $this->getMockBuilder($this->getApiClass())
->setMethods(
array_merge(
array('get', 'post', 'postRaw', 'patch', 'delete', 'put'),
$methods
)
)
->setConstructorArgs(array($client))
->getMock();
}

protected function getClientWithMockedHttpClient()
{
$token = new ApiToken('abcdef');

$httpClient = $this->getMock('Guzzle\Http\Client', array('send'));
$httpClient = $this->getMockedHttpClient();
$httpClient
->expects($this->any())
->method('send');
Expand All @@ -42,9 +57,11 @@ protected function getApiMock()
$client = new \Tmdb\Client($token, $httpClient);
$client->setHttpClient($mock);

return $this->getMockBuilder($this->getApiClass())
->setMethods(array('get', 'post', 'postRaw', 'patch', 'delete', 'put'))
->setConstructorArgs(array($client))
->getMock();
return $client;
}

protected function getMockedHttpClient()
{
return $this->getMock('Guzzle\Http\Client', array('send'));
}
}
47 changes: 41 additions & 6 deletions test/Tmdb/Tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,52 @@
* @copyright (c) 2013, Michael Roterman
* @version 0.0.1
*/
class ClientTest extends \PHPUnit_Framework_TestCase
class ClientTest extends \Tmdb\Tests\TestCase
{
private $client = null;

public function setUp()
{
$token = new \Tmdb\ApiToken('abcdef');
$client = new \Tmdb\Client($token);

$this->client = $client;
}

/**
* @test
*/
public function shouldNotHaveToPassHttpClientToConstructor()
{
$token = new \Tmdb\ApiToken('abcdef');
$client = new \Tmdb\Client($token);

$this->assertInstanceOf('Tmdb\HttpClient\HttpClient', $client->getHttpClient());
$this->assertInstanceOf('Tmdb\HttpClient\HttpClient', $this->client->getHttpClient());
}

}
/**
* @test
*/
public function assertInstances()
{
$this->assertInstancesOf(
$this->client,
array(
'getAuthenticationApi' => 'Tmdb\Api\Authentication',
'getAccountApi' => 'Tmdb\Api\Account',
'getCollectionsApi' => 'Tmdb\Api\Collections',
'getMoviesApi' => 'Tmdb\Api\Movies',
'getTvApi' => 'Tmdb\Api\Tv',
'getTvSeasonApi' => 'Tmdb\Api\TvSeason',
'getTvEpisodeApi' => 'Tmdb\Api\TvEpisode',
'getPeopleApi' => 'Tmdb\Api\People',
'getListsApi' => 'Tmdb\Api\Lists',
'getCompaniesApi' => 'Tmdb\Api\Companies',
'getGenresApi' => 'Tmdb\Api\Genres',
'getKeywordsApi' => 'Tmdb\Api\Keywords',
'getDiscoverApi' => 'Tmdb\Api\Discover',
'getSearchApi' => 'Tmdb\Api\Search',
'getReviewsApi' => 'Tmdb\Api\Reviews',
'getChangesApi' => 'Tmdb\Api\Changes',
'getJobsApi' => 'Tmdb\Api\Jobs',
)
);
}
}
2 changes: 0 additions & 2 deletions test/Tmdb/Tests/Factory/GenreFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
*/
namespace Tmdb\Tests\Factory;

use Tmdb\Factory\GenreFactory;

class GenreFactoryTest extends TestCase
{
const GENRE_ID = 28;
Expand Down
47 changes: 47 additions & 0 deletions test/Tmdb/Tests/Model/MovieTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
/**
* This file is part of the Tmdb PHP API created by Michael Roterman.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @package Tmdb
* @author Michael Roterman <[email protected]>
* @copyright (c) 2013, Michael Roterman
* @version 0.0.1
*/
namespace Tmdb\Tests\Model;

use Tmdb\Model\Movie;

class MovieTest extends TestCase
{
/**
* @test
*/
public function shouldConstructMovie()
{
$person = new Movie();

$this->assertInstancesOf(
$person,
array(
/** Constructor */
'getGenres' => 'Tmdb\Model\Collection\Genres',
'getProductionCompanies' => 'Tmdb\Model\Common\GenericCollection',
'getProductionCountries' => 'Tmdb\Model\Common\GenericCollection',
'getSpokenLanguages' => 'Tmdb\Model\Common\GenericCollection',
'getAlternativeTitles' => 'Tmdb\Model\Common\GenericCollection',
'getChanges' => 'Tmdb\Model\Common\GenericCollection',
'getCredits' => 'Tmdb\Model\Collection\Credits',
'getImages' => 'Tmdb\Model\Collection\Images',
'getKeywords' => 'Tmdb\Model\Common\GenericCollection',
'getLists' => 'Tmdb\Model\Common\GenericCollection',
'getReleases' => 'Tmdb\Model\Common\GenericCollection',
'getSimilarMovies' => 'Tmdb\Model\Common\GenericCollection',
'getTrailers' => 'Tmdb\Model\Common\GenericCollection',
'getTranslations' => 'Tmdb\Model\Common\GenericCollection',
)
);
}
}
35 changes: 35 additions & 0 deletions test/Tmdb/Tests/Model/PersonTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
/**
* This file is part of the Tmdb PHP API created by Michael Roterman.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @package Tmdb
* @author Michael Roterman <[email protected]>
* @copyright (c) 2013, Michael Roterman
* @version 0.0.1
*/
namespace Tmdb\Tests\Model;

use Tmdb\Model\Person;

class GenreTest extends TestCase
{
/**
* @test
*/
public function shouldConstructGenres()
{
$person = new Person();

$this->assertInstancesOf(
$person,
array(
'getCredits' => 'Tmdb\Model\Collection\Credits',
'getImages' => 'Tmdb\Model\Collection\Images',
'getChanges' => 'Tmdb\Model\Common\GenericCollection'
)
);
}
}
19 changes: 19 additions & 0 deletions test/Tmdb/Tests/Model/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
/**
* This file is part of the Tmdb PHP API created by Michael Roterman.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @package Tmdb
* @author Michael Roterman <[email protected]>
* @copyright (c) 2013, Michael Roterman
* @version 0.0.1
*/
namespace Tmdb\Tests\Model;

use Tmdb\Tests\TestCase as Base;

abstract class TestCase extends Base
{
}
36 changes: 36 additions & 0 deletions test/Tmdb/Tests/Model/Tv/TvEpisodeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
/**
* This file is part of the Tmdb PHP API created by Michael Roterman.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @package Tmdb
* @author Michael Roterman <[email protected]>
* @copyright (c) 2013, Michael Roterman
* @version 0.0.1
*/
namespace Tmdb\Tests\Model\Tv;

use Tmdb\Tests\Model\TestCase;
use Tmdb\Model\Tv\Episode;

class TvEpisodeTest extends TestCase
{
/**
* @test
*/
public function shouldConstructTvEpisode()
{
$episode = new Episode();

$this->assertInstancesOf(
$episode,
array(
'getCredits' => 'Tmdb\Model\Collection\Credits',
'getExternalIds' => 'Tmdb\Model\Tv\ExternalIds',
'getImages' => 'Tmdb\Model\Collection\Images',
)
);
}
}
37 changes: 37 additions & 0 deletions test/Tmdb/Tests/Model/Tv/TvSeasonTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
/**
* This file is part of the Tmdb PHP API created by Michael Roterman.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @package Tmdb
* @author Michael Roterman <[email protected]>
* @copyright (c) 2013, Michael Roterman
* @version 0.0.1
*/
namespace Tmdb\Tests\Model\Tv;

use Tmdb\Tests\Model\TestCase;
use Tmdb\Model\Tv\Season;

class TvSeasonTest extends TestCase
{
/**
* @test
*/
public function shouldConstructTvSeason()
{
$season = new Season();

$this->assertInstancesOf(
$season,
array(
'getCredits' => 'Tmdb\Model\Collection\Credits',
'getExternalIds' => 'Tmdb\Model\Tv\ExternalIds',
'getImages' => 'Tmdb\Model\Collection\Images',
'getEpisodes' => 'Tmdb\Model\Common\GenericCollection',
)
);
}
}
Loading

0 comments on commit f73fc2a

Please sign in to comment.