Skip to content

Commit 74518b1

Browse files
author
Alexandre
committed
Merge pull request #5 from alwex/stable
Stable
2 parents cfe34f6 + 2cd06d0 commit 74518b1

13 files changed

Lines changed: 633 additions & 48 deletions

Migrate/Command/AbstractEnvCommand.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
class AbstractEnvCommand extends AbstractComand
2020
{
2121

22-
protected static $progressBarFormat = '%current%/%max% [%bar%] %percent% % %memory% [%message%]';
22+
protected static $progressBarFormat = '%current%/%max% [%bar%] %percent% % [%message%]';
2323

2424
/**
2525
* @var \PDO
@@ -76,10 +76,14 @@ protected function init(InputInterface $input, OutputInterface $output, $env = n
7676
$password = ArrayUtil::get($conf['connection'], 'password');
7777

7878
$uri = $driver;
79-
$uri .= ($dbname == null) ?: ":dbname=$dbname";
80-
$uri .= ($host == null) ?: ";host=$host";
81-
$uri .= ($port == null) ?: ";port=$port";
8279

80+
if ($driver == 'sqlite') {
81+
$uri .= ":$dbname";
82+
} else {
83+
$uri .= ($dbname == null) ?: ":dbname=$dbname";
84+
$uri .= ($host == null) ?: ";host=$host";
85+
$uri .= ($port == null) ?: ";port=$port";
86+
}
8387
$this->db = new \PDO(
8488
$uri,
8589
$username,
@@ -98,6 +102,7 @@ public function getLocalMigrations()
98102
$fileList = scandir($this->getMigrationDir());
99103
$fileList = ArrayUtil::filter($fileList);
100104

105+
$migrations = array();
101106
foreach ($fileList as $file) {
102107
$migration = Migration::createFromFile($file, $this->getMigrationDir());
103108
$migrations[$migration->getId()] = $migration;

Migrate/Command/CreateCommand.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
4949
file_put_contents($migrationFullPath, $templateFile);
5050
$output->writeln("<info>$migrationFullPath created</info>");
5151

52-
system("vim $migrationFullPath > `tty`");
52+
if (!defined('PHPUNIT')) {
53+
system("vim $migrationFullPath > `tty`");
54+
}
5355
}
5456

5557
}

Migrate/Enum/Directory.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: aguidet
5+
* Date: 01/03/15
6+
* Time: 01:41
7+
*/
8+
9+
namespace Migrate\Enum;
10+
11+
12+
class Directory {
13+
14+
public static $appDirectory = '.php-database-migration';
15+
16+
public static function getEnvPath()
17+
{
18+
return self::$appDirectory . '/environments';
19+
}
20+
21+
public static function getMigrationsPath()
22+
{
23+
return self::$appDirectory . '/migrations';
24+
}
25+
}

Migrate/Utils/InputStreamUtil.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: aguidet
5+
* Date: 01/03/15
6+
* Time: 01:45
7+
*/
8+
9+
namespace Migrate\Utils;
10+
11+
12+
class InputStreamUtil {
13+
14+
public static function type($input)
15+
{
16+
$stream = fopen('php://memory', 'r+', false);
17+
fputs($stream, $input);
18+
rewind($stream);
19+
20+
return $stream;
21+
}
22+
23+
}

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Warning, all migrate commands must be executed on your root folder like `bin/mig
1818
}
1919
],
2020
"require": {
21-
"php-database-migration/php-database-migration" :"3.3.*"
21+
"php-database-migration/php-database-migration" :"3.4.*"
2222
},
2323
"config": {
2424
"bin-dir": "bin"

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,8 @@
3030
},
3131
"autoload": {
3232
"psr-4": {"Migrate\\": "Migrate/"}
33+
},
34+
"autoload-dev": {
35+
"psr-4": { "Migrate\\Test\\": "tests/" }
3336
}
3437
}

composer.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
/**
3+
* User: aguidet
4+
* Date: 02/03/15
5+
* Time: 15:19
6+
*/
7+
8+
namespace Migrate\Test\Command;
9+
10+
use Migrate\Command\AddEnvCommand;
11+
use Migrate\Command\InitCommand;
12+
use Migrate\Enum\Directory;
13+
use Migrate\Utils\InputStreamUtil;
14+
use Symfony\Component\Console\Application;
15+
use Symfony\Component\Console\Helper\QuestionHelper;
16+
use Symfony\Component\Console\Tester\CommandTester;
17+
18+
class AbstractCommandTester extends \PHPUnit_Framework_TestCase
19+
{
20+
public static $env = 'testing';
21+
public static $driver = 'sqlite';
22+
public static $bddName = 'migrate_test';
23+
public static $username = 'aguidet';
24+
public static $password = 'aguidet';
25+
public static $host = 'localhost';
26+
public static $port = '5432';
27+
28+
public function cleanEnv()
29+
{
30+
exec("rm -rf .php-database-migration");
31+
32+
if (file_exists('test.sqlite')) {
33+
exec("rm test.sqlite");
34+
}
35+
}
36+
37+
public function createEnv()
38+
{
39+
$application = new Application();
40+
$application->add(new AddEnvCommand());
41+
42+
$command = $application->find('migrate:addenv');
43+
$commandTester = new CommandTester($command);
44+
45+
/* @var $question QuestionHelper */
46+
$question = $command->getHelper('question');
47+
$question->setInputStream(InputStreamUtil::type("testing\n1\ntest.sqlite\n\n\n\n\nchangelog\nvim\n"));
48+
49+
$commandTester->execute(array('command' => $command->getName()));
50+
}
51+
52+
public function initEnv()
53+
{
54+
$application = new Application();
55+
$application->add(new InitCommand());
56+
57+
$command = $application->find('migrate:init');
58+
$commandTester = new CommandTester($command);
59+
60+
$commandTester->execute(array(
61+
'command' => $command->getName(),
62+
'env' => 'testing'
63+
));
64+
}
65+
66+
public function createMigration($timestamp, $sqlUp, $sqlDown)
67+
{
68+
$filename = Directory::getMigrationsPath() . '/' . $timestamp . '_migration.sql';
69+
70+
$content =<<<SQL
71+
--// unit testing migration
72+
-- Migration SQL that makes the change goes here.
73+
$sqlUp
74+
75+
-- @UNDO
76+
-- SQL to undo the change goes here.
77+
$sqlDown
78+
79+
SQL;
80+
81+
file_put_contents($filename, $content);
82+
}
83+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
/**
3+
* User: aguidet
4+
* Date: 27/02/15
5+
* Time: 17:21
6+
*/
7+
8+
namespace Migrate\Command;
9+
10+
use Migrate\Enum\Directory;
11+
use Migrate\Utils\InputStreamUtil;
12+
use Symfony\Component\Console\Application;
13+
use Symfony\Component\Console\Helper\QuestionHelper;
14+
use Symfony\Component\Console\Tester\CommandTester;
15+
use Migrate\Test\Command\AbstractCommandTester;
16+
17+
class AddenvCommandTest extends AbstractCommandTester
18+
{
19+
public function setUp()
20+
{
21+
$this->cleanEnv();
22+
}
23+
24+
public function tearDown()
25+
{
26+
$this->cleanEnv();
27+
}
28+
29+
public function testExecute()
30+
{
31+
$application = new Application();
32+
$application->add(new AddEnvCommand());
33+
34+
$command = $application->find('migrate:addenv');
35+
$commandTester = new CommandTester($command);
36+
37+
/* @var $question QuestionHelper */
38+
$question = $command->getHelper('question');
39+
$question->setInputStream(InputStreamUtil::type("testing\n1\nmigrate_test\nlocalhost\n5432\naguidet\naguidet\nchangelog\nvim\n"));
40+
41+
$commandTester->execute(array('command' => $command->getName()));
42+
43+
$expected = "Please enter the name of the new environment (default dev): Please chose your pdo driver\n [0] pgsql\n [1] sqlite\n > Please enter the database name (or the database file location): Please enter the database host (if needed): Please enter the database port (if needed): Please enter the database user name (if needed): Please enter the database user password (if needed): Please enter the changelog table (default changelog): Please enter the text editor to use by default (default vim): ";
44+
$this->assertEquals($expected, $commandTester->getDisplay());
45+
46+
$envDir = Directory::getEnvPath();
47+
48+
$expected = <<<EXPECTED
49+
connection:
50+
host: localhost
51+
driver: sqlite
52+
port: 5432
53+
username: aguidet
54+
password: aguidet
55+
database: migrate_test
56+
57+
changelog: changelog
58+
default_editor: vim
59+
EXPECTED;
60+
61+
$fileContent = file_get_contents($envDir . '/testing.yml');
62+
63+
$this->assertEquals($expected, $fileContent);
64+
}
65+
66+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: aguidet
5+
* Date: 01/03/15
6+
* Time: 02:15
7+
*/
8+
9+
namespace Migrate\Command;
10+
11+
12+
use Migrate\Test\Command\AbstractCommandTester;
13+
use Migrate\Utils\InputStreamUtil;
14+
use Symfony\Component\Console\Application;
15+
use Symfony\Component\Console\Helper\QuestionHelper;
16+
use Symfony\Component\Console\Tester\CommandTester;
17+
18+
define('PHPUNIT', true);
19+
20+
class CreateCommandTest extends AbstractCommandTester
21+
{
22+
23+
public function setUp()
24+
{
25+
$this->cleanEnv();
26+
$this->createEnv();
27+
$this->initEnv();
28+
}
29+
30+
public function tearDown()
31+
{
32+
$this->cleanEnv();
33+
}
34+
35+
public function testExecute()
36+
{
37+
$application = new Application();
38+
$application->add(new CreateCommand());
39+
40+
$command = $application->find('migrate:create');
41+
$commandTester = new CommandTester($command);
42+
/* @var $question QuestionHelper */
43+
$question = $command->getHelper('question');
44+
$question->setInputStream(InputStreamUtil::type("je suis une super migration &&&ééé\n\n:x\n"));
45+
46+
$commandTester->execute(array('command' => $command->getName()));
47+
48+
$matches = array();
49+
preg_match('/.*: (.*) created/', $commandTester->getDisplay(), $matches);
50+
51+
$fileName = $matches[1];
52+
53+
$this->assertFileExists($fileName);
54+
$content = file_get_contents($fileName);
55+
$expected =<<<EXPECTED
56+
--// je suis une super migration &&&ééé\n-- Migration SQL that makes the change goes here.\n\n-- @UNDO\n-- SQL to undo the change goes here.\n
57+
EXPECTED;
58+
59+
$this->assertEquals($expected, $content);
60+
}
61+
}

0 commit comments

Comments
 (0)