Skip to content

Commit

Permalink
Updating unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wtfzdotnet committed Jan 27, 2014
1 parent 0a174ac commit 5a7be3a
Show file tree
Hide file tree
Showing 13 changed files with 272 additions and 49 deletions.
10 changes: 9 additions & 1 deletion lib/Tmdb/Repository/CompanyRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ public function getFactory()
return new CompanyFactory();
}

/**
* @return MovieFactory
*/
public function getMovieFactory()
{
return new MovieFactory();
}

/**
* Create an collection of an array
*
Expand All @@ -81,7 +89,7 @@ private function createMovieCollection($data){
}

foreach($data as $item) {
$collection->add(null, MovieFactory::create($item));
$collection->add(null, $this->getMovieFactory()->create($item));
}

return $collection;
Expand Down
23 changes: 1 addition & 22 deletions lib/Tmdb/Repository/TvEpisodeRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@
*/
namespace Tmdb\Repository;

use \RuntimeException;
use Tmdb\Exception\RuntimeException;
use Tmdb\Factory\TvEpisodeFactory;
use Tmdb\Model\Common\GenericCollection;
use Tmdb\Model\Tv\Episode\QueryParameter\AppendToResponse;
use Tmdb\Model\Tv;
use Tmdb\Model\Tv\Season;
Expand Down Expand Up @@ -85,24 +84,4 @@ public function getFactory()
{
return new TvEpisodeFactory();
}

/**
* Create an collection of an array
*
* @param $data
* @return Episode[]
*/
private function createCollection($data){
$collection = new GenericCollection();

if (array_key_exists('results', $data)) {
$data = $data['results'];
}

foreach($data as $item) {
$collection->add(null, $this->getFactory()->create($item));
}

return $collection;
}
}
2 changes: 1 addition & 1 deletion lib/Tmdb/Repository/TvRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ private function createCollection($data){
}

foreach($data as $item) {
$collection->add(null, TvFactory::create($item));
$collection->add(null, $this->getFactory()->create($item));
}

return $collection;
Expand Down
25 changes: 1 addition & 24 deletions lib/Tmdb/Repository/TvSeasonRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@
*/
namespace Tmdb\Repository;

use \RuntimeException;
use Tmdb\Exception\RuntimeException;

use Tmdb\Factory\TvSeasonFactory;
use Tmdb\Model\Common\GenericCollection;

use \Tmdb\Model\Tv\Season\QueryParameter\AppendToResponse;
use Tmdb\Model\Tv\Season;
Expand Down Expand Up @@ -81,26 +80,4 @@ public function getFactory()
{
return new TvSeasonFactory();
}

/**
* Create an collection of an array
*
* @todo Allow an array of Season objects to pass ( custom collection )
*
* @param $data
* @return Season[]
*/
private function createCollection($data){
$collection = new GenericCollection();

if (array_key_exists('results', $data)) {
$data = $data['results'];
}

foreach($data as $item) {
$collection->add(null, $this->getFactory()->create($item));
}

return $collection;
}
}
37 changes: 37 additions & 0 deletions test/Tmdb/Tests/Factory/GenreFactoryTest.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\Factory;

class GenreFactoryTest extends TestCase
{
const GENRE_ID = 28;

/**
* @test
*/
public function shouldConstructGenre()
{
$object = $this->loadByFile(
'file.json'
);

$this->assertInstanceOf('Tmdb\Model\Genre', $object);
$this->assertEquals('28', $object->getId());
$this->assertEquals('Action', $object->getName());
}

protected function getFactoryClass()
{
return 'Tmdb\Factory\GenreFactory';
}
}
31 changes: 31 additions & 0 deletions test/Tmdb/Tests/Factory/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?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\Factory;

abstract class TestCase extends \PHPUnit_Framework_TestCase
{
protected $factory;

protected function loadByFile($file)
{
$class = $this->getFactoryClass();
$factory = new $class();

//$data = json_decode(file_get_contents($file));
$data = array();

return $factory->create($data);
}

abstract protected function getFactoryClass();
}
64 changes: 64 additions & 0 deletions test/Tmdb/Tests/Repository/ChangesRepositoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?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\Repository;

use Tmdb\Model\Query\ChangesQuery;

class ChangesRepositoryTest extends TestCase
{
/**
* @test
*/
public function shouldGetMovieChanges()
{
$repository = $this->getRepositoryWithMockedHttpClient();

$query = new ChangesQuery();

$repository->getMovieChanges($query);
}

/**
* @test
*/
public function shouldGetPeopleChanges()
{
$repository = $this->getRepositoryWithMockedHttpClient();

$query = new ChangesQuery();

$repository->getPeopleChanges($query);
}

/**
* There is no generic factory for changes so it should never be called.
*
* @expectedException Tmdb\Exception\NotImplementedException
* @test
*/
public function getFactoryShouldThrowException()
{
$repository = $this->getRepositoryWithMockedHttpClient();
$repository->getFactory();
}

protected function getApiClass()
{
return 'Tmdb\Api\Changes';
}

protected function getRepositoryClass()
{
return 'Tmdb\Repository\ChangesRepository';
}
}
11 changes: 11 additions & 0 deletions test/Tmdb/Tests/Repository/CompanyRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@ public function shouldLoadCompany()
$repository->load(self::COMPANY_ID);
}

/**
* @test
*/
public function shouldGetMovies()
{
$repository = $this->getRepositoryWithMockedHttpClient();

$repository->getMovies(self::COMPANY_ID);
}


protected function getApiClass()
{
return 'Tmdb\Api\Company';
Expand Down
2 changes: 1 addition & 1 deletion test/Tmdb/Tests/Repository/MovieRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function shouldGetTopRated()
{
$repository = $this->getRepositoryWithMockedHttpClient();

$repository->getLatest();
$repository->getTopRated();
}

protected function getApiClass()
Expand Down
53 changes: 53 additions & 0 deletions test/Tmdb/Tests/Repository/TvEpisodeRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
*/
namespace Tmdb\Tests\Repository;

use Tmdb\Model\Tv\Episode;
use Tmdb\Model\Tv\Season;
use Tmdb\Model\Tv;

class TvEpisodeRepositoryTest extends TestCase
{
const TV_ID = 3572;
Expand All @@ -28,6 +32,55 @@ public function shouldLoadTvEpisode()
$repository->load(self::TV_ID, self::SEASON_ID, self::EPISODE_ID);
}

/**
* @test
*/
public function shouldBeAbleToLoadTvSeasonWithTvAndSeason()
{
$repository = $this->getRepositoryWithMockedHttpClient();

$tv = new Tv();
$tv->setId(self::TV_ID);

$season = new Season();
$season->setId(self::SEASON_ID);

$episode = new Episode();
$episode->setId(self::EPISODE_ID);

$repository->load($tv, $season, $episode);
}

/**
* @expectedException Tmdb\Exception\RuntimeException
* @test
*/
public function shouldThrowExceptionWhenConditionsNotMet()
{
$repository = $this->getRepositoryWithMockedHttpClient();

$tv = new Tv();
$tv->setId(self::TV_ID);

$season = new Season();
$season->setId(self::SEASON_ID);

$repository->load($tv, $season, null);
}


/**
* @expectedException Tmdb\Exception\RuntimeException
* @test
*/
public function shouldThrowExceptionWhenConditionsNotMetAll()
{
$repository = $this->getRepositoryWithMockedHttpClient();

$repository->load(null, null, null);
}


protected function getApiClass()
{
return 'Tmdb\Api\TvEpisode';
Expand Down
20 changes: 20 additions & 0 deletions test/Tmdb/Tests/Repository/TvRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,26 @@ public function shouldLoadTv()
$repository->load(self::TV_ID);
}

/**
* @test
*/
public function shouldGetPopular()
{
$repository = $this->getRepositoryWithMockedHttpClient();

$repository->getPopular();
}

/**
* @test
*/
public function shouldGetTopRated()
{
$repository = $this->getRepositoryWithMockedHttpClient();

$repository->getTopRated();
}

protected function getApiClass()
{
return 'Tmdb\Api\Tv';
Expand Down
Loading

0 comments on commit 5a7be3a

Please sign in to comment.