Skip to content

Commit

Permalink
Working on more unit tests and fixing irregularities.
Browse files Browse the repository at this point in the history
  • Loading branch information
wtfzdotnet committed Feb 3, 2014
1 parent c9c8e64 commit 98afbf6
Show file tree
Hide file tree
Showing 25 changed files with 580 additions and 68 deletions.
37 changes: 37 additions & 0 deletions lib/Tmdb/Factory/CompanyFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,30 @@

class CompanyFactory extends AbstractFactory
{
/**
* @var ImageFactory
*/
private $imageFactory;

/**
* Constructor
*/
public function __construct()
{
$this->imageFactory = new ImageFactory();
}

/**
* {@inheritdoc}
*/
public function create(array $data = array())
{
$company = new Company();

if (array_key_exists('logo_path', $data)) {
$company->setLogo($this->getImageFactory()->createFromPath($data['logo_path'], 'logo_path'));
}

return $this->hydrate(new Company(), $data);
}

Expand All @@ -31,4 +50,22 @@ public function createCollection(array $data = array())
{
return array();
}

/**
* @param \Tmdb\Factory\ImageFactory $imageFactory
* @return $this
*/
public function setImageFactory($imageFactory)
{
$this->imageFactory = $imageFactory;
return $this;
}

/**
* @return \Tmdb\Factory\ImageFactory
*/
public function getImageFactory()
{
return $this->imageFactory;
}
}
3 changes: 2 additions & 1 deletion lib/Tmdb/Factory/FindFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/
namespace Tmdb\Factory;

use Tmdb\Exception\NotImplementedException;
use Tmdb\Model\Common\GenericCollection;
use Tmdb\Model\Find;

Expand Down Expand Up @@ -69,7 +70,7 @@ public function create(array $data = array())
*/
public function createCollection(array $data = array())
{
return array();
throw new NotImplementedException(sprintf('Method "%s" is not implemented.', __METHOD__));
}

/**
Expand Down
6 changes: 5 additions & 1 deletion lib/Tmdb/Factory/GenreFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,12 @@ public function createCollection(array $data = array())
{
$collection = new Genres();

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

foreach($data as $item) {
$collection->addGenre($this->create((array) $item));
$collection->addGenre($this->create($item));
}

return $collection;
Expand Down
10 changes: 3 additions & 7 deletions lib/Tmdb/Factory/ImageFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,6 @@ public function create(array $data = array(), $key = null)
{
$type = self::resolveImageType($key);

if (is_string($data)) {
$data = array(
'file_path' => $data
);
}

return $this->hydrate($type, $data);
}

Expand All @@ -55,10 +49,12 @@ public function createFromPath($path, $key)
}

/**
* Helper function to obtain a new object for an image type
*
* @param string|null $key
* @return Image|Image\BackdropImage|Image\LogoImage|Image\PosterImage|Image\ProfileImage|Image\StillImage
*/
private function resolveImageType($key = null)
public function resolveImageType($key = null)
{
switch($key) {
case 'poster':
Expand Down
2 changes: 1 addition & 1 deletion lib/Tmdb/Factory/People/CastFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function createCollection(array $data = array(), $person = null)
$collection = new Cast();

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

return $collection;
Expand Down
2 changes: 1 addition & 1 deletion lib/Tmdb/Factory/People/CrewFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function createCollection(array $data = array(), $person = null)
$collection = new Crew();

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

return $collection;
Expand Down
12 changes: 1 addition & 11 deletions lib/Tmdb/Repository/GenreRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,7 @@ public function loadCollection(array $parameters = array(), array $headers = arr
* @return GenericCollection|Genre[]
*/
private function createCollection($data){
$collection = new GenericCollection();

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

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

return $collection;
return $this->getFactory()->createCollection($data);
}

/**
Expand Down
12 changes: 1 addition & 11 deletions lib/Tmdb/Repository/MovieRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,16 +148,6 @@ public function getTopRated(array $options = array())
* @return Movie[]
*/
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;
return $this->getFactory()->createCollection($data);
}
}
12 changes: 1 addition & 11 deletions lib/Tmdb/Repository/TvRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,6 @@ public function getTopRated(array $options = array())
* @return Tv[]
*/
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;
return $this->getFactory()->createCollection($data);
}
}
7 changes: 1 addition & 6 deletions test/Tmdb/Tests/Api/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,4 @@ protected function getApiMock(array $methods = array())
->setConstructorArgs(array($client))
->getMock();
}

protected function getMockedHttpClient()
{
return $this->getMock('Guzzle\Http\Client', array('send'));
}
}
}
18 changes: 18 additions & 0 deletions test/Tmdb/Tests/Factory/CollectionFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,24 @@ public function shouldBeAbleToSetFactories()
$this->assertInstanceOf('stdClass', $factory->getImageFactory());
}


/**
* @test
*/
public function shouldBeAbleToCreateCollection()
{
$factory = $this->getFactory();

$data = array(
array('id' => 1),
array('id' => 2),
);

$collection = $factory->createCollection($data);

$this->assertEquals(2, count($collection));
}

protected function getFactoryClass()
{
return 'Tmdb\Factory\CollectionFactory';
Expand Down
13 changes: 13 additions & 0 deletions test/Tmdb/Tests/Factory/CompanyFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,19 @@ public function callingCollectionReturnsEmptyArray()
$this->assertEquals(array(), $collection);
}

/**
* @test
*/
public function shouldBeAbleToSetFactories()
{
$factory = $this->getFactory();

$class = new \stdClass();
$factory->setImageFactory($class);

$this->assertInstanceOf('stdClass', $factory->getImageFactory());
}

protected function getFactoryClass()
{
return 'Tmdb\Factory\CompanyFactory';
Expand Down
28 changes: 28 additions & 0 deletions test/Tmdb/Tests/Factory/FindFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,34 @@ public function shouldConstructFind()
}
}

/**
* @test
*/
public function shouldBeAbleToSetFactories()
{
$factory = $this->getFactory();

$class = new \stdClass();

$factory->setMovieFactory($class);
$factory->setPeopleFactory($class);
$factory->setTvFactory($class);

$this->assertInstanceOf('stdClass', $factory->getMovieFactory());
$this->assertInstanceOf('stdClass', $factory->getPeopleFactory());
$this->assertInstanceOf('stdClass', $factory->getTvFactory());
}

/**
* @test
* @expectedException Tmdb\Exception\NotImplementedException
*/
public function shouldThrowExceptionForCreateCollection()
{
$factory = $this->getFactory();
$factory->createCollection(array());
}

protected function getFactoryClass()
{
return 'Tmdb\Factory\FindFactory';
Expand Down
17 changes: 17 additions & 0 deletions test/Tmdb/Tests/Factory/GenreFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,23 @@ public function shouldCollaborateWithCollection()
$this->assertEquals(null, $genre);
}

/**
* @test
*/
public function shouldBeAbleToDissectResults()
{
$factory = $this->getFactory();

$data = array('genres' => array(
array('id' => 1),
array('id' => 2),
));

$collection = $factory->createCollection($data);

$this->assertEquals(2, count($collection));
}

protected function getFactoryClass()
{
return 'Tmdb\Factory\GenreFactory';
Expand Down
13 changes: 11 additions & 2 deletions test/Tmdb/Tests/Factory/ImageFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,18 @@ class ImageFactoryTest extends TestCase
/**
* @test
*/
public function shouldConstructGenres()
public function shouldBeAbleToCreateCollection()
{
$this->assertEquals(true,true);
$factory = $this->getFactory();

$data = array(
array('id' => 1),
array('id' => 2),
);

$collection = $factory->createCollection($data);

$this->assertEquals(2, count($collection));
}

protected function getFactoryClass()
Expand Down
18 changes: 18 additions & 0 deletions test/Tmdb/Tests/Factory/Movie/AlternativeTitleFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,24 @@ public function shouldConstructAlternativeTitle()
$this->assertEquals('Kaas', $title->getTitle());
}


/**
* @test
*/
public function shouldBeAbleToCreateCollection()
{
$factory = $this->getFactory();

$data = array(
array('id' => 1),
array('id' => 2),
);

$collection = $factory->createCollection($data);

$this->assertEquals(2, count($collection));
}

protected function getFactoryClass()
{
return 'Tmdb\Factory\Movie\AlternativeTitleFactory';
Expand Down
Loading

0 comments on commit 98afbf6

Please sign in to comment.