PageRenderTime 28ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/Zend/Service/Yahoo/OnlineTest.php

http://github.com/zendframework/zf2
PHP | 377 lines | 209 code | 49 blank | 119 comment | 2 complexity | cdf2fb184d7e4e6b3fafcb2869d81507 MD5 | raw file
Possible License(s): BSD-3-Clause
  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_Yahoo
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /**
  22. * @category Zend
  23. * @package Zend_Service_Yahoo
  24. * @subpackage UnitTests
  25. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  26. * @license http://framework.zend.com/license/new-bsd New BSD License
  27. * @group Zend_Service
  28. * @group Zend_Service_Yahoo
  29. */
  30. class Zend_Service_Yahoo_OnlineTest extends PHPUnit_Framework_TestCase
  31. {
  32. /**
  33. * Reference to Yahoo service consumer object
  34. *
  35. * @var Zend_Service_Yahoo
  36. */
  37. protected $_yahoo;
  38. /**
  39. * Socket based HTTP client adapter
  40. *
  41. * @var Zend_Http_Client_Adapter_Socket
  42. */
  43. protected $_httpClientAdapterSocket;
  44. /**
  45. * Sets up this test case
  46. *
  47. * @return void
  48. */
  49. public function setUp()
  50. {
  51. if (!constant('TESTS_ZEND_SERVICE_YAHOO_ONLINE_ENABLED')) {
  52. $this->markTestSkipped('Zend_Service_Yahoo online tests are not enabled');
  53. }
  54. $this->_yahoo = new Zend_Service_Yahoo(constant('TESTS_ZEND_SERVICE_YAHOO_ONLINE_APPID'));
  55. $this->_httpClientAdapterSocket = new Zend_Http_Client_Adapter_Socket();
  56. $this->_yahoo->getRestClient()
  57. ->getHttpClient()
  58. ->setAdapter($this->_httpClientAdapterSocket);
  59. }
  60. /**
  61. * Ensures that inlinkDataSearch() works as expected given 'http://framework.zend.com/' as a query
  62. *
  63. * @return void
  64. */
  65. public function testInlinkDataSearchPhp()
  66. {
  67. $inlinkDataResultSet = $this->_yahoo->inlinkDataSearch('http://framework.zend.com/');
  68. $this->assertTrue($inlinkDataResultSet instanceof Zend_Service_Yahoo_InlinkDataResultSet);
  69. $this->assertTrue($inlinkDataResultSet->totalResultsAvailable > 10);
  70. $this->assertEquals(50, $inlinkDataResultSet->totalResultsReturned);
  71. $this->assertEquals(50, $inlinkDataResultSet->totalResults());
  72. $this->assertEquals(1, $inlinkDataResultSet->firstResultPosition);
  73. $this->assertEquals(0, $inlinkDataResultSet->key());
  74. try {
  75. $inlinkDataResultSet->seek(-1);
  76. $this->fail('Expected OutOfBoundsException not thrown');
  77. } catch (OutOfBoundsException $e) {
  78. $this->assertContains('Illegal index', $e->getMessage());
  79. }
  80. foreach ($inlinkDataResultSet as $inlinkDataResult) {
  81. $this->assertTrue($inlinkDataResult instanceof Zend_Service_Yahoo_InlinkDataResult);
  82. }
  83. $this->assertEquals(50, $inlinkDataResultSet->key());
  84. $inlinkDataResultSet->seek(0);
  85. $this->assertEquals(0, $inlinkDataResultSet->key());
  86. }
  87. /**
  88. * Ensures that imageSearch() works as expected given 'php' as a query
  89. *
  90. * @return void
  91. */
  92. public function testImageSearchPhp()
  93. {
  94. $imageResultSet = $this->_yahoo->imageSearch('php');
  95. $this->assertTrue($imageResultSet instanceof Zend_Service_Yahoo_ImageResultSet);
  96. $this->assertTrue($imageResultSet->totalResultsAvailable > 10);
  97. $this->assertEquals(10, $imageResultSet->totalResultsReturned);
  98. $this->assertEquals(10, $imageResultSet->totalResults());
  99. $this->assertEquals(1, $imageResultSet->firstResultPosition);
  100. $this->assertEquals(0, $imageResultSet->key());
  101. try {
  102. $imageResultSet->seek(-1);
  103. $this->fail('Expected OutOfBoundsException not thrown');
  104. } catch (OutOfBoundsException $e) {
  105. $this->assertContains('Illegal index', $e->getMessage());
  106. }
  107. foreach ($imageResultSet as $imageResult) {
  108. $this->assertTrue($imageResult instanceof Zend_Service_Yahoo_ImageResult);
  109. }
  110. $this->assertEquals(10, $imageResultSet->key());
  111. $imageResultSet->seek(0);
  112. $this->assertEquals(0, $imageResultSet->key());
  113. }
  114. /**
  115. * Ensures that imageSearch() throws an exception when the adult_ok option is invalid
  116. *
  117. * @return void
  118. */
  119. public function testImageSearchExceptionAdultOkInvalid()
  120. {
  121. try {
  122. $this->_yahoo->imageSearch('php', array('adult_ok' => -1));
  123. $this->fail('Expected Zend_Service_Exception not thrown');
  124. } catch (Zend_Service_Exception $e) {
  125. $this->assertContains('error occurred sending request', $e->getMessage());
  126. }
  127. }
  128. /**
  129. * Ensures that localSearch() works as expected when searching for restaurants in ZIP 95014
  130. *
  131. * @return void
  132. */
  133. public function testLocalSearchRestaurants()
  134. {
  135. $localResultSet = $this->_yahoo->localSearch('restaurants', array('zip' => '95014'));
  136. $this->assertTrue($localResultSet instanceof Zend_Service_Yahoo_LocalResultSet);
  137. $this->assertTrue($localResultSet->totalResultsAvailable > 10);
  138. $this->assertEquals(10, $localResultSet->totalResultsReturned);
  139. $this->assertEquals(10, $localResultSet->totalResults());
  140. $this->assertEquals(1, $localResultSet->firstResultPosition);
  141. foreach ($localResultSet as $localResult) {
  142. $this->assertTrue($localResult instanceof Zend_Service_Yahoo_LocalResult);
  143. }
  144. }
  145. /**
  146. * Ensures that localSearch() throws an exception when the radius option is invalid
  147. *
  148. * @return void
  149. */
  150. public function testLocalSearchExceptionRadiusInvalid()
  151. {
  152. try {
  153. $this->_yahoo->localSearch('php', array('zip' => '95014', 'radius' => -1));
  154. $this->fail('Expected Zend_Service_Exception not thrown');
  155. } catch (Zend_Service_Exception $e) {
  156. $this->assertContains('error occurred sending request', $e->getMessage());
  157. }
  158. }
  159. /**
  160. * Ensures that newsSearch() works as expected when searching for 'php'
  161. *
  162. * @return void
  163. */
  164. public function testNewsSearchPhp()
  165. {
  166. $newsResultSet = $this->_yahoo->newsSearch('php');
  167. $this->assertTrue($newsResultSet instanceof Zend_Service_Yahoo_NewsResultSet);
  168. $this->assertTrue($newsResultSet->totalResultsAvailable > 10);
  169. $this->assertEquals(10, $newsResultSet->totalResultsReturned);
  170. $this->assertEquals(10, $newsResultSet->totalResults());
  171. $this->assertEquals(1, $newsResultSet->firstResultPosition);
  172. foreach ($newsResultSet as $newsResult) {
  173. $this->assertTrue($newsResult instanceof Zend_Service_Yahoo_NewsResult);
  174. }
  175. }
  176. /**
  177. * Ensures that pageDataSearch() works as expected given 'http://framework.zend.com/' as a query
  178. *
  179. * @return void
  180. */
  181. public function testPageDataSearchPhp()
  182. {
  183. $pageDataResultSet = $this->_yahoo->pageDataSearch('http://framework.zend.com/');
  184. $this->assertTrue($pageDataResultSet instanceof Zend_Service_Yahoo_PageDataResultSet);
  185. $this->assertTrue($pageDataResultSet->totalResultsAvailable > 10);
  186. $this->assertEquals(50, $pageDataResultSet->totalResultsReturned);
  187. $this->assertEquals(50, $pageDataResultSet->totalResults());
  188. $this->assertEquals(1, $pageDataResultSet->firstResultPosition);
  189. $this->assertEquals(0, $pageDataResultSet->key());
  190. try {
  191. $pageDataResultSet->seek(-1);
  192. $this->fail('Expected OutOfBoundsException not thrown');
  193. } catch (OutOfBoundsException $e) {
  194. $this->assertContains('Illegal index', $e->getMessage());
  195. }
  196. foreach ($pageDataResultSet as $pageDataResult) {
  197. $this->assertTrue($pageDataResult instanceof Zend_Service_Yahoo_PageDataResult);
  198. }
  199. $this->assertEquals(50, $pageDataResultSet->key());
  200. $pageDataResultSet->seek(0);
  201. $this->assertEquals(0, $pageDataResultSet->key());
  202. }
  203. /**
  204. * Ensures that videoSearch() works as expected given 'php' as a query
  205. *
  206. * @return void
  207. */
  208. public function testVideoSearchPhp()
  209. {
  210. $videoResultSet = $this->_yahoo->videoSearch('php');
  211. $this->assertTrue($videoResultSet instanceof Zend_Service_Yahoo_VideoResultSet);
  212. $this->assertTrue($videoResultSet->totalResultsAvailable > 10);
  213. $this->assertEquals(10, $videoResultSet->totalResultsReturned);
  214. $this->assertEquals(10, $videoResultSet->totalResults());
  215. $this->assertEquals(1, $videoResultSet->firstResultPosition);
  216. $this->assertEquals(0, $videoResultSet->key());
  217. try {
  218. $videoResultSet->seek(-1);
  219. $this->fail('Expected OutOfBoundsException not thrown');
  220. } catch (OutOfBoundsException $e) {
  221. $this->assertContains('Illegal index', $e->getMessage());
  222. }
  223. foreach ($videoResultSet as $videoResult) {
  224. $this->assertTrue($videoResult instanceof Zend_Service_Yahoo_VideoResult);
  225. }
  226. $this->assertEquals(10, $videoResultSet->key());
  227. $videoResultSet->seek(0);
  228. $this->assertEquals(0, $videoResultSet->key());
  229. }
  230. /**
  231. * Ensures that webSearch() works as expected when searching for 'php'
  232. *
  233. * @return void
  234. */
  235. public function testWebSearchPhp()
  236. {
  237. $webResultSet = $this->_yahoo->webSearch('php');
  238. $this->assertTrue($webResultSet instanceof Zend_Service_Yahoo_WebResultSet);
  239. $this->assertTrue($webResultSet->totalResultsAvailable > 10);
  240. $this->assertEquals(10, $webResultSet->totalResultsReturned);
  241. $this->assertEquals(10, $webResultSet->totalResults());
  242. $this->assertEquals(1, $webResultSet->firstResultPosition);
  243. foreach ($webResultSet as $webResult) {
  244. $this->assertTrue($webResult instanceof Zend_Service_Yahoo_WebResult);
  245. }
  246. }
  247. /**
  248. * Ensures that webSearch() throws an exception when the adult_ok option is invalid
  249. *
  250. * @return void
  251. */
  252. public function testWebSearchExceptionAdultOkInvalid()
  253. {
  254. try {
  255. $this->_yahoo->webSearch('php', array('adult_ok' => 'oops'));
  256. $this->fail('Expected Zend_Service_Exception not thrown');
  257. } catch (Zend_Service_Exception $e) {
  258. $this->assertContains('error occurred sending request', $e->getMessage());
  259. }
  260. }
  261. /**
  262. * Ensures that webSearch() throws an exception when the similar_ok option is invalid
  263. *
  264. * @return void
  265. */
  266. public function testWebSearchExceptionSimilarOkInvalid()
  267. {
  268. try {
  269. $this->_yahoo->webSearch('php', array('similar_ok' => 'oops'));
  270. $this->fail('Expected Zend_Service_Exception not thrown');
  271. } catch (Zend_Service_Exception $e) {
  272. $this->assertContains('error occurred sending request', $e->getMessage());
  273. }
  274. }
  275. /**
  276. * Check support for the region option and ensure that it throws an exception
  277. * for unsupported regions
  278. *
  279. * @group ZF-3222
  280. * @return void
  281. */
  282. public function testWebSearchRegion()
  283. {
  284. $this->_yahoo->webSearch('php', array('region' => 'nl'));
  285. try {
  286. $this->_yahoo->webSearch('php', array('region' => 'oops'));
  287. $this->fail('Expected Zend_Service_Exception not thrown');
  288. }catch (Zend_Service_Exception $e) {
  289. $this->assertContains("Invalid value for option 'region': oops", $e->getMessage());
  290. }
  291. }
  292. /**
  293. * Ensures that webSearch() works as expected when searching for 'php'
  294. *
  295. * @group ZF-2358
  296. */
  297. public function testWebSearchForSite()
  298. {
  299. $webResultSet = $this->_yahoo->webSearch('php', array('site' => 'www.php.net'));
  300. $this->assertTrue($webResultSet instanceof Zend_Service_Yahoo_WebResultSet);
  301. $this->assertTrue($webResultSet->totalResultsAvailable > 10);
  302. $this->assertEquals(10, $webResultSet->totalResultsReturned);
  303. $this->assertEquals(10, $webResultSet->totalResults());
  304. $this->assertEquals(1, $webResultSet->firstResultPosition);
  305. foreach ($webResultSet as $webResult) {
  306. $this->assertTrue($webResult instanceof Zend_Service_Yahoo_WebResult);
  307. }
  308. }
  309. }
  310. /**
  311. * @category Zend
  312. * @package Zend_Service_Yahoo
  313. * @subpackage UnitTests
  314. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  315. * @license http://framework.zend.com/license/new-bsd New BSD License
  316. * @group Zend_Service
  317. * @group Zend_Service_Yahoo
  318. */
  319. class Zend_Service_Yahoo_OnlineTest_Skip extends PHPUnit_Framework_TestCase
  320. {
  321. public function setUp()
  322. {
  323. $this->markTestSkipped('Zend_Service_Yahoo online tests not enabled with an APPID in TestConfiguration.php');
  324. }
  325. public function testNothing()
  326. {
  327. }
  328. }