Skip to content

Commit

Permalink
Fixing unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wtfzdotnet committed Feb 2, 2014
1 parent 83b48f7 commit dc8f050
Show file tree
Hide file tree
Showing 8 changed files with 168 additions and 89 deletions.
50 changes: 0 additions & 50 deletions lib/Tmdb/HttpClient/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ class HttpClient

protected $options = array();
protected $base_url = null;
protected $headers = array();

/**
* @var Response
Expand All @@ -52,8 +51,6 @@ public function __construct($baseUrl, array $options, ClientInterface $client)
$this->base_url = $baseUrl;
$this->options = $options;
$this->client = $client;

$this->clearHeaders();
}

/**
Expand All @@ -66,53 +63,6 @@ public function addSubscriber(EventSubscriberInterface $subscriber)
$this->client->addSubscriber($subscriber);
}

/**
* Clear up the headers
* @return void
*/
public function clearHeaders()
{
$this->headers = array();
}

/**
* @return array
*/
public function getHeaders()
{
return $this->headers;
}

/**
* @param array $headers
*/
public function setHeaders(array $headers = array())
{
$this->headers = $headers;
}

/**
* Set options
*
* @param string $key
* @param mixed $value
*/
public function setOption($key, $value)
{
$this->options[$key] = $value;
}

/**
* Get an option
*
* @param $key
* @return mixed
*/
public function getOption($key)
{
return $this->options[$key];
}

/**
* Set the query parameters
*
Expand Down
18 changes: 0 additions & 18 deletions lib/Tmdb/HttpClient/HttpClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,22 +85,4 @@ public function delete($path, $body = null, array $parameters = array(), array $
*/
public function request(RequestInterface $request);

/**
* Change an option value.
*
* @param string $name The option name
* @param mixed $value The value
*
* @throws InvalidArgumentException
* @return void
*/
public function setOption($name, $value);

/**
* Set HTTP headers
*
* @param array $headers
* @return void
*/
public function setHeaders(array $headers);
}
8 changes: 8 additions & 0 deletions lib/Tmdb/Model/Person.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ public function getBiography()
*/
public function setBirthday($birthday)
{
if (!$birthday instanceof \DateTime) {
$birthday = new \DateTime($birthday);
}

$this->birthday = $birthday;
return $this;
}
Expand Down Expand Up @@ -177,6 +181,10 @@ public function getCredits()
*/
public function setDeathday($deathday)
{
if (!$deathday instanceof \DateTime) {
$deathday = new \DateTime($deathday);
}

$this->deathday = $deathday;
return $this;
}
Expand Down
21 changes: 0 additions & 21 deletions test/Tmdb/Tests/Api/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,27 +41,6 @@ protected function getApiMock(array $methods = array())
->getMock();
}

protected function getClientWithMockedHttpClient()
{
$token = new ApiToken('abcdef');

$httpClient = $this->getMockedHttpClient();
$httpClient
->expects($this->any())
->method('send');

$mock = $this->getMock(
'Tmdb\HttpClient\HttpClientInterface',
array(),
array(array(), $httpClient)
);

$client = new \Tmdb\Client($token, $httpClient);
$client->setHttpClient($mock);

return $client;
}

protected function getMockedHttpClient()
{
return $this->getMock('Guzzle\Http\Client', array('send'));
Expand Down
50 changes: 50 additions & 0 deletions test/Tmdb/Tests/Factory/Movie/AlternativeTitleFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?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\Movie;

use Tmdb\Factory\Movie\AlternativeTitleFactory;
use Tmdb\Model\Movie\AlternativeTitle;
use Tmdb\Tests\Factory\TestCase;

class AlternativeTitleFactoryTest extends TestCase
{

/**
* @test
*/
public function shouldConstructAlternativeTitle()
{
/**
* @var AlternativeTitleFactory $factory
*/
$factory = $this->getFactory();
$data = array(
'iso_3166_1' => 'nl',
'title' => 'Kaas'
);

/**
* @var AlternativeTitle
*/
$title = $factory->create($data);

$this->assertInstanceOf('Tmdb\Model\Movie\AlternativeTitle', $title);
$this->assertEquals('nl', $title->getIso31661());
$this->assertEquals('Kaas', $title->getTitle());
}

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

use Guzzle\Common\Event;
use Guzzle\Http\Message\Request;
use Tmdb\HttpClient\Plugin\AcceptJsonHeaderPlugin;
use Tmdb\Tests\TestCase;

class AcceptJsonHeaderPluginTest extends TestCase
{
/**
* @test
*/
public function shouldAddToken()
{
/**
* @var Request $request
*/
$request = new Request('GET', '/');

$event = new Event();
$event['request'] = $request;

$plugin = new AcceptJsonHeaderPlugin();

$plugin->onBeforeSend($event);

$header = $event['request']->getHeaders()->get('accept');

$this->assertEquals('application/json', (string) $header);
}
}
40 changes: 40 additions & 0 deletions test/Tmdb/Tests/HttpClient/Plugin/ApiTokenPluginTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?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\HttpClient\Plugin;

use Guzzle\Common\Event;
use Guzzle\Http\Message\Request;
use Tmdb\ApiToken;
use Tmdb\HttpClient\Plugin\ApiTokenPlugin;
use Tmdb\Tests\TestCase;

class ApiTokenPluginTest extends TestCase
{
/**
* @test
*/
public function shouldAddToken()
{
$token = new ApiToken('abcdef');
$request = new Request('GET', '/');

$event = new Event();
$event['request'] = $request;

$plugin = new ApiTokenPlugin($token);

$plugin->onBeforeSend($event);

$this->assertEquals('/?api_key=abcdef', $event['request']->getUrl());
}
}
27 changes: 27 additions & 0 deletions test/Tmdb/Tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/
namespace Tmdb\Tests;

use Tmdb\ApiToken;
use Tmdb\Common\ObjectHydrator;

abstract class TestCase extends \PHPUnit_Framework_TestCase
Expand Down Expand Up @@ -59,6 +60,32 @@ protected function loadByFile($file)
);
}

/**
* Get a TMDB Client with an mocked HTTP dependency
*
* @return \Tmdb\Client
*/
protected function getClientWithMockedHttpClient()
{
$token = new ApiToken('abcdef');

$httpClient = $this->getMockedHttpClient();
$httpClient
->expects($this->any())
->method('send');

$mock = $this->getMock(
'Tmdb\HttpClient\HttpClientInterface',
array(),
array(array(), $httpClient)
);

$client = new \Tmdb\Client($token, $httpClient);
$client->setHttpClient($mock);

return $client;
}

/**
* Hydrate object
*
Expand Down

0 comments on commit dc8f050

Please sign in to comment.