Skip to content

Commit

Permalink
Adding unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wtfzdotnet committed Jan 27, 2014
1 parent 74c4bd7 commit 9add642
Show file tree
Hide file tree
Showing 18 changed files with 548 additions and 73 deletions.
22 changes: 3 additions & 19 deletions lib/Tmdb/Common/ObjectHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down Expand Up @@ -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)
)
);
}
}
10 changes: 0 additions & 10 deletions lib/Tmdb/Repository/AbstractRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
2 changes: 1 addition & 1 deletion lib/Tmdb/Repository/ConfigurationRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
43 changes: 2 additions & 41 deletions lib/Tmdb/Repository/DiscoverRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -95,39 +76,19 @@ 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();
}

/**
* @return \Tmdb\Factory\TvFactory
*/
public function getTvFactory()
{
return $this->tvFactory;
return new TvFactory();
}
}
2 changes: 0 additions & 2 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,5 @@
<log type="coverage-html" target="phpunit/coverage" charset="UTF-8"
yui="true" highlight="true"
lowUpperBound="35" highLowerBound="70"/>
<log type="coverage-clover" target="phpunit/clover.xml"/>
<log type="junit" target="phpunit/logs/junit.xml" logIncompleteSkipped="false"/>
</logging>
</phpunit>
25 changes: 25 additions & 0 deletions test/Tmdb/Tests/ApiTokenTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?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
*/
class ApiTokenTest extends \PHPUnit_Framework_TestCase
{
/**
* @test
*/
public function testSetGet()
{
$token = new \Tmdb\ApiToken();
$token->setApiToken('abcdefg');

$this->assertEquals('abcdefg', $token->getApiToken());
}
}
13 changes: 13 additions & 0 deletions test/Tmdb/Tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
);
}
}
85 changes: 85 additions & 0 deletions test/Tmdb/Tests/Common/ObjectHydratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?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
*/
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');
}
48 changes: 48 additions & 0 deletions test/Tmdb/Tests/Repository/CollectionRepositoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?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;

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';
}
}
38 changes: 38 additions & 0 deletions test/Tmdb/Tests/Repository/CompanyRepositoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?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;

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';
}
}
Loading

0 comments on commit 9add642

Please sign in to comment.