TwitterSearchTest.php 5.5 KB

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