From d958f28b6a13f03da7f5bf884e3c24acfc83f0fe Mon Sep 17 00:00:00 2001 From: Michael Roterman Date: Wed, 26 Feb 2014 00:31:40 +0100 Subject: [PATCH] Implementing Lists API into the modelled sections --- examples/lists/model/add.php | 25 ++++++ examples/lists/model/create_list.php | 25 ++++++ examples/lists/model/remove.php | 25 ++++++ lib/Tmdb/Api/Lists.php | 6 +- lib/Tmdb/Factory/ListFactory.php | 22 ++++- lib/Tmdb/Model/Lists/Result.php | 71 ++++++++++++++++ lib/Tmdb/Model/Lists/ResultWithListId.php | 47 +++++++++++ lib/Tmdb/Repository/ListRepository.php | 72 +++++++++++++++-- test/Tmdb/Tests/Factory/ListFactoryTest.php | 80 +++++++++++++++++++ .../Tests/Repository/ListRepositoryTest.php | 47 ++++++++++- test/Tmdb/Tests/Resources/lists/add.json | 4 + .../Tests/Resources/lists/item_status.json | 4 + .../Tests/Resources/lists/list_create.json | 5 ++ .../Tests/Resources/lists/list_delete.json | 4 + test/Tmdb/Tests/Resources/lists/remove.json | 4 + 15 files changed, 427 insertions(+), 14 deletions(-) create mode 100644 examples/lists/model/add.php create mode 100644 examples/lists/model/create_list.php create mode 100644 examples/lists/model/remove.php create mode 100644 lib/Tmdb/Model/Lists/Result.php create mode 100644 lib/Tmdb/Model/Lists/ResultWithListId.php create mode 100644 test/Tmdb/Tests/Resources/lists/add.json create mode 100644 test/Tmdb/Tests/Resources/lists/item_status.json create mode 100644 test/Tmdb/Tests/Resources/lists/list_create.json create mode 100644 test/Tmdb/Tests/Resources/lists/list_delete.json create mode 100644 test/Tmdb/Tests/Resources/lists/remove.json diff --git a/examples/lists/model/add.php b/examples/lists/model/add.php new file mode 100644 index 00000000..93f6e8c8 --- /dev/null +++ b/examples/lists/model/add.php @@ -0,0 +1,25 @@ + + * @copyright (c) 2013, Michael Roterman + * @version 0.0.1 + */ +require_once('../../../vendor/autoload.php'); +require_once('../../../apikey.php'); + +$token = new \Tmdb\ApiToken(TMDB_API_KEY); +$client = new \Tmdb\Client($token); + +$sessionToken = new \Tmdb\SessionToken(TMDB_SESSION_TOKEN); +$client->setSessionToken($sessionToken); + +$repository = new \Tmdb\Repository\ListRepository($client); +$result = $repository->add(TMDB_LIST_ID, 150); + +var_dump($result); \ No newline at end of file diff --git a/examples/lists/model/create_list.php b/examples/lists/model/create_list.php new file mode 100644 index 00000000..de82a8ef --- /dev/null +++ b/examples/lists/model/create_list.php @@ -0,0 +1,25 @@ + + * @copyright (c) 2013, Michael Roterman + * @version 0.0.1 + */ +require_once('../../../vendor/autoload.php'); +require_once('../../../apikey.php'); + +$token = new \Tmdb\ApiToken(TMDB_API_KEY); +$client = new \Tmdb\Client($token); + +$sessionToken = new \Tmdb\SessionToken(TMDB_SESSION_TOKEN); +$client->setSessionToken($sessionToken); + +$repository = new \Tmdb\Repository\ListRepository($client); +$result = $repository->createList('php-tmdb-api', 'just a test'); + +var_dump($result); \ No newline at end of file diff --git a/examples/lists/model/remove.php b/examples/lists/model/remove.php new file mode 100644 index 00000000..6ecaa480 --- /dev/null +++ b/examples/lists/model/remove.php @@ -0,0 +1,25 @@ + + * @copyright (c) 2013, Michael Roterman + * @version 0.0.1 + */ +require_once('../../../vendor/autoload.php'); +require_once('../../../apikey.php'); + +$token = new \Tmdb\ApiToken(TMDB_API_KEY); +$client = new \Tmdb\Client($token); + +$sessionToken = new \Tmdb\SessionToken(TMDB_SESSION_TOKEN); +$client->setSessionToken($sessionToken); + +$repository = new \Tmdb\Repository\ListRepository($client); +$result = $repository->remove(TMDB_LIST_ID, 150); + +var_dump($result); \ No newline at end of file diff --git a/lib/Tmdb/Api/Lists.php b/lib/Tmdb/Api/Lists.php index 2c44b732..84f2ef4a 100644 --- a/lib/Tmdb/Api/Lists.php +++ b/lib/Tmdb/Api/Lists.php @@ -61,7 +61,7 @@ public function getItemStatus($id, $movieId, array $parameters = array(), array } /** - * Get the cast information for a specific list id. + * This method lets users add new movies to a list that they created. A valid session id is required. * * @param string $id * @param string $mediaId @@ -73,7 +73,7 @@ public function addMediaToList($id, $mediaId) } /** - * Get the images (posters and backdrops) for a specific list id. + * This method lets users delete movies from a list that they created. A valid session id is required. * * @param string $id * @param string $mediaId @@ -85,7 +85,7 @@ public function removeMediaFromList($id, $mediaId) } /** - * Get the plot keywords for a specific list id. + * This method lets users delete a list that they created. A valid session id is required. * * @param string $id * @return mixed diff --git a/lib/Tmdb/Factory/ListFactory.php b/lib/Tmdb/Factory/ListFactory.php index ee691bc2..0f98e3ec 100644 --- a/lib/Tmdb/Factory/ListFactory.php +++ b/lib/Tmdb/Factory/ListFactory.php @@ -63,13 +63,33 @@ public function create(array $data = array()) /** * @param array $data * - * @return Movie + * @return Lists\ItemStatus */ public function createItemStatus(array $data = array()) { return $this->hydrate(new Lists\ItemStatus(), $data); } + /** + * @param array $data + * + * @return Lists\Result + */ + public function createResult(array $data = array()) + { + return $this->hydrate(new Lists\Result(), $data); + } + + /** + * @param array $data + * + * @return Lists\ResultWithListId + */ + public function createResultWithListId(array $data = array()) + { + return $this->hydrate(new Lists\ResultWithListId(), $data); + } + /** * {@inheritdoc} */ diff --git a/lib/Tmdb/Model/Lists/Result.php b/lib/Tmdb/Model/Lists/Result.php new file mode 100644 index 00000000..261c2377 --- /dev/null +++ b/lib/Tmdb/Model/Lists/Result.php @@ -0,0 +1,71 @@ + + * @copyright (c) 2013, Michael Roterman + * @version 0.0.1 + */ +namespace Tmdb\Model\Lists; + +use Tmdb\Model\AbstractModel; + +class Result extends AbstractModel { + /** + * @var int + */ + private $statusCode; + + /** + * @var string + */ + private $statusMessage; + + /** + * @var array + */ + public static $_properties = array( + 'status_code', + 'status_message' + ); + + /** + * @param int $statusCode + * @return $this + */ + public function setStatusCode($statusCode) + { + $this->statusCode = $statusCode; + return $this; + } + + /** + * @return int + */ + public function getStatusCode() + { + return $this->statusCode; + } + + /** + * @param string $statusMessage + * @return $this + */ + public function setStatusMessage($statusMessage) + { + $this->statusMessage = $statusMessage; + return $this; + } + + /** + * @return string + */ + public function getStatusMessage() + { + return $this->statusMessage; + } +} diff --git a/lib/Tmdb/Model/Lists/ResultWithListId.php b/lib/Tmdb/Model/Lists/ResultWithListId.php new file mode 100644 index 00000000..a526babf --- /dev/null +++ b/lib/Tmdb/Model/Lists/ResultWithListId.php @@ -0,0 +1,47 @@ + + * @copyright (c) 2013, Michael Roterman + * @version 0.0.1 + */ +namespace Tmdb\Model\Lists; + +class ResultWithListId extends Result { + /** + * @var string + */ + private $listId; + + /** + * @var array + */ + public static $_properties = array( + 'status_code', + 'status_message', + 'list_id' + ); + + /** + * @param string $listId + * @return $this + */ + public function setListId($listId) + { + $this->listId = $listId; + return $this; + } + + /** + * @return string + */ + public function getListId() + { + return $this->listId; + } +} diff --git a/lib/Tmdb/Repository/ListRepository.php b/lib/Tmdb/Repository/ListRepository.php index 3d70cb69..88c8b995 100644 --- a/lib/Tmdb/Repository/ListRepository.php +++ b/lib/Tmdb/Repository/ListRepository.php @@ -13,8 +13,8 @@ namespace Tmdb\Repository; use Tmdb\Factory\ListFactory; -use Tmdb\Model\Collection\Jobs; -use Tmdb\Model\Job; +use Tmdb\Model\Lists\ItemStatus; +use Tmdb\Model\Lists; class ListRepository extends AbstractRepository { /** @@ -23,7 +23,7 @@ class ListRepository extends AbstractRepository { * @param string $id * @param array $parameters * @param array $headers - * @return Job + * @return Lists */ public function load($id, array $parameters = array(), array $headers = array()) { return $this->getFactory()->create( @@ -34,15 +34,73 @@ public function load($id, array $parameters = array(), array $headers = array()) /** * Check to see if a movie ID is already added to a list. * - * @param int $id + * @param string $id + * @param int $mediaId * @param array $parameters * @param array $headers - * @return Jobs|Job[] + * @return ItemStatus */ - public function getItemStatus($id, array $parameters = array(), array $headers = array()) + public function getItemStatus($id, $mediaId, array $parameters = array(), array $headers = array()) { return $this->getFactory()->createItemStatus( - $this->getApi()->getItemStatus($id, $parameters, $headers) + $this->getApi()->getItemStatus($id, $mediaId, $parameters, $headers) + ); + } + + /** + * This method lets users create a new list. A valid session id is required. + * + * @param string $name + * @param string $description + * @param array $parameters + * @param array $headers + * @return string The list id + */ + public function createList($name, $description = null, array $parameters = array(), array $headers = array()) + { + return $this->getFactory()->createResultWithListId( + $this->getApi()->createList($name, $description, $parameters, $headers) + ); + } + + /** + * This method lets users add new movies to a list that they created. A valid session id is required. + * + * @param string $id + * @param int $mediaId + * @return ItemStatus + */ + public function add($id, $mediaId) + { + return $this->getFactory()->createResult( + $this->getApi()->addMediaToList($id, $mediaId) + ); + } + + /** + * This method lets users delete movies from a list that they created. A valid session id is required. + * + * @param string $id + * @param int $mediaId + * @return ItemStatus + */ + public function remove($id, $mediaId) + { + return $this->getFactory()->createResult( + $this->getApi()->removeMediaFromList($id, $mediaId) + ); + } + + /** + * This method lets users delete a list that they created. A valid session id is required. + * + * @param string $id + * @return ItemStatus + */ + public function deleteList($id) + { + return $this->getFactory()->createResult( + $this->getApi()->deleteList($id) ); } diff --git a/test/Tmdb/Tests/Factory/ListFactoryTest.php b/test/Tmdb/Tests/Factory/ListFactoryTest.php index 4f3929d7..fa0a75d6 100644 --- a/test/Tmdb/Tests/Factory/ListFactoryTest.php +++ b/test/Tmdb/Tests/Factory/ListFactoryTest.php @@ -73,7 +73,87 @@ public function shouldBeFunctional() $this->assertEquals('Best Picture Winners - The Academy Awards', $this->lists->getName()); $this->assertEquals('/efBm2Nm2v5kQnO0w3hYcW6hVsJU.jpg', $this->lists->getPosterPath()); $this->assertInstanceOf('Tmdb\Model\Image\PosterImage', $this->lists->getPosterImage()); + } + + /** + * @test + */ + public function shouldGetItemStatus() + { + /** + * @var ListFactory $factory + */ + $factory = $this->getFactory(); + + $result = $factory->createItemStatus($this->loadByFile('lists/item_status.json')); + + $this->assertEquals('509ec17b19c2950a0600050d', $result->getId()); + $this->assertEquals(true, $result->getItemPresent()); + } + + /** + * @test + */ + public function shouldCreateList() + { + /** + * @var ListFactory $factory + */ + $factory = $this->getFactory(); + + $result = $factory->createResultWithListId($this->loadByFile('lists/list_create.json')); + + $this->assertEquals(1, $result->getStatusCode()); + $this->assertEquals('Success', $result->getStatusMessage()); + $this->assertEquals('50941077760ee35e1500000c', $result->getListId()); + } + + /** + * @test + */ + public function shouldAddItemToList() + { + /** + * @var ListFactory $factory + */ + $factory = $this->getFactory(); + + $result = $factory->createResult($this->loadByFile('lists/add.json')); + + $this->assertEquals(12, $result->getStatusCode()); + $this->assertEquals('The item/record was updated successfully', $result->getStatusMessage()); + } + + /** + * @test + */ + public function shouldRemoveItemFromList() + { + /** + * @var ListFactory $factory + */ + $factory = $this->getFactory(); + + $result = $factory->createResult($this->loadByFile('lists/remove.json')); + + $this->assertEquals(12, $result->getStatusCode()); + $this->assertEquals('The item/record was updated successfully', $result->getStatusMessage()); + } + + /** + * @test + */ + public function shouldRemoveList() + { + /** + * @var ListFactory $factory + */ + $factory = $this->getFactory(); + + $result = $factory->createResult($this->loadByFile('lists/list_delete.json')); + $this->assertEquals(13, $result->getStatusCode()); + $this->assertEquals('The item/record was deleted successfully', $result->getStatusMessage()); } protected function getFactoryClass() diff --git a/test/Tmdb/Tests/Repository/ListRepositoryTest.php b/test/Tmdb/Tests/Repository/ListRepositoryTest.php index 8c5f3d73..3c6a045d 100644 --- a/test/Tmdb/Tests/Repository/ListRepositoryTest.php +++ b/test/Tmdb/Tests/Repository/ListRepositoryTest.php @@ -14,7 +14,8 @@ class ListRepositoryTest extends TestCase { - const LIST_ID = '509ec17b19c2950a0600050d'; + const LIST_ID = '509ec17b19c2950a0600050d'; + const MOVIE_ID = 150; /** * @test @@ -29,11 +30,51 @@ public function shouldLoadList() /** * @test */ - public function shouldLoadListItem() + public function shouldGetItemStatus() { $repository = $this->getRepositoryWithMockedHttpClient(); - $repository->getItemStatus(self::LIST_ID); + $repository->getItemStatus(self::LIST_ID, self::MOVIE_ID); + } + + /** + * @test + */ + public function shouldCreateList() + { + $repository = $this->getRepositoryWithMockedHttpClient(); + + $repository->createList('list-name', 'list-description'); + } + + /** + * @test + */ + public function shouldAdd() + { + $repository = $this->getRepositoryWithMockedHttpClient(); + + $repository->add('list-id', 'movie-id'); + } + + /** + * @test + */ + public function shouldRemove() + { + $repository = $this->getRepositoryWithMockedHttpClient(); + + $repository->remove('list-id', 'movie-id'); + } + + /** + * @test + */ + public function shouldDeleteList() + { + $repository = $this->getRepositoryWithMockedHttpClient(); + + $repository->deleteList('list-id'); } protected function getApiClass() diff --git a/test/Tmdb/Tests/Resources/lists/add.json b/test/Tmdb/Tests/Resources/lists/add.json new file mode 100644 index 00000000..0738a914 --- /dev/null +++ b/test/Tmdb/Tests/Resources/lists/add.json @@ -0,0 +1,4 @@ +{ + "status_code": 12, + "status_message": "The item/record was updated successfully" +} \ No newline at end of file diff --git a/test/Tmdb/Tests/Resources/lists/item_status.json b/test/Tmdb/Tests/Resources/lists/item_status.json new file mode 100644 index 00000000..ec36d481 --- /dev/null +++ b/test/Tmdb/Tests/Resources/lists/item_status.json @@ -0,0 +1,4 @@ +{ + "id": "509ec17b19c2950a0600050d", + "item_present": true +} \ No newline at end of file diff --git a/test/Tmdb/Tests/Resources/lists/list_create.json b/test/Tmdb/Tests/Resources/lists/list_create.json new file mode 100644 index 00000000..32e64db8 --- /dev/null +++ b/test/Tmdb/Tests/Resources/lists/list_create.json @@ -0,0 +1,5 @@ +{ + "status_code": 1, + "status_message": "Success", + "list_id": "50941077760ee35e1500000c" +} \ No newline at end of file diff --git a/test/Tmdb/Tests/Resources/lists/list_delete.json b/test/Tmdb/Tests/Resources/lists/list_delete.json new file mode 100644 index 00000000..3702660e --- /dev/null +++ b/test/Tmdb/Tests/Resources/lists/list_delete.json @@ -0,0 +1,4 @@ +{ + "status_code": 13, + "status_message": "The item/record was deleted successfully" +} \ No newline at end of file diff --git a/test/Tmdb/Tests/Resources/lists/remove.json b/test/Tmdb/Tests/Resources/lists/remove.json new file mode 100644 index 00000000..0738a914 --- /dev/null +++ b/test/Tmdb/Tests/Resources/lists/remove.json @@ -0,0 +1,4 @@ +{ + "status_code": 12, + "status_message": "The item/record was updated successfully" +} \ No newline at end of file