PageRenderTime 53ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/suzuki/candycane
PHP | 499 lines | 249 code | 71 blank | 179 comment | 0 complexity | 3f80a7c05f3ccf05da548b1a38e3a7c9 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-2012, 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-2012, 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->assertEquals('fixture_tests', $Fixture->table);
  189. $this->assertEquals('id', $Fixture->primaryKey);
  190. $Fixture = new CakeTestFixtureTestFixture();
  191. $Fixture->primaryKey = 'my_random_key';
  192. $Fixture->init();
  193. $this->assertEquals('my_random_key', $Fixture->primaryKey);
  194. }
  195. /**
  196. * test that init() correctly sets the fixture table when the connection
  197. * or model have prefixes defined.
  198. *
  199. * @return void
  200. */
  201. public function testInitDbPrefix() {
  202. $this->skipIf($this->db instanceof Sqlite, 'Cannot open 2 connections to Sqlite');
  203. $db = ConnectionManager::getDataSource('test');
  204. $Source = new CakeTestFixtureTestFixture();
  205. $Source->drop($db);
  206. $Source->create($db);
  207. $Source->insert($db);
  208. $Fixture = new CakeTestFixtureTestFixture();
  209. $expected = array('id', 'name', 'created');
  210. $this->assertEquals($expected, array_keys($Fixture->fields));
  211. $config = $db->config;
  212. $config['prefix'] = 'fixture_test_suite_';
  213. ConnectionManager::create('fixture_test_suite', $config);
  214. $Fixture->fields = $Fixture->records = null;
  215. $Fixture->import = array('table' => 'fixture_tests', 'connection' => 'test', 'records' => true);
  216. $Fixture->init();
  217. $this->assertEquals(count($Fixture->records), count($Source->records));
  218. $Fixture->create(ConnectionManager::getDataSource('fixture_test_suite'));
  219. $Fixture = new CakeTestFixtureImportFixture();
  220. $Fixture->fields = $Fixture->records = $Fixture->table = null;
  221. $Fixture->import = array('model' => 'FixtureImportTestModel', 'connection' => 'test');
  222. $Fixture->init();
  223. $this->assertEquals(array('id', 'name', 'created'), array_keys($Fixture->fields));
  224. $this->assertEquals('fixture_tests', $Fixture->table);
  225. $keys = array_flip(ClassRegistry::keys());
  226. $this->assertFalse(array_key_exists('fixtureimporttestmodel', $keys));
  227. $Fixture->drop(ConnectionManager::getDataSource('fixture_test_suite'));
  228. $Source->drop($db);
  229. }
  230. /**
  231. * test that fixtures don't duplicate the test db prefix.
  232. *
  233. * @return void
  234. */
  235. public function testInitDbPrefixDuplication() {
  236. $this->skipIf($this->db instanceof Sqlite, 'Cannot open 2 connections to Sqlite');
  237. $db = ConnectionManager::getDataSource('test');
  238. $backPrefix = $db->config['prefix'];
  239. $db->config['prefix'] = 'cake_fixture_test_';
  240. ConnectionManager::create('fixture_test_suite', $db->config);
  241. $newDb = ConnectionManager::getDataSource('fixture_test_suite');
  242. $newDb->config['prefix'] = 'cake_fixture_test_';
  243. $Source = new CakeTestFixtureTestFixture();
  244. $Source->create($db);
  245. $Source->insert($db);
  246. $Fixture = new CakeTestFixtureImportFixture();
  247. $Fixture->fields = $Fixture->records = $Fixture->table = null;
  248. $Fixture->import = array('model' => 'FixtureImportTestModel', 'connection' => 'test');
  249. $Fixture->init();
  250. $this->assertEquals(array('id', 'name', 'created'), array_keys($Fixture->fields));
  251. $this->assertEquals('fixture_tests', $Fixture->table);
  252. $Source->drop($db);
  253. $db->config['prefix'] = $backPrefix;
  254. }
  255. /**
  256. * test init with a model that has a tablePrefix declared.
  257. *
  258. * @return void
  259. */
  260. public function testInitModelTablePrefix() {
  261. $this->skipIf($this->db instanceof Sqlite, 'Cannot open 2 connections to Sqlite');
  262. $this->skipIf(!empty($this->db->config['prefix']), 'Cannot run this test, you have a database connection prefix.');
  263. $Source = new CakeTestFixtureTestFixture();
  264. $Source->create($this->db);
  265. $Source->insert($this->db);
  266. $Fixture = new CakeTestFixtureTestFixture();
  267. unset($Fixture->table);
  268. $Fixture->fields = $Fixture->records = null;
  269. $Fixture->import = array('model' => 'FixturePrefixTest', 'connection' => 'test', 'records' => false);
  270. $Fixture->init();
  271. $this->assertEquals('fixture_tests', $Fixture->table);
  272. $keys = array_flip(ClassRegistry::keys());
  273. $this->assertFalse(array_key_exists('fixtureimporttestmodel', $keys));
  274. $Source->drop($this->db);
  275. }
  276. /**
  277. * testImport
  278. *
  279. * @return void
  280. */
  281. public function testImport() {
  282. $testSuiteDb = ConnectionManager::getDataSource('test');
  283. $testSuiteConfig = $testSuiteDb->config;
  284. ConnectionManager::create('new_test_suite', array_merge($testSuiteConfig, array('prefix' => 'new_' . $testSuiteConfig['prefix'])));
  285. $newTestSuiteDb = ConnectionManager::getDataSource('new_test_suite');
  286. $Source = new CakeTestFixtureTestFixture();
  287. $Source->create($newTestSuiteDb);
  288. $Source->insert($newTestSuiteDb);
  289. $Fixture = new CakeTestFixtureDefaultImportFixture();
  290. $Fixture->fields = $Fixture->records = null;
  291. $Fixture->import = array('model' => 'FixtureImportTestModel', 'connection' => 'new_test_suite');
  292. $Fixture->init();
  293. $this->assertEquals(array('id', 'name', 'created'), array_keys($Fixture->fields));
  294. $keys = array_flip(ClassRegistry::keys());
  295. $this->assertFalse(array_key_exists('fixtureimporttestmodel', $keys));
  296. $Source->drop($newTestSuiteDb);
  297. }
  298. /**
  299. * test that importing with records works. Make sure to try with postgres as its
  300. * handling of aliases is a workaround at best.
  301. *
  302. * @return void
  303. */
  304. public function testImportWithRecords() {
  305. $testSuiteDb = ConnectionManager::getDataSource('test');
  306. $testSuiteConfig = $testSuiteDb->config;
  307. ConnectionManager::create('new_test_suite', array_merge($testSuiteConfig, array('prefix' => 'new_' . $testSuiteConfig['prefix'])));
  308. $newTestSuiteDb = ConnectionManager::getDataSource('new_test_suite');
  309. $Source = new CakeTestFixtureTestFixture();
  310. $Source->create($newTestSuiteDb);
  311. $Source->insert($newTestSuiteDb);
  312. $Fixture = new CakeTestFixtureDefaultImportFixture();
  313. $Fixture->fields = $Fixture->records = null;
  314. $Fixture->import = array(
  315. 'model' => 'FixtureImportTestModel', 'connection' => 'new_test_suite', 'records' => true
  316. );
  317. $Fixture->init();
  318. $this->assertEquals(array('id', 'name', 'created'), array_keys($Fixture->fields));
  319. $this->assertFalse(empty($Fixture->records[0]), 'No records loaded on importing fixture.');
  320. $this->assertTrue(isset($Fixture->records[0]['name']), 'No name loaded for first record');
  321. $Source->drop($newTestSuiteDb);
  322. }
  323. /**
  324. * test create method
  325. *
  326. * @return void
  327. */
  328. public function testCreate() {
  329. $Fixture = new CakeTestFixtureTestFixture();
  330. $this->criticDb->expects($this->atLeastOnce())->method('execute');
  331. $this->criticDb->expects($this->atLeastOnce())->method('createSchema');
  332. $return = $Fixture->create($this->criticDb);
  333. $this->assertTrue($this->criticDb->fullDebug);
  334. $this->assertTrue($return);
  335. unset($Fixture->fields);
  336. $return = $Fixture->create($this->criticDb);
  337. $this->assertFalse($return);
  338. }
  339. /**
  340. * test the insert method
  341. *
  342. * @return void
  343. */
  344. public function testInsert() {
  345. $Fixture = new CakeTestFixtureTestFixture();
  346. $this->criticDb->expects($this->atLeastOnce())
  347. ->method('insertMulti')
  348. ->will($this->returnCallback(array($this, 'insertCallback')));
  349. $return = $Fixture->insert($this->criticDb);
  350. $this->assertTrue(!empty($this->insertMulti));
  351. $this->assertTrue($this->criticDb->fullDebug);
  352. $this->assertTrue($return);
  353. $this->assertEquals('fixture_tests', $this->insertMulti['table']);
  354. $this->assertEquals(array('name', 'created'), $this->insertMulti['fields']);
  355. $expected = array(
  356. array('Gandalf', '2009-04-28 19:20:00'),
  357. array('Captain Picard', '2009-04-28 19:20:00'),
  358. array('Chewbacca', '2009-04-28 19:20:00')
  359. );
  360. $this->assertEquals($expected, $this->insertMulti['values']);
  361. }
  362. /**
  363. * Helper function to be used as callback and store the parameters of an insertMulti call
  364. *
  365. * @param string $table
  366. * @param string $fields
  367. * @param string $values
  368. * @return boolean true
  369. */
  370. public function insertCallback($table, $fields, $values) {
  371. $this->insertMulti['table'] = $table;
  372. $this->insertMulti['fields'] = $fields;
  373. $this->insertMulti['values'] = $values;
  374. return true;
  375. }
  376. /**
  377. * test the insert method
  378. *
  379. * @return void
  380. */
  381. public function testInsertStrings() {
  382. $Fixture = new StringsTestFixture();
  383. $this->criticDb->expects($this->atLeastOnce())
  384. ->method('insertMulti')
  385. ->will($this->returnCallback(array($this, 'insertCallback')));
  386. $return = $Fixture->insert($this->criticDb);
  387. $this->assertTrue($this->criticDb->fullDebug);
  388. $this->assertTrue($return);
  389. $this->assertEquals('strings', $this->insertMulti['table']);
  390. $this->assertEquals(array('email', 'name', 'age'), $this->insertMulti['fields']);
  391. $expected = array(
  392. array('Mark Doe', 'mark.doe@email.com', null),
  393. array('John Doe', 'john.doe@email.com', 20),
  394. array('Jane Doe', 'jane.doe@email.com', 30),
  395. );
  396. $this->assertEquals($expected, $this->insertMulti['values']);
  397. }
  398. /**
  399. * Test the drop method
  400. *
  401. * @return void
  402. */
  403. public function testDrop() {
  404. $Fixture = new CakeTestFixtureTestFixture();
  405. $this->criticDb->expects($this->at(1))->method('execute')->will($this->returnValue(true));
  406. $this->criticDb->expects($this->at(3))->method('execute')->will($this->returnValue(false));
  407. $this->criticDb->expects($this->exactly(2))->method('dropSchema');
  408. $return = $Fixture->drop($this->criticDb);
  409. $this->assertTrue($this->criticDb->fullDebug);
  410. $this->assertTrue($return);
  411. $return = $Fixture->drop($this->criticDb);
  412. $this->assertTrue($return);
  413. unset($Fixture->fields);
  414. $return = $Fixture->drop($this->criticDb);
  415. $this->assertFalse($return);
  416. }
  417. /**
  418. * Test the truncate method.
  419. *
  420. * @return void
  421. */
  422. public function testTruncate() {
  423. $Fixture = new CakeTestFixtureTestFixture();
  424. $this->criticDb->expects($this->atLeastOnce())->method('truncate');
  425. $Fixture->truncate($this->criticDb);
  426. $this->assertTrue($this->criticDb->fullDebug);
  427. }
  428. }