TwitterSearchTest.php 5.6 KB

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