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