OnlineTest.php 13 KB

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