From dc8f050a072b013d27d53ac865aa79a58b5b7460 Mon Sep 17 00:00:00 2001 From: Michael Roterman Date: Sun, 2 Feb 2014 18:17:59 +0100 Subject: [PATCH] Fixing unit tests --- lib/Tmdb/HttpClient/HttpClient.php | 50 ------------------- lib/Tmdb/HttpClient/HttpClientInterface.php | 18 ------- lib/Tmdb/Model/Person.php | 8 +++ test/Tmdb/Tests/Api/TestCase.php | 21 -------- .../Movie/AlternativeTitleFactoryTest.php | 50 +++++++++++++++++++ .../Plugin/AcceptJsonHeaderPluginTest.php | 43 ++++++++++++++++ .../HttpClient/Plugin/ApiTokenPluginTest.php | 40 +++++++++++++++ test/Tmdb/Tests/TestCase.php | 27 ++++++++++ 8 files changed, 168 insertions(+), 89 deletions(-) create mode 100644 test/Tmdb/Tests/Factory/Movie/AlternativeTitleFactoryTest.php create mode 100644 test/Tmdb/Tests/HttpClient/Plugin/AcceptJsonHeaderPluginTest.php create mode 100644 test/Tmdb/Tests/HttpClient/Plugin/ApiTokenPluginTest.php diff --git a/lib/Tmdb/HttpClient/HttpClient.php b/lib/Tmdb/HttpClient/HttpClient.php index 9eb6817d..5eab9c77 100644 --- a/lib/Tmdb/HttpClient/HttpClient.php +++ b/lib/Tmdb/HttpClient/HttpClient.php @@ -28,7 +28,6 @@ class HttpClient protected $options = array(); protected $base_url = null; - protected $headers = array(); /** * @var Response @@ -52,8 +51,6 @@ public function __construct($baseUrl, array $options, ClientInterface $client) $this->base_url = $baseUrl; $this->options = $options; $this->client = $client; - - $this->clearHeaders(); } /** @@ -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 * diff --git a/lib/Tmdb/HttpClient/HttpClientInterface.php b/lib/Tmdb/HttpClient/HttpClientInterface.php index 44155184..f844d1f0 100644 --- a/lib/Tmdb/HttpClient/HttpClientInterface.php +++ b/lib/Tmdb/HttpClient/HttpClientInterface.php @@ -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); } diff --git a/lib/Tmdb/Model/Person.php b/lib/Tmdb/Model/Person.php index ef7809fb..13158b59 100644 --- a/lib/Tmdb/Model/Person.php +++ b/lib/Tmdb/Model/Person.php @@ -123,6 +123,10 @@ public function getBiography() */ public function setBirthday($birthday) { + if (!$birthday instanceof \DateTime) { + $birthday = new \DateTime($birthday); + } + $this->birthday = $birthday; return $this; } @@ -177,6 +181,10 @@ public function getCredits() */ public function setDeathday($deathday) { + if (!$deathday instanceof \DateTime) { + $deathday = new \DateTime($deathday); + } + $this->deathday = $deathday; return $this; } diff --git a/test/Tmdb/Tests/Api/TestCase.php b/test/Tmdb/Tests/Api/TestCase.php index a28bb306..68fb398b 100644 --- a/test/Tmdb/Tests/Api/TestCase.php +++ b/test/Tmdb/Tests/Api/TestCase.php @@ -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')); diff --git a/test/Tmdb/Tests/Factory/Movie/AlternativeTitleFactoryTest.php b/test/Tmdb/Tests/Factory/Movie/AlternativeTitleFactoryTest.php new file mode 100644 index 00000000..3c426531 --- /dev/null +++ b/test/Tmdb/Tests/Factory/Movie/AlternativeTitleFactoryTest.php @@ -0,0 +1,50 @@ + + * @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'; + } +} diff --git a/test/Tmdb/Tests/HttpClient/Plugin/AcceptJsonHeaderPluginTest.php b/test/Tmdb/Tests/HttpClient/Plugin/AcceptJsonHeaderPluginTest.php new file mode 100644 index 00000000..6c55b91f --- /dev/null +++ b/test/Tmdb/Tests/HttpClient/Plugin/AcceptJsonHeaderPluginTest.php @@ -0,0 +1,43 @@ + + * @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); + } +} \ No newline at end of file diff --git a/test/Tmdb/Tests/HttpClient/Plugin/ApiTokenPluginTest.php b/test/Tmdb/Tests/HttpClient/Plugin/ApiTokenPluginTest.php new file mode 100644 index 00000000..ed2583c7 --- /dev/null +++ b/test/Tmdb/Tests/HttpClient/Plugin/ApiTokenPluginTest.php @@ -0,0 +1,40 @@ + + * @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()); + } +} \ No newline at end of file diff --git a/test/Tmdb/Tests/TestCase.php b/test/Tmdb/Tests/TestCase.php index 1135c45a..45a2049f 100644 --- a/test/Tmdb/Tests/TestCase.php +++ b/test/Tmdb/Tests/TestCase.php @@ -12,6 +12,7 @@ */ namespace Tmdb\Tests; +use Tmdb\ApiToken; use Tmdb\Common\ObjectHydrator; abstract class TestCase extends \PHPUnit_Framework_TestCase @@ -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 *