PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/cake/tests/cases/libs/cake_test_fixture.test.php

https://github.com/hardsshah/bookmarks
PHP | 289 lines | 132 code | 19 blank | 138 comment | 0 complexity | d5f756a7e7a8193e625326c971ec85e0 MD5 | raw file
  1. <?php
  2. /* SVN FILE: $Id$ */
  3. /**
  4. * CakeTestFixture file
  5. *
  6. * Long description for file
  7. *
  8. * PHP versions 4 and 5
  9. *
  10. * CakePHP(tm) Tests <https://trac.cakephp.org/wiki/Developement/TestSuite>
  11. * Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
  12. *
  13. * Licensed under The Open Group Test Suite License
  14. * Redistributions of files must retain the above copyright notice.
  15. *
  16. * @filesource
  17. * @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
  18. * @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests
  19. * @package cake
  20. * @subpackage cake.cake.tests.libs
  21. * @since CakePHP(tm) v 1.2.0.4667
  22. * @version $Revision$
  23. * @modifiedby $LastChangedBy$
  24. * @lastmodified $Date$
  25. * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
  26. */
  27. App::import('Core', 'DboSource');
  28. /**
  29. * CakeTestFixtureTestFixture class
  30. *
  31. * @package cake
  32. * @subpackage cake.cake.tests.cases.libs
  33. */
  34. class CakeTestFixtureTestFixture extends CakeTestFixture {
  35. /**
  36. * name Property
  37. *
  38. * @var string
  39. */
  40. var $name = 'FixtureTest';
  41. /**
  42. * table property
  43. *
  44. * @var string
  45. */
  46. var $table = 'fixture_tests';
  47. /**
  48. * Fields array
  49. *
  50. * @var array
  51. */
  52. var $fields = array(
  53. 'id' => array('type' => 'integer', 'key' => 'primary'),
  54. 'name' => array('type' => 'text', 'length' => '255'),
  55. 'created' => array('type' => 'datetime'),
  56. );
  57. /**
  58. * Records property
  59. *
  60. * @var array
  61. */
  62. var $records = array(
  63. array('name' => 'Gandalf'),
  64. array('name' => 'Captain Picard'),
  65. array('name' => 'Chewbacca')
  66. );
  67. }
  68. /**
  69. * CakeTestFixtureImportFixture class
  70. *
  71. * @package cake
  72. * @subpackage cake.cake.tests.cases.libs
  73. */
  74. class CakeTestFixtureImportFixture extends CakeTestFixture {
  75. /**
  76. * Name property
  77. *
  78. * @var string
  79. */
  80. var $name = 'ImportFixture';
  81. /**
  82. * Import property
  83. *
  84. * @var mixed
  85. */
  86. var $import = array('table' => 'fixture_tests', 'connection' => 'test_suite');
  87. }
  88. /**
  89. * CakeTestFixtureDefaultImportFixture class
  90. *
  91. * @package cake
  92. * @subpackage cake.cake.tests.cases.libs
  93. */
  94. class CakeTestFixtureDefaultImportFixture extends CakeTestFixture {
  95. /**
  96. * Name property
  97. *
  98. * @var string
  99. */
  100. var $name = 'ImportFixture';
  101. }
  102. /**
  103. * FixtureImportTestModel class
  104. *
  105. * @package default
  106. * @subpackage cake.cake.tests.cases.libs.
  107. **/
  108. class FixtureImportTestModel extends Model {
  109. var $name = 'FixtureImport';
  110. var $useTable = 'fixture_tests';
  111. var $useDbConfig = 'test_suite';
  112. }
  113. Mock::generate('DboSource', 'FixtureMockDboSource');
  114. /**
  115. * Test case for CakeTestFixture
  116. *
  117. * @package cake
  118. * @subpackage cake.cake.tests.cases.libs
  119. */
  120. class CakeTestFixtureTest extends CakeTestCase {
  121. /**
  122. * setUp method
  123. *
  124. * @access public
  125. * @return void
  126. */
  127. function setUp() {
  128. $this->criticDb =& new FixtureMockDboSource();
  129. $this->criticDb->fullDebug = true;
  130. }
  131. /**
  132. * tearDown
  133. *
  134. * @access public
  135. * @return void
  136. */
  137. function tearDown() {
  138. unset($this->criticDb);
  139. }
  140. /**
  141. * testInit
  142. *
  143. * @access public
  144. * @return void
  145. */
  146. function testInit() {
  147. $Fixture =& new CakeTestFixtureTestFixture();
  148. unset($Fixture->table);
  149. $Fixture->init();
  150. $this->assertEqual($Fixture->table, 'fixture_tests');
  151. $this->assertEqual($Fixture->primaryKey, 'id');
  152. $Fixture =& new CakeTestFixtureTestFixture();
  153. $Fixture->primaryKey = 'my_random_key';
  154. $Fixture->init();
  155. $this->assertEqual($Fixture->primaryKey, 'my_random_key');
  156. $this->_initDb();
  157. $Source =& new CakeTestFixtureTestFixture();
  158. $Source->create($this->db);
  159. $Source->insert($this->db);
  160. $Fixture =& new CakeTestFixtureImportFixture();
  161. $expected = array('id', 'name', 'created');
  162. $this->assertEqual(array_keys($Fixture->fields), $expected);
  163. $db =& ConnectionManager::getDataSource('test_suite');
  164. $config = $db->config;
  165. $config['prefix'] = 'fixture_test_suite_';
  166. ConnectionManager::create('fixture_test_suite', $config);
  167. $Fixture->fields = $Fixture->records = null;
  168. $Fixture->import = array('table' => 'fixture_tests', 'connection' => 'test_suite', 'records' => true);
  169. $Fixture->init();
  170. $this->assertEqual(count($Fixture->records), count($Source->records));
  171. $Fixture =& new CakeTestFixtureImportFixture();
  172. $Fixture->fields = $Fixture->records = null;
  173. $Fixture->import = array('model' => 'FixtureImportTestModel', 'connection' => 'test_suite');
  174. $Fixture->init();
  175. $this->assertEqual(array_keys($Fixture->fields), array('id', 'name', 'created'));
  176. $keys = array_flip(ClassRegistry::keys());
  177. $this->assertFalse(array_key_exists('fixtureimporttestmodel', $keys));
  178. $Source->drop($this->db);
  179. }
  180. /**
  181. * testImport
  182. *
  183. * @access public
  184. * @return void
  185. */
  186. function testImport() {
  187. $this->_initDb();
  188. $defaultDb =& ConnectionManager::getDataSource('default');
  189. $testSuiteDb =& ConnectionManager::getDataSource('test_suite');
  190. $defaultConfig = $defaultDb->config;
  191. $testSuiteConfig = $testSuiteDb->config;
  192. ConnectionManager::create('new_test_suite', array_merge($testSuiteConfig, array('prefix' => 'new_' . $testSuiteConfig['prefix'])));
  193. $newTestSuiteDb =& ConnectionManager::getDataSource('new_test_suite');
  194. $Source =& new CakeTestFixtureTestFixture();
  195. $Source->create($newTestSuiteDb);
  196. $Source->insert($newTestSuiteDb);
  197. $defaultDb->config = $newTestSuiteDb->config;
  198. $Fixture =& new CakeTestFixtureDefaultImportFixture();
  199. $Fixture->fields = $Fixture->records = null;
  200. $Fixture->import = array('model' => 'FixtureImportTestModel', 'connection' => 'new_test_suite');
  201. $Fixture->init();
  202. $this->assertEqual(array_keys($Fixture->fields), array('id', 'name', 'created'));
  203. $defaultDb->config = $defaultConfig;
  204. $keys = array_flip(ClassRegistry::keys());
  205. $this->assertFalse(array_key_exists('fixtureimporttestmodel', $keys));
  206. $Source->drop($newTestSuiteDb);
  207. }
  208. /**
  209. * test create method
  210. *
  211. * @access public
  212. * @return void
  213. */
  214. function testCreate() {
  215. $Fixture =& new CakeTestFixtureTestFixture();
  216. $this->criticDb->expectAtLeastOnce('execute');
  217. $this->criticDb->expectAtLeastOnce('createSchema');
  218. $return = $Fixture->create($this->criticDb);
  219. $this->assertTrue($this->criticDb->fullDebug);
  220. $this->assertTrue($return);
  221. unset($Fixture->fields);
  222. $return = $Fixture->create($this->criticDb);
  223. $this->assertFalse($return);
  224. }
  225. /**
  226. * test the insert method
  227. *
  228. * @access public
  229. * @return void
  230. */
  231. function testInsert() {
  232. $Fixture =& new CakeTestFixtureTestFixture();
  233. $this->criticDb->setReturnValue('insertMulti', true);
  234. $this->criticDb->expectAtLeastOnce('insertMulti');
  235. $return = $Fixture->insert($this->criticDb);
  236. $this->assertTrue($this->criticDb->fullDebug);
  237. $this->assertTrue($return);
  238. }
  239. /**
  240. * Test the drop method
  241. *
  242. * @access public
  243. * @return void
  244. */
  245. function testDrop() {
  246. $Fixture =& new CakeTestFixtureTestFixture();
  247. $this->criticDb->setReturnValueAt(0, 'execute', true);
  248. $this->criticDb->expectAtLeastOnce('execute');
  249. $this->criticDb->expectAtLeastOnce('dropSchema');
  250. $return = $Fixture->drop($this->criticDb);
  251. $this->assertTrue($this->criticDb->fullDebug);
  252. $this->assertTrue($return);
  253. $this->criticDb->setReturnValueAt(1, 'execute', false);
  254. $return = $Fixture->drop($this->criticDb);
  255. $this->assertFalse($return);
  256. }
  257. /**
  258. * Test the truncate method.
  259. *
  260. * @access public
  261. * @return void
  262. */
  263. function testTruncate() {
  264. $Fixture =& new CakeTestFixtureTestFixture();
  265. $this->criticDb->expectAtLeastOnce('truncate');
  266. $Fixture->truncate($this->criticDb);
  267. $this->assertTrue($this->criticDb->fullDebug);
  268. }
  269. }
  270. ?>