Skip to content

Commit

Permalink
More unit testing
Browse files Browse the repository at this point in the history
  • Loading branch information
wtfzdotnet committed Feb 2, 2014
1 parent c81e0c7 commit 83b48f7
Show file tree
Hide file tree
Showing 3 changed files with 209 additions and 0 deletions.
148 changes: 148 additions & 0 deletions test/Tmdb/Tests/Model/Common/GenericCollectionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
<?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\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));
}
}
46 changes: 46 additions & 0 deletions test/Tmdb/Tests/Model/Common/Trailer/YoutubeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?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\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());
}
}
15 changes: 15 additions & 0 deletions test/Tmdb/Tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
*/
namespace Tmdb\Tests;

use Tmdb\Common\ObjectHydrator;

abstract class TestCase extends \PHPUnit_Framework_TestCase
{
/**
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 83b48f7

Please sign in to comment.