OfflineTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Service_Flickr
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * Test helper
  24. */
  25. require_once dirname(__FILE__) . '/../../../TestHelper.php';
  26. /**
  27. * @see Zend_Service_Flickr
  28. */
  29. require_once 'Zend/Service/Flickr.php';
  30. /**
  31. * @category Zend
  32. * @package Zend_Service_Flickr
  33. * @subpackage UnitTests
  34. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. */
  37. class Zend_Service_Flickr_OfflineTest extends PHPUnit_Framework_TestCase
  38. {
  39. /**
  40. * Reference to Flickr service consumer object
  41. *
  42. * @var Zend_Service_Flickr
  43. */
  44. protected $_flickr;
  45. /**
  46. * Proxy to protected methods of Zend_Service_Flickr
  47. *
  48. * @var Zend_Service_Flickr_OfflineTest_FlickrProtectedMethodProxy
  49. */
  50. protected $_flickrProxy;
  51. /**
  52. * Path to test data files
  53. *
  54. * @var string
  55. */
  56. protected $_filesPath;
  57. /**
  58. * HTTP client adapter for testing
  59. *
  60. * @var Zend_Http_Client_Adapter_Test
  61. */
  62. protected $_httpClientAdapterTest;
  63. /**
  64. * Socket based HTTP client adapter
  65. *
  66. * @var Zend_Http_Client_Adapter_Socket
  67. */
  68. protected $_httpClientAdapterSocket;
  69. /**
  70. * Sets up this test case
  71. *
  72. * @return void
  73. */
  74. public function setUp()
  75. {
  76. $this->_flickr = new Zend_Service_Flickr(constant('TESTS_ZEND_SERVICE_FLICKR_ONLINE_APIKEY'));
  77. $this->_flickrProxy = new Zend_Service_Flickr_OfflineTest_FlickrProtectedMethodProxy(
  78. constant('TESTS_ZEND_SERVICE_FLICKR_ONLINE_APIKEY')
  79. );
  80. $this->_filesPath = dirname(__FILE__) . '/_files';
  81. /**
  82. * @see Zend_Http_Client_Adapter_Socket
  83. */
  84. require_once 'Zend/Http/Client/Adapter/Socket.php';
  85. $this->_httpClientAdapterSocket = new Zend_Http_Client_Adapter_Socket();
  86. /**
  87. * @see Zend_Http_Client_Adapter_Test
  88. */
  89. require_once 'Zend/Http/Client/Adapter/Test.php';
  90. $this->_httpClientAdapterTest = new Zend_Http_Client_Adapter_Test();
  91. }
  92. /**
  93. * Basic testing to ensure that tagSearch() works as expected
  94. *
  95. * @return void
  96. */
  97. public function testTagSearchBasic()
  98. {
  99. $this->_flickr->getRestClient()
  100. ->getHttpClient()
  101. ->setAdapter($this->_httpClientAdapterTest);
  102. $this->_httpClientAdapterTest->setResponse($this->_loadResponse(__FUNCTION__));
  103. $options = array(
  104. 'per_page' => 10,
  105. 'page' => 1,
  106. 'tag_mode' => 'or',
  107. 'extras' => 'license, date_upload, date_taken, owner_name, icon_server'
  108. );
  109. $resultSet = $this->_flickr->tagSearch('php', $options);
  110. $this->assertEquals(4285, $resultSet->totalResultsAvailable);
  111. $this->assertEquals(10, $resultSet->totalResults());
  112. $this->assertEquals(10, $resultSet->totalResultsReturned);
  113. $this->assertEquals(1, $resultSet->firstResultPosition);
  114. $this->assertEquals(0, $resultSet->key());
  115. try {
  116. $resultSet->seek(-1);
  117. $this->fail('Expected OutOfBoundsException not thrown');
  118. } catch (OutOfBoundsException $e) {
  119. $this->assertContains('Illegal index', $e->getMessage());
  120. }
  121. $resultSet->seek(9);
  122. try {
  123. $resultSet->seek(10);
  124. $this->fail('Expected OutOfBoundsException not thrown');
  125. } catch (OutOfBoundsException $e) {
  126. $this->assertContains('Illegal index', $e->getMessage());
  127. }
  128. $resultSet->rewind();
  129. $resultSetIds = array(
  130. '428222530',
  131. '427883929',
  132. '427884403',
  133. '427887192',
  134. '427883923',
  135. '427884394',
  136. '427883930',
  137. '427884398',
  138. '427883924',
  139. '427884401'
  140. );
  141. $this->assertTrue($resultSet->valid());
  142. foreach ($resultSetIds as $resultSetId) {
  143. $this->_httpClientAdapterTest->setResponse($this->_loadResponse(__FUNCTION__ . "-result_$resultSetId"));
  144. $result = $resultSet->current();
  145. $this->assertTrue($result instanceof Zend_Service_Flickr_Result);
  146. $resultSet->next();
  147. }
  148. $this->assertFalse($resultSet->valid());
  149. }
  150. /**
  151. * Ensures that userSearch() throws an exception when an invalid username is given
  152. *
  153. * @return void
  154. */
  155. public function testUserSearchExceptionUsernameInvalid()
  156. {
  157. $this->_flickr->getRestClient()
  158. ->getHttpClient()
  159. ->setAdapter($this->_httpClientAdapterTest);
  160. $this->_httpClientAdapterTest->setResponse($this->_loadResponse(__FUNCTION__));
  161. try {
  162. $this->_flickr->userSearch('2e38a9d9425d7e2c9d0788455e9ccc61');
  163. $this->fail('Expected Zend_Service_Exception not thrown');
  164. } catch (Zend_Service_Exception $e) {
  165. $this->assertContains('User not found', $e->getMessage());
  166. }
  167. }
  168. /**
  169. * Ensures that userSearch() throws an exception when an invalid e-mail address is given
  170. *
  171. * @return void
  172. */
  173. public function testUserSearchExceptionEmailInvalid()
  174. {
  175. $this->_flickr->getRestClient()
  176. ->getHttpClient()
  177. ->setAdapter($this->_httpClientAdapterTest);
  178. $this->_httpClientAdapterTest->setResponse($this->_loadResponse(__FUNCTION__));
  179. try {
  180. $this->_flickr->userSearch('2e38a9d9425d7e2c9d0788455e9ccc61@example.com');
  181. $this->fail('Expected Zend_Service_Exception not thrown');
  182. } catch (Zend_Service_Exception $e) {
  183. $this->assertContains('User not found', $e->getMessage());
  184. }
  185. }
  186. /**
  187. * Ensures that getIdByUsername() throws an exception given an empty argument
  188. *
  189. * @return void
  190. */
  191. public function testGetIdByUsernameExceptionUsernameEmpty()
  192. {
  193. try {
  194. $this->_flickr->getIdByUsername('0');
  195. $this->fail('Expected Zend_Service_Exception not thrown');
  196. } catch (Zend_Service_Exception $e) {
  197. $this->assertContains('supply a username', $e->getMessage());
  198. }
  199. }
  200. /**
  201. * Ensures that getIdByEmail() throws an exception given an empty argument
  202. *
  203. * @return void
  204. */
  205. public function testGetIdByEmailExceptionEmailEmpty()
  206. {
  207. try {
  208. $this->_flickr->getIdByEmail('0');
  209. $this->fail('Expected Zend_Service_Exception not thrown');
  210. } catch (Zend_Service_Exception $e) {
  211. $this->assertContains('supply an e-mail address', $e->getMessage());
  212. }
  213. }
  214. /**
  215. * Ensures that getImageDetails() throws an exception given an empty argument
  216. *
  217. * @return void
  218. */
  219. public function testGetImageDetailsExceptionIdEmpty()
  220. {
  221. try {
  222. $this->_flickr->getImageDetails('0');
  223. $this->fail('Expected Zend_Service_Exception not thrown');
  224. } catch (Zend_Service_Exception $e) {
  225. $this->assertContains('supply a photo ID', $e->getMessage());
  226. }
  227. }
  228. /**
  229. * Ensures that _validateUserSearch() throws an exception when the per_page option is invalid
  230. *
  231. * @return void
  232. */
  233. public function testValidateUserSearchExceptionPerPageInvalid()
  234. {
  235. try {
  236. $this->_flickrProxy->proxyValidateUserSearch(array('per_page' => -1));
  237. $this->fail('Expected Zend_Service_Exception not thrown');
  238. } catch (Zend_Service_Exception $e) {
  239. $this->assertContains('"per_page" option', $e->getMessage());
  240. }
  241. }
  242. /**
  243. * Ensures that _validateUserSearch() throws an exception when the page option is invalid
  244. *
  245. * @return void
  246. */
  247. public function testValidateUserSearchExceptionPageInvalid()
  248. {
  249. try {
  250. $this->_flickrProxy->proxyValidateUserSearch(array('per_page' => 10, 'page' => 1.23));
  251. $this->fail('Expected Zend_Service_Exception not thrown');
  252. } catch (Zend_Service_Exception $e) {
  253. $this->assertContains('"page" option', $e->getMessage());
  254. }
  255. }
  256. /**
  257. * Ensures that _validateTagSearch() throws an exception when the per_page option is invalid
  258. *
  259. * @return void
  260. */
  261. public function testValidateTagSearchExceptionPerPageInvalid()
  262. {
  263. try {
  264. $this->_flickrProxy->proxyValidateTagSearch(array('per_page' => -1));
  265. $this->fail('Expected Zend_Service_Exception not thrown');
  266. } catch (Zend_Service_Exception $e) {
  267. $this->assertContains('"per_page" option', $e->getMessage());
  268. }
  269. }
  270. /**
  271. * Ensures that _validateTagSearch() throws an exception when the page option is invalid
  272. *
  273. * @return void
  274. */
  275. public function testValidateTagSearchExceptionPageInvalid()
  276. {
  277. try {
  278. $this->_flickrProxy->proxyValidateTagSearch(array('per_page' => 10, 'page' => 1.23));
  279. $this->fail('Expected Zend_Service_Exception not thrown');
  280. } catch (Zend_Service_Exception $e) {
  281. $this->assertContains('"page" option', $e->getMessage());
  282. }
  283. }
  284. /**
  285. * Ensures that _compareOptions() throws an exception when an option is invalid
  286. *
  287. * @return void
  288. */
  289. public function testCompareOptionsExceptionOptionInvalid()
  290. {
  291. try {
  292. $this->_flickrProxy->proxyCompareOptions(array('unexpected' => null), array());
  293. $this->fail('Expected Zend_Service_Exception not thrown');
  294. } catch (Zend_Service_Exception $e) {
  295. $this->assertContains('parameters are invalid', $e->getMessage());
  296. }
  297. }
  298. /**
  299. * Ensures that tagSearch() throws an exception when an option is invalid
  300. *
  301. * @return void
  302. */
  303. public function testTagSearchExceptionOptionInvalid()
  304. {
  305. try {
  306. $this->_flickr->tagSearch('irrelevant', array('unexpected' => null));
  307. $this->fail('Expected Zend_Service_Exception not thrown');
  308. } catch (Zend_Service_Exception $e) {
  309. $this->assertContains('parameters are invalid', $e->getMessage());
  310. }
  311. }
  312. /**
  313. * Basic testing to ensure that groupPoolGetPhotos() works as expected
  314. *
  315. * @return void
  316. */
  317. public function testGroupPoolGetPhotosBasic()
  318. {
  319. $this->_flickr->getRestClient()
  320. ->getHttpClient()
  321. ->setAdapter($this->_httpClientAdapterTest);
  322. $this->_httpClientAdapterTest->setResponse($this->_loadResponse(__FUNCTION__));
  323. $options = array(
  324. 'per_page' => 10,
  325. 'page' => 1,
  326. 'extras' => 'license, date_upload, date_taken, owner_name, icon_server'
  327. );
  328. $resultSet = $this->_flickr->groupPoolGetPhotos('20083316@N00', $options);
  329. $this->assertEquals(4285, $resultSet->totalResultsAvailable);
  330. $this->assertEquals(10, $resultSet->totalResults());
  331. $this->assertEquals(10, $resultSet->totalResultsReturned);
  332. $this->assertEquals(1, $resultSet->firstResultPosition);
  333. $this->assertEquals(0, $resultSet->key());
  334. try {
  335. $resultSet->seek(-1);
  336. $this->fail('Expected OutOfBoundsException not thrown');
  337. } catch (OutOfBoundsException $e) {
  338. $this->assertContains('Illegal index', $e->getMessage());
  339. }
  340. $resultSet->seek(9);
  341. try {
  342. $resultSet->seek(10);
  343. $this->fail('Expected OutOfBoundsException not thrown');
  344. } catch (OutOfBoundsException $e) {
  345. $this->assertContains('Illegal index', $e->getMessage());
  346. }
  347. $resultSet->rewind();
  348. $resultSetIds = array(
  349. '428222530',
  350. '427883929',
  351. '427884403',
  352. '427887192',
  353. '427883923',
  354. '427884394',
  355. '427883930',
  356. '427884398',
  357. '427883924',
  358. '427884401'
  359. );
  360. $this->assertTrue($resultSet->valid());
  361. foreach ($resultSetIds as $resultSetId) {
  362. $this->_httpClientAdapterTest->setResponse($this->_loadResponse(__FUNCTION__ . "-result_$resultSetId"));
  363. $result = $resultSet->current();
  364. $this->assertTrue($result instanceof Zend_Service_Flickr_Result);
  365. $resultSet->next();
  366. }
  367. $this->assertFalse($resultSet->valid());
  368. }
  369. /**
  370. * Ensures that groupPoolGetPhotos() throws an exception when an option is invalid
  371. *
  372. * @return void
  373. */
  374. public function testGroupPoolGetPhotosExceptionOptionInvalid()
  375. {
  376. try {
  377. $this->_flickr->groupPoolGetPhotos('irrelevant', array('unexpected' => null));
  378. $this->fail('Expected Zend_Service_Exception not thrown');
  379. } catch (Zend_Service_Exception $e) {
  380. $this->assertContains('parameters are invalid', $e->getMessage());
  381. }
  382. }
  383. /**
  384. * Ensures that _validateGroupPoolGetPhotos() throws an exception when the per_page option is invalid
  385. *
  386. * @return void
  387. */
  388. public function testValidateGroupPoolGetPhotosExceptionPerPageInvalid()
  389. {
  390. try {
  391. $this->_flickrProxy->proxyValidateGroupPoolGetPhotos(array('per_page' => -1));
  392. $this->fail('Expected Zend_Service_Exception not thrown');
  393. } catch (Zend_Service_Exception $e) {
  394. $this->assertContains('"per_page" option', $e->getMessage());
  395. }
  396. }
  397. /**
  398. * Ensures that _validateGroupPoolGetPhotos() throws an exception when the page option is invalid
  399. *
  400. * @return void
  401. */
  402. public function testValidateGroupPoolGetPhotosExceptionPageInvalid()
  403. {
  404. try {
  405. $this->_flickrProxy->proxyValidateGroupPoolGetPhotos(array('per_page' => 10, 'page' => 1.23));
  406. $this->fail('Expected Zend_Service_Exception not thrown');
  407. } catch (Zend_Service_Exception $e) {
  408. $this->assertContains('"page" option', $e->getMessage());
  409. }
  410. }
  411. /**
  412. * Ensures that groupPoolGetPhotos() throws an exception when an invalid group_id is given
  413. *
  414. * @return void
  415. */
  416. public function testGroupPoolGetPhotosExceptionGroupIdInvalid()
  417. {
  418. $this->_flickr->getRestClient()
  419. ->getHttpClient()
  420. ->setAdapter($this->_httpClientAdapterTest);
  421. $this->_httpClientAdapterTest->setResponse($this->_loadResponse(__FUNCTION__));
  422. try {
  423. $this->_flickr->groupPoolGetPhotos('2e38a9d9425d7e2c9d0788455e9ccc61');
  424. $this->fail('Expected Zend_Service_Exception not thrown');
  425. } catch (Zend_Service_Exception $e) {
  426. $this->assertContains('Group not found', $e->getMessage());
  427. }
  428. }
  429. /**
  430. * Ensures that groupPoolGetPhotos() throws an exception when an invalid group_id is given
  431. *
  432. * @return void
  433. */
  434. public function testGroupPoolGetPhotosExceptionGroupIdEmpty()
  435. {
  436. try {
  437. $this->_flickr->groupPoolGetPhotos('0');
  438. $this->fail('Expected Zend_Service_Exception not thrown');
  439. } catch (Zend_Service_Exception $e) {
  440. $this->assertContains('supply a group id', $e->getMessage());
  441. }
  442. }
  443. /**
  444. * Ensures that groupPoolGetPhotos() throws an exception when an array is given for group_id
  445. *
  446. * @return void
  447. */
  448. public function testGroupPoolGetPhotosExceptionGroupIdArray()
  449. {
  450. try {
  451. $this->_flickr->groupPoolGetPhotos(array());
  452. $this->fail('Expected Zend_Service_Exception not thrown');
  453. } catch (Zend_Service_Exception $e) {
  454. $this->assertContains('supply a group id', $e->getMessage());
  455. }
  456. }
  457. /**
  458. * Utility method that saves an HTTP response to a file
  459. *
  460. * @param string $name
  461. * @return void
  462. */
  463. protected function _saveResponse($name)
  464. {
  465. file_put_contents("$this->_filesPath/$name.response",
  466. $this->_flickr->getRestClient()->getHttpClient()->getLastResponse()->asString());
  467. }
  468. /**
  469. * Utility method for returning a string HTTP response, which is loaded from a file
  470. *
  471. * @param string $name
  472. * @return string
  473. */
  474. protected function _loadResponse($name)
  475. {
  476. return file_get_contents("$this->_filesPath/$name.response");
  477. }
  478. }
  479. class Zend_Service_Flickr_OfflineTest_FlickrProtectedMethodProxy extends Zend_Service_Flickr
  480. {
  481. public function proxyValidateUserSearch(array $options)
  482. {
  483. $this->_validateUserSearch($options);
  484. }
  485. public function proxyValidateTagSearch(array $options)
  486. {
  487. $this->_validateTagSearch($options);
  488. }
  489. public function proxyValidateGroupPoolGetPhotos(array $options)
  490. {
  491. $this->_validateGroupPoolGetPhotos($options);
  492. }
  493. public function proxyCompareOptions(array $options, array $validOptions)
  494. {
  495. $this->_compareOptions($options, $validOptions);
  496. }
  497. }