PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Cake/Test/Case/TestSuite/CakeTestFixtureTest.php

https://bitbucket.org/luobailiang/cake
PHP | 492 lines | 249 code | 65 blank | 178 comment | 0 complexity | f9949aa5990fe8aa33003ed99f6b3a8f MD5 | raw file
  1. <?php
  2. /**
  3. * CakeTestFixture file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
  8. * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice
  12. *
  13. * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
  15. * @package Cake.Test.Case.TestSuite
  16. * @since CakePHP(tm) v 1.2.0.4667
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('DboSource', 'Model/Datasource');
  20. App::uses('Model', 'Model');
  21. App::uses('CakeTestFixture', 'TestSuite/Fixture');
  22. /**
  23. * CakeTestFixtureTestFixture class
  24. *
  25. * @package Cake.Test.Case.TestSuite
  26. */
  27. class CakeTestFixtureTestFixture extends CakeTestFixture {
  28. /**
  29. * Name property
  30. *
  31. * @var string
  32. */
  33. public $name = 'FixtureTest';
  34. /**
  35. * Table property
  36. *
  37. * @var string
  38. */
  39. public $table = 'fixture_tests';
  40. /**
  41. * Fields array
  42. *
  43. * @var array
  44. */
  45. public $fields = array(
  46. 'id' => array('type' => 'integer', 'key' => 'primary'),
  47. 'name' => array('type' => 'string', 'length' => '255'),
  48. 'created' => array('type' => 'datetime')
  49. );
  50. /**
  51. * Records property
  52. *
  53. * @var array
  54. */
  55. public $records = array(
  56. array('name' => 'Gandalf', 'created' => '2009-04-28 19:20:00'),
  57. array('name' => 'Captain Picard', 'created' => '2009-04-28 19:20:00'),
  58. array('name' => 'Chewbacca', 'created' => '2009-04-28 19:20:00')
  59. );
  60. }
  61. /**
  62. * StringFieldsTestFixture class
  63. *
  64. * @package Cake.Test.Case.TestSuite
  65. * @subpackage cake.cake.tests.cases.libs
  66. */
  67. class StringsTestFixture extends CakeTestFixture {
  68. /**
  69. * Name property
  70. *
  71. * @var string
  72. */
  73. public $name = 'Strings';
  74. /**
  75. * Table property
  76. *
  77. * @var string
  78. */
  79. public $table = 'strings';
  80. /**
  81. * Fields array
  82. *
  83. * @var array
  84. */
  85. public $fields = array(
  86. 'id' => array('type' => 'integer', 'key' => 'primary'),
  87. 'name' => array('type' => 'string', 'length' => '255'),
  88. 'email' => array('type' => 'string', 'length' => '255'),
  89. 'age' => array('type' => 'integer', 'default' => 10)
  90. );
  91. /**
  92. * Records property
  93. *
  94. * @var array
  95. */
  96. public $records = array(
  97. array('name' => 'Mark Doe', 'email' => 'mark.doe@email.com'),
  98. array('name' => 'John Doe', 'email' => 'john.doe@email.com', 'age' => 20),
  99. array('email' => 'jane.doe@email.com', 'name' => 'Jane Doe', 'age' => 30)
  100. );
  101. }
  102. /**
  103. * CakeTestFixtureImportFixture class
  104. *
  105. * @package Cake.Test.Case.TestSuite
  106. */
  107. class CakeTestFixtureImportFixture extends CakeTestFixture {
  108. /**
  109. * Name property
  110. *
  111. * @var string
  112. */
  113. public $name = 'ImportFixture';
  114. /**
  115. * Import property
  116. *
  117. * @var mixed
  118. */
  119. public $import = array('table' => 'fixture_tests', 'connection' => 'fixture_test_suite');
  120. }
  121. /**
  122. * CakeTestFixtureDefaultImportFixture class
  123. *
  124. * @package Cake.Test.Case.TestSuite
  125. */
  126. class CakeTestFixtureDefaultImportFixture extends CakeTestFixture {
  127. /**
  128. * Name property
  129. *
  130. * @var string
  131. */
  132. public $name = 'ImportFixture';
  133. }
  134. /**
  135. * FixtureImportTestModel class
  136. *
  137. * @package Cake.Test.Case.TestSuite
  138. * @package Cake.Test.Case.TestSuite
  139. */
  140. class FixtureImportTestModel extends Model {
  141. public $name = 'FixtureImport';
  142. public $useTable = 'fixture_tests';
  143. public $useDbConfig = 'test';
  144. }
  145. class FixturePrefixTest extends Model {
  146. public $name = 'FixturePrefix';
  147. public $useTable = '_tests';
  148. public $tablePrefix = 'fixture';
  149. public $useDbConfig = 'test';
  150. }
  151. /**
  152. * Test case for CakeTestFixture
  153. *
  154. * @package Cake.Test.Case.TestSuite
  155. */
  156. class CakeTestFixtureTest extends CakeTestCase {
  157. /**
  158. * setUp method
  159. *
  160. * @return void
  161. */
  162. public function setUp() {
  163. $methods = array_diff(get_class_methods('DboSource'), array('enabled'));
  164. $methods[] = 'connect';
  165. $this->criticDb = $this->getMock('DboSource', $methods);
  166. $this->criticDb->fullDebug = true;
  167. $this->db = ConnectionManager::getDataSource('test');
  168. $this->_backupConfig = $this->db->config;
  169. }
  170. /**
  171. * tearDown
  172. *
  173. * @return void
  174. */
  175. public function tearDown() {
  176. unset($this->criticDb);
  177. $this->db->config = $this->_backupConfig;
  178. }
  179. /**
  180. * testInit
  181. *
  182. * @return void
  183. */
  184. public function testInit() {
  185. $Fixture = new CakeTestFixtureTestFixture();
  186. unset($Fixture->table);
  187. $Fixture->init();
  188. $this->assertEqual($Fixture->table, 'fixture_tests');
  189. $this->assertEqual($Fixture->primaryKey, 'id');
  190. $Fixture = new CakeTestFixtureTestFixture();
  191. $Fixture->primaryKey = 'my_random_key';
  192. $Fixture->init();
  193. $this->assertEqual($Fixture->primaryKey, 'my_random_key');
  194. }
  195. /**
  196. * test that init() correctly sets the fixture table when the connection or model have prefixes defined.
  197. *
  198. * @return void
  199. */
  200. public function testInitDbPrefix() {
  201. $this->skipIf($this->db instanceof Sqlite, 'Cannot open 2 connections to Sqlite');
  202. $db = ConnectionManager::getDataSource('test');
  203. $Source = new CakeTestFixtureTestFixture();
  204. $Source->drop($db);
  205. $Source->create($db);
  206. $Source->insert($db);
  207. $Fixture = new CakeTestFixtureTestFixture();
  208. $expected = array('id', 'name', 'created');
  209. $this->assertEqual(array_keys($Fixture->fields), $expected);
  210. $config = $db->config;
  211. $config['prefix'] = 'fixture_test_suite_';
  212. ConnectionManager::create('fixture_test_suite', $config);
  213. $Fixture->fields = $Fixture->records = null;
  214. $Fixture->import = array('table' => 'fixture_tests', 'connection' => 'test', 'records' => true);
  215. $Fixture->init();
  216. $this->assertEqual(count($Fixture->records), count($Source->records));
  217. $Fixture->create(ConnectionManager::getDataSource('fixture_test_suite'));
  218. $Fixture = new CakeTestFixtureImportFixture();
  219. $Fixture->fields = $Fixture->records = $Fixture->table = null;
  220. $Fixture->import = array('model' => 'FixtureImportTestModel', 'connection' => 'test');
  221. $Fixture->init();
  222. $this->assertEqual(array_keys($Fixture->fields), array('id', 'name', 'created'));
  223. $this->assertEqual($Fixture->table, 'fixture_tests');
  224. $keys = array_flip(ClassRegistry::keys());
  225. $this->assertFalse(array_key_exists('fixtureimporttestmodel', $keys));
  226. $Fixture->drop(ConnectionManager::getDataSource('fixture_test_suite'));
  227. $Source->drop($db);
  228. }
  229. /**
  230. * test that fixtures don't duplicate the test db prefix.
  231. *
  232. * @return void
  233. */
  234. public function testInitDbPrefixDuplication() {
  235. $this->skipIf($this->db instanceof Sqlite, 'Cannot open 2 connections to Sqlite');
  236. $db = ConnectionManager::getDataSource('test');
  237. $backPrefix = $db->config['prefix'];
  238. $db->config['prefix'] = 'cake_fixture_test_';
  239. ConnectionManager::create('fixture_test_suite', $db->config);
  240. $newDb = ConnectionManager::getDataSource('fixture_test_suite');
  241. $newDb->config['prefix'] = 'cake_fixture_test_';
  242. $Source = new CakeTestFixtureTestFixture();
  243. $Source->create($db);
  244. $Source->insert($db);
  245. $Fixture = new CakeTestFixtureImportFixture();
  246. $Fixture->fields = $Fixture->records = $Fixture->table = null;
  247. $Fixture->import = array('model' => 'FixtureImportTestModel', 'connection' => 'test');
  248. $Fixture->init();
  249. $this->assertEqual(array_keys($Fixture->fields), array('id', 'name', 'created'));
  250. $this->assertEqual($Fixture->table, 'fixture_tests');
  251. $Source->drop($db);
  252. $db->config['prefix'] = $backPrefix;
  253. }
  254. /**
  255. * test init with a model that has a tablePrefix declared.
  256. *
  257. * @return void
  258. */
  259. public function testInitModelTablePrefix() {
  260. $this->skipIf($this->db instanceof Sqlite, 'Cannot open 2 connections to Sqlite');
  261. $this->skipIf(!empty($this->db->config['prefix']), 'Cannot run this test, you have a database connection prefix.');
  262. $Source = new CakeTestFixtureTestFixture();
  263. $Source->create($this->db);
  264. $Source->insert($this->db);
  265. $Fixture = new CakeTestFixtureTestFixture();
  266. unset($Fixture->table);
  267. $Fixture->fields = $Fixture->records = null;
  268. $Fixture->import = array('model' => 'FixturePrefixTest', 'connection' => 'test', 'records' => false);
  269. $Fixture->init();
  270. $this->assertEqual($Fixture->table, 'fixture_tests');
  271. $keys = array_flip(ClassRegistry::keys());
  272. $this->assertFalse(array_key_exists('fixtureimporttestmodel', $keys));
  273. $Source->drop($this->db);
  274. }
  275. /**
  276. * testImport
  277. *
  278. * @return void
  279. */
  280. public function testImport() {
  281. $testSuiteDb = ConnectionManager::getDataSource('test');
  282. $testSuiteConfig = $testSuiteDb->config;
  283. ConnectionManager::create('new_test_suite', array_merge($testSuiteConfig, array('prefix' => 'new_' . $testSuiteConfig['prefix'])));
  284. $newTestSuiteDb = ConnectionManager::getDataSource('new_test_suite');
  285. $Source = new CakeTestFixtureTestFixture();
  286. $Source->create($newTestSuiteDb);
  287. $Source->insert($newTestSuiteDb);
  288. $Fixture = new CakeTestFixtureDefaultImportFixture();
  289. $Fixture->fields = $Fixture->records = null;
  290. $Fixture->import = array('model' => 'FixtureImportTestModel', 'connection' => 'new_test_suite');
  291. $Fixture->init();
  292. $this->assertEqual(array_keys($Fixture->fields), array('id', 'name', 'created'));
  293. $keys = array_flip(ClassRegistry::keys());
  294. $this->assertFalse(array_key_exists('fixtureimporttestmodel', $keys));
  295. $Source->drop($newTestSuiteDb);
  296. }
  297. /**
  298. * test that importing with records works. Make sure to try with postgres as its
  299. * handling of aliases is a workaround at best.
  300. *
  301. * @return void
  302. */
  303. public function testImportWithRecords() {
  304. $testSuiteDb = ConnectionManager::getDataSource('test');
  305. $testSuiteConfig = $testSuiteDb->config;
  306. ConnectionManager::create('new_test_suite', array_merge($testSuiteConfig, array('prefix' => 'new_' . $testSuiteConfig['prefix'])));
  307. $newTestSuiteDb = ConnectionManager::getDataSource('new_test_suite');
  308. $Source = new CakeTestFixtureTestFixture();
  309. $Source->create($newTestSuiteDb);
  310. $Source->insert($newTestSuiteDb);
  311. $Fixture = new CakeTestFixtureDefaultImportFixture();
  312. $Fixture->fields = $Fixture->records = null;
  313. $Fixture->import = array(
  314. 'model' => 'FixtureImportTestModel', 'connection' => 'new_test_suite', 'records' => true
  315. );
  316. $Fixture->init();
  317. $this->assertEqual(array_keys($Fixture->fields), array('id', 'name', 'created'));
  318. $this->assertFalse(empty($Fixture->records[0]), 'No records loaded on importing fixture.');
  319. $this->assertTrue(isset($Fixture->records[0]['name']), 'No name loaded for first record');
  320. $Source->drop($newTestSuiteDb);
  321. }
  322. /**
  323. * test create method
  324. *
  325. * @return void
  326. */
  327. public function testCreate() {
  328. $Fixture = new CakeTestFixtureTestFixture();
  329. $this->criticDb->expects($this->atLeastOnce())->method('execute');
  330. $this->criticDb->expects($this->atLeastOnce())->method('createSchema');
  331. $return = $Fixture->create($this->criticDb);
  332. $this->assertTrue($this->criticDb->fullDebug);
  333. $this->assertTrue($return);
  334. unset($Fixture->fields);
  335. $return = $Fixture->create($this->criticDb);
  336. $this->assertFalse($return);
  337. }
  338. /**
  339. * test the insert method
  340. *
  341. * @return void
  342. */
  343. public function testInsert() {
  344. $Fixture = new CakeTestFixtureTestFixture();
  345. $this->criticDb->expects($this->atLeastOnce())
  346. ->method('insertMulti')
  347. ->will($this->returnCallback(array($this, '_insertCallback')));
  348. $return = $Fixture->insert($this->criticDb);
  349. $this->assertTrue(!empty($this->insertMulti));
  350. $this->assertTrue($this->criticDb->fullDebug);
  351. $this->assertTrue($return);
  352. $this->assertEqual('fixture_tests', $this->insertMulti['table']);
  353. $this->assertEqual(array('name', 'created'), $this->insertMulti['fields']);
  354. $expected = array(
  355. array('Gandalf', '2009-04-28 19:20:00'),
  356. array('Captain Picard', '2009-04-28 19:20:00'),
  357. array('Chewbacca', '2009-04-28 19:20:00')
  358. );
  359. $this->assertEqual($expected, $this->insertMulti['values']);
  360. }
  361. /**
  362. * Helper function to be used as callback and store the parameters of an insertMulti call
  363. *
  364. * @param string $table
  365. * @param string $fields
  366. * @param string $values
  367. * @return boolean true
  368. */
  369. function _insertCallback($table, $fields, $values) {
  370. $this->insertMulti['table'] = $table;
  371. $this->insertMulti['fields'] = $fields;
  372. $this->insertMulti['values'] = $values;
  373. return true;
  374. }
  375. /**
  376. * test the insert method
  377. *
  378. * @return void
  379. */
  380. public function testInsertStrings() {
  381. $Fixture = new StringsTestFixture();
  382. $this->criticDb->expects($this->atLeastOnce())
  383. ->method('insertMulti')
  384. ->will($this->returnCallback(array($this, '_insertCallback')));
  385. $return = $Fixture->insert($this->criticDb);
  386. $this->assertTrue($this->criticDb->fullDebug);
  387. $this->assertTrue($return);
  388. $this->assertEqual('strings', $this->insertMulti['table']);
  389. $this->assertEqual(array('email', 'name', 'age'), $this->insertMulti['fields']);
  390. $expected = array(
  391. array('Mark Doe', 'mark.doe@email.com', null),
  392. array('John Doe', 'john.doe@email.com', 20),
  393. array('Jane Doe', 'jane.doe@email.com', 30),
  394. );
  395. $this->assertEqual($expected, $this->insertMulti['values']);
  396. }
  397. /**
  398. * Test the drop method
  399. *
  400. * @return void
  401. */
  402. public function testDrop() {
  403. $Fixture = new CakeTestFixtureTestFixture();
  404. $this->criticDb->expects($this->at(1))->method('execute')->will($this->returnValue(true));
  405. $this->criticDb->expects($this->at(3))->method('execute')->will($this->returnValue(false));
  406. $this->criticDb->expects($this->exactly(2))->method('dropSchema');
  407. $return = $Fixture->drop($this->criticDb);
  408. $this->assertTrue($this->criticDb->fullDebug);
  409. $this->assertTrue($return);
  410. $return = $Fixture->drop($this->criticDb);
  411. $this->assertTrue($return);
  412. unset($Fixture->fields);
  413. $return = $Fixture->drop($this->criticDb);
  414. $this->assertFalse($return);
  415. }
  416. /**
  417. * Test the truncate method.
  418. *
  419. * @return void
  420. */
  421. public function testTruncate() {
  422. $Fixture = new CakeTestFixtureTestFixture();
  423. $this->criticDb->expects($this->atLeastOnce())->method('truncate');
  424. $Fixture->truncate($this->criticDb);
  425. $this->assertTrue($this->criticDb->fullDebug);
  426. }
  427. }