From 38450c0107fed502e44fadcaa7bdf18382aa73c7 Mon Sep 17 00:00:00 2001 From: Michael Roterman Date: Thu, 23 Jan 2014 01:42:27 +0100 Subject: [PATCH] Adding basic tests for movies --- lib/Tmdb/HttpClient/HttpClient.php | 18 ++++++ lib/Tmdb/Repository/AbstractRepository.php | 6 +- test/Tmdb/Tests/Api/MoviesTest.php | 17 ++---- test/Tmdb/Tests/Api/TestCase.php | 16 ++++-- .../Tests/Repository/MovieRepositoryTest.php | 43 +++++++++++++++ test/Tmdb/Tests/Repository/TestCase.php | 55 +++++++++++++++++++ 6 files changed, 135 insertions(+), 20 deletions(-) create mode 100644 test/Tmdb/Tests/Repository/MovieRepositoryTest.php create mode 100644 test/Tmdb/Tests/Repository/TestCase.php diff --git a/lib/Tmdb/HttpClient/HttpClient.php b/lib/Tmdb/HttpClient/HttpClient.php index a2950b42..5c0ca343 100644 --- a/lib/Tmdb/HttpClient/HttpClient.php +++ b/lib/Tmdb/HttpClient/HttpClient.php @@ -209,4 +209,22 @@ public function request(RequestInterface $request) return $response; } + + /** + * @param \Guzzle\Http\ClientInterface $client + * @return $this + */ + public function setClient($client) + { + $this->client = $client; + return $this; + } + + /** + * @return \Guzzle\Http\ClientInterface + */ + public function getClient() + { + return $this->client; + } } \ No newline at end of file diff --git a/lib/Tmdb/Repository/AbstractRepository.php b/lib/Tmdb/Repository/AbstractRepository.php index 67ea4e7d..854e214b 100644 --- a/lib/Tmdb/Repository/AbstractRepository.php +++ b/lib/Tmdb/Repository/AbstractRepository.php @@ -18,7 +18,7 @@ abstract class AbstractRepository { - private static $client = null; + protected $client = null; protected $api = null; @@ -32,7 +32,7 @@ abstract class AbstractRepository { */ public function __construct(Client $client) { - self::$client = $client; + $this->client = $client; } /** @@ -42,7 +42,7 @@ public function __construct(Client $client) */ public function getClient() { - return self::$client; + return $this->client; } /** diff --git a/test/Tmdb/Tests/Api/MoviesTest.php b/test/Tmdb/Tests/Api/MoviesTest.php index bff89d50..cbf14e13 100644 --- a/test/Tmdb/Tests/Api/MoviesTest.php +++ b/test/Tmdb/Tests/Api/MoviesTest.php @@ -1,13 +1,13 @@ - * @copyright (c) 2013, B-Found Internet Marketing & Services + * + * @package Tmdb + * @author Michael Roterman + * @copyright (c) 2013, Michael Roterman * @version 0.0.1 */ namespace Tmdb\Tests\Api; @@ -28,6 +28,7 @@ public function shouldGetMovie() $api->getMovie(self::MOVIE_ID); } + /** * @test */ @@ -80,7 +81,6 @@ public function shouldGetKeywords() $api->getKeywords(self::MOVIE_ID); } - /** * @test */ @@ -94,7 +94,6 @@ public function getReleases() $api->getReleases(self::MOVIE_ID); } - /** * @test */ @@ -134,7 +133,6 @@ public function shouldGetSimilarMovies() $api->getSimilarMovies(self::MOVIE_ID); } - /** * @test */ @@ -148,7 +146,6 @@ public function shouldGetReviews() $api->getReviews(self::MOVIE_ID); } - /** * @test */ @@ -201,7 +198,6 @@ public function shouldGetUpcoming() $api->getUpcoming(); } - /** * @test */ @@ -228,7 +224,6 @@ public function shouldGetPopular() $api->getPopular(); } - /** * @test */ diff --git a/test/Tmdb/Tests/Api/TestCase.php b/test/Tmdb/Tests/Api/TestCase.php index 2272b167..2b9e8646 100644 --- a/test/Tmdb/Tests/Api/TestCase.php +++ b/test/Tmdb/Tests/Api/TestCase.php @@ -1,13 +1,13 @@ - * @copyright (c) 2013, B-Found Internet Marketing & Services + * + * @package Tmdb + * @author Michael Roterman + * @copyright (c) 2013, Michael Roterman * @version 0.0.1 */ namespace Tmdb\Tests\Api; @@ -27,7 +27,11 @@ protected function getApiMock() ->expects($this->any()) ->method('send'); - $mock = $this->getMock('Tmdb\HttpClient\HttpClientInterface', array(), array(array(), $httpClient)); + $mock = $this->getMock( + 'Tmdb\HttpClient\HttpClientInterface', + array(), + array(array(), $httpClient) + ); $client = new \Tmdb\Client($token, $httpClient); $client->setHttpClient($mock); diff --git a/test/Tmdb/Tests/Repository/MovieRepositoryTest.php b/test/Tmdb/Tests/Repository/MovieRepositoryTest.php new file mode 100644 index 00000000..78ca2c43 --- /dev/null +++ b/test/Tmdb/Tests/Repository/MovieRepositoryTest.php @@ -0,0 +1,43 @@ + + * @copyright (c) 2013, Michael Roterman + * @version 0.0.1 + */ +namespace Tmdb\Tests\Repository; + +class MovieRepositoryTest extends TestCase +{ + const MOVIE_ID = 120; + + /** + * @test + */ + public function shouldLoadMovie() + { + $repository = $this->getRepositoryWithMockedClient(); + + $repository->getClient()->getHttpClient()->getClient() + ->expects($this->once()) + ->method('send') + ; + + $repository->load(self::MOVIE_ID); + } + + protected function getApiClass() + { + return 'Tmdb\Api\Movies'; + } + + protected function getRepositoryClass() + { + return 'Tmdb\Repository\MovieRepository'; + } +} \ No newline at end of file diff --git a/test/Tmdb/Tests/Repository/TestCase.php b/test/Tmdb/Tests/Repository/TestCase.php new file mode 100644 index 00000000..79697ae3 --- /dev/null +++ b/test/Tmdb/Tests/Repository/TestCase.php @@ -0,0 +1,55 @@ + + * @copyright (c) 2013, Michael Roterman + * @version 0.0.1 + */ +namespace Tmdb\Tests\Repository; + +use Guzzle\Http\Message\Response; +use Tmdb\ApiToken; +use Tmdb\Client; + +abstract class TestCase extends \PHPUnit_Framework_TestCase +{ + protected $repository; + + abstract protected function getRepositoryClass(); + + protected function getRepositoryWithMockedClient() + { + $class = $this->getRepositoryClass(); + + return new $class($this->getMockedTmdbClient()); + } + + protected function getRepositoryMock($client = null, array $methods = array()) + { + if ($client == null) { + $client = $this->getMockedTmdbClient(); + } + + return $this->getMock($this->getRepositoryClass(), array('getApi'), array($client)); + } + + protected function getMockedTmdbClient() + { + $token = new ApiToken('abcdef'); + $response = new Response('200'); + + $httpClient = $this->getMock('Guzzle\Http\Client', array('send')); + $httpClient + ->expects($this->any()) + ->method('send') + ->will($this->returnValue($response)) + ; + + return new Client($token, $httpClient); + } +} \ No newline at end of file