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

/tests/TestOfDAOFactory.php

https://github.com/garethjo/ThinkUp
PHP | 305 lines | 178 code | 27 blank | 100 comment | 2 complexity | 0146984a20218b59b4ac3000fac9fa22 MD5 | raw file
  1. <?php
  2. /**
  3. *
  4. * ThinkUp/tests/TestOfDAOFactory.php
  5. *
  6. * Copyright (c) 2009-2011 Gina Trapani, Mark Wilkie, Christoffer Viken
  7. *
  8. * LICENSE:
  9. *
  10. * This file is part of ThinkUp (http://thinkupapp.com).
  11. *
  12. * ThinkUp is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
  13. * License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any
  14. * later version.
  15. *
  16. * ThinkUp is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
  17. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  18. * details.
  19. *
  20. * You should have received a copy of the GNU General Public License along with ThinkUp. If not, see
  21. * <http://www.gnu.org/licenses/>.
  22. *
  23. *
  24. * Test of DAOFactory
  25. *
  26. * @license http://www.gnu.org/licenses/gpl.html
  27. * @copyright 2009-2011 Gina Trapani, Mark Wilkie, Christoffer Viken
  28. * @author Mark Wilkie
  29. * @author Gina Trapani <ginatrapani[at]gmail[dot]com>
  30. *
  31. */
  32. require_once dirname(__FILE__).'/init.tests.php';
  33. require_once THINKUP_ROOT_PATH.'webapp/_lib/extlib/simpletest/autorun.php';
  34. require_once THINKUP_ROOT_PATH.'webapp/config.inc.php';
  35. require_once THINKUP_ROOT_PATH.'webapp/plugins/twitter/model/class.TwitterInstanceMySQLDAO.php';
  36. class TestOfDAOFactory extends ThinkUpUnitTestCase {
  37. public function __construct() {
  38. $this->UnitTestCase('DAOFactory test');
  39. }
  40. public function setUp() {
  41. parent::setUp();
  42. $this->builders = self::buildData();
  43. }
  44. protected function buildData() {
  45. $builders = array();
  46. // test table for our test dao
  47. $test_table_sql = 'CREATE TABLE tu_test_table(' .
  48. 'id int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,' .
  49. 'test_name varchar(20),' .
  50. 'test_id int(11),' .
  51. 'unique key test_id_idx (test_id)' .
  52. ')';
  53. $this->testdb_helper->runSQL($test_table_sql);
  54. //some test data as well
  55. for($i = 1; $i <= 20; $i++) {
  56. $builders[] = FixtureBuilder::build('test_table', array('test_name'=>'name'.$i, 'test_id'=>$i));
  57. }
  58. return $builders;
  59. }
  60. public function tearDown() {
  61. $this->builders = null;
  62. parent::tearDown();
  63. //make sure our db_type is set to the default...
  64. Config::getInstance()->setValue('db_type', 'mysql');
  65. }
  66. /*
  67. * test fetching the proper db_type
  68. */
  69. public function testDAODBType() {
  70. Config::getInstance()->setValue('db_type', null);
  71. $type = DAOFactory::getDBType();
  72. $this->assertEqual($type, 'mysql', 'should default to mysql');
  73. Config::getInstance()->setValue('db_type', 'some_sql_server');
  74. $type = DAOFactory::getDBType();
  75. $this->assertEqual($type, 'some_sql_server', 'is set to some_sql_server');
  76. }
  77. /*
  78. * test init DAOs, bad params and all...
  79. */
  80. public function testGetTestDAO() {
  81. // no map for this DAO
  82. try {
  83. DAOFactory::getDAO('NoSuchDAO');
  84. $this->fail('should throw an exception');
  85. } catch(Exception $e) {
  86. $this->assertPattern('/No DAO mapping defined for: NoSuchDAO/', $e->getMessage(), 'no dao mapping');
  87. }
  88. // invalid db type for this dao
  89. Config::getInstance()->setValue('db_type', 'nodb');
  90. try {
  91. DAOFactory::getDAO('TestDAO');
  92. $this->fail('should throw an exception');
  93. } catch(Exception $e) {
  94. $this->assertPattern("/No db mapping defined for 'TestDAO'/", $e->getMessage(), 'no dao db_type mapping');
  95. }
  96. // valid mysql test dao
  97. Config::getInstance()->setValue('db_type', 'mysql');
  98. $test_dao = DAOFactory::getDAO('TestDAO');
  99. $this->assertIsA($test_dao, 'TestMysqlDAO', 'we are a mysql dao');
  100. $data_obj = $test_dao->selectRecord(1);
  101. $this->assertNotNull($data_obj);
  102. $this->assertEqual($data_obj->test_name, 'name1');
  103. $this->assertEqual($data_obj->test_id, 1);
  104. // valid fuax test dao
  105. Config::getInstance()->setValue('db_type', 'faux');
  106. $test_dao = DAOFactory::getDAO('TestDAO');
  107. $this->assertIsA($test_dao, 'TestFauxDAO', 'we are a mysql dao');
  108. $data_obj = $test_dao->selectRecord(1);
  109. $this->assertNotNull($data_obj);
  110. $this->assertEqual($data_obj->test_name, 'Mojo Jojo');
  111. $this->assertEqual($data_obj->test_id, 2001);
  112. }
  113. /**
  114. * Test get InstanceDAO
  115. */
  116. public function testGetInstanceDAO(){
  117. $dao = DAOFactory::getDAO('InstanceDAO');
  118. $this->assertTrue(isset($dao));
  119. $this->assertIsA($dao, 'InstanceMySQLDAO');
  120. }
  121. /**
  122. * Test get FollowDAO
  123. */
  124. public function testGetFollowDAO(){
  125. $dao = DAOFactory::getDAO('FollowDAO');
  126. $this->assertTrue(isset($dao));
  127. $this->assertIsA($dao, 'FollowMySQLDAO');
  128. }
  129. /**
  130. * Test get PostErrorDAO
  131. */
  132. public function testGetPostErrorDAO(){
  133. $dao = DAOFactory::getDAO('PostErrorDAO');
  134. $this->assertTrue(isset($dao));
  135. $this->assertIsA($dao, 'PostErrorMySQLDAO');
  136. }
  137. /**
  138. * Test get PostDAO
  139. */
  140. public function testGetPostDAO(){
  141. $dao = DAOFactory::getDAO('PostDAO');
  142. $this->assertTrue(isset($dao));
  143. $this->assertIsA($dao, 'PostMySQLDAO');
  144. }
  145. /**
  146. * Test get UserDAO
  147. */
  148. public function testGetUserDAO(){
  149. $dao = DAOFactory::getDAO('UserDAO');
  150. $this->assertTrue(isset($dao));
  151. $this->assertIsA($dao, 'UserMySQLDAO');
  152. }
  153. /**
  154. * Test get UserErrorDAO
  155. */
  156. public function testGetUserErrorDAO(){
  157. $dao = DAOFactory::getDAO('UserErrorDAO');
  158. $this->assertTrue(isset($dao));
  159. $this->assertIsA($dao, 'UserErrorMySQLDAO');
  160. }
  161. /**
  162. * Test get OwnerDAO
  163. */
  164. public function testGetOwnerDAO(){
  165. $dao = DAOFactory::getDAO('OwnerDAO');
  166. $this->assertTrue(isset($dao));
  167. $this->assertIsA($dao, 'OwnerMySQLDAO');
  168. }
  169. /**
  170. * Test get OwnerDAO without a config file, override with array of config values
  171. */
  172. public function testGetOwnerDAONoConfigFile(){
  173. $this->removeConfigFile();
  174. Config::destroyInstance();
  175. $cfg_values = array("table_prefix"=>"tu_", "db_host"=>"localhost");
  176. $config = Config::getInstance($cfg_values);
  177. $dao = DAOFactory::getDAO('OwnerDAO', $cfg_values);
  178. $this->assertTrue(isset($dao));
  179. $this->assertIsA($dao, 'OwnerMySQLDAO');
  180. $this->restoreConfigFile();
  181. }
  182. /**
  183. * Test get LinkDAO
  184. */
  185. public function testGetLinkDAO(){
  186. $dao = DAOFactory::getDAO('LinkDAO');
  187. $this->assertTrue(isset($dao));
  188. $this->assertIsA($dao, 'LinkMySQLDAO');
  189. }
  190. /**
  191. * Test get OwnerInstanceDAO
  192. */
  193. public function testGetOwnerInstanceDAO() {
  194. $owner_instance_dao = DAOFactory::getDAO('OwnerInstanceDAO');
  195. $this->assertNotNull($owner_instance_dao);
  196. $this->assertIsA($owner_instance_dao, 'OwnerInstanceMySQLDAO');
  197. }
  198. /**
  199. * Test get PluginDAO
  200. */
  201. public function testGetPluginDAO() {
  202. $plugin_dao = DAOFactory::getDAO('PluginDAO');
  203. $this->assertNotNull($plugin_dao);
  204. $this->assertIsA($plugin_dao, 'PluginMySQLDAO');
  205. }
  206. /**
  207. * Test get PluginOptionDAO
  208. */
  209. public function testGetPluginOptionDAO() {
  210. $plugin_dao = DAOFactory::getDAO('PluginOptionDAO');
  211. $this->assertNotNull($plugin_dao);
  212. $this->assertIsA($plugin_dao, 'PluginOptionMySQLDAO');
  213. }
  214. /**
  215. * Test get FollowerCountDAO
  216. */
  217. public function testGetFollowerCountDAO() {
  218. $plugin_dao = DAOFactory::getDAO('FollowerCountDAO');
  219. $this->assertNotNull($plugin_dao);
  220. $this->assertIsA($plugin_dao, 'FollowerCountMySQLDAO');
  221. }
  222. /**
  223. * Test get MutexDAO
  224. */
  225. public function testGetMutexDAO() {
  226. $mutex_dao = DAOFactory::getDAO('MutexDAO');
  227. $this->assertNotNull($mutex_dao);
  228. $this->assertIsA($mutex_dao, 'MutexMySQLDAO');
  229. }
  230. /**
  231. * Test get OptionDAO
  232. */
  233. public function testGetOptionDAO() {
  234. $dao = DAOFactory::getDAO('OptionDAO');
  235. $this->assertNotNull($dao);
  236. $this->assertIsA($dao, 'OptionMySQLDAO');
  237. }
  238. /**
  239. * Test get BackupDAO
  240. */
  241. public function testGetBackupDAO() {
  242. $dao = DAOFactory::getDAO('BackupDAO');
  243. $this->assertNotNull($dao);
  244. $this->assertIsA($dao, 'BackupMySQLDAO');
  245. }
  246. /**
  247. * Test get FavoritePostDAO
  248. */
  249. public function testGetFavoritePostDAO() {
  250. $dao = DAOFactory::getDAO('FavoritePostDAO');
  251. $this->assertNotNull($dao);
  252. $this->assertIsA($dao, 'FavoritePostMySQLDAO');
  253. }
  254. /**
  255. * Test get TwitterInstanceDAO
  256. */
  257. public function testGetTwitterInstanceDAO() {
  258. $dao = DAOFactory::getDAO('TwitterInstanceDAO');
  259. $this->assertNotNull($dao);
  260. $this->assertIsA($dao, 'TwitterInstanceMySQLDAO');
  261. }
  262. /**
  263. * Test get InstallerDAO without a config file, override with array of config values
  264. */
  265. public function testGetInstallerDAONoConfigFile(){
  266. $this->removeConfigFile();
  267. Config::destroyInstance();
  268. $cfg_values = array("table_prefix"=>"tu_", "db_host"=>"localhost");
  269. $config = Config::getInstance($cfg_values);
  270. $dao = DAOFactory::getDAO('InstallerDAO', $cfg_values);
  271. $this->assertTrue(isset($dao));
  272. $this->assertIsA($dao, 'InstallerMySQLDAO');
  273. $result = $dao->getTables();
  274. $this->assertEqual(sizeof($result), 16);
  275. $this->assertEqual($result[0], $cfg_values["table_prefix"].'encoded_locations');
  276. $this->restoreConfigFile();
  277. }
  278. }