OnlineTest.php 13 KB

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