diff --git a/lib/Tmdb/Common/ObjectHydrator.php b/lib/Tmdb/Common/ObjectHydrator.php index bd9bea0d..ba6ab3b2 100644 --- a/lib/Tmdb/Common/ObjectHydrator.php +++ b/lib/Tmdb/Common/ObjectHydrator.php @@ -40,15 +40,15 @@ public static function hydrate(AbstractModel $object, $data = array()) sprintf('set_%s', $k) ); - if (!method_exists($object, $method)) { + if (!is_callable(array($object, $method))) { throw new RuntimeException(sprintf( 'Trying to call method "%s" on "%s" but it does not exist or is private.', $method, get_class($object) )); + }else{ + $object->$method($v); } - - $object->$method($v); } } } @@ -77,20 +77,4 @@ private function camelize($candidate) ) ); } - - /** - * Transforms a camelCasedString to an under_scored_one - * - * @see https://gist.github.com/troelskn/751517 - * - * @param $camelized - * @return string - */ - private function uncamelize($camelized) { - return implode('_', - array_map('strtolower', - preg_split('/([A-Z]{1}[^A-Z]*)/', $camelized, -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY) - ) - ); - } } \ No newline at end of file diff --git a/lib/Tmdb/Repository/AbstractRepository.php b/lib/Tmdb/Repository/AbstractRepository.php index 5b86da8e..4ffcbec4 100644 --- a/lib/Tmdb/Repository/AbstractRepository.php +++ b/lib/Tmdb/Repository/AbstractRepository.php @@ -78,16 +78,6 @@ protected function parseHeaders(array $headers = array()) return $headers; } - /** - * @param null|\Tmdb\Factory\FactoryInterface $factory - * @return $this - */ - public function setFactory($factory) - { - $this->factory = $factory; - return $this; - } - /** * Return the API Class * diff --git a/lib/Tmdb/Repository/ConfigurationRepository.php b/lib/Tmdb/Repository/ConfigurationRepository.php index d4e388b4..f6b47392 100644 --- a/lib/Tmdb/Repository/ConfigurationRepository.php +++ b/lib/Tmdb/Repository/ConfigurationRepository.php @@ -26,7 +26,7 @@ class ConfigurationRepository extends AbstractRepository { public function load(array $headers = array()) { $data = $this->getApi()->getConfiguration($headers); - return $this->factory->create($data); + return $this->getFactory()->create($data); } /** diff --git a/lib/Tmdb/Repository/DiscoverRepository.php b/lib/Tmdb/Repository/DiscoverRepository.php index afef904d..2e5d5738 100644 --- a/lib/Tmdb/Repository/DiscoverRepository.php +++ b/lib/Tmdb/Repository/DiscoverRepository.php @@ -23,25 +23,6 @@ use Tmdb\Model\Tv; class DiscoverRepository extends AbstractRepository { - /** - * @var MovieFactory - */ - private $movieFactory; - - /** - * @var TvFactory - */ - private $tvFactory; - - /** - * Constructor - */ - public function __construct() - { - $this->movieFactory = new MovieFactory(); - $this->tvFactory = new TvFactory(); - } - /** * Discover movies by different types of data like average rating, number of votes, genres and certifications. * @@ -95,32 +76,12 @@ public function getFactory(){ throw new NotImplementedException('Discover does not support a generic factory.'); } - /** - * @param \Tmdb\Factory\MovieFactory $movieFactory - * @return $this - */ - public function setMovieFactory($movieFactory) - { - $this->movieFactory = $movieFactory; - return $this; - } - /** * @return \Tmdb\Factory\MovieFactory */ public function getMovieFactory() { - return $this->movieFactory; - } - - /** - * @param \Tmdb\Factory\TvFactory $tvFactory - * @return $this - */ - public function setTvFactory($tvFactory) - { - $this->tvFactory = $tvFactory; - return $this; + return new MovieFactory(); } /** @@ -128,6 +89,6 @@ public function setTvFactory($tvFactory) */ public function getTvFactory() { - return $this->tvFactory; + return new TvFactory(); } } diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 26e4417a..122309f9 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -33,7 +33,5 @@ - - \ No newline at end of file diff --git a/test/Tmdb/Tests/ApiTokenTest.php b/test/Tmdb/Tests/ApiTokenTest.php new file mode 100644 index 00000000..afa4fbe0 --- /dev/null +++ b/test/Tmdb/Tests/ApiTokenTest.php @@ -0,0 +1,25 @@ + + * @copyright (c) 2013, Michael Roterman + * @version 0.0.1 + */ +class ApiTokenTest extends \PHPUnit_Framework_TestCase +{ + /** + * @test + */ + public function testSetGet() + { + $token = new \Tmdb\ApiToken(); + $token->setApiToken('abcdefg'); + + $this->assertEquals('abcdefg', $token->getApiToken()); + } +} diff --git a/test/Tmdb/Tests/ClientTest.php b/test/Tmdb/Tests/ClientTest.php index d2bcae9b..0f4c60fd 100644 --- a/test/Tmdb/Tests/ClientTest.php +++ b/test/Tmdb/Tests/ClientTest.php @@ -22,4 +22,17 @@ public function shouldNotHaveToPassHttpClientToConstructor() $this->assertInstanceOf('Tmdb\HttpClient\HttpClient', $client->getHttpClient()); } + + /** + * @test + */ + public function clearHeadersFunctionActuallyClears() + { + $token = new \Tmdb\ApiToken('abcdef'); + $client = new \Tmdb\Client($token); + + $headers = array( + 'a' => 'b' + ); + } } \ No newline at end of file diff --git a/test/Tmdb/Tests/Common/ObjectHydratorTest.php b/test/Tmdb/Tests/Common/ObjectHydratorTest.php new file mode 100644 index 00000000..35a671b8 --- /dev/null +++ b/test/Tmdb/Tests/Common/ObjectHydratorTest.php @@ -0,0 +1,85 @@ + + * @copyright (c) 2013, Michael Roterman + * @version 0.0.1 + */ +class ObjectHydratorTest extends \PHPUnit_Framework_TestCase +{ + /** + * @test + */ + public function canHydrateObject() + { + $subject = \Tmdb\Common\ObjectHydrator::hydrate(new TestModel(), array( + 'id' => 15, + 'name' => 'Michael' + )); + + $this->assertInstanceOf('TestModel', $subject); + $this->assertEquals(15, $subject->getId()); + $this->assertEquals('Michael', $subject->getName()); + } + + /** + * @expectedException \Tmdb\Exception\RuntimeException + * @test + */ + public function callingNonExistingMethodThrowsException() + { + \Tmdb\Common\ObjectHydrator::hydrate(new FailingTestModel(), array('lastname' => 'Roterman')); + } +} + +class TestModel extends \Tmdb\Model\AbstractModel { + private $id; + private $name; + + static $_properties = array('id', 'name'); + + /** + * @param mixed $id + * @return $this + */ + public function setId($id) + { + $this->id = $id; + return $this; + } + + /** + * @return mixed + */ + public function getId() + { + return $this->id; + } + + /** + * @param mixed $name + * @return $this + */ + public function setName($name) + { + $this->name = $name; + return $this; + } + + /** + * @return mixed + */ + public function getName() + { + return $this->name; + } +} + +class FailingTestModel extends \Tmdb\Model\AbstractModel { + static $_properties = array('lastname'); +} \ No newline at end of file diff --git a/test/Tmdb/Tests/Repository/CollectionRepositoryTest.php b/test/Tmdb/Tests/Repository/CollectionRepositoryTest.php new file mode 100644 index 00000000..c4e38a22 --- /dev/null +++ b/test/Tmdb/Tests/Repository/CollectionRepositoryTest.php @@ -0,0 +1,48 @@ + + * @copyright (c) 2013, Michael Roterman + * @version 0.0.1 + */ +namespace Tmdb\Tests\Repository; + +class CollectionRepositoryTest extends TestCase +{ + const COLLECTION_ID = 120; + + /** + * @test + */ + public function shouldLoadCollection() + { + $repository = $this->getRepositoryWithMockedHttpClient(); + + $repository->load(self::COLLECTION_ID); + } + + /** + * @test + */ + public function shouldGetImages() + { + $repository = $this->getRepositoryWithMockedHttpClient(); + + $repository->load(self::COLLECTION_ID); + } + + protected function getApiClass() + { + return 'Tmdb\Api\Collection'; + } + + protected function getRepositoryClass() + { + return 'Tmdb\Repository\CollectionRepository'; + } +} \ No newline at end of file diff --git a/test/Tmdb/Tests/Repository/CompanyRepositoryTest.php b/test/Tmdb/Tests/Repository/CompanyRepositoryTest.php new file mode 100644 index 00000000..0c00f917 --- /dev/null +++ b/test/Tmdb/Tests/Repository/CompanyRepositoryTest.php @@ -0,0 +1,38 @@ + + * @copyright (c) 2013, Michael Roterman + * @version 0.0.1 + */ +namespace Tmdb\Tests\Repository; + +class CompanyRepositoryTest extends TestCase +{ + const COMPANY_ID = 120; + + /** + * @test + */ + public function shouldLoadCompany() + { + $repository = $this->getRepositoryWithMockedHttpClient(); + + $repository->load(self::COMPANY_ID); + } + + protected function getApiClass() + { + return 'Tmdb\Api\Company'; + } + + protected function getRepositoryClass() + { + return 'Tmdb\Repository\CompanyRepository'; + } +} \ No newline at end of file diff --git a/test/Tmdb/Tests/Repository/ConfigurationRepositoryTest.php b/test/Tmdb/Tests/Repository/ConfigurationRepositoryTest.php new file mode 100644 index 00000000..9382e2ca --- /dev/null +++ b/test/Tmdb/Tests/Repository/ConfigurationRepositoryTest.php @@ -0,0 +1,36 @@ + + * @copyright (c) 2013, Michael Roterman + * @version 0.0.1 + */ +namespace Tmdb\Tests\Repository; + +class ConfigurationRepositoryTest extends TestCase +{ + /** + * @test + */ + public function shouldLoadConfiguration() + { + $repository = $this->getRepositoryWithMockedHttpClient(); + + $repository->load(); + } + + protected function getApiClass() + { + return 'Tmdb\Api\Configuration'; + } + + protected function getRepositoryClass() + { + return 'Tmdb\Repository\ConfigurationRepository'; + } +} \ No newline at end of file diff --git a/test/Tmdb/Tests/Repository/DiscoverRepositoryTest.php b/test/Tmdb/Tests/Repository/DiscoverRepositoryTest.php new file mode 100644 index 00000000..e7e8fec2 --- /dev/null +++ b/test/Tmdb/Tests/Repository/DiscoverRepositoryTest.php @@ -0,0 +1,54 @@ + + * @copyright (c) 2013, Michael Roterman + * @version 0.0.1 + */ +namespace Tmdb\Tests\Repository; + +use Tmdb\Model\Query\Discover\DiscoverMoviesQuery; +use Tmdb\Model\Query\Discover\DiscoverTvQuery; + +class DiscoverRepositoryTest extends TestCase +{ + + /** + * @test + */ + public function shouldDiscoverMovies() + { + $repository = $this->getRepositoryWithMockedHttpClient(); + + $query = new DiscoverMoviesQuery(); + + $repository->discoverMovies($query); + } + + /** + * @test + */ + public function shouldDiscoverTv() + { + $repository = $this->getRepositoryWithMockedHttpClient(); + + $query = new DiscoverTvQuery(); + + $repository->discoverTv($query); + } + + protected function getApiClass() + { + return 'Tmdb\Api\Discover'; + } + + protected function getRepositoryClass() + { + return 'Tmdb\Repository\DiscoverRepository'; + } +} \ No newline at end of file diff --git a/test/Tmdb/Tests/Repository/GenreRepositoryTest.php b/test/Tmdb/Tests/Repository/GenreRepositoryTest.php new file mode 100644 index 00000000..84a94864 --- /dev/null +++ b/test/Tmdb/Tests/Repository/GenreRepositoryTest.php @@ -0,0 +1,48 @@ + + * @copyright (c) 2013, Michael Roterman + * @version 0.0.1 + */ +namespace Tmdb\Tests\Repository; + +class GenreRepositoryTest extends TestCase +{ + const GENRE_ID = 28; + + /** + * @test + */ + public function shouldLoadGenre() + { + $repository = $this->getRepositoryWithMockedHttpClient(); + + $repository->load(self::GENRE_ID); + } + + /** + * @test + */ + public function shouldLoadCollection() + { + $repository = $this->getRepositoryWithMockedHttpClient(); + + $repository->loadCollection(); + } + + protected function getApiClass() + { + return 'Tmdb\Api\Genres'; + } + + protected function getRepositoryClass() + { + return 'Tmdb\Repository\GenreRepository'; + } +} \ No newline at end of file diff --git a/test/Tmdb/Tests/Repository/MovieRepositoryTest.php b/test/Tmdb/Tests/Repository/MovieRepositoryTest.php index 30ca309b..16f5ab52 100644 --- a/test/Tmdb/Tests/Repository/MovieRepositoryTest.php +++ b/test/Tmdb/Tests/Repository/MovieRepositoryTest.php @@ -36,6 +36,46 @@ public function shouldGetLatestMovie() $repository->getLatest(); } + /** + * @test + */ + public function shouldGetUpcoming() + { + $repository = $this->getRepositoryWithMockedHttpClient(); + + $repository->getUpcoming(); + } + + /** + * @test + */ + public function shouldGetNowPlaying() + { + $repository = $this->getRepositoryWithMockedHttpClient(); + + $repository->getNowPlaying(); + } + + /** + * @test + */ + public function shouldGetPopular() + { + $repository = $this->getRepositoryWithMockedHttpClient(); + + $repository->getPopular(); + } + + /** + * @test + */ + public function shouldGetTopRated() + { + $repository = $this->getRepositoryWithMockedHttpClient(); + + $repository->getLatest(); + } + protected function getApiClass() { return 'Tmdb\Api\Movies'; diff --git a/test/Tmdb/Tests/Repository/PeopleRepositoryTest.php b/test/Tmdb/Tests/Repository/PeopleRepositoryTest.php new file mode 100644 index 00000000..5ecce7a8 --- /dev/null +++ b/test/Tmdb/Tests/Repository/PeopleRepositoryTest.php @@ -0,0 +1,38 @@ + + * @copyright (c) 2013, Michael Roterman + * @version 0.0.1 + */ +namespace Tmdb\Tests\Repository; + +class PeopleRepositoryTest extends TestCase +{ + const PERSON_ID = 287; + + /** + * @test + */ + public function shouldLoadPerson() + { + $repository = $this->getRepositoryWithMockedHttpClient(); + + $repository->load(self::PERSON_ID); + } + + protected function getApiClass() + { + return 'Tmdb\Api\People'; + } + + protected function getRepositoryClass() + { + return 'Tmdb\Repository\PeopleRepository'; + } +} \ No newline at end of file diff --git a/test/Tmdb/Tests/Repository/TvEpisodeRepositoryTest.php b/test/Tmdb/Tests/Repository/TvEpisodeRepositoryTest.php new file mode 100644 index 00000000..91d1fb91 --- /dev/null +++ b/test/Tmdb/Tests/Repository/TvEpisodeRepositoryTest.php @@ -0,0 +1,40 @@ + + * @copyright (c) 2013, Michael Roterman + * @version 0.0.1 + */ +namespace Tmdb\Tests\Repository; + +class TvEpisodeRepositoryTest extends TestCase +{ + const TV_ID = 3572; + const SEASON_ID = 1; + const EPISODE_ID = 1; + + /** + * @test + */ + public function shouldLoadTvEpisode() + { + $repository = $this->getRepositoryWithMockedHttpClient(); + + $repository->load(self::TV_ID, self::SEASON_ID, self::EPISODE_ID); + } + + protected function getApiClass() + { + return 'Tmdb\Api\TvEpisode'; + } + + protected function getRepositoryClass() + { + return 'Tmdb\Repository\TvEpisodeRepository'; + } +} \ No newline at end of file diff --git a/test/Tmdb/Tests/Repository/TvRepositoryTest.php b/test/Tmdb/Tests/Repository/TvRepositoryTest.php new file mode 100644 index 00000000..3ee79ba8 --- /dev/null +++ b/test/Tmdb/Tests/Repository/TvRepositoryTest.php @@ -0,0 +1,38 @@ + + * @copyright (c) 2013, Michael Roterman + * @version 0.0.1 + */ +namespace Tmdb\Tests\Repository; + +class TvRepositoryTest extends TestCase +{ + const TV_ID = 3572; + + /** + * @test + */ + public function shouldLoadTv() + { + $repository = $this->getRepositoryWithMockedHttpClient(); + + $repository->load(self::TV_ID); + } + + protected function getApiClass() + { + return 'Tmdb\Api\Tv'; + } + + protected function getRepositoryClass() + { + return 'Tmdb\Repository\TvRepository'; + } +} \ No newline at end of file diff --git a/test/Tmdb/Tests/Repository/TvSeasonRepositoryTest.php b/test/Tmdb/Tests/Repository/TvSeasonRepositoryTest.php new file mode 100644 index 00000000..ce41426a --- /dev/null +++ b/test/Tmdb/Tests/Repository/TvSeasonRepositoryTest.php @@ -0,0 +1,39 @@ + + * @copyright (c) 2013, Michael Roterman + * @version 0.0.1 + */ +namespace Tmdb\Tests\Repository; + +class TvSeasonRepositoryTest extends TestCase +{ + const TV_ID = 3572; + const SEASON_ID = 1; + + /** + * @test + */ + public function shouldLoadTvSeason() + { + $repository = $this->getRepositoryWithMockedHttpClient(); + + $repository->load(self::TV_ID, self::SEASON_ID); + } + + protected function getApiClass() + { + return 'Tmdb\Api\TvSeason'; + } + + protected function getRepositoryClass() + { + return 'Tmdb\Repository\TvSeasonRepository'; + } +} \ No newline at end of file