OfflineTest.php 18 KB

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