From 83b48f7a6367894bc2785dc2a77a9f8867328809 Mon Sep 17 00:00:00 2001 From: Michael Roterman Date: Sun, 2 Feb 2014 15:52:33 +0100 Subject: [PATCH] More unit testing --- .../Model/Common/GenericCollectionTest.php | 148 ++++++++++++++++++ .../Model/Common/Trailer/YoutubeTest.php | 46 ++++++ test/Tmdb/Tests/TestCase.php | 15 ++ 3 files changed, 209 insertions(+) create mode 100644 test/Tmdb/Tests/Model/Common/GenericCollectionTest.php create mode 100644 test/Tmdb/Tests/Model/Common/Trailer/YoutubeTest.php diff --git a/test/Tmdb/Tests/Model/Common/GenericCollectionTest.php b/test/Tmdb/Tests/Model/Common/GenericCollectionTest.php new file mode 100644 index 00000000..2f910f51 --- /dev/null +++ b/test/Tmdb/Tests/Model/Common/GenericCollectionTest.php @@ -0,0 +1,148 @@ + + * @copyright (c) 2013, Michael Roterman + * @version 0.0.1 + */ +namespace Tmdb\Tests\Model\Common; + +use Tmdb\Model\Common\GenericCollection; +use Tmdb\Tests\Model\TestCase; + +class GenericCollectionTest extends TestCase +{ + /** + * @var GenericCollection + */ + private $collection; + + public function setUp() + { + $this->collection = new GenericCollection(array( + 'id' => 1, + 'name' => 'Dave' + )); + } + + /** + * @test + */ + public function shouldConstructGenericCollection() + { + $emptyArray = array(); + $dataArray = array('id' => 1); + + $emptyConstructCollection = new GenericCollection(); + $emptyCollection = new GenericCollection($emptyArray); + $dataCollection = new GenericCollection($dataArray); + + $this->assertEquals($emptyArray, $emptyConstructCollection->toArray()); + $this->assertEquals($emptyArray, $emptyCollection->toArray()); + $this->assertEquals($dataArray, $dataCollection->toArray()); + } + + /** + * @test + */ + public function shouldBeArrayAccess() + { + $this->assertEquals(true, isset($this->collection['id'])); + $this->assertEquals(1, $this->collection['id']); + + $this->collection['id'] = 2; + $this->assertEquals(2, $this->collection['id']); + + unset($this->collection['id']); + $this->assertEquals(1, count($this->collection)); + + $this->collection['id'] = 1; + $this->assertEquals(2, count($this->collection)); + $this->assertEquals(1, $this->collection['id']); + } + + /** + * @test + */ + public function shouldBeIteratorAggregate() + { + $this->assertInstanceOf('\ArrayIterator', $this->collection->getIterator()); + } + + /** + * @test + */ + public function shouldBeCountable() + { + $this->assertEquals(2, count($this->collection)); + } + + /** + * @test + */ + public function shouldBeAbleToRemove() + { + $this->setUp(); + + $this->assertEquals(2, count($this->collection)); + + $this->collection->remove('id'); + + $this->assertEquals(1, count($this->collection)); + } + + /** + * @test + */ + public function shouldBeAbleToSetAndGet() + { + $this->setUp(); + + $this->collection->set('id', 2); + + $this->assertEquals(2, $this->collection['id']); + + $object = new \stdClass(); + $object->id = 1; + + $hash = spl_object_hash($object); + + $this->collection->set(null, $object); + + $this->assertEquals($object, $this->collection->get($hash)); + $this->assertEquals(3, count($this->collection)); + + $retrievedObject = $this->collection->get($object); + $this->assertEquals($object, $retrievedObject); + + $this->collection->remove($object); + $this->assertEquals(2, count($this->collection)); + + $keys = $this->collection->getKeys(); + $this->assertEquals(array('id', 'name'), $keys); + + $this->assertEquals(true, $this->collection->hasKey('id')); + + $this->assertEquals('id', $this->collection->keySearch('id')); + $this->assertEquals(false, $this->collection->keySearch('parent')); + + $this->collection->replace(array('id' => 2)); + $this->assertEquals(1, count($this->collection)); + $this->assertEquals(2, $this->collection->get('id')); + + $this->collection->merge(array('id' => 1)); + $this->assertEquals(array(2, 1), $this->collection->get('id')); + + $this->setUp(); + $this->assertEquals(array('id' => 1, 'name' => 'Dave'), $this->collection->getAll()); + $this->assertEquals(array('name' => 'Dave'), $this->collection->getAll(array('name'))); + + $this->collection->clear(); + $this->assertEquals(0, count($this->collection)); + } +} diff --git a/test/Tmdb/Tests/Model/Common/Trailer/YoutubeTest.php b/test/Tmdb/Tests/Model/Common/Trailer/YoutubeTest.php new file mode 100644 index 00000000..ecffd5cd --- /dev/null +++ b/test/Tmdb/Tests/Model/Common/Trailer/YoutubeTest.php @@ -0,0 +1,46 @@ + + * @copyright (c) 2013, Michael Roterman + * @version 0.0.1 + */ +namespace Tmdb\Tests\Model\Common\Trailer; + +use Tmdb\Model\Common\Trailer\Youtube; +use Tmdb\Tests\Model\TestCase; + +class YoutubeTest extends TestCase +{ + /** + * @var Youtube + */ + private $subject; + + public function setUp() + { + $this->subject = $this->hydrate(new Youtube(), array( + 'name' => 'Trailer 1', + 'size' => 'HD', + 'source' => 'SUXWAEX2jlg', + 'type' => 'Trailer' + )); + } + + /** + * @test + */ + public function shouldBeFunctional() + { + $this->assertEquals('Trailer 1', $this->subject->getName()); + $this->assertEquals('HD', $this->subject->getSize()); + $this->assertEquals('SUXWAEX2jlg', $this->subject->getSource()); + $this->assertEquals('Trailer', $this->subject->getType()); + $this->assertEquals('http://www.youtube.com/watch?v=SUXWAEX2jlg', $this->subject->getUrl()); + } +} diff --git a/test/Tmdb/Tests/TestCase.php b/test/Tmdb/Tests/TestCase.php index e254585c..1135c45a 100644 --- a/test/Tmdb/Tests/TestCase.php +++ b/test/Tmdb/Tests/TestCase.php @@ -12,6 +12,8 @@ */ namespace Tmdb\Tests; +use Tmdb\Common\ObjectHydrator; + abstract class TestCase extends \PHPUnit_Framework_TestCase { /** @@ -56,4 +58,17 @@ protected function loadByFile($file) true ); } + + /** + * Hydrate object + * + * @param $object + * @param $data + * @return \Tmdb\Model\AbstractModel + */ + protected function hydrate($object, $data) { + $objectHydrator = new ObjectHydrator(); + + return $objectHydrator->hydrate($object, $data); + } }