diff --git a/lib/Tmdb/Api/Genres.php b/lib/Tmdb/Api/Genres.php index da81d28a..b72dbc41 100644 --- a/lib/Tmdb/Api/Genres.php +++ b/lib/Tmdb/Api/Genres.php @@ -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; @@ -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; + } } diff --git a/lib/Tmdb/Model/Tv.php b/lib/Tmdb/Model/Tv.php index f3b18905..072b75f9 100644 --- a/lib/Tmdb/Model/Tv.php +++ b/lib/Tmdb/Model/Tv.php @@ -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(); diff --git a/test/Tmdb/Tests/Api/GenresTest.php b/test/Tmdb/Tests/Api/GenresTest.php index 81b6fa1f..4a715b92 100644 --- a/test/Tmdb/Tests/Api/GenresTest.php +++ b/test/Tmdb/Tests/Api/GenresTest.php @@ -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'; } -} \ No newline at end of file +} diff --git a/test/Tmdb/Tests/Api/TestCase.php b/test/Tmdb/Tests/Api/TestCase.php index 3009c01a..d4888f3f 100644 --- a/test/Tmdb/Tests/Api/TestCase.php +++ b/test/Tmdb/Tests/Api/TestCase.php @@ -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'); @@ -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')); } } \ No newline at end of file diff --git a/test/Tmdb/Tests/ClientTest.php b/test/Tmdb/Tests/ClientTest.php index fd5eedb2..3065d6a2 100644 --- a/test/Tmdb/Tests/ClientTest.php +++ b/test/Tmdb/Tests/ClientTest.php @@ -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()); } -} \ No newline at end of file + /** + * @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', + ) + ); + } +} diff --git a/test/Tmdb/Tests/Factory/GenreFactoryTest.php b/test/Tmdb/Tests/Factory/GenreFactoryTest.php index a5a3a832..6234e438 100644 --- a/test/Tmdb/Tests/Factory/GenreFactoryTest.php +++ b/test/Tmdb/Tests/Factory/GenreFactoryTest.php @@ -12,8 +12,6 @@ */ namespace Tmdb\Tests\Factory; -use Tmdb\Factory\GenreFactory; - class GenreFactoryTest extends TestCase { const GENRE_ID = 28; diff --git a/test/Tmdb/Tests/Model/MovieTest.php b/test/Tmdb/Tests/Model/MovieTest.php new file mode 100644 index 00000000..938fa573 --- /dev/null +++ b/test/Tmdb/Tests/Model/MovieTest.php @@ -0,0 +1,47 @@ + + * @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', + ) + ); + } +} \ No newline at end of file diff --git a/test/Tmdb/Tests/Model/PersonTest.php b/test/Tmdb/Tests/Model/PersonTest.php new file mode 100644 index 00000000..424101fc --- /dev/null +++ b/test/Tmdb/Tests/Model/PersonTest.php @@ -0,0 +1,35 @@ + + * @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' + ) + ); + } +} \ No newline at end of file diff --git a/test/Tmdb/Tests/Model/TestCase.php b/test/Tmdb/Tests/Model/TestCase.php new file mode 100644 index 00000000..7f77ef78 --- /dev/null +++ b/test/Tmdb/Tests/Model/TestCase.php @@ -0,0 +1,19 @@ + + * @copyright (c) 2013, Michael Roterman + * @version 0.0.1 + */ +namespace Tmdb\Tests\Model; + +use Tmdb\Tests\TestCase as Base; + +abstract class TestCase extends Base +{ +} diff --git a/test/Tmdb/Tests/Model/Tv/TvEpisodeTest.php b/test/Tmdb/Tests/Model/Tv/TvEpisodeTest.php new file mode 100644 index 00000000..1c5775a8 --- /dev/null +++ b/test/Tmdb/Tests/Model/Tv/TvEpisodeTest.php @@ -0,0 +1,36 @@ + + * @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', + ) + ); + } +} \ No newline at end of file diff --git a/test/Tmdb/Tests/Model/Tv/TvSeasonTest.php b/test/Tmdb/Tests/Model/Tv/TvSeasonTest.php new file mode 100644 index 00000000..fd88cded --- /dev/null +++ b/test/Tmdb/Tests/Model/Tv/TvSeasonTest.php @@ -0,0 +1,37 @@ + + * @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', + ) + ); + } +} diff --git a/test/Tmdb/Tests/Model/TvTest.php b/test/Tmdb/Tests/Model/TvTest.php new file mode 100644 index 00000000..716cbcd2 --- /dev/null +++ b/test/Tmdb/Tests/Model/TvTest.php @@ -0,0 +1,43 @@ + + * @copyright (c) 2013, Michael Roterman + * @version 0.0.1 + */ +namespace Tmdb\Tests\Model; + +use Tmdb\Model\Tv; + +class TveTest extends TestCase +{ + /** + * @test + */ + public function shouldConstructMovie() + { + $tv = new Tv(); + + $this->assertInstancesOf( + $tv, + array( + 'getCreatedBy' => 'Tmdb\Model\Collection\Images', + 'getEpisodeRuntime' => 'Tmdb\Model\Common\GenericCollection', + 'getGenres' => 'Tmdb\Model\Collection\Genres', + 'getLanguages' => 'Tmdb\Model\Common\GenericCollection', + 'getNetworks' => 'Tmdb\Model\Common\GenericCollection', + 'getOriginCountry' => 'Tmdb\Model\Common\GenericCollection', + 'getSeasons' => 'Tmdb\Model\Common\GenericCollection', + 'getCredits' => 'Tmdb\Model\Collection\Credits', + 'getExternalIds' => 'Tmdb\Model\Tv\ExternalIds', + 'getImages' => 'Tmdb\Model\Collection\Images', + 'getTranslations' => 'Tmdb\Model\Common\GenericCollection', + ) + ); + } +} \ No newline at end of file diff --git a/test/Tmdb/Tests/TestCase.php b/test/Tmdb/Tests/TestCase.php new file mode 100644 index 00000000..479fd8b3 --- /dev/null +++ b/test/Tmdb/Tests/TestCase.php @@ -0,0 +1,39 @@ + + * @copyright (c) 2013, Michael Roterman + * @version 0.0.1 + */ +namespace Tmdb\Tests; + +abstract class TestCase extends \PHPUnit_Framework_TestCase +{ + /** + * Assert that an array of methods and corresponding classes match + * + * @param $subject + * @param array $instances + * @throws \Exception + */ + protected function assertInstancesOf($subject, array $instances = array()) + { + foreach($instances as $method => $instance) { + try { + $this->assertInstanceOf($instance, $subject->$method()); + } + catch(\Exception $e){ + throw new \Exception(sprintf( + 'Failed asserting that calling "%s" returns an instance of expected "%s".', + sprintf('%s::%s', get_class($subject), $method), + $instance + )); + } + } + } +}