--- tests/Zend/Service/Flickr/OfflineTest.php (revision 7796) +++ tests/Zend/Service/Flickr/OfflineTest.php (working copy) @@ -352,8 +352,175 @@ $this->assertContains('parameters are invalid', $e->getMessage()); } } + + /** + * Basic testing to ensure that groupPoolGetPhotos() works as expected + * + * @return void + */ + public function testGroupPoolGetPhotosBasic() + { + $this->_flickr->getRestClient() + ->getHttpClient() + ->setAdapter($this->_httpClientAdapterTest); + $this->_httpClientAdapterTest->setResponse($this->_loadResponse(__FUNCTION__)); + + $options = array( + 'per_page' => 10, + 'page' => 1, + 'extras' => 'license, date_upload, date_taken, owner_name, icon_server' + ); + + $resultSet = $this->_flickr->groupPoolGetPhotos('20083316@N00', $options); + + $this->assertEquals(4285, $resultSet->totalResultsAvailable); + $this->assertEquals(10, $resultSet->totalResults()); + $this->assertEquals(10, $resultSet->totalResultsReturned); + $this->assertEquals(1, $resultSet->firstResultPosition); + + $this->assertEquals(0, $resultSet->key()); + + try { + $resultSet->seek(-1); + $this->fail('Expected OutOfBoundsException not thrown'); + } catch (OutOfBoundsException $e) { + $this->assertContains('Illegal index', $e->getMessage()); + } + + $resultSet->seek(9); + + try { + $resultSet->seek(10); + $this->fail('Expected OutOfBoundsException not thrown'); + } catch (OutOfBoundsException $e) { + $this->assertContains('Illegal index', $e->getMessage()); + } + + $resultSet->rewind(); + + $resultSetIds = array( + '428222530', + '427883929', + '427884403', + '427887192', + '427883923', + '427884394', + '427883930', + '427884398', + '427883924', + '427884401' + ); + + $this->assertTrue($resultSet->valid()); + + foreach ($resultSetIds as $resultSetId) { + $this->_httpClientAdapterTest->setResponse($this->_loadResponse(__FUNCTION__ . "-result_$resultSetId")); + $result = $resultSet->current(); + $this->assertTrue($result instanceof Zend_Service_Flickr_Result); + $resultSet->next(); + } + + $this->assertFalse($resultSet->valid()); + } + /** + * Ensures that groupPoolGetPhotos() throws an exception when an option is invalid + * + * @return void + */ + public function testGroupPoolGetPhotosExceptionOptionInvalid() + { + try { + $this->_flickr->groupPoolGetPhotos('irrelevant', array('unexpected' => null)); + $this->fail('Expected Zend_Service_Exception not thrown'); + } catch (Zend_Service_Exception $e) { + $this->assertContains('parameters are invalid', $e->getMessage()); + } + } + + /** + * Ensures that _validateGroupPoolGetPhotos() throws an exception when the per_page option is invalid + * + * @return void + */ + public function testValidateGroupPoolGetPhotosExceptionPerPageInvalid() + { + try { + $this->_flickrProxy->proxyValidateGroupPoolGetPhotos(array('per_page' => -1)); + $this->fail('Expected Zend_Service_Exception not thrown'); + } catch (Zend_Service_Exception $e) { + $this->assertContains('"per_page" option', $e->getMessage()); + } + } + + /** + * Ensures that _validateGroupPoolGetPhotos() throws an exception when the page option is invalid + * + * @return void + */ + public function testValidateGroupPoolGetPhotosExceptionPageInvalid() + { + try { + $this->_flickrProxy->proxyValidateGroupPoolGetPhotos(array('per_page' => 10, 'page' => 1.23)); + $this->fail('Expected Zend_Service_Exception not thrown'); + } catch (Zend_Service_Exception $e) { + $this->assertContains('"page" option', $e->getMessage()); + } + } + + /** + * Ensures that groupPoolGetPhotos() throws an exception when an invalid group_id is given + * + * @return void + */ + public function testGroupPoolGetPhotosExceptionGroupIdInvalid() + { + $this->_flickr->getRestClient() + ->getHttpClient() + ->setAdapter($this->_httpClientAdapterTest); + + $this->_httpClientAdapterTest->setResponse($this->_loadResponse(__FUNCTION__)); + + try { + $this->_flickr->groupPoolGetPhotos('2e38a9d9425d7e2c9d0788455e9ccc61'); + $this->fail('Expected Zend_Service_Exception not thrown'); + } catch (Zend_Service_Exception $e) { + $this->assertContains('Group not found', $e->getMessage()); + } + } + + /** + * Ensures that groupPoolGetPhotos() throws an exception when an invalid group_id is given + * + * @return void + */ + public function testGroupPoolGetPhotosExceptionGroupIdEmpty() + { + try { + $this->_flickr->groupPoolGetPhotos('0'); + $this->fail('Expected Zend_Service_Exception not thrown'); + } catch (Zend_Service_Exception $e) { + $this->assertContains('supply a group id', $e->getMessage()); + } + } + + /** + * Ensures that groupPoolGetPhotos() throws an exception when an array is given for group_id + * + * @return void + */ + public function testGroupPoolGetPhotosExceptionGroupIdArray() + { + try { + $this->_flickr->groupPoolGetPhotos(array()); + $this->fail('Expected Zend_Service_Exception not thrown'); + } catch (Zend_Service_Exception $e) { + $this->assertContains('supply a group id', $e->getMessage()); + } + } + + /** * Utility method that saves an HTTP response to a file * * @param string $name @@ -389,6 +556,11 @@ { $this->_validateTagSearch($options); } + + public function proxyValidateGroupPoolGetPhotos(array $options) + { + $this->_validateGroupPoolGetPhotos($options); + } public function proxyCompareOptions(array $options, array $validOptions) { Index: tests/Zend/Service/Flickr/OnlineTest.php =================================================================== --- tests/Zend/Service/Flickr/OnlineTest.php (revision 7796) +++ tests/Zend/Service/Flickr/OnlineTest.php (working copy) @@ -74,7 +74,54 @@ ->getHttpClient() ->setAdapter($this->_httpClientAdapterSocket); } + + /** + * Basic testing to ensure that groupPoolGetPhotos works as expected + * + * @return void + */ + public function testGroupPoolGetPhotosBasic() + { + $options = array('per_page' => 10, + 'page' => 1, + 'extras' => 'license, date_upload, date_taken, owner_name, icon_server'); + + $resultSet = $this->_flickr->groupPoolGetPhotos('20083316@N00', $options); + + $this->assertEquals(21770, $resultSet->totalResultsAvailable); + $this->assertEquals(10, $resultSet->totalResults()); + $this->assertEquals(10, $resultSet->totalResultsReturned); + $this->assertEquals(1, $resultSet->firstResultPosition); + $this->assertEquals(0, $resultSet->key()); + + try { + $resultSet->seek(-1); + $this->fail('Expected OutOfBoundsException not thrown'); + } catch (OutOfBoundsException $e) { + $this->assertContains('Illegal index', $e->getMessage()); + } + + $resultSet->seek(9); + + try { + $resultSet->seek(10); + $this->fail('Expected OutOfBoundsException not thrown'); + } catch (OutOfBoundsException $e) { + $this->assertContains('Illegal index', $e->getMessage()); + } + + $resultSet->rewind(); + + $count = 0; + foreach ($resultSet as $result) { + $this->assertTrue($result instanceof Zend_Service_Flickr_Result); + $count++; + } + + $this->assertEquals(10, $count); + } + /** * Basic testing to ensure that userSearch() works as expected * Index: library/Zend/Service/Flickr.php =================================================================== --- library/Zend/Service/Flickr.php (revision 7796) +++ library/Zend/Service/Flickr.php (working copy) @@ -192,8 +192,64 @@ require_once 'Zend/Service/Flickr/ResultSet.php'; return new Zend_Service_Flickr_ResultSet($dom, $this); } + + /** + * Finds photos in a group's pool. + * + * @param string $query group id + * @param array $options Additional parameters to refine your query. + * @return Zend_Service_Flickr_ResultSet + * @throws Zend_Service_Exception + */ + public function groupPoolGetPhotos($query, array $options = array()) + { + static $method = 'flickr.groups.pools.getPhotos'; + static $defaultOptions = array('per_page' => 10, + 'page' => 1, + 'extras' => 'license, date_upload, date_taken, owner_name, icon_server'); + if (empty($query) || !is_string($query)) { + /** + * @see Zend_Service_Exception + */ + require_once 'Zend/Service/Exception.php'; + throw new Zend_Service_Exception('You must supply a group id'); + } + $options['group_id'] = $query; + + $options = $this->_prepareOptions($method, $options, $defaultOptions); + + $this->_validateGroupPoolGetPhotos($options); + + // now search for photos + $restClient = $this->getRestClient(); + $restClient->getHttpClient()->resetParameters(); + $response = $restClient->restGet('/services/rest/', $options); + + if ($response->isError()) { + /** + * @see Zend_Service_Exception + */ + require_once 'Zend/Service/Exception.php'; + throw new Zend_Service_Exception('An error occurred sending request. Status code: ' + . $response->getStatus()); + } + + $dom = new DOMDocument(); + $dom->loadXML($response->getBody()); + + self::_checkErrors($dom); + + /** + * @see Zend_Service_Flickr_ResultSet + */ + require_once 'Zend/Service/Flickr/ResultSet.php'; + return new Zend_Service_Flickr_ResultSet($dom, $this); + } + + + /** * Utility function to find Flickr User IDs for usernames. * @@ -453,8 +509,63 @@ } } + + + /** + * Validate Group Search Options + * + * @param array $options + * @throws Zend_Service_Exception + * @return void + */ + protected function _validateGroupPoolGetPhotos(array $options) + { + $validOptions = array('api_key', 'tags', 'method', 'group_id', 'per_page', 'page', 'extras', 'min_upload_date', + 'min_taken_date', 'max_upload_date', 'max_taken_date'); + $this->_compareOptions($options, $validOptions); + /** + * @see Zend_Validate_Between + */ + require_once 'Zend/Validate/Between.php'; + $between = new Zend_Validate_Between(1, 500, true); + if (!$between->isValid($options['per_page'])) { + /** + * @see Zend_Service_Exception + */ + require_once 'Zend/Service/Exception.php'; + throw new Zend_Service_Exception($options['per_page'] . ' is not valid for the "per_page" option'); + } + + /** + * @see Zend_Validate_Int + */ + require_once 'Zend/Validate/Int.php'; + $int = new Zend_Validate_Int(); + + if (!$int->isValid($options['page'])) { + /** + * @see Zend_Service_Exception + */ + require_once 'Zend/Service/Exception.php'; + throw new Zend_Service_Exception($options['page'] . ' is not valid for the "page" option'); + } + + // validate extras, which are delivered in csv format + if (isset($options['extras'])) { + $extras = explode(',', $options['extras']); + $validExtras = array('license', 'date_upload', 'date_taken', 'owner_name', 'icon_server'); + foreach($extras as $extra) { + /** + * @todo The following does not do anything [yet], so it is commented out. + */ + //in_array(trim($extra), $validExtras); + } + } + } + + /** * Throws an exception if and only if the response status indicates a failure *