PageRenderTime 52ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/Test/Case/Console/Command/MigrationShellTest.php

https://bitbucket.org/rglcompanies/migrations
PHP | 871 lines | 638 code | 80 blank | 153 comment | 9 complexity | 5664231b5665eb81f7c29e2c61bbfd22 MD5 | raw file
  1. <?php
  2. App::uses('ShellDispatcher', 'Console');
  3. App::uses('MigrationShell', 'Migrations.Console/Command');
  4. /**
  5. * TestMigrationShell
  6. *
  7. * @package migrations
  8. * @subpackage migrations.tests.cases.shells
  9. */
  10. class TestMigrationShell extends MigrationShell {
  11. /**
  12. * output property
  13. *
  14. * @var string
  15. */
  16. public $output = '';
  17. /**
  18. * out method
  19. *
  20. * @param $string
  21. * @return void
  22. */
  23. function out($string = null) {
  24. $this->output .= $string . "\n";
  25. }
  26. /**
  27. * fromComparison method
  28. *
  29. * @param $migration
  30. * @param $comparison
  31. * @param $oldTables
  32. * @param $currentTables
  33. * @return void
  34. */
  35. function fromComparison($migration, $comparison, $oldTables, $currentTables) {
  36. return $this->_fromComparison($migration, $comparison, $oldTables, $currentTables);
  37. }
  38. /**
  39. * writeMigration method
  40. *
  41. * @param $name
  42. * @param $class
  43. * @param $migration
  44. * @return void
  45. */
  46. function writeMigration($name, $class, $migration) {
  47. return $this->_writeMigration($name, $class, $migration);
  48. }
  49. }
  50. /**
  51. * MigrationShellTest
  52. *
  53. * @package migrations
  54. * @subpackage migrations.tests.cases.shells
  55. */
  56. class MigrationShellTest extends CakeTestCase {
  57. /**
  58. * fixtures property
  59. *
  60. * @var array
  61. */
  62. public $fixtures = array('plugin.migrations.schema_migrations', 'core.article', 'core.post', 'core.user');
  63. /**
  64. * setUp method
  65. *
  66. * @return void
  67. */
  68. public function setUp() {
  69. parent::setUp();
  70. $out = $this->getMock('ConsoleOutput', array(), array(), '', false);
  71. $in = $this->getMock('ConsoleInput', array(), array(), '', false);
  72. $this->Shell = $this->getMock(
  73. 'TestMigrationShell',
  74. array('in', 'hr', 'createFile', 'error', 'err', '_stop', '_showInfo', 'dispatchShell'),
  75. array($out, $out, $in));
  76. $this->Shell->Version = $this->getMock(
  77. 'MigrationVersion',
  78. array('getMapping', 'getVersion', 'run'),
  79. array(array('connection' => 'test')));
  80. $this->Shell->type = 'TestMigrationPlugin';
  81. $this->Shell->path = TMP . 'tests' . DS;
  82. $this->Shell->connection = 'test';
  83. $plugins = $this->plugins = App::path('plugins');
  84. $plugins[] = dirname(dirname(dirname(dirname(__FILE__)))) . DS . 'test_app' . DS . 'Plugin' . DS;
  85. App::build(array('Plugin' => $plugins), true);
  86. App::objects('plugins', null, false);
  87. CakePlugin::load('TestMigrationPlugin');
  88. CakePlugin::load('TestMigrationPlugin2');
  89. CakePlugin::load('TestMigrationPlugin3');
  90. }
  91. /**
  92. * tearDown method
  93. *
  94. * @return void
  95. */
  96. public function tearDown() {
  97. parent::tearDown();
  98. CakePlugin::unload('TestMigrationPlugin');
  99. CakePlugin::unload('TestMigrationPlugin2');
  100. CakePlugin::unload('TestMigrationPlugin3');
  101. App::build(array('Plugin' => $this->plugins), true);
  102. App::objects('plugins', null, false);
  103. unset($this->Dispatcher, $this->Shell, $this->plugins);
  104. foreach (glob(TMP . 'tests' . DS . '*.php') as $f) {
  105. unlink($f);
  106. }
  107. }
  108. /**
  109. * tables property
  110. *
  111. * @var array
  112. */
  113. public $tables = array(
  114. 'users' => array(
  115. 'id' => array('type' => 'integer', 'key' => 'primary'),
  116. 'user' => array('type' => 'string', 'null' => false),
  117. 'password' => array('type' => 'string', 'null' => false),
  118. 'created' => 'datetime',
  119. 'updated' => 'datetime'
  120. ),
  121. 'posts' => array(
  122. 'id' => array('type' => 'integer', 'key' => 'primary'),
  123. 'author_id' => array('type' => 'integer', 'null' => false),
  124. 'title' => array('type' => 'string', 'null' => false),
  125. 'body' => 'text',
  126. 'published' => array('type' => 'string', 'length' => 1, 'default' => 'N'),
  127. 'created' => 'datetime',
  128. 'updated' => 'datetime'
  129. )
  130. );
  131. /**
  132. * testStartup method
  133. *
  134. * @return void
  135. **/
  136. public function testStartup() {
  137. $this->Shell->connection = 'default';
  138. $this->assertEqual($this->Shell->type, 'TestMigrationPlugin');
  139. $this->Shell->params = array(
  140. 'connection' => 'test',
  141. 'plugin' => 'Migrations',
  142. 'no-auto-init' => false
  143. );
  144. $this->Shell->startup();
  145. $this->assertEqual($this->Shell->connection, 'test');
  146. $this->assertEqual($this->Shell->type, 'Migrations');
  147. }
  148. /**
  149. * testRun method
  150. *
  151. * @return void
  152. **/
  153. public function testRun() {
  154. $mapping = array();
  155. for ($i = 1; $i <= 10; $i++) {
  156. $mapping[$i] = array(
  157. 'version' => $i, 'name' => '001_schema_dump',
  158. 'class' => 'M4af9d151e1484b74ad9d007058157726',
  159. 'type' => $this->Shell->type, 'migrated' => null
  160. );
  161. }
  162. $this->Shell->expects($this->any())->method('_stop')->will($this->returnValue(false));
  163. // Variable used on expectArgumentsAt method
  164. $runCount = $versionCount = $inCount = 0;
  165. // cake migration run - no mapping
  166. $this->Shell->Version->expects($this->at(0))->method('getMapping')->will($this->returnValue(false));
  167. $this->Shell->args = array();
  168. $this->assertFalse($this->Shell->run());
  169. // cake migration run up
  170. $this->Shell->Version->expects($this->any())->method('getMapping')->will($this->returnValue($mapping));
  171. $this->Shell->Version->expects($this->at(1))->method('getVersion')->will($this->returnValue(0));
  172. $this->Shell->Version->expects($this->at(2))->method('run')->with($this->equalTo(array(
  173. 'type' => 'TestMigrationPlugin',
  174. 'callback' => $this->Shell,
  175. 'direction' => 'up',
  176. 'version' => 1
  177. )));
  178. $this->Shell->args = array('up');
  179. $this->assertTrue($this->Shell->run());
  180. // cake migration run up - on last version == stop
  181. $this->Shell->Version->expects($this->at(1))->method('getVersion')->will($this->returnValue(10));
  182. $this->Shell->args = array('up');
  183. $this->assertFalse($this->Shell->run());
  184. // cake migration run down - on version 0 == stop
  185. $this->Shell->Version->expects($this->at(1))->method('getVersion')->will($this->returnValue(0));
  186. $this->Shell->args = array('down');
  187. $this->assertFalse($this->Shell->run());
  188. // cake migration run down
  189. $this->Shell->Version->expects($this->at(1))->method('getVersion')->will($this->returnValue(1));
  190. $this->Shell->Version->expects($this->at(2))->method('run')->with($this->equalTo(array(
  191. 'type' => 'TestMigrationPlugin',
  192. 'callback' => $this->Shell,
  193. 'direction' => 'down',
  194. 'version' => 1
  195. )));
  196. $this->Shell->args = array('down');
  197. $this->assertTrue($this->Shell->run());
  198. // cake migration run all
  199. $this->Shell->Version->expects($this->at(1))->method('getVersion')->will($this->returnValue(1));
  200. $this->Shell->Version->expects($this->at(2))->method('run')->with($this->equalTo(array(
  201. 'type' => 'TestMigrationPlugin',
  202. 'callback' => $this->Shell,
  203. 'version' => 10,
  204. 'direction' => 'up'
  205. )));
  206. $this->Shell->args = array('all');
  207. $this->assertTrue($this->Shell->run());
  208. // cake migration run reset
  209. $this->Shell->Version->expects($this->at(1))->method('getVersion')->will($this->returnValue(9));
  210. $this->Shell->Version->expects($this->at(2))->method('run')->with($this->equalTo(array(
  211. 'type' => 'TestMigrationPlugin',
  212. 'callback' => $this->Shell,
  213. 'version' => 0,
  214. 'direction' => 'down',
  215. 'reset' => true
  216. )));
  217. $this->Shell->args = array('reset');
  218. $this->assertTrue($this->Shell->run());
  219. // cake migration run - answers 0, 11, 1
  220. $this->Shell->Version->expects($this->at(1))->method('getVersion')->will($this->returnValue(0));
  221. $this->Shell->Version->expects($this->at(2))->method('run')->with($this->equalTo(array(
  222. 'type' => 'TestMigrationPlugin',
  223. 'callback' => $this->Shell,
  224. 'version' => 1,
  225. 'direction' => 'up'
  226. )));
  227. $this->Shell->expects($this->at(2))->method('in')->will($this->returnValue(0));
  228. $this->Shell->expects($this->at(4))->method('in')->will($this->returnValue(11));
  229. $this->Shell->expects($this->at(6))->method('in')->will($this->returnValue(1));
  230. $this->Shell->args = array();
  231. $this->assertTrue($this->Shell->run());
  232. // cake migration run - answers 10
  233. $this->Shell->Version->expects($this->at(1))->method('getVersion')->will($this->returnValue(9));
  234. $this->Shell->Version->expects($this->at(2))->method('run')->with($this->equalTo(array(
  235. 'type' => 'TestMigrationPlugin',
  236. 'callback' => $this->Shell,
  237. 'version' => 10,
  238. 'direction' => 'up'
  239. )));
  240. $this->Shell->expects($this->at(2))->method('in')->will($this->returnValue(10));
  241. $this->Shell->args = array();
  242. $this->assertTrue($this->Shell->run());
  243. // cake migration run 1
  244. $this->Shell->Version->expects($this->at(1))->method('getVersion')->will($this->returnValue(0));
  245. $this->Shell->Version->expects($this->at(2))->method('run')->with($this->equalTo(array(
  246. 'type' => 'TestMigrationPlugin',
  247. 'callback' => $this->Shell,
  248. 'version' => 1,
  249. )));
  250. $this->Shell->args = array('1');
  251. $this->assertTrue($this->Shell->run());
  252. // cake migration run 11
  253. $this->Shell->Version->expects($this->at(1))->method('getVersion')->will($this->returnValue(0));
  254. $this->Shell->args = array('11');
  255. $this->assertFalse($this->Shell->run());
  256. }
  257. /**
  258. * testRunWithFailuresOnce method
  259. *
  260. * @return void
  261. **/
  262. public function testRunWithFailuresOnce() {
  263. $this->Shell->expects($this->any())->method('_stop')->will($this->returnValue(false));
  264. $mapping = array(1 => array(
  265. 'version' => 1, 'name' => '001_schema_dump',
  266. 'class' => 'M4af9d151e1484b74ad9d007058157726',
  267. 'type' => $this->Shell->type, 'migrated' => null
  268. ));
  269. $migration = new CakeMigration();
  270. $migration->info = $mapping[1];
  271. $exception = new MigrationException($migration, 'Exception message');
  272. $this->Shell->Version->expects($this->any())->method('getMapping')->will($this->returnValue($mapping));
  273. $this->Shell->Version->expects($this->any())->method('getVersion')->will($this->returnValue(0));
  274. $this->Shell->Version->expects($this->at(2))->method('run')->will($this->throwException($exception));
  275. $this->Shell->expects($this->at(1))->method('in')->will($this->returnValue('y'));
  276. $this->Shell->args = array('up');
  277. $this->assertTrue($this->Shell->run());
  278. $result = $this->Shell->output;
  279. $pattern = <<<TEXT
  280. /Running migrations:
  281. An error occurred when processing the migration:
  282. Migration: 001_schema_dump
  283. Error: Exception message
  284. All migrations have completed./
  285. TEXT;
  286. $this->assertPattern(str_replace("\r\n", "\n", $pattern), str_replace("\r\n", "\n", $result));
  287. }
  288. /**
  289. * testRunWithFailuresNotOnce method
  290. *
  291. * @return void
  292. **/
  293. public function testRunWithFailuresNotOnce() {
  294. $this->Shell->expects($this->any())->method('_stop')->will($this->returnValue(false));
  295. $mapping = array(
  296. 1 => array(
  297. 'version' => 1, 'name' => '001_schema_dump',
  298. 'class' => 'M4af9d151e1484b74ad9d007058157726',
  299. 'type' => $this->Shell->type, 'migrated' => null
  300. ),
  301. );
  302. $migration = new CakeMigration();
  303. $migration->info = $mapping[1];
  304. $exception = new MigrationException($migration, 'Exception message');
  305. $this->Shell->Version->expects($this->any())->method('getMapping')->will($this->returnValue($mapping));
  306. $this->Shell->Version->expects($this->any())->method('getVersion')->will($this->returnValue(0));
  307. $this->Shell->Version->expects($this->at(2))->method('run')->will($this->throwException($exception));
  308. $this->Shell->Version->expects($this->at(3))->method('run')->will($this->returnValue(true));
  309. $this->Shell->expects($this->at(1))->method('in')->will($this->returnValue('y'));
  310. $this->Shell->args = array('all');
  311. $this->assertTrue($this->Shell->run());
  312. $result = $this->Shell->output;
  313. $pattern = <<<TEXT
  314. /Running migrations:
  315. All migrations have completed./
  316. TEXT;
  317. $this->assertPattern(str_replace("\r\n", "\n", $pattern), str_replace("\n\n", "\n", $result));
  318. }
  319. /**
  320. * testFromComparisonTableActions method
  321. *
  322. * @return void
  323. **/
  324. public function testFromComparisonTableActions() {
  325. $comparison = array(
  326. 'users' => array('add' => $this->tables['users']),
  327. 'posts' => array('add' => $this->tables['posts'])
  328. );
  329. $oldTables = array();
  330. $result = $this->Shell->fromComparison(array(), $comparison, $oldTables, $this->tables);
  331. $expected = array(
  332. 'up' => array('create_table' => $this->tables),
  333. 'down' => array('drop_table' => array('users', 'posts'))
  334. );
  335. $this->assertEqual($result, $expected);
  336. $comparison = array('posts' => array('add' => $this->tables['posts']));
  337. $oldTables = array('users' => $this->tables['users']);
  338. $result = $this->Shell->fromComparison(array(), $comparison, $oldTables, $this->tables);
  339. $expected = array(
  340. 'up' => array(
  341. 'create_table' => array('posts' => $this->tables['posts'])
  342. ),
  343. 'down' => array(
  344. 'drop_table' => array('posts')
  345. )
  346. );
  347. $this->assertEqual($result, $expected);
  348. $comparison = array();
  349. $oldTables = array('posts' => $this->tables['posts'], 'users' => $this->tables['users']);
  350. $currentTables = array('users' => $this->tables['users']);
  351. $result = $this->Shell->fromComparison(array(), $comparison, $oldTables, $currentTables);
  352. $expected = array(
  353. 'up' => array(
  354. 'drop_table' => array('posts')
  355. ),
  356. 'down' => array(
  357. 'create_table' => array('posts' => $this->tables['posts'])
  358. )
  359. );
  360. $this->assertEqual($result, $expected);
  361. }
  362. /**
  363. * testFromComparisonFieldActions method
  364. *
  365. * @return void
  366. **/
  367. public function testFromComparisonFieldActions() {
  368. // Add field/index
  369. $oldTables = array('posts' => $this->tables['posts']);
  370. $newTables = array('posts' => array());
  371. $comparison = array(
  372. 'posts' => array('add' => array(
  373. 'views' => array('type' => 'integer', 'null' => false)
  374. ))
  375. );
  376. $result = $this->Shell->fromComparison(array(), $comparison, $oldTables, $newTables);
  377. $expected = array(
  378. 'up' => array(
  379. 'create_field' => array(
  380. 'posts' => array('views' => array('type' => 'integer', 'null' => false))
  381. )
  382. ),
  383. 'down' => array(
  384. 'drop_field' => array(
  385. 'posts' => array('views')
  386. )
  387. )
  388. );
  389. $this->assertEqual($result, $expected);
  390. $comparison = array(
  391. 'posts' => array('add' => array(
  392. 'indexes' => array('VIEW_COUNT' => array('column' => 'views', 'unique' => false))
  393. ))
  394. );
  395. $result = $this->Shell->fromComparison(array(), $comparison, $oldTables, $newTables);
  396. $expected = array(
  397. 'up' => array(
  398. 'create_field' => array(
  399. 'posts' => array(
  400. 'indexes' => array('VIEW_COUNT' => array('column' => 'views', 'unique' => false))
  401. )
  402. )
  403. ),
  404. 'down' => array(
  405. 'drop_field' => array(
  406. 'posts' => array('indexes' => array('VIEW_COUNT'))
  407. )
  408. )
  409. );
  410. $this->assertEqual($result, $expected);
  411. $comparison = array(
  412. 'posts' => array('add' => array(
  413. 'views' => array('type' => 'integer', 'null' => false),
  414. 'indexes' => array('VIEW_COUNT' => array('column' => 'views', 'unique' => false))
  415. ))
  416. );
  417. $result = $this->Shell->fromComparison(array(), $comparison, $oldTables, $newTables);
  418. $expected = array(
  419. 'up' => array(
  420. 'create_field' => array(
  421. 'posts' => array(
  422. 'views' => array('type' => 'integer', 'null' => false),
  423. 'indexes' => array('VIEW_COUNT' => array('column' => 'views', 'unique' => false))
  424. )
  425. )
  426. ),
  427. 'down' => array(
  428. 'drop_field' => array(
  429. 'posts' => array('views', 'indexes' => array('VIEW_COUNT'))
  430. )
  431. )
  432. );
  433. $this->assertEqual($result, $expected);
  434. // Drop field/index
  435. $oldTables['posts']['views'] = array('type' => 'integer', 'null' => false);
  436. $oldTables['posts']['indexes'] = array('VIEW_COUNT' => array('column' => 'views', 'unique' => false));
  437. $comparison = array(
  438. 'posts' => array('drop' => array(
  439. 'views' => array('type' => 'integer', 'null' => false)
  440. ))
  441. );
  442. $result = $this->Shell->fromComparison(array(), $comparison, $oldTables, $newTables);
  443. $expected = array(
  444. 'up' => array(
  445. 'drop_field' => array(
  446. 'posts' => array('views')
  447. )
  448. ),
  449. 'down' => array(
  450. 'create_field' => array(
  451. 'posts' => array('views' => array('type' => 'integer', 'null' => false))
  452. )
  453. )
  454. );
  455. $this->assertEqual($result, $expected);
  456. $comparison = array(
  457. 'posts' => array('drop' => array(
  458. 'indexes' => array('VIEW_COUNT' => array('column' => 'views', 'unique' => false))
  459. ))
  460. );
  461. $result = $this->Shell->fromComparison(array(), $comparison, $oldTables, $newTables);
  462. $expected = array(
  463. 'up' => array(
  464. 'drop_field' => array(
  465. 'posts' => array('indexes' => array('VIEW_COUNT'))
  466. )
  467. ),
  468. 'down' => array(
  469. 'create_field' => array(
  470. 'posts' => array('indexes' => array('VIEW_COUNT' => array('column' => 'views', 'unique' => false)))
  471. )
  472. )
  473. );
  474. $this->assertEqual($result, $expected);
  475. $comparison = array(
  476. 'posts' => array('drop' => array(
  477. 'views' => array('type' => 'integer', 'null' => false),
  478. 'indexes' => array('VIEW_COUNT' => array('column' => 'views', 'unique' => false))
  479. ))
  480. );
  481. $result = $this->Shell->fromComparison(array(), $comparison, $oldTables, $newTables);
  482. $expected = array(
  483. 'up' => array(
  484. 'drop_field' => array(
  485. 'posts' => array('views', 'indexes' => array('VIEW_COUNT'))
  486. )
  487. ),
  488. 'down' => array(
  489. 'create_field' => array(
  490. 'posts' => array(
  491. 'views' => array('type' => 'integer', 'null' => false),
  492. 'indexes' => array('VIEW_COUNT' => array('column' => 'views', 'unique' => false))
  493. )
  494. )
  495. )
  496. );
  497. $this->assertEqual($result, $expected);
  498. // Change field
  499. $comparison = array(
  500. 'posts' => array('change' => array(
  501. 'views' => array('type' => 'integer', 'null' => false, 'length' => 2),
  502. ))
  503. );
  504. $result = $this->Shell->fromComparison(array(), $comparison, $oldTables, $newTables);
  505. $expected = array(
  506. 'up' => array(
  507. 'alter_field' => array(
  508. 'posts' => array(
  509. 'views' => array('type' => 'integer', 'null' => false, 'length' => 2)
  510. )
  511. )
  512. ),
  513. 'down' => array(
  514. 'alter_field' => array(
  515. 'posts' => array(
  516. 'views' => array('type' => 'integer', 'null' => false)
  517. )
  518. )
  519. )
  520. );
  521. $this->assertEqual($result, $expected);
  522. // Change field with/out length
  523. $oldTables = array('users' => $this->tables['users']);
  524. $newTables = array('users' => array());
  525. $oldTables['users']['last_login'] = array('type' => 'integer', 'null' => false, 'length' => 11);
  526. $comparison = array(
  527. 'users' => array('change' => array(
  528. 'last_login' => array('type' => 'datetime', 'null' => false),
  529. ))
  530. );
  531. $result = $this->Shell->fromComparison(array(), $comparison, $oldTables, $newTables);
  532. $expected = array(
  533. 'up' => array(
  534. 'alter_field' => array(
  535. 'users' => array(
  536. 'last_login' => array('type' => 'datetime', 'null' => false, 'length' => null)
  537. )
  538. )
  539. ),
  540. 'down' => array(
  541. 'alter_field' => array(
  542. 'users' => array(
  543. 'last_login' => array('type' => 'integer', 'null' => false, 'length' => 11)
  544. )
  545. )
  546. )
  547. );
  548. $this->assertEqual($result, $expected);
  549. }
  550. /**
  551. * testWriteMigration method
  552. *
  553. * @return void
  554. **/
  555. public function testWriteMigration() {
  556. // Remove if exists
  557. $this->__unlink('12345_migration_test_file.php');
  558. $users = $this->tables['users'];
  559. $users['indexes'] = array('UNIQUE_USER' => array('column' => 'user', 'unique' => true));
  560. $migration = array(
  561. 'up' => array(
  562. 'create_table' => array('users' => $users),
  563. 'create_field' => array(
  564. 'posts' => array(
  565. 'views' => array('type' => 'integer', 'null' => false),
  566. 'indexes' => array('VIEW_COUNT' => array('column' => 'views', 'unique' => false))
  567. )
  568. )
  569. ),
  570. 'down' => array(
  571. 'drop_table' => array('users'),
  572. 'drop_field' => array(
  573. 'posts' => array('views', 'indexes' => array('VIEW_COUNT'))
  574. )
  575. )
  576. );
  577. $this->assertFalse(file_exists(TMP . 'tests' . DS . '12345_migration_test_file.php'));
  578. $this->assertTrue($this->Shell->writeMigration('migration_test_file', 12345, $migration));
  579. $this->assertTrue(file_exists(TMP . 'tests' . DS . '12345_migration_test_file.php'));
  580. $result = $this->__getMigrationVariable(TMP . 'tests' . DS . '12345_migration_test_file.php');
  581. $expected = <<<TEXT
  582. public \$migration = array(
  583. 'up' => array(
  584. 'create_table' => array(
  585. 'users' => array(
  586. 'id' => array('type' => 'integer', 'key' => 'primary'),
  587. 'user' => array('type' => 'string', 'null' => false),
  588. 'password' => array('type' => 'string', 'null' => false),
  589. 'created' => 'datetime',
  590. 'updated' => 'datetime',
  591. 'indexes' => array(
  592. 'UNIQUE_USER' => array('column' => 'user', 'unique' => true),
  593. ),
  594. ),
  595. ),
  596. 'create_field' => array(
  597. 'posts' => array(
  598. 'views' => array('type' => 'integer', 'null' => false),
  599. 'indexes' => array(
  600. 'VIEW_COUNT' => array('column' => 'views', 'unique' => false),
  601. ),
  602. ),
  603. ),
  604. ),
  605. 'down' => array(
  606. 'drop_table' => array(
  607. 'users'
  608. ),
  609. 'drop_field' => array(
  610. 'posts' => array('views', 'indexes' => array('VIEW_COUNT')),
  611. ),
  612. ),
  613. );
  614. TEXT;
  615. $this->assertEqual($result, str_replace("\r\n", "\n", $expected));
  616. $this->__unlink('12345_migration_test_file.php');
  617. }
  618. /**
  619. * testGenerate method
  620. *
  621. * @return void
  622. */
  623. public function testGenerate() {
  624. $this->Shell->expects($this->at(0))->method('in')->will($this->returnValue('n'));
  625. $this->Shell->expects($this->at(1))->method('in')->will($this->returnValue('n'));
  626. $this->Shell->expects($this->at(2))->method('in')->will($this->returnValue('Initial Schema'));
  627. $this->Shell->generate();
  628. $files = glob(TMP . 'tests' . DS . '*initial_schema.php');
  629. foreach ($files as $f) {
  630. unlink($f);
  631. }
  632. $this->assertNotEmpty(preg_grep('/([0-9])+_initial_schema\.php$/i', $files));
  633. }
  634. /**
  635. * testGenerate2 method
  636. *
  637. * @return void
  638. */
  639. public function testGenerate2() {
  640. $this->Shell->expects($this->atLeastOnce())->method('err');
  641. $this->Shell->expects($this->at(0))->method('in')->will($this->returnValue('n'));
  642. $this->Shell->expects($this->at(1))->method('in')->will($this->returnValue('n'));
  643. $this->Shell->expects($this->at(2))->method('in')->will($this->returnValue('002 invalid name'));
  644. $this->Shell->expects($this->at(4))->method('in')->will($this->returnValue('invalid-name'));
  645. $this->Shell->expects($this->at(6))->method('in')->will($this->returnValue('create some sample_data'));
  646. $this->Shell->generate();
  647. $files = glob(TMP . 'tests' . DS . '*create_some_sample_data.php');
  648. foreach ($files as $f) {
  649. unlink($f);
  650. }
  651. $this->assertNotEmpty(preg_grep('/([0-9])+_create_some_sample_data\.php$/i', $files));
  652. }
  653. /**
  654. * testGenerateComparison method
  655. *
  656. * @return void
  657. */
  658. public function testGenerateComparison() {
  659. $this->Shell->expects($this->at(0))->method('in')->will($this->returnValue('y'));
  660. $this->Shell->expects($this->at(2))->method('in')->will($this->returnValue('n'));
  661. $this->Shell->expects($this->at(3))->method('in')->will($this->returnValue('drop slug field'));
  662. $this->Shell->expects($this->at(4))->method('in')->will($this->returnValue('y'));
  663. $this->Shell->expects($this->at(5))->method('dispatchShell')->with('schema generate --connection test --force');
  664. $this->Shell->Version->expects($this->any())->method('getMapping')->will($this->returnCallback(array($this, 'returnMapping')));
  665. $this->assertEmpty(glob(TMP . 'tests' . DS . '*drop_slug_field.php'));
  666. $this->Shell->params['force'] = true;
  667. $this->Shell->generate();
  668. $files = glob(TMP . 'tests' . DS . '*drop_slug_field.php');
  669. $this->assertNotEmpty($files);
  670. $result = $this->__getMigrationVariable(current($files));
  671. foreach ($files as $f) {
  672. unlink($f);
  673. }
  674. $this->assertNoPattern('/\'schema_migrations\'/', $result);
  675. $pattern = <<<TEXT
  676. / 'drop_field' => array\(
  677. 'articles' => array\('slug',\),
  678. \),/
  679. TEXT;
  680. $this->assertPattern(str_replace("\r\n", "\n", $pattern), $result);
  681. $pattern = <<<TEXT
  682. / 'create_field' => array\(
  683. 'articles' => array\(
  684. 'slug' => array\('type' => 'string', 'null' => false\),
  685. \),
  686. \),/
  687. TEXT;
  688. $this->assertPattern(str_replace("\r\n", "\n", $pattern), $result);
  689. }
  690. public function returnMapping() {
  691. return array(
  692. gmdate('U') => array('class' => 'M4af9d15154844819b7a0007058157726')
  693. );
  694. }
  695. /**
  696. * testGenerateDump method
  697. *
  698. * @return void
  699. */
  700. public function testGenerateDump() {
  701. $this->Shell->expects($this->at(0))->method('in')->will($this->returnValue('y'));
  702. $this->Shell->expects($this->at(2))->method('in')->will($this->returnValue('n'));
  703. $this->Shell->expects($this->at(3))->method('in')->will($this->returnValue('schema dump'));
  704. $mapping = array(
  705. gmdate('U') => array('class' => 'M4af9d15154844819b7a0007058157726')
  706. );
  707. $this->Shell->Version->expects($this->any())->method('getMapping')->will($this->returnCallback(array($this, 'returnMapping')));
  708. $this->assertEmpty(glob(TMP . 'tests' . DS . '*schema_dump.php'));
  709. $this->Shell->type = 'TestMigrationPlugin2';
  710. $this->Shell->params['force'] = true;
  711. $this->Shell->generate();
  712. $files = glob(TMP . 'tests' . DS . '*schema_dump.php');
  713. $this->assertNotEmpty($files);
  714. $result = $this->__getMigrationVariable(current($files));
  715. foreach ($files as $f) {
  716. unlink($f);
  717. }
  718. $expected = file_get_contents(CakePlugin::path('Migrations') . '/Test/Fixture/test_migration.txt');
  719. $this->assertEquals($expected, $result);
  720. }
  721. /**
  722. * testStatus method
  723. *
  724. * @return void
  725. */
  726. public function testStatus() {
  727. $this->Shell->Version = new MigrationVersion(array('connection' => 'test'));
  728. $this->Shell->status();
  729. $result = $this->Shell->output;
  730. $pattern = <<<TEXT
  731. /Migrations Plugin
  732. Current version:
  733. #003 003_increase_class_name_length
  734. Latest version:
  735. #003 003_increase_class_name_length/
  736. TEXT;
  737. $this->assertPattern(str_replace("\r\n", "\n", $pattern), $result);
  738. $this->Shell->output = '';
  739. $this->Shell->args = array('outdated');
  740. $this->Shell->status();
  741. $result = $this->Shell->output;
  742. $this->assertNoPattern(str_replace("\r\n", "\n", $pattern), $result);
  743. $this->Shell->Version->setVersion(3, 'migrations', false);
  744. $this->Shell->output = '';
  745. $this->Shell->args = array('outdated');
  746. $this->Shell->status();
  747. $result = $this->Shell->output;
  748. $pattern = <<<TEXT
  749. /Migrations Plugin
  750. Current version:
  751. #002 002_convert_version_to_class_names
  752. Latest version:
  753. #003 003_increase_class_name_length/
  754. TEXT;
  755. $this->assertPattern(str_replace("\r\n", "\n", $pattern), $result);
  756. $this->Shell->Version->setVersion(1, 'migrations');
  757. }
  758. /**
  759. * Strip all the content surrounding the $migration variable
  760. *
  761. * @param string $file
  762. * @return string
  763. */
  764. private function __getMigrationVariable($file) {
  765. $result = array();
  766. $array = explode("\n", str_replace("\r\n", "\n", file_get_contents($file)));
  767. foreach ($array as $line) {
  768. if ($line == "\tpublic \$migration = array(") {
  769. $result[] = $line;
  770. } else if (!empty($result) && $line == "\t);") {
  771. $result[] = $line;
  772. break;
  773. } else if (!empty($result)) {
  774. $result[] = $line;
  775. }
  776. }
  777. return implode("\n", $result);
  778. }
  779. /**
  780. * Unlink test files from filesystem
  781. *
  782. * @param mixed files
  783. * @return void
  784. */
  785. private function __unlink() {
  786. $files = func_get_args();
  787. foreach ($files as $file) {
  788. @unlink(TMP . 'tests' . DS . $file);
  789. }
  790. }
  791. }