TwitterSearchTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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_Twitter_Search
  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. * @version $Id$
  21. */
  22. if (!defined('PHPUnit_MAIN_METHOD')) {
  23. define('PHPUnit_MAIN_METHOD', 'Zend_Service_Twitter_TwitterSearchTest::main');
  24. }
  25. /** Zend_Service_Twitter_Search */
  26. require_once 'Zend/Service/Twitter/Search.php';
  27. /** Zend_Http_Client */
  28. require_once 'Zend/Http/Client.php';
  29. /** Zend_Http_Client_Adapter_Test */
  30. require_once 'Zend/Http/Client/Adapter/Test.php';
  31. /**
  32. * @category Zend
  33. * @package Zend_Service_Twitter
  34. * @subpackage UnitTests
  35. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  36. * @license http://framework.zend.com/license/new-bsd New BSD License
  37. * @group Zend_Service
  38. * @group Zend_Service_Twitter
  39. */
  40. class Zend_Service_Twitter_TwitterSearchTest extends PHPUnit_Framework_TestCase
  41. {
  42. /**
  43. * Runs the test methods of this class.
  44. *
  45. * @return void
  46. */
  47. public static function main()
  48. {
  49. $suite = new PHPUnit_Framework_TestSuite(__CLASS__);
  50. $result = PHPUnit_TextUI_TestRunner::run($suite);
  51. }
  52. /**
  53. * Sets up the fixture, for example, open a network connection.
  54. * This method is called before a test is executed.
  55. *
  56. * @return void
  57. */
  58. protected function setUp()
  59. {
  60. if (!defined('TESTS_ZEND_SERVICE_TWITTER_ONLINE_ENABLED')
  61. || !constant('TESTS_ZEND_SERVICE_TWITTER_ONLINE_ENABLED')
  62. ) {
  63. $this->markTestSkipped('Twitter tests are not enabled');
  64. return;
  65. }
  66. $this->twitter = new Zend_Service_Twitter_Search();
  67. }
  68. public function testSetResponseTypeToJSON()
  69. {
  70. $this->twitter->setResponseType('json');
  71. $this->assertEquals('json', $this->twitter->getResponseType());
  72. }
  73. public function testSetResponseTypeToATOM()
  74. {
  75. $this->twitter->setResponseType('atom');
  76. $this->assertEquals('atom', $this->twitter->getResponseType());
  77. }
  78. public function testInvalidResponseTypeShouldThrowException()
  79. {
  80. try {
  81. $this->twitter->setResponseType('xml');
  82. $this->fail('Setting an invalid response type should throw an exception');
  83. } catch(Exception $e) {
  84. }
  85. }
  86. public function testValidResponseTypeShouldNotThrowException()
  87. {
  88. try {
  89. $this->twitter->setResponseType('atom');
  90. } catch(Exception $e) {
  91. $this->fail('Setting a valid response type should not throw an exception');
  92. }
  93. }
  94. public function testSearchTrendsReturnsArray()
  95. {
  96. $response = $this->twitter->trends();
  97. $this->assertType('array', $response);
  98. }
  99. public function testJsonSearchContainsWordReturnsArray()
  100. {
  101. $this->twitter->setResponseType('json');
  102. $response = $this->twitter->search('zend');
  103. $this->assertType('array', $response);
  104. }
  105. public function testAtomSearchContainsWordReturnsObject()
  106. {
  107. $this->twitter->setResponseType('atom');
  108. $response = $this->twitter->search('zend');
  109. $this->assertTrue($response instanceof Zend_Feed_Atom);
  110. }
  111. public function testJsonSearchRestrictsLanguageReturnsArray()
  112. {
  113. $this->twitter->setResponseType('json');
  114. $response = $this->twitter->search('zend', array('lang' => 'de'));
  115. $this->assertType('array', $response);
  116. $this->assertTrue((isset($response['results'][0]) && $response['results'][0]['iso_language_code'] == "de"));
  117. }
  118. public function testAtomSearchRestrictsLanguageReturnsObject()
  119. {
  120. $this->twitter->setResponseType('atom');
  121. /* @var $response Zend_Feed_Atom */
  122. $response = $this->twitter->search('zend', array('lang' => 'de'));
  123. $this->assertTrue($response instanceof Zend_Feed_Atom);
  124. $this->assertTrue((strpos($response->link('self'), 'lang=de') !== false));
  125. }
  126. public function testJsonSearchReturnThirtyResultsReturnsArray()
  127. {
  128. $this->twitter->setResponseType('json');
  129. $response = $this->twitter->search('zend', array('rpp' => '30'));
  130. $this->assertType('array', $response);
  131. $this->assertTrue((count($response['results']) == 30));
  132. }
  133. public function testAtomSearchReturnThirtyResultsReturnsObject()
  134. {
  135. $this->twitter->setResponseType('atom');
  136. /* @var $response Zend_Feed_Atom */
  137. $response = $this->twitter->search('zend', array('rpp' => '30'));
  138. $this->assertTrue($response instanceof Zend_Feed_Atom);
  139. $this->assertTrue(($response->count() == 30));
  140. }
  141. public function testAtomSearchShowUserReturnsObject()
  142. {
  143. $this->twitter->setResponseType('atom');
  144. /* @var $response Zend_Feed_Atom */
  145. $response = $this->twitter->search('zend', array('show_user' => 'true'));
  146. $this->assertTrue($response instanceof Zend_Feed_Atom);
  147. }
  148. }
  149. if (PHPUnit_MAIN_METHOD == 'Zend_Service_TwitterSearchTest::main') {
  150. Zend_Service_TwitterSearchTest::main();
  151. }