diff --git a/lib/Tmdb/Repository/SearchRepository.php b/lib/Tmdb/Repository/SearchRepository.php index 176fba57..f1f892ab 100644 --- a/lib/Tmdb/Repository/SearchRepository.php +++ b/lib/Tmdb/Repository/SearchRepository.php @@ -35,6 +35,7 @@ use Tmdb\Model\Search\SearchQuery\MovieSearchQuery; use Tmdb\Model\Search\SearchQuery\PersonSearchQuery; use Tmdb\Model\Search\SearchQuery\TvSearchQuery; +use Tmdb\Model\Search\SearchQuery; use Tmdb\Model\Tv; class SearchRepository extends AbstractRepository { @@ -94,8 +95,8 @@ public function __construct(Client $client) * * @return ResultCollection|Movie[] */ - public function searchMovie($query, MovieSearchQuery $parameters = null, array $headers = array()) { - $data = $this->getApi()->searchMovies($query, $parameters->toArray(), $headers); + public function searchMovie($query, MovieSearchQuery $parameters, array $headers = array()) { + $data = $this->getApi()->searchMovies($query, $this->getParameters($parameters), $headers); return $this->getMovieFactory()->createResultCollection($data); } @@ -107,8 +108,8 @@ public function searchMovie($query, MovieSearchQuery $parameters = null, array $ * * @return ResultCollection|Collection[] */ - public function searchCollection($query, CollectionSearchQuery $parameters = null, array $headers = array()) { - $data = $this->getApi()->searchCollection($query, $parameters->toArray(), $headers); + public function searchCollection($query, CollectionSearchQuery $parameters, array $headers = array()) { + $data = $this->getApi()->searchCollection($query, $this->getParameters($parameters), $headers); return $this->getCollectionFactory()->createResultCollection($data); } @@ -120,8 +121,8 @@ public function searchCollection($query, CollectionSearchQuery $parameters = nul * * @return ResultCollection|Tv[] */ - public function searchTv($query, TvSearchQuery $parameters = null, array $headers = array()) { - $data = $this->getApi()->searchTv($query, $parameters->toArray(), $headers); + public function searchTv($query, TvSearchQuery $parameters, array $headers = array()) { + $data = $this->getApi()->searchTv($query, $this->getParameters($parameters), $headers); return $this->getTvFactory()->createResultCollection($data); } @@ -133,8 +134,8 @@ public function searchTv($query, TvSearchQuery $parameters = null, array $header * * @return ResultCollection|Person[] */ - public function searchPerson($query, PersonSearchQuery $parameters = null, array $headers = array()) { - $data = $this->getApi()->searchPersons($query, $parameters->toArray(), $headers); + public function searchPerson($query, PersonSearchQuery $parameters, array $headers = array()) { + $data = $this->getApi()->searchPersons($query, $this->getParameters($parameters), $headers); return $this->getPeopleFactory()->createResultCollection($data); } @@ -147,8 +148,8 @@ public function searchPerson($query, PersonSearchQuery $parameters = null, array * @todo fix return docblock * @return ResultCollection */ - public function searchList($query, ListSearchQuery $parameters = null, array $headers = array()) { - $data = $this->getApi()->searchList($query, $parameters->toArray(), $headers); + public function searchList($query, ListSearchQuery $parameters, array $headers = array()) { + $data = $this->getApi()->searchList($query, $this->getParameters($parameters), $headers); return $this->getListitemFactory()->createResultCollection($data); } @@ -160,8 +161,8 @@ public function searchList($query, ListSearchQuery $parameters = null, array $he * * @return ResultCollection|Company[] */ - public function searchCompany($query, CompanySearchQuery $parameters = null, array $headers = array()) { - $data = $this->getApi()->searchTv($query, $parameters->toArray(), $headers); + public function searchCompany($query, CompanySearchQuery $parameters, array $headers = array()) { + $data = $this->getApi()->searchTv($query, $this->getParameters($parameters), $headers); return $this->getCompanyFactory()->createResultCollection($data); } @@ -174,12 +175,20 @@ public function searchCompany($query, CompanySearchQuery $parameters = null, arr * * @return ResultCollection|Keyword[] */ - public function searchKeyword($query, KeywordSearchQuery $parameters = null, array $headers = array()) { - $data = $this->getApi()->searchKeyword($query, $parameters->toArray(), $headers); + public function searchKeyword($query, KeywordSearchQuery $parameters, array $headers = array()) { + $data = $this->getApi()->searchKeyword($query, $this->getParameters($parameters), $headers); return $this->getKeywordFactory()->createResultCollection($data); } + private function getParameters($parameters = array()) { + if ($parameters instanceof SearchQuery) { + return $parameters->toArray(); + } + + return $parameters; + } + /** * Return the related API class * diff --git a/test/Tmdb/Tests/Repository/SearchRepositoryTest.php b/test/Tmdb/Tests/Repository/SearchRepositoryTest.php new file mode 100644 index 00000000..bb832de2 --- /dev/null +++ b/test/Tmdb/Tests/Repository/SearchRepositoryTest.php @@ -0,0 +1,126 @@ + + * @copyright (c) 2013, Michael Roterman + * @version 0.0.1 + */ +namespace Tmdb\Tests\Repository; + +use Tmdb\Model\Search\SearchQuery\CollectionSearchQuery; +use Tmdb\Model\Search\SearchQuery\CompanySearchQuery; +use Tmdb\Model\Search\SearchQuery\KeywordSearchQuery; +use Tmdb\Model\Search\SearchQuery\ListSearchQuery; +use Tmdb\Model\Search\SearchQuery\MovieSearchQuery; +use Tmdb\Model\Search\SearchQuery\PersonSearchQuery; +use Tmdb\Model\Search\SearchQuery\TvSearchQuery; +use Tmdb\Repository\SearchRepository; + +class SearchRepositoryTest extends TestCase +{ + /** + * @test + */ + public function shouldSearchMovie() + { + /** + * @var SearchRepository $repository + */ + $repository = $this->getRepositoryWithMockedHttpClient(); + + $repository->searchMovie('rush hour', new MovieSearchQuery()); + } + + /** + * @test + */ + public function shouldSearchCollection() + { + /** + * @var SearchRepository $repository + */ + $repository = $this->getRepositoryWithMockedHttpClient(); + + $repository->searchCollection('the matrix', new CollectionSearchQuery()); + } + + /** + * @test + */ + public function shouldSearchTv() + { + /** + * @var SearchRepository $repository + */ + $repository = $this->getRepositoryWithMockedHttpClient(); + + $repository->searchTv('breaking bad', new TvSearchQuery()); + } + + /** + * @test + */ + public function shouldSearchPerson() + { + /** + * @var SearchRepository $repository + */ + $repository = $this->getRepositoryWithMockedHttpClient(); + + $repository->searchPerson('johnny knoxville', new PersonSearchQuery()); + } + + /** + * @test + */ + public function shouldSearchList() + { + /** + * @var SearchRepository $repository + */ + $repository = $this->getRepositoryWithMockedHttpClient(); + + $repository->searchList('golden', new ListSearchQuery()); + } + + /** + * @test + */ + public function shouldSearchCompany() + { + /** + * @var SearchRepository $repository + */ + $repository = $this->getRepositoryWithMockedHttpClient(); + + $repository->searchCompany('disney', new CompanySearchQuery()); + } + + /** + * @test + */ + public function shouldSearchKeyword() + { + /** + * @var SearchRepository $repository + */ + $repository = $this->getRepositoryWithMockedHttpClient(); + + $repository->searchKeyword('alien', new KeywordSearchQuery()); + } + + protected function getApiClass() + { + return 'Tmdb\Api\Search'; + } + + protected function getRepositoryClass() + { + return 'Tmdb\Repository\SearchRepository'; + } +} \ No newline at end of file