From c9c8e642de23cca9712857f4a6fe9805ecd65cb6 Mon Sep 17 00:00:00 2001 From: Michael Roterman Date: Sun, 2 Feb 2014 22:04:16 +0100 Subject: [PATCH] Fixing up more unit tests --- lib/Tmdb/Factory/GenreFactory.php | 2 +- lib/Tmdb/Factory/ImageFactory.php | 2 +- lib/Tmdb/Model/Collection/Keywords.php | 4 +- test/Tmdb/Tests/Factory/GenreFactoryTest.php | 33 +++++++- test/Tmdb/Tests/Factory/ImageFactoryTest.php | 29 +++++++ test/Tmdb/Tests/Factory/MovieFactoryTest.php | 34 ++++++++ .../Tests/Model/Collection/KeywordsTest.php | 52 ++++++++++++ .../Tests/Model/Collection/PeopleTest.php | 52 ++++++++++++ test/Tmdb/Tests/Model/ImageTest.php | 83 +++++++++++++++++++ 9 files changed, 285 insertions(+), 6 deletions(-) create mode 100644 test/Tmdb/Tests/Factory/ImageFactoryTest.php create mode 100644 test/Tmdb/Tests/Model/Collection/KeywordsTest.php create mode 100644 test/Tmdb/Tests/Model/Collection/PeopleTest.php create mode 100644 test/Tmdb/Tests/Model/ImageTest.php diff --git a/lib/Tmdb/Factory/GenreFactory.php b/lib/Tmdb/Factory/GenreFactory.php index 96776cfa..9e73b5f2 100644 --- a/lib/Tmdb/Factory/GenreFactory.php +++ b/lib/Tmdb/Factory/GenreFactory.php @@ -33,7 +33,7 @@ public function createCollection(array $data = array()) $collection = new Genres(); foreach($data as $item) { - $collection->add(null, $this->create((array) $item)); + $collection->addGenre($this->create((array) $item)); } return $collection; diff --git a/lib/Tmdb/Factory/ImageFactory.php b/lib/Tmdb/Factory/ImageFactory.php index 38ccf8e4..10f05f86 100644 --- a/lib/Tmdb/Factory/ImageFactory.php +++ b/lib/Tmdb/Factory/ImageFactory.php @@ -132,7 +132,7 @@ public function createImageCollection(array $data = array()) if (array_key_exists($format, Image::$_formats)) { $item = $this->create($item, $format); - $collection->add(null, $item); + $collection->addImage($item); } } } diff --git a/lib/Tmdb/Model/Collection/Keywords.php b/lib/Tmdb/Model/Collection/Keywords.php index eac8d594..d76e0c96 100644 --- a/lib/Tmdb/Model/Collection/Keywords.php +++ b/lib/Tmdb/Model/Collection/Keywords.php @@ -10,10 +10,10 @@ * @copyright (c) 2013, Michael Roterman * @version 0.0.1 */ -namespace Tmdb\Model\Common\Collection; +namespace Tmdb\Model\Collection; use Tmdb\Model\Common\GenericCollection; -use Tmdb\Model\Keyword; +use Tmdb\Model\Movie\Keyword; class Keywords extends GenericCollection { diff --git a/test/Tmdb/Tests/Factory/GenreFactoryTest.php b/test/Tmdb/Tests/Factory/GenreFactoryTest.php index 64fa0ce4..502a2448 100644 --- a/test/Tmdb/Tests/Factory/GenreFactoryTest.php +++ b/test/Tmdb/Tests/Factory/GenreFactoryTest.php @@ -12,6 +12,8 @@ */ namespace Tmdb\Tests\Factory; +use Tmdb\Model\Collection\Genres; + class GenreFactoryTest extends TestCase { const GENRE_ID = 28; @@ -46,14 +48,41 @@ public function shouldFilter() $factory = $this->getFactory(); $data = $this->loadByFile('genre/result.json'); - $collection = $factory->createCollection($data['genres']); - $filteredGenres = $collection->filterId(self::GENRE_ID); + /** + * @var Genres $genres + */ + $genres = $factory->createCollection($data['genres']); + + $filteredGenres = $genres->filterId(self::GENRE_ID); foreach($filteredGenres as $filteredGenre) { $this->assertEquals('Action', $filteredGenre->getName()); } } + /** + * @test + */ + public function shouldCollaborateWithCollection() + { + $factory = $this->getFactory(); + $data = $this->loadByFile('genre/result.json'); + + /** + * @var Genres $genres + */ + $genres = $factory->createCollection($data['genres']); + + + $this->assertEquals(count($data['genres']), count($genres->getGenres())); + + $genre = $genres->getGenre(28); + $this->assertEquals('Action', $genre->getName()); + + $genre = $genres->getGenre(-1); + $this->assertEquals(null, $genre); + } + protected function getFactoryClass() { return 'Tmdb\Factory\GenreFactory'; diff --git a/test/Tmdb/Tests/Factory/ImageFactoryTest.php b/test/Tmdb/Tests/Factory/ImageFactoryTest.php new file mode 100644 index 00000000..10ddbcd6 --- /dev/null +++ b/test/Tmdb/Tests/Factory/ImageFactoryTest.php @@ -0,0 +1,29 @@ + + * @copyright (c) 2013, Michael Roterman + * @version 0.0.1 + */ +namespace Tmdb\Tests\Factory; + +class ImageFactoryTest extends TestCase +{ + /** + * @test + */ + public function shouldConstructGenres() + { + $this->assertEquals(true,true); + } + + protected function getFactoryClass() + { + return 'Tmdb\Factory\ImageFactory'; + } +} diff --git a/test/Tmdb/Tests/Factory/MovieFactoryTest.php b/test/Tmdb/Tests/Factory/MovieFactoryTest.php index e0f50146..c0b430d2 100644 --- a/test/Tmdb/Tests/Factory/MovieFactoryTest.php +++ b/test/Tmdb/Tests/Factory/MovieFactoryTest.php @@ -63,9 +63,43 @@ public function shouldBeAbleToSetFactories() $this->assertInstanceOf('stdClass', $factory->getImageFactory()); } + /** + * @test + */ public function shouldBeFunctional() { $this->assertEquals(false, $this->movie->getAdult()); + $this->assertEquals('/7DlIoyQ3ecGMklVWyKsneZmVnsi.jpg', $this->movie->getBackdropPath()); + $this->assertInstanceOf('Tmdb\Model\Image\BackdropImage', $this->movie->getBackdrop()); + $this->assertEquals(true, is_array($this->movie->getBelongsToCollection())); + $this->assertEquals(true, is_int($this->movie->getBudget())); + $this->assertInstanceOf('Tmdb\Model\Collection\Genres', $this->movie->getGenres()); + $this->assertEquals('http://www.riddick-movie.com', $this->movie->getHomepage()); + $this->assertEquals(87421, $this->movie->getId()); + $this->assertEquals('Riddick', $this->movie->getOriginalTitle()); + $this->assertEquals('Betrayed by his own kind and left for dead on a desolate planet, Riddick fights for survival against alien predators and becomes more powerful and dangerous than ever before. Soon bounty hunters from throughout the galaxy descend on Riddick only to find themselves pawns in his greater scheme for revenge. With his enemies right where he wants them, Riddick unleashes a vicious attack of vengeance before returning to his home planet of Furya to save it from destruction.', $this->movie->getOverview()); + $this->assertEquals(93.491722439366, $this->movie->getPopularity()); + $this->assertInstanceOf('Tmdb\Model\Image\PosterImage', $this->movie->getPoster()); + $this->assertEquals('/1NfhdnQAEqcBRCulEhOFSkRrrLv.jpg', $this->movie->getPosterPath()); + $this->assertInstanceOf('Tmdb\Model\Common\GenericCollection', $this->movie->getProductionCompanies()); + $this->assertInstanceOf('Tmdb\Model\Common\GenericCollection', $this->movie->getProductionCountries()); + // release + $this->assertEquals(42025135, $this->movie->getRevenue()); + $this->assertEquals(119, $this->movie->getRuntime()); + $this->assertInstanceOf('Tmdb\Model\Common\GenericCollection', $this->movie->getSpokenLanguages()); + $this->assertEquals('Released', $this->movie->getStatus()); + $this->assertEquals('Survival Is His Revenge', $this->movie->getTagline()); + $this->assertEquals(6.2, $this->movie->getVoteAverage()); + $this->assertEquals(625, $this->movie->getVoteCount()); + $this->assertInstanceOf('Tmdb\Model\Common\GenericCollection', $this->movie->getAlternativeTitles()); + $this->assertInstanceOf('Tmdb\Model\Common\GenericCollection', $this->movie->getChanges()); + $this->assertInstanceOf('Tmdb\Model\Collection\Credits', $this->movie->getCredits()); + $this->assertInstanceOf('Tmdb\Model\Collection\Images', $this->movie->getImages()); + $this->assertInstanceOf('Tmdb\Model\Common\GenericCollection', $this->movie->getKeywords()); + $this->assertInstanceOf('Tmdb\Model\Common\GenericCollection', $this->movie->getLists()); + $this->assertInstanceOf('Tmdb\Model\Common\GenericCollection', $this->movie->getReleases()); + $this->assertInstanceOf('Tmdb\Model\Common\GenericCollection', $this->movie->getSimilarMovies()); + $this->assertInstanceOf('Tmdb\Model\Common\GenericCollection', $this->movie->getTrailers()); } protected function getFactoryClass() diff --git a/test/Tmdb/Tests/Model/Collection/KeywordsTest.php b/test/Tmdb/Tests/Model/Collection/KeywordsTest.php new file mode 100644 index 00000000..72f3f32b --- /dev/null +++ b/test/Tmdb/Tests/Model/Collection/KeywordsTest.php @@ -0,0 +1,52 @@ + + * @copyright (c) 2013, Michael Roterman + * @version 0.0.1 + */ +namespace Tmdb\Tests\Model\Common; + +use Tmdb\Model\Collection\Keywords; +use Tmdb\Model\Movie\Keyword; +use Tmdb\Tests\Model\TestCase; + +class KeywordsTest extends TestCase +{ + /** + * @var Keywords + */ + private $collection; + + private $keywords = array( + array('id' => 1, 'name' => 'dark'), + array('id' => 2, 'name' => 'light') + ); + + public function setUp() + { + $this->collection = new Keywords(); + + foreach($this->keywords as $keyword) { + $object = $this->hydrate(new Keyword(), $keyword); + + $this->collection->addKeyword($object); + } + } + + /** + * @test + */ + public function shouldGetAndSet() + { + $this->assertEquals(count($this->keywords), count($this->collection->getKeywords())); + + $this->assertEquals('dark', $this->collection->getKeyword(1)->getName()); + $this->assertEquals(null, $this->collection->getKeyword(3)); + } +} diff --git a/test/Tmdb/Tests/Model/Collection/PeopleTest.php b/test/Tmdb/Tests/Model/Collection/PeopleTest.php new file mode 100644 index 00000000..ad5a1686 --- /dev/null +++ b/test/Tmdb/Tests/Model/Collection/PeopleTest.php @@ -0,0 +1,52 @@ + + * @copyright (c) 2013, Michael Roterman + * @version 0.0.1 + */ +namespace Tmdb\Tests\Model\Common; + +use Tmdb\Model\Collection\People; +use Tmdb\Model\Person; +use Tmdb\Tests\Model\TestCase; + +class PeopleTest extends TestCase +{ + /** + * @var People + */ + private $collection; + + private $people = array( + array('id' => 1, 'name' => 'james blunt'), + array('id' => 2, 'name' => 'afrojack') + ); + + public function setUp() + { + $this->collection = new People(); + + foreach($this->people as $keyword) { + $object = $this->hydrate(new Person(), $keyword); + + $this->collection->addPerson($object); + } + } + + /** + * @test + */ + public function shouldGetAndSet() + { + $this->assertEquals(count($this->people), count($this->collection->getPeople())); + + $this->assertEquals('james blunt', $this->collection->getPerson(1)->getName()); + $this->assertEquals(null, $this->collection->getPerson(3)); + } +} diff --git a/test/Tmdb/Tests/Model/ImageTest.php b/test/Tmdb/Tests/Model/ImageTest.php new file mode 100644 index 00000000..b125e576 --- /dev/null +++ b/test/Tmdb/Tests/Model/ImageTest.php @@ -0,0 +1,83 @@ + + * @copyright (c) 2013, Michael Roterman + * @version 0.0.1 + */ +namespace Tmdb\Tests\Model; + +use Tmdb\Factory\ImageFactory; +use Tmdb\Model\Collection\Images; +use Tmdb\Model\Image; + +class ImageTest extends TestCase +{ + /** + * @var Images + */ + private $collection; + + private $images = array( + array( + 'file_path' => '/abc.jpg', + 'width' => 1000, + 'height' => 750, + 'iso_639_1' => 'en', + 'aspect_ratio' => 0.75, + 'vote_average' => 2.25, + 'vote_count' => 25 + ), + ); + + public function setUp() + { + $this->collection = new Images(); + + foreach($this->images as $image) { + $factory = new ImageFactory(); + $object = $factory->create($image); + + $this->collection->addImage($object); + } + } + + /** + * @test + */ + public function shouldGetAndSet() + { + $this->assertEquals(count($this->images), count($this->collection->getImages())); + + /** + * @var Image $subject + */ + $subject = array_pop($this->collection->toArray()); + + $this->assertEquals('/abc.jpg', $subject->getFilePath()); + $this->assertEquals(1000, $subject->getWidth()); + $this->assertEquals(750, $subject->getHeight()); + $this->assertEquals('en', $subject->getIso6391()); + $this->assertEquals(0.75, $subject->getAspectRatio()); + $this->assertEquals(2.25, $subject->getVoteAverage()); + $this->assertEquals(25, $subject->getVoteCount()); + } + + /** + * @test + */ + public function shouldReturnCorrectTypes() + { + $this->assertEquals('poster', Image::getTypeFromCollectionName('posters')); + $this->assertEquals('backdrop', Image::getTypeFromCollectionName('backdrops')); + $this->assertEquals('profile', Image::getTypeFromCollectionName('profiles')); + $this->assertEquals('logo', Image::getTypeFromCollectionName('logos')); + $this->assertEquals('still', Image::getTypeFromCollectionName('stills')); + $this->assertEquals(null, Image::getTypeFromCollectionName('sheeps')); + } +}