PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/olc_baker/plugins/migrations/Test/Case/libs/model/CakeMigrationTest.php

https://github.com/kiang/olc_baker
PHP | 450 lines | 277 code | 51 blank | 122 comment | 0 complexity | 953f6aa361b08caf5f0e999ffd916659 MD5 | raw file
  1. <?php
  2. App::import('Model', 'Migrations.CakeMigration', false);
  3. /**
  4. * TestCakeMigration
  5. *
  6. * @package migrations
  7. * @subpackage migrations.tests.cases.libs
  8. */
  9. class TestCakeMigration extends CakeMigration
  10. {
  11. /**
  12. * Connection used
  13. *
  14. * @var string
  15. * @access public
  16. */
  17. public $connection = 'test_suite';
  18. }
  19. /**
  20. * TestCallbackCakeMigration
  21. *
  22. * @package migrations
  23. * @subpackage migrations.tests.cases.libs
  24. */
  25. class TestCallbackCakeMigration
  26. {
  27. /**
  28. * calls property
  29. *
  30. * @var array
  31. * @access public
  32. */
  33. public $calls = array();
  34. /**
  35. * beforeMigration method
  36. *
  37. * @access public
  38. * @return void
  39. */
  40. public function beforeMigration(&$Migration, $type)
  41. {
  42. $this->calls[$Migration->direction]['beforeMigration'] = $type;
  43. }
  44. /**
  45. * afterMigration method
  46. *
  47. * @access public
  48. * @return void
  49. */
  50. public function afterMigration(&$Migration, $type)
  51. {
  52. $this->calls[$Migration->direction]['afterMigration'] = $type;
  53. }
  54. /**
  55. * beforeAction method
  56. *
  57. * @access public
  58. * @return void
  59. */
  60. public function beforeAction(&$Migration, $type, $data)
  61. {
  62. $this->calls[$Migration->direction]['beforeAction'][] = array('type' => $type, 'data' => $data);
  63. }
  64. /**
  65. * afterAction method
  66. *
  67. * @access public
  68. * @return void
  69. */
  70. public function afterAction(&$Migration, $type, $data)
  71. {
  72. $this->calls[$Migration->direction]['afterAction'][] = array('type' => $type, 'data' => $data);
  73. }
  74. }
  75. /**
  76. * CakeMigrationTest
  77. *
  78. * @package migration
  79. * @subpackage migration.tests.cases.libs
  80. */
  81. class CakeMigrationTest extends CakeTestCase
  82. {
  83. /**
  84. * fixtures property
  85. *
  86. * @var array
  87. * @access public
  88. */
  89. public $fixtures = array(
  90. 'core.user', 'core.post'
  91. );
  92. /**
  93. * autoFixtures property
  94. *
  95. * @var array
  96. * @access public
  97. */
  98. public $autoFixtures = false;
  99. /**
  100. * tables property
  101. *
  102. * @var array
  103. * @access public
  104. */
  105. public $tables = array(
  106. 'users' => array(
  107. 'id' => array('type' => 'integer', 'key' => 'primary'),
  108. 'user' => array('type' => 'string', 'null' => false),
  109. 'password' => array('type' => 'string', 'null' => false),
  110. 'created' => 'datetime',
  111. 'updated' => 'datetime'
  112. ),
  113. 'posts' => array(
  114. 'id' => array('type' => 'integer', 'key' => 'primary'),
  115. 'author_id' => array('type' => 'integer', 'null' => false),
  116. 'title' => array('type' => 'string', 'null' => false),
  117. 'body' => 'text',
  118. 'published' => array('type' => 'string', 'length' => 1, 'default' => 'N'),
  119. 'created' => 'datetime',
  120. 'updated' => 'datetime'
  121. )
  122. );
  123. /**
  124. * testCreateTable method
  125. *
  126. * @access public
  127. * @return void
  128. */
  129. public function testCreateDropTable()
  130. {
  131. $migration = new TestCakeMigration(array(
  132. 'up' => array('create_table' => array('migration_posts' => $this->tables['posts'], 'migration_users' => $this->tables['users'])),
  133. 'down' => array('drop_table' => array('migration_posts', 'migration_users'))
  134. ));
  135. $sources = $this->db->listSources();
  136. $this->assertFalse(in_array($this->db->fullTableName('migration_user', false), $sources));
  137. $this->assertFalse(in_array($this->db->fullTableName('migration_posts', false), $sources));
  138. $this->assertTrue($migration->run('up'));
  139. $sources = $this->db->listSources();
  140. $this->assertTrue(in_array($this->db->fullTableName('migration_users', false), $sources));
  141. $this->assertTrue(in_array($this->db->fullTableName('migration_posts', false), $sources));
  142. $this->assertTrue($migration->run('down'));
  143. $sources = $this->db->listSources();
  144. $this->assertFalse(in_array($this->db->fullTableName('migration_users', false), $sources));
  145. $this->assertFalse(in_array($this->db->fullTableName('migration_posts', false), $sources));
  146. }
  147. /**
  148. * testRenameTable method
  149. *
  150. * @access public
  151. * @return void
  152. */
  153. public function testRenameTable()
  154. {
  155. $this->loadFixtures('User', 'Post');
  156. $migration = new TestCakeMigration(array(
  157. 'up' => array('rename_table' => array('posts' => 'renamed_posts')),
  158. 'down' => array('rename_table' => array('renamed_posts' => 'posts'))
  159. ));
  160. $sources = $this->db->listSources();
  161. $this->assertTrue(in_array($this->db->fullTableName('posts', false), $sources));
  162. $this->assertFalse(in_array($this->db->fullTableName('renamed_posts', false), $sources));
  163. $this->assertTrue($migration->run('up'));
  164. $sources = $this->db->listSources();
  165. $this->assertFalse(in_array($this->db->fullTableName('posts', false), $sources));
  166. $this->assertTrue(in_array($this->db->fullTableName('renamed_posts', false), $sources));
  167. $this->assertTrue($migration->run('down'));
  168. $sources = $this->db->listSources();
  169. $this->assertTrue(in_array($this->db->fullTableName('posts', false), $sources));
  170. $this->assertFalse(in_array($this->db->fullTableName('renamed_posts', false), $sources));
  171. }
  172. /**
  173. * testCreateDropField method
  174. *
  175. * @access public
  176. * @return void
  177. */
  178. public function testCreateDropField()
  179. {
  180. $this->loadFixtures('User', 'Post');
  181. $model = new Model(array('table' => 'posts', 'ds' => 'test_suite'));
  182. $migration = new TestCakeMigration(array(
  183. 'up' => array(
  184. 'create_field' => array(
  185. 'posts' => array('views' => array('type' => 'integer', 'null' => false))
  186. )
  187. ),
  188. 'down' => array(
  189. 'drop_field' => array('posts' => array('views'))
  190. )
  191. ));
  192. $fields = $this->db->describe($model);
  193. $this->assertFalse(isset($fields['views']));
  194. $this->assertTrue($migration->run('up'));
  195. $fields = $this->db->describe($model);
  196. $this->assertTrue(isset($fields['views']));
  197. $this->assertTrue($migration->run('down'));
  198. $fields = $this->db->describe($model);
  199. $this->assertFalse(isset($fields['views']));
  200. // Indexes
  201. $migration = new TestCakeMigration(array(
  202. 'up' => array(
  203. 'create_field' => array(
  204. 'posts' => array(
  205. 'views' => array('type' => 'integer', 'null' => false),
  206. 'indexes' => array(
  207. 'VIEW_COUNT' => array('column' => 'views', 'unique' => false),
  208. 'UNIQUE_AUTHOR_TITLE' => array('column' => array('author_id', 'title'), 'unique' => true)
  209. )
  210. )
  211. )
  212. ),
  213. 'down' => array(
  214. 'drop_field' => array('posts' => array('views', 'indexes' => array('UNIQUE_AUTHOR_TITLE')))
  215. )
  216. ));
  217. $fields = $this->db->describe($model);
  218. $this->assertFalse(isset($fields['views']));
  219. $this->assertTrue($migration->run('up'));
  220. $fields = $this->db->describe($model);
  221. $this->assertTrue(isset($fields['views']));
  222. $this->assertEqual($fields['views']['key'], 'index');
  223. $this->assertTrue($migration->run('down'));
  224. $fields = $this->db->describe($model);
  225. $this->assertFalse(isset($fields['views']));
  226. }
  227. /**
  228. * testAlterField method
  229. *
  230. * @access public
  231. * @return void
  232. */
  233. public function testAlterField()
  234. {
  235. $this->loadFixtures('User', 'Post');
  236. $model = new Model(array('table' => 'posts', 'ds' => 'test_suite'));
  237. $migration = new TestCakeMigration(array(
  238. 'up' => array(
  239. 'alter_field' => array(
  240. 'posts' => array('published' => array('default' => 'Y'))
  241. )
  242. ),
  243. 'down' => array(
  244. 'alter_field' => array(
  245. 'posts' => array('published' => array('default' => 'N'))
  246. )
  247. )
  248. ));
  249. $fields = $this->db->describe($model);
  250. $this->assertEqual($fields['published']['default'], 'N');
  251. $this->assertTrue($migration->run('up'));
  252. $fields = $this->db->describe($model);
  253. $this->assertEqual($fields['published']['default'], 'Y');
  254. $this->assertTrue($migration->run('down'));
  255. $fields = $this->db->describe($model);
  256. $this->assertEqual($fields['published']['default'], 'N');
  257. // Alter and rename field
  258. $migration = new TestCakeMigration(array(
  259. 'up' => array(
  260. 'alter_field' => array(
  261. 'posts' => array('published' => array('name' => 'renamed_published', 'default' => 'Y'))
  262. )
  263. ),
  264. 'down' => array(
  265. 'alter_field' => array(
  266. 'posts' => array('renamed_published' => array('name' => 'published', 'default' => 'N'))
  267. )
  268. )
  269. ));
  270. $fields = $this->db->describe($model);
  271. $this->assertTrue(isset($fields['published']));
  272. $this->assertFalse(isset($fields['renamed_published']));
  273. $this->assertEqual($fields['published']['default'], 'N');
  274. $this->assertTrue($migration->run('up'));
  275. $fields = $this->db->describe($model);
  276. $this->assertFalse(isset($fields['published']));
  277. $this->assertTrue(isset($fields['renamed_published']));
  278. $this->assertEqual($fields['renamed_published']['default'], 'Y');
  279. $this->assertTrue($migration->run('down'));
  280. $fields = $this->db->describe($model);
  281. $this->assertTrue(isset($fields['published']));
  282. $this->assertFalse(isset($fields['renamed_published']));
  283. $this->assertEqual($fields['published']['default'], 'N');
  284. }
  285. /**
  286. * testRenameField method
  287. *
  288. * @access public
  289. * @return void
  290. */
  291. public function testRenameField()
  292. {
  293. $this->loadFixtures('User', 'Post');
  294. $model = new Model(array('table' => 'posts', 'ds' => 'test_suite'));
  295. $migration = new TestCakeMigration(array(
  296. 'up' => array('rename_field' => array('posts' => array('updated' => 'renamed_updated'))),
  297. 'down' => array('rename_field' => array('posts' => array('renamed_updated' => 'updated'))),
  298. ));
  299. $fields = $this->db->describe($model);
  300. $this->assertTrue(isset($fields['updated']));
  301. $this->assertFalse(isset($fields['renamed_updated']));
  302. $this->assertTrue($migration->run('up'));
  303. $fields = $this->db->describe($model);
  304. $this->assertFalse(isset($fields['updated']));
  305. $this->assertTrue(isset($fields['renamed_updated']));
  306. $this->assertTrue($migration->run('down'));
  307. $fields = $this->db->describe($model);
  308. $this->assertTrue(isset($fields['updated']));
  309. $this->assertFalse(isset($fields['renamed_updated']));
  310. }
  311. /**
  312. * testCallbacks method
  313. *
  314. * @access public
  315. * @return void
  316. */
  317. public function testCallbacks()
  318. {
  319. $this->loadFixtures('User');
  320. $callback = new TestCallbackCakeMigration();
  321. $migration = new TestCakeMigration(array(
  322. 'up' => array(
  323. 'create_table' => array('migration_posts' => $this->tables['posts']),
  324. 'create_field' => array(
  325. 'users' => array(
  326. 'email' => array('type' => 'string', 'null' => false),
  327. 'indexes' => array('UNIQUE_USER' => array('column' => 'user', 'unique' => true))
  328. )
  329. ),
  330. ),
  331. 'down' => array(
  332. 'drop_table' => array('migration_posts'),
  333. 'drop_field' => array('users' => array('email', 'indexes' => array('UNIQUE_USER')))
  334. ),
  335. 'callback' => $callback
  336. ));
  337. $this->assertTrue($migration->run('up'));
  338. $this->assertTrue(isset($callback->calls['up']));
  339. $result = $callback->calls['up'];
  340. $expected = array(
  341. array('type' => 'create_table', 'data' => array('table' => 'migration_posts')),
  342. array('type' => 'add_field', 'data' => array('table' => 'users', 'field' => 'email')),
  343. array('type' => 'add_index', 'data' => array('table' => 'users', 'index' => 'UNIQUE_USER'))
  344. );
  345. $this->assertEqual($result['afterMigration'], 'up');
  346. $this->assertEqual($result['beforeMigration'], 'up');
  347. $this->assertEqual($result['afterAction'], $expected);
  348. $this->assertEqual($result['beforeAction'], $expected);
  349. $this->assertEqual(array_keys($result), array('beforeMigration', 'beforeAction', 'afterAction', 'afterMigration'));
  350. $this->assertTrue($migration->run('down'));
  351. $this->assertTrue(isset($callback->calls['down']));
  352. $result = $callback->calls['down'];
  353. $expected = array(
  354. array('type' => 'drop_table', 'data' => array('table' => 'migration_posts')),
  355. array('type' => 'drop_field', 'data' => array('table' => 'users', 'field' => 'email')),
  356. array('type' => 'drop_index', 'data' => array('table' => 'users', 'index' => 'UNIQUE_USER'))
  357. );
  358. $this->assertEqual($result['afterMigration'], 'down');
  359. $this->assertEqual($result['beforeMigration'], 'down');
  360. $this->assertEqual($result['afterAction'], $expected);
  361. $this->assertEqual($result['beforeAction'], $expected);
  362. $this->assertEqual(array_keys($result), array('beforeMigration', 'beforeAction', 'afterAction', 'afterMigration'));
  363. }
  364. /**
  365. * testGenerateModel method
  366. *
  367. * @access public
  368. * @return void
  369. */
  370. public function testGenerateModel()
  371. {
  372. $migration = new TestCakeMigration();
  373. $return = $migration->generateModel('Post');
  374. $this->assertIsA($return, 'AppModel');
  375. $this->assertEqual($return->name, 'Post');
  376. $this->assertEqual($return->table, 'posts');
  377. $return = $migration->generateModel('Post', 'users');
  378. $this->assertIsA($return, 'AppModel');
  379. $this->assertEqual($return->name, 'Post');
  380. $this->assertEqual($return->table, 'users');
  381. }
  382. /**
  383. * Test run method with invalid syntaxes
  384. *
  385. * @access public
  386. * @return void
  387. */
  388. public function testRunInvalidSyntaxes()
  389. {
  390. $migration = new TestCakeMigration(array(
  391. 'up' => array('do_something' => array('posts' => array('updated' => 'renamed_updated'))),
  392. 'down' => array('undo_something' => array('posts' => array('renamed_updated' => 'updated'))),
  393. ));
  394. $this->expectError('Migration direction (last) is not one of valid directions.');
  395. $this->assertFalse($migration->run('last'));
  396. $this->expectError('Migration action type (do_something) is not one of valid actions type.');
  397. $this->assertTrue($migration->run('up'));
  398. }
  399. }