OnlineTest.php 12 KB

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