PageRenderTime 26ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/webapp/plugins/twitterrealtime/tests/TestOfConsumerUserStream.php

https://github.com/devsatish/ThinkUp
PHP | 200 lines | 139 code | 21 blank | 40 comment | 7 complexity | 9f56f4e288e9e0a27fe04686cf18026a MD5 | raw file
  1. <?php
  2. /**
  3. * ThinkUp/webapp/plugins/twitterrealtime/tests/TestOfConsumerUserStream.php
  4. *
  5. * Copyright (c) 2011 Amy Unruh, Mark Wilkie
  6. *
  7. * LICENSE:
  8. *
  9. * This file is part of ThinkUp (http://thinkupapp.com).
  10. *
  11. * ThinkUp is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
  12. * License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any
  13. * later version.
  14. *
  15. * ThinkUp is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
  16. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  17. * details.
  18. *
  19. * You should have received a copy of the GNU General Public License along with ThinkUp. If not, see
  20. * <http://www.gnu.org/licenses/>.
  21. *
  22. * Test of ConsumerUserStream
  23. *
  24. * @license http://www.gnu.org/licenses/gpl.html
  25. * @copyright 2011 Amy Unruh, Mark Wilkie
  26. * @author Amy Unruh
  27. *
  28. */
  29. require_once 'tests/init.tests.php';
  30. require_once THINKUP_ROOT_PATH.'webapp/config.inc.php';
  31. require_once THINKUP_ROOT_PATH.'webapp/_lib/extlib/simpletest/autorun.php';
  32. require_once THINKUP_ROOT_PATH.'tests/classes/class.ThinkUpUnitTestCase.php';
  33. require_once THINKUP_ROOT_PATH.'webapp/plugins/twitterrealtime/tests/classes/mock.Redis.php';
  34. require_once THINKUP_ROOT_PATH.'webapp/plugins/twitterrealtime/model/class.ConsumerUserStream.php';
  35. require_once THINKUP_ROOT_PATH.'webapp/plugins/twitterrealtime/model/class.StreamMessageQueue.php';
  36. require_once THINKUP_ROOT_PATH.'webapp/plugins/twitterrealtime/model/class.StreamMessageQueueMySQL.php';
  37. require_once THINKUP_ROOT_PATH.'webapp/plugins/twitterrealtime/model/class.StreamMessageQueueFactory.php';
  38. class TestOfConsumerUserStream extends ThinkUpUnitTestCase {
  39. public function setUp() {
  40. parent::setUp();
  41. StreamMessageQueueFactory::$queue = null;
  42. $this->logger = Logger::getInstance();
  43. $this->config = Config::getInstance();
  44. }
  45. public function tearDown() {
  46. // $this->builders = null;
  47. parent::tearDown();
  48. StreamMessageQueueFactory::$queue = null;
  49. }
  50. public function testGetInstance() {
  51. $stream_data = $this->setUpData(true);
  52. $twitter_data = $this->setUpTwitterData();
  53. $consumer_user_stream = ConsumerUserStream::getInstance('token', 'secret');
  54. $this->assertNotNull($consumer_user_stream);
  55. }
  56. public function testEnqueueStatusMockRedis() {
  57. //dont run redis test for php less than 5.3
  58. $version = explode('.', PHP_VERSION);
  59. if (! ($version[0] >= 5 && $version[1] >= 3)) {
  60. //error_log("PHP version less than 5.3, Skipping Redis Tests...");
  61. return;
  62. } else {
  63. require_once THINKUP_ROOT_PATH.'webapp/plugins/twitterrealtime/model/class.StreamMessageQueueRedis.php';
  64. }
  65. $stream_data = $this->setUpData(true);
  66. $twitter_data = $this->setUpTwitterData();
  67. $queue = new StreamMessageQueueRedis();
  68. $queue->redis = new MockRedis();
  69. StreamMessageQueueFactory::$queue = $queue;
  70. $consumer_user_stream = new ConsumerUserStream('username', 'password');
  71. $consumer_user_stream->setKey('mark@example.com', 1);
  72. $procs_data = FixtureBuilder::build('stream_procs', array('process_id'=>getmypid(),
  73. 'email'=>'mark@example.com', 'instance_id' => 1));
  74. $consumer_user_stream->enqueueStatus("string1");
  75. $consumer_user_stream->enqueueStatus("string2");
  76. $this->assertIdentical(array('string1', 'string2'), MockRedis::$queue);
  77. StreamMessageQueueFactory::$queue = null;
  78. MockRedis::$queue = null;
  79. // stream proc data set
  80. $sql = "select process_id, email, instance_id, unix_timestamp(last_report) as last_report from " .
  81. $this->table_prefix . "stream_procs";
  82. $stmt = PluginOptionMysqlDAO::$PDO->query($sql);
  83. $data = $stmt->fetchAll();
  84. $process_id = getmypid();
  85. $this->assertIdentical($data[0]['process_id'], $process_id . '');
  86. $recent_time = time() - 50;
  87. $this->assertTrue($data[0]['last_report'] > $recent_time);
  88. }
  89. public function testEnqueueStatusRedis() {
  90. //dont run redis test for php less than 5.3
  91. $version = explode('.', PHP_VERSION);
  92. if (! ($version[0] >= 5 && $version[1] >= 3)) {
  93. //error_log("PHP version less than 5.3, Skipping Redis Tests...");
  94. return;
  95. }
  96. if((getenv('WITH_REDIS')!==false)) {
  97. if($this->DEBUG) { print "NOTE: Running redis test againt a local redis server\n"; }
  98. $stream_data = $this->setUpData('true');
  99. $twitter_data = $this->setUpTwitterData();
  100. $consumer_user_stream = new ConsumerUserStream('username', 'password');
  101. $consumer_user_stream->setKey('mark@example.com', 1);
  102. $procs_data = FixtureBuilder::build('stream_procs', array('process_id' => getmypid(),
  103. 'email' => 'mark@example.com', 'instance_id' => 1));
  104. $consumer_user_stream->enqueueStatus("string1");
  105. $consumer_user_stream->enqueueStatus("string2");
  106. $queue = new StreamMessageQueueRedis();
  107. $this->assertEqual($queue->processStreamData(), 'string1');
  108. $this->assertEqual($queue->processStreamData(), 'string2');
  109. $this->assertNull($queue->processStreamData());
  110. // stream proc data set
  111. $sql = "select process_id, email, instance_id, unix_timestamp(last_report) as last_report from " .
  112. $this->table_prefix . "stream_procs";
  113. $stmt = PluginOptionMysqlDAO::$PDO->query($sql);
  114. $data = $stmt->fetchAll();
  115. $process_id = getmypid();
  116. $this->assertIdentical($data[0]['process_id'], $process_id . '');
  117. $recent_time = time() - 50;
  118. $this->assertTrue($data[0]['last_report'] > $recent_time);
  119. }
  120. }
  121. public function testEnqueueStatusMySQL() {
  122. $stream_data = $this->setUpData();
  123. $twitter_data = $this->setUpTwitterData();
  124. $consumer_user_stream = new ConsumerUserStream('username', 'password');
  125. $consumer_user_stream->setKey('mark@example.com', 1);
  126. $procs_data = FixtureBuilder::build('stream_procs', array('process_id' => getmypid(),
  127. 'email' => 'mark@example.com', 'instance_id' => 1));
  128. $consumer_user_stream->enqueueStatus("string1");
  129. $consumer_user_stream->enqueueStatus("string2");
  130. $sql = "select * from " . $this->table_prefix . "stream_data";
  131. $stmt = PluginOptionMysqlDAO::$PDO->query($sql);
  132. $data = $stmt->fetchAll();
  133. $this->assertIdentical($data[0][0],'1');
  134. $this->assertIdentical($data[0][1],'string1');
  135. $this->assertIdentical($data[0][2],'twitter');
  136. $this->assertIdentical($data[1][0],'2');
  137. $this->assertIdentical($data[1][1],'string2');
  138. $this->assertIdentical($data[1][2],'twitter');
  139. $sql = "select process_id, email, instance_id, unix_timestamp(last_report) as last_report from " .
  140. $this->table_prefix . "stream_procs";
  141. $stmt = PluginOptionMysqlDAO::$PDO->query($sql);
  142. $data = $stmt->fetchAll();
  143. $process_id = getmypid();
  144. $this->assertIdentical($data[0]['process_id'], $process_id . '');
  145. $recent_time = time() - 50;
  146. $this->assertTrue($data[0]['last_report'] > $recent_time);
  147. }
  148. /**
  149. * set up stream data
  150. */
  151. private function setUpData($use_redis = 0) {
  152. $builder_owner = FixtureBuilder::build('owners', array('email' => 'me@example.com', 'user_activated' => 1) );
  153. $builder_plugin = FixtureBuilder::build('plugins', array('folder_name' => 'twitterrealtime',
  154. 'is_active' => 1) );
  155. $plugin_id = $builder_plugin->columns['last_insert_id'];
  156. $namespace = OptionDAO::PLUGIN_OPTIONS . '-' .$plugin_id;
  157. $builder_plugin_options =
  158. FixtureBuilder::build('options',
  159. array('namespace' => $namespace, 'option_name' => 'use_redis', 'option_value' => $use_redis) );
  160. $this->simulateLogin('me@example.com');
  161. $owner_dao = DAOFactory::getDAO('OwnerDAO');
  162. $owner = $owner_dao->getByEmail(Session::getLoggedInUser());
  163. return array($builder_owner, $builder_plugin, $builder_plugin_options);
  164. }
  165. /**
  166. * set up twitter data
  167. */
  168. private function setUpTwitterData() {
  169. $plugin_id = 1;
  170. $namespace = OptionDAO::PLUGIN_OPTIONS . '-' .$plugin_id;
  171. $builder_plugin_option1 =
  172. FixtureBuilder::build('options',
  173. array('namespace' => $namespace, 'option_name' => 'oauth_consumer_key', 'option_value' => 'token'));
  174. $builder_plugin_option2 =
  175. FixtureBuilder::build('options',
  176. array('namespace' => $namespace, 'option_name' => 'oauth_consumer_secret', 'option_value' => 'secret'));
  177. return array($builder_plugin_option1, $builder_plugin_option2);
  178. }
  179. }