diff --git a/.travis.yml b/.travis.yml index e38e1805f..6e3151495 100644 --- a/.travis.yml +++ b/.travis.yml @@ -38,6 +38,7 @@ env: - TEST_DB_POSTGRESQL_USER="postgres" - TEST_DB_POSTGRESQL_PASSWD="" - TEST_DB_POSTGRESQL_NAME="devtools" + - TEST_DB_POSTGRESQL_SCHEMA="public" - TEST_DB_MYSQL_DSN="mysql:host=127.0.0.1;dbname=devtools" - TEST_DB_MYSQL_HOST="127.0.0.1" - TEST_DB_MYSQL_PORT="3306" diff --git a/scripts/Phalcon/Builder/AllModels.php b/scripts/Phalcon/Builder/AllModels.php index 1128e2dda..5ba434c6c 100644 --- a/scripts/Phalcon/Builder/AllModels.php +++ b/scripts/Phalcon/Builder/AllModels.php @@ -62,7 +62,11 @@ public function build() $this->options->offsetSet('directory', $this->path->getRootPath()); - $config = $this->getConfig(); + if (gettype($this->options->get('config')) == 'object') { + $config = $this->options->get('config'); + } else { + $config = $this->getConfig(); + } if (!$modelsDir = $this->options->get('modelsDir')) { if (!isset($config->application->modelsDir)) { @@ -195,6 +199,7 @@ public function build() $modelBuilder = new Model([ 'name' => $name, + 'config' => $config, 'schema' => $schema, 'extends' => $this->options->get('extends'), 'namespace' => $this->options->get('namespace'), diff --git a/scripts/Phalcon/Builder/Model.php b/scripts/Phalcon/Builder/Model.php index caa95b8c0..bac7c96bf 100644 --- a/scripts/Phalcon/Builder/Model.php +++ b/scripts/Phalcon/Builder/Model.php @@ -146,7 +146,11 @@ public function build() $this->path->setRootPath($this->options->get('directory')); } - $config = $this->getConfig(); + if (gettype($this->options->get('config')) == 'object') { + $config = $this->options->get('config'); + } else { + $config = $this->getConfig(); + } if (!$modelsDir = $this->options->get('modelsDir')) { if (!$config->get('application') || !isset($config->get('application')->modelsDir)) { diff --git a/scripts/Phalcon/Commands/Builtin/Model.php b/scripts/Phalcon/Commands/Builtin/Model.php index 9ff25225a..0eadada8c 100644 --- a/scripts/Phalcon/Commands/Builtin/Model.php +++ b/scripts/Phalcon/Commands/Builtin/Model.php @@ -46,6 +46,7 @@ public function getPossibleParams() return [ 'name=s' => 'Table name', 'schema=s' => 'Name of the schema [optional]', + 'config=s' => 'Configuration file [optional]', 'namespace=s' => "Model's namespace [optional]", 'get-set' => 'Attributes will be protected and have setters/getters [optional]', 'extends=s' => 'Model extends the class name supplied [optional]', @@ -74,10 +75,32 @@ public function run(array $parameters) $name = $this->getOption(['name', 1]); $className = Utils::camelize(isset($parameters[1]) ? $parameters[1] : $name, '_-'); + if ($this->isReceivedOption('config')) { + if (false == $this->path->isAbsolutePath($this->getOption('config'))) { + $configPath = $this->path->getRootPath() . $this->getOption('config'); + } else { + $configPath = $this->getOption('config'); + } + + if (preg_match('/.*(:?\.ini)(?:\s)?$/i', $configPath)) { + $config = new ConfigIni($configPath); + } else { + $config = include $configPath; + + if (is_array($config)) { + $config = new Config($config); + } + } + + } else { + $config = $this->path->getConfig(); + } + $modelBuilder = new ModelBuilder( [ 'name' => $name, 'schema' => $this->getOption('schema'), + 'config' => $config, 'className' => $className, 'fileName' => Text::uncamelize($className), 'genSettersGetters' => $this->isReceivedOption('get-set'), diff --git a/scripts/Phalcon/Migrations.php b/scripts/Phalcon/Migrations.php index 71777fc4b..f5c7f921d 100644 --- a/scripts/Phalcon/Migrations.php +++ b/scripts/Phalcon/Migrations.php @@ -498,6 +498,7 @@ public static function getCurrentVersion($options) if ($version = trim($version) ?: null) { $version = preg_split('/\r\n|\r|\n/', $version, -1, PREG_SPLIT_NO_EMPTY); + natsort($version); $version = array_pop($version); } diff --git a/scripts/Phalcon/Mvc/Model/Migration.php b/scripts/Phalcon/Mvc/Model/Migration.php index 76faa4ee1..8953e63be 100644 --- a/scripts/Phalcon/Mvc/Model/Migration.php +++ b/scripts/Phalcon/Mvc/Model/Migration.php @@ -37,6 +37,7 @@ use Phalcon\Db\Adapter\Pdo\MysqlExtended as AdapterMysqlExtended; use Phalcon\Db\Dialect\PostgresqlExtended; use Phalcon\Db\Adapter\Pdo\PostgresqlExtended as AdapterPostgresqlExtended; +use Phalcon\Utils\Nullify; /** * Phalcon\Mvc\Model\Migration @@ -860,7 +861,8 @@ function ($value) { $line ); - self::$_connection->insert($tableName, $values, $fields); + $nullify = new Nullify(); + self::$_connection->insert($tableName, $nullify($values), $fields); unset($line); } fclose($batchHandler); diff --git a/scripts/Phalcon/Utils/Nullify.php b/scripts/Phalcon/Utils/Nullify.php new file mode 100644 index 000000000..b08e8bd5d --- /dev/null +++ b/scripts/Phalcon/Utils/Nullify.php @@ -0,0 +1,43 @@ + | + +------------------------------------------------------------------------+ +*/ + +namespace Phalcon\Utils; + +class Nullify +{ + public function __invoke($values) + { + $vals = array_map( + function ($value) { + if (function_exists('mb_strtolower')){ + return mb_strtolower($value); + } + }, + $values + ); + + foreach ($vals as $key => $value) { + if ($value == 'null'){ + $values[$key] = null; + } + } + + return $values; + } +} diff --git a/tests/_bootstrap.php b/tests/_bootstrap.php index 8efd62fb5..409b3195d 100644 --- a/tests/_bootstrap.php +++ b/tests/_bootstrap.php @@ -45,7 +45,7 @@ "TEST_DB_POSTGRESQL_HOST" => '127.0.0.1', "TEST_DB_POSTGRESQL_PORT" => 5432, "TEST_DB_POSTGRESQL_USER" => 'postgres', - "TEST_DB_POSTGRESQL_PASSWD" => 'secret', + "TEST_DB_POSTGRESQL_PASSWD" => '', "TEST_DB_POSTGRESQL_NAME" => 'devtools', "TEST_DB_POSTGRESQL_SCHEMA" => 'public', ]; diff --git a/tests/_data/console/app/postgresql/config.php b/tests/_data/console/app/postgresql/config.php new file mode 100644 index 000000000..6a1bdb295 --- /dev/null +++ b/tests/_data/console/app/postgresql/config.php @@ -0,0 +1,22 @@ + [ + 'adapter' => 'Postgresql', + 'host' => env('TEST_DB_POSTGRESQL_HOST', '127.0.0.1'), + 'username' => env('TEST_DB_POSTGRESQL_USER', 'postgres'), + 'password' => env('TEST_DB_POSTGRESQL_PASSWD', ''), + 'dbname' => env('TEST_DB_POSTGRESQL_NAME', 'devtools'), + 'schema' => env('TEST_DB_POSTGRESQL_SCHEMA', 'public') + ], + 'logger' => [ + 'path' => tests_path('_output/logs/console/postgresql/'), + 'format' => '%date% [%type%] %message%', + 'date' => 'D j H:i:s', + 'logLevel' => Logger::DEBUG, + 'filename' => 'tests.log', + ] +]); diff --git a/tests/_data/console/app/test_insert_delete/migrations/1.0.0/test_insert_delete.dat b/tests/_data/console/app/test_insert_delete/migrations/1.0.0/test_insert_delete.dat new file mode 100644 index 000000000..e69de29bb diff --git a/tests/_data/console/app/test_insert_delete/migrations/1.0.0/test_insert_delete.php b/tests/_data/console/app/test_insert_delete/migrations/1.0.0/test_insert_delete.php new file mode 100644 index 000000000..eb7c8e416 --- /dev/null +++ b/tests/_data/console/app/test_insert_delete/migrations/1.0.0/test_insert_delete.php @@ -0,0 +1,124 @@ +morphTable('test_insert_delete', [ + 'columns' => [ + new Column( + 'id', + [ + 'type' => Column::TYPE_INTEGER, + 'notNull' => true, + 'autoIncrement' => true, + 'first' => true + ] + ), + new Column( + 'login_type_id', + [ + 'type' => Column::TYPE_INTEGER, + 'notNull' => true, + 'after' => 'id' + ] + ), + new Column( + 'username', + [ + 'type' => Column::TYPE_VARCHAR, + 'notNull' => true, + 'size' => 255, + 'after' => 'login_type_id' + ] + ), + new Column( + 'name', + [ + 'type' => Column::TYPE_VARCHAR, + 'notNull' => true, + 'size' => 255, + 'after' => 'username' + ] + ), + new Column( + 'email_addr', + [ + 'type' => Column::TYPE_VARCHAR, + 'notNull' => true, + 'size' => 255, + 'after' => 'name' + ] + ), + new Column( + 'password', + [ + 'type' => Column::TYPE_VARCHAR, + 'notNull' => true, + 'size' => 255, + 'after' => 'email_addr' + ] + ), + new Column( + 'phone_num', + [ + 'type' => Column::TYPE_BIGINTEGER, + 'after' => 'password' + ] + ), + new Column( + 'token_id', + [ + 'type' => Column::TYPE_BIGINTEGER, + 'after' => 'phone_num' + ] + ) + ], + ] + ); + } + + /** + * Run the migrations + * + * @return void + */ + public function up() + { + $this->batchInsert('test_insert_delete', [ + 'id', + 'login_type_id', + 'username', + 'name', + 'email_addr', + 'password', + 'phone_num', + 'token_id' + ] + ); + } + + /** + * Reverse the migrations + * + * @return void + */ + public function down() + { + $this->batchDelete('test_insert_delete'); + } + +} diff --git a/tests/_data/console/app/test_insert_delete/migrations/1.0.1/test_insert_delete.dat b/tests/_data/console/app/test_insert_delete/migrations/1.0.1/test_insert_delete.dat new file mode 100644 index 000000000..323c33f55 --- /dev/null +++ b/tests/_data/console/app/test_insert_delete/migrations/1.0.1/test_insert_delete.dat @@ -0,0 +1 @@ +1,1,superadmin1,"First name Last name",email@test.com,,1,NULL diff --git a/tests/_data/console/app/test_insert_delete/migrations/1.0.1/test_insert_delete.php b/tests/_data/console/app/test_insert_delete/migrations/1.0.1/test_insert_delete.php new file mode 100644 index 000000000..d0d53ef22 --- /dev/null +++ b/tests/_data/console/app/test_insert_delete/migrations/1.0.1/test_insert_delete.php @@ -0,0 +1,124 @@ +morphTable('test_insert_delete', [ + 'columns' => [ + new Column( + 'id', + [ + 'type' => Column::TYPE_INTEGER, + 'notNull' => true, + 'autoIncrement' => true, + 'first' => true + ] + ), + new Column( + 'login_type_id', + [ + 'type' => Column::TYPE_INTEGER, + 'notNull' => true, + 'after' => 'id' + ] + ), + new Column( + 'username', + [ + 'type' => Column::TYPE_VARCHAR, + 'notNull' => true, + 'size' => 255, + 'after' => 'login_type_id' + ] + ), + new Column( + 'name', + [ + 'type' => Column::TYPE_VARCHAR, + 'notNull' => true, + 'size' => 255, + 'after' => 'username' + ] + ), + new Column( + 'email_addr', + [ + 'type' => Column::TYPE_VARCHAR, + 'notNull' => true, + 'size' => 255, + 'after' => 'name' + ] + ), + new Column( + 'password', + [ + 'type' => Column::TYPE_VARCHAR, + 'notNull' => true, + 'size' => 255, + 'after' => 'email_addr' + ] + ), + new Column( + 'phone_num', + [ + 'type' => Column::TYPE_BIGINTEGER, + 'after' => 'password' + ] + ), + new Column( + 'token_id', + [ + 'type' => Column::TYPE_BIGINTEGER, + 'after' => 'phone_num' + ] + ) + ], + ] + ); + } + + /** + * Run the migrations + * + * @return void + */ + public function up() + { + $this->batchInsert('test_insert_delete', [ + 'id', + 'login_type_id', + 'username', + 'name', + 'email_addr', + 'password', + 'phone_num', + 'token_id' + ] + ); + } + + /** + * Reverse the migrations + * + * @return void + */ + public function down() + { + $this->batchDelete('test_insert_delete'); + } + +} diff --git a/tests/_data/console/app/test_insert_delete/migrations/1.0.2/test_insert_delete.dat b/tests/_data/console/app/test_insert_delete/migrations/1.0.2/test_insert_delete.dat new file mode 100644 index 000000000..26e61f005 --- /dev/null +++ b/tests/_data/console/app/test_insert_delete/migrations/1.0.2/test_insert_delete.dat @@ -0,0 +1,2 @@ +1,1,superadmin1,"First name Last name",email@test.com,,1,NULL +2,2,superadmin2,"First name Last name",email@test.com,,NULL,NULL diff --git a/tests/_data/console/app/test_insert_delete/migrations/1.0.2/test_insert_delete.php b/tests/_data/console/app/test_insert_delete/migrations/1.0.2/test_insert_delete.php new file mode 100644 index 000000000..4411eaba6 --- /dev/null +++ b/tests/_data/console/app/test_insert_delete/migrations/1.0.2/test_insert_delete.php @@ -0,0 +1,124 @@ +morphTable('test_insert_delete', [ + 'columns' => [ + new Column( + 'id', + [ + 'type' => Column::TYPE_INTEGER, + 'notNull' => true, + 'autoIncrement' => true, + 'first' => true + ] + ), + new Column( + 'login_type_id', + [ + 'type' => Column::TYPE_INTEGER, + 'notNull' => true, + 'after' => 'id' + ] + ), + new Column( + 'username', + [ + 'type' => Column::TYPE_VARCHAR, + 'notNull' => true, + 'size' => 255, + 'after' => 'login_type_id' + ] + ), + new Column( + 'name', + [ + 'type' => Column::TYPE_VARCHAR, + 'notNull' => true, + 'size' => 255, + 'after' => 'username' + ] + ), + new Column( + 'email_addr', + [ + 'type' => Column::TYPE_VARCHAR, + 'notNull' => true, + 'size' => 255, + 'after' => 'name' + ] + ), + new Column( + 'password', + [ + 'type' => Column::TYPE_VARCHAR, + 'notNull' => true, + 'size' => 255, + 'after' => 'email_addr' + ] + ), + new Column( + 'phone_num', + [ + 'type' => Column::TYPE_BIGINTEGER, + 'after' => 'password' + ] + ), + new Column( + 'token_id', + [ + 'type' => Column::TYPE_BIGINTEGER, + 'after' => 'phone_num' + ] + ) + ], + ] + ); + } + + /** + * Run the migrations + * + * @return void + */ + public function up() + { + $this->batchInsert('test_insert_delete', [ + 'id', + 'login_type_id', + 'username', + 'name', + 'email_addr', + 'password', + 'phone_num', + 'token_id' + ] + ); + } + + /** + * Reverse the migrations + * + * @return void + */ + public function down() + { + $this->batchDelete('test_insert_delete'); + } + +} diff --git a/tests/_data/console/app/test_many_running/migrations/1.0.0/test_many_running.php b/tests/_data/console/app/test_many_running/migrations/1.0.0/test_many_running.php new file mode 100644 index 000000000..6aa589b56 --- /dev/null +++ b/tests/_data/console/app/test_many_running/migrations/1.0.0/test_many_running.php @@ -0,0 +1,94 @@ +morphTable('test_many_running', [ + 'columns' => [ + new Column( + 'id', + [ + 'type' => Column::TYPE_INTEGER, + 'unsigned' => true, + 'notNull' => true, + 'autoIncrement' => true, + 'size' => 10, + 'first' => true + ] + ), + new Column( + 'name', + [ + 'type' => Column::TYPE_VARCHAR, + 'notNull' => true, + 'size' => 45, + 'after' => 'id' + ] + ), + new Column( + 'created_at', + [ + 'type' => Column::TYPE_DATETIME, + 'notNull' => true, + 'size' => 1, + 'after' => 'name' + ] + ), + new Column( + 'active', + [ + 'type' => Column::TYPE_INTEGER, + 'notNull' => true, + 'size' => 1, + 'after' => 'created_at' + ] + ) + ], + 'indexes' => [ + new Index('PRIMARY', ['id'], 'PRIMARY') + ], + 'options' => [ + 'TABLE_TYPE' => 'BASE TABLE', + 'AUTO_INCREMENT' => '1', + 'ENGINE' => 'InnoDB', + 'TABLE_COLLATION' => 'utf8_general_ci' + ], + ] + ); + } + + /** + * Run the migrations + * + * @return void + */ + public function up() + { + + } + + /** + * Reverse the migrations + * + * @return void + */ + public function down() + { + + } + +} diff --git a/tests/_data/console/app/test_many_running/migrations/1.0.1/test_many_running.php b/tests/_data/console/app/test_many_running/migrations/1.0.1/test_many_running.php new file mode 100644 index 000000000..2138630a7 --- /dev/null +++ b/tests/_data/console/app/test_many_running/migrations/1.0.1/test_many_running.php @@ -0,0 +1,94 @@ +morphTable('test_many_running', [ + 'columns' => [ + new Column( + 'id', + [ + 'type' => Column::TYPE_INTEGER, + 'unsigned' => true, + 'notNull' => true, + 'autoIncrement' => true, + 'size' => 10, + 'first' => true + ] + ), + new Column( + 'name', + [ + 'type' => Column::TYPE_VARCHAR, + 'notNull' => true, + 'size' => 45, + 'after' => 'id' + ] + ), + new Column( + 'created_at', + [ + 'type' => Column::TYPE_DATETIME, + 'notNull' => true, + 'size' => 1, + 'after' => 'name' + ] + ), + new Column( + 'active', + [ + 'type' => Column::TYPE_INTEGER, + 'notNull' => true, + 'size' => 1, + 'after' => 'created_at' + ] + ) + ], + 'indexes' => [ + new Index('PRIMARY', ['id'], 'PRIMARY') + ], + 'options' => [ + 'TABLE_TYPE' => 'BASE TABLE', + 'AUTO_INCREMENT' => '1', + 'ENGINE' => 'InnoDB', + 'TABLE_COLLATION' => 'utf8_general_ci' + ], + ] + ); + } + + /** + * Run the migrations + * + * @return void + */ + public function up() + { + + } + + /** + * Reverse the migrations + * + * @return void + */ + public function down() + { + + } + +} diff --git a/tests/_data/console/app/test_many_running/migrations/1.0.10/test_many_running.php b/tests/_data/console/app/test_many_running/migrations/1.0.10/test_many_running.php new file mode 100644 index 000000000..7160469d1 --- /dev/null +++ b/tests/_data/console/app/test_many_running/migrations/1.0.10/test_many_running.php @@ -0,0 +1,94 @@ +morphTable('test_many_running', [ + 'columns' => [ + new Column( + 'id', + [ + 'type' => Column::TYPE_INTEGER, + 'unsigned' => true, + 'notNull' => true, + 'autoIncrement' => true, + 'size' => 10, + 'first' => true + ] + ), + new Column( + 'name', + [ + 'type' => Column::TYPE_VARCHAR, + 'notNull' => true, + 'size' => 45, + 'after' => 'id' + ] + ), + new Column( + 'created_at', + [ + 'type' => Column::TYPE_DATETIME, + 'notNull' => true, + 'size' => 1, + 'after' => 'name' + ] + ), + new Column( + 'active', + [ + 'type' => Column::TYPE_INTEGER, + 'notNull' => true, + 'size' => 1, + 'after' => 'created_at' + ] + ) + ], + 'indexes' => [ + new Index('PRIMARY', ['id'], 'PRIMARY') + ], + 'options' => [ + 'TABLE_TYPE' => 'BASE TABLE', + 'AUTO_INCREMENT' => '1', + 'ENGINE' => 'InnoDB', + 'TABLE_COLLATION' => 'utf8_general_ci' + ], + ] + ); + } + + /** + * Run the migrations + * + * @return void + */ + public function up() + { + + } + + /** + * Reverse the migrations + * + * @return void + */ + public function down() + { + + } + +} diff --git a/tests/_data/console/app/test_many_running/migrations/1.0.11/test_many_running.php b/tests/_data/console/app/test_many_running/migrations/1.0.11/test_many_running.php new file mode 100644 index 000000000..c3c15ba49 --- /dev/null +++ b/tests/_data/console/app/test_many_running/migrations/1.0.11/test_many_running.php @@ -0,0 +1,94 @@ +morphTable('test_many_running', [ + 'columns' => [ + new Column( + 'id', + [ + 'type' => Column::TYPE_INTEGER, + 'unsigned' => true, + 'notNull' => true, + 'autoIncrement' => true, + 'size' => 10, + 'first' => true + ] + ), + new Column( + 'name', + [ + 'type' => Column::TYPE_VARCHAR, + 'notNull' => true, + 'size' => 45, + 'after' => 'id' + ] + ), + new Column( + 'created_at', + [ + 'type' => Column::TYPE_DATETIME, + 'notNull' => true, + 'size' => 1, + 'after' => 'name' + ] + ), + new Column( + 'active', + [ + 'type' => Column::TYPE_INTEGER, + 'notNull' => true, + 'size' => 1, + 'after' => 'created_at' + ] + ) + ], + 'indexes' => [ + new Index('PRIMARY', ['id'], 'PRIMARY') + ], + 'options' => [ + 'TABLE_TYPE' => 'BASE TABLE', + 'AUTO_INCREMENT' => '1', + 'ENGINE' => 'InnoDB', + 'TABLE_COLLATION' => 'utf8_general_ci' + ], + ] + ); + } + + /** + * Run the migrations + * + * @return void + */ + public function up() + { + + } + + /** + * Reverse the migrations + * + * @return void + */ + public function down() + { + + } + +} diff --git a/tests/_data/console/app/test_many_running/migrations/1.0.12/test_many_running.php b/tests/_data/console/app/test_many_running/migrations/1.0.12/test_many_running.php new file mode 100644 index 000000000..5f8789969 --- /dev/null +++ b/tests/_data/console/app/test_many_running/migrations/1.0.12/test_many_running.php @@ -0,0 +1,94 @@ +morphTable('test_many_running', [ + 'columns' => [ + new Column( + 'id', + [ + 'type' => Column::TYPE_INTEGER, + 'unsigned' => true, + 'notNull' => true, + 'autoIncrement' => true, + 'size' => 10, + 'first' => true + ] + ), + new Column( + 'name', + [ + 'type' => Column::TYPE_VARCHAR, + 'notNull' => true, + 'size' => 45, + 'after' => 'id' + ] + ), + new Column( + 'created_at', + [ + 'type' => Column::TYPE_DATETIME, + 'notNull' => true, + 'size' => 1, + 'after' => 'name' + ] + ), + new Column( + 'active', + [ + 'type' => Column::TYPE_INTEGER, + 'notNull' => true, + 'size' => 1, + 'after' => 'created_at' + ] + ) + ], + 'indexes' => [ + new Index('PRIMARY', ['id'], 'PRIMARY') + ], + 'options' => [ + 'TABLE_TYPE' => 'BASE TABLE', + 'AUTO_INCREMENT' => '1', + 'ENGINE' => 'InnoDB', + 'TABLE_COLLATION' => 'utf8_general_ci' + ], + ] + ); + } + + /** + * Run the migrations + * + * @return void + */ + public function up() + { + + } + + /** + * Reverse the migrations + * + * @return void + */ + public function down() + { + + } + +} diff --git a/tests/_data/console/app/test_many_running/migrations/1.0.2/test_many_running.php b/tests/_data/console/app/test_many_running/migrations/1.0.2/test_many_running.php new file mode 100644 index 000000000..510ef22fa --- /dev/null +++ b/tests/_data/console/app/test_many_running/migrations/1.0.2/test_many_running.php @@ -0,0 +1,94 @@ +morphTable('test_many_running', [ + 'columns' => [ + new Column( + 'id', + [ + 'type' => Column::TYPE_INTEGER, + 'unsigned' => true, + 'notNull' => true, + 'autoIncrement' => true, + 'size' => 10, + 'first' => true + ] + ), + new Column( + 'name', + [ + 'type' => Column::TYPE_VARCHAR, + 'notNull' => true, + 'size' => 45, + 'after' => 'id' + ] + ), + new Column( + 'created_at', + [ + 'type' => Column::TYPE_DATETIME, + 'notNull' => true, + 'size' => 1, + 'after' => 'name' + ] + ), + new Column( + 'active', + [ + 'type' => Column::TYPE_INTEGER, + 'notNull' => true, + 'size' => 1, + 'after' => 'created_at' + ] + ) + ], + 'indexes' => [ + new Index('PRIMARY', ['id'], 'PRIMARY') + ], + 'options' => [ + 'TABLE_TYPE' => 'BASE TABLE', + 'AUTO_INCREMENT' => '1', + 'ENGINE' => 'InnoDB', + 'TABLE_COLLATION' => 'utf8_general_ci' + ], + ] + ); + } + + /** + * Run the migrations + * + * @return void + */ + public function up() + { + + } + + /** + * Reverse the migrations + * + * @return void + */ + public function down() + { + + } + +} diff --git a/tests/_data/console/app/test_many_running/migrations/1.0.3/test_many_running.php b/tests/_data/console/app/test_many_running/migrations/1.0.3/test_many_running.php new file mode 100644 index 000000000..113c922f9 --- /dev/null +++ b/tests/_data/console/app/test_many_running/migrations/1.0.3/test_many_running.php @@ -0,0 +1,94 @@ +morphTable('test_many_running', [ + 'columns' => [ + new Column( + 'id', + [ + 'type' => Column::TYPE_INTEGER, + 'unsigned' => true, + 'notNull' => true, + 'autoIncrement' => true, + 'size' => 10, + 'first' => true + ] + ), + new Column( + 'name', + [ + 'type' => Column::TYPE_VARCHAR, + 'notNull' => true, + 'size' => 45, + 'after' => 'id' + ] + ), + new Column( + 'created_at', + [ + 'type' => Column::TYPE_DATETIME, + 'notNull' => true, + 'size' => 1, + 'after' => 'name' + ] + ), + new Column( + 'active', + [ + 'type' => Column::TYPE_INTEGER, + 'notNull' => true, + 'size' => 1, + 'after' => 'created_at' + ] + ) + ], + 'indexes' => [ + new Index('PRIMARY', ['id'], 'PRIMARY') + ], + 'options' => [ + 'TABLE_TYPE' => 'BASE TABLE', + 'AUTO_INCREMENT' => '1', + 'ENGINE' => 'InnoDB', + 'TABLE_COLLATION' => 'utf8_general_ci' + ], + ] + ); + } + + /** + * Run the migrations + * + * @return void + */ + public function up() + { + + } + + /** + * Reverse the migrations + * + * @return void + */ + public function down() + { + + } + +} diff --git a/tests/_data/console/app/test_many_running/migrations/1.0.4/test_many_running.php b/tests/_data/console/app/test_many_running/migrations/1.0.4/test_many_running.php new file mode 100644 index 000000000..6c1d4fb19 --- /dev/null +++ b/tests/_data/console/app/test_many_running/migrations/1.0.4/test_many_running.php @@ -0,0 +1,94 @@ +morphTable('test_many_running', [ + 'columns' => [ + new Column( + 'id', + [ + 'type' => Column::TYPE_INTEGER, + 'unsigned' => true, + 'notNull' => true, + 'autoIncrement' => true, + 'size' => 10, + 'first' => true + ] + ), + new Column( + 'name', + [ + 'type' => Column::TYPE_VARCHAR, + 'notNull' => true, + 'size' => 45, + 'after' => 'id' + ] + ), + new Column( + 'created_at', + [ + 'type' => Column::TYPE_DATETIME, + 'notNull' => true, + 'size' => 1, + 'after' => 'name' + ] + ), + new Column( + 'active', + [ + 'type' => Column::TYPE_INTEGER, + 'notNull' => true, + 'size' => 1, + 'after' => 'created_at' + ] + ) + ], + 'indexes' => [ + new Index('PRIMARY', ['id'], 'PRIMARY') + ], + 'options' => [ + 'TABLE_TYPE' => 'BASE TABLE', + 'AUTO_INCREMENT' => '1', + 'ENGINE' => 'InnoDB', + 'TABLE_COLLATION' => 'utf8_general_ci' + ], + ] + ); + } + + /** + * Run the migrations + * + * @return void + */ + public function up() + { + + } + + /** + * Reverse the migrations + * + * @return void + */ + public function down() + { + + } + +} diff --git a/tests/_data/console/app/test_many_running/migrations/1.0.5/test_many_running.php b/tests/_data/console/app/test_many_running/migrations/1.0.5/test_many_running.php new file mode 100644 index 000000000..8c9d3a456 --- /dev/null +++ b/tests/_data/console/app/test_many_running/migrations/1.0.5/test_many_running.php @@ -0,0 +1,94 @@ +morphTable('test_many_running', [ + 'columns' => [ + new Column( + 'id', + [ + 'type' => Column::TYPE_INTEGER, + 'unsigned' => true, + 'notNull' => true, + 'autoIncrement' => true, + 'size' => 10, + 'first' => true + ] + ), + new Column( + 'name', + [ + 'type' => Column::TYPE_VARCHAR, + 'notNull' => true, + 'size' => 45, + 'after' => 'id' + ] + ), + new Column( + 'created_at', + [ + 'type' => Column::TYPE_DATETIME, + 'notNull' => true, + 'size' => 1, + 'after' => 'name' + ] + ), + new Column( + 'active', + [ + 'type' => Column::TYPE_INTEGER, + 'notNull' => true, + 'size' => 1, + 'after' => 'created_at' + ] + ) + ], + 'indexes' => [ + new Index('PRIMARY', ['id'], 'PRIMARY') + ], + 'options' => [ + 'TABLE_TYPE' => 'BASE TABLE', + 'AUTO_INCREMENT' => '1', + 'ENGINE' => 'InnoDB', + 'TABLE_COLLATION' => 'utf8_general_ci' + ], + ] + ); + } + + /** + * Run the migrations + * + * @return void + */ + public function up() + { + + } + + /** + * Reverse the migrations + * + * @return void + */ + public function down() + { + + } + +} diff --git a/tests/_data/console/app/test_many_running/migrations/1.0.6/test_many_running.php b/tests/_data/console/app/test_many_running/migrations/1.0.6/test_many_running.php new file mode 100644 index 000000000..425ae9b9b --- /dev/null +++ b/tests/_data/console/app/test_many_running/migrations/1.0.6/test_many_running.php @@ -0,0 +1,94 @@ +morphTable('test_many_running', [ + 'columns' => [ + new Column( + 'id', + [ + 'type' => Column::TYPE_INTEGER, + 'unsigned' => true, + 'notNull' => true, + 'autoIncrement' => true, + 'size' => 10, + 'first' => true + ] + ), + new Column( + 'name', + [ + 'type' => Column::TYPE_VARCHAR, + 'notNull' => true, + 'size' => 45, + 'after' => 'id' + ] + ), + new Column( + 'created_at', + [ + 'type' => Column::TYPE_DATETIME, + 'notNull' => true, + 'size' => 1, + 'after' => 'name' + ] + ), + new Column( + 'active', + [ + 'type' => Column::TYPE_INTEGER, + 'notNull' => true, + 'size' => 1, + 'after' => 'created_at' + ] + ) + ], + 'indexes' => [ + new Index('PRIMARY', ['id'], 'PRIMARY') + ], + 'options' => [ + 'TABLE_TYPE' => 'BASE TABLE', + 'AUTO_INCREMENT' => '1', + 'ENGINE' => 'InnoDB', + 'TABLE_COLLATION' => 'utf8_general_ci' + ], + ] + ); + } + + /** + * Run the migrations + * + * @return void + */ + public function up() + { + + } + + /** + * Reverse the migrations + * + * @return void + */ + public function down() + { + + } + +} diff --git a/tests/_data/console/app/test_many_running/migrations/1.0.7/test_many_running.php b/tests/_data/console/app/test_many_running/migrations/1.0.7/test_many_running.php new file mode 100644 index 000000000..98a7c9eb5 --- /dev/null +++ b/tests/_data/console/app/test_many_running/migrations/1.0.7/test_many_running.php @@ -0,0 +1,94 @@ +morphTable('test_many_running', [ + 'columns' => [ + new Column( + 'id', + [ + 'type' => Column::TYPE_INTEGER, + 'unsigned' => true, + 'notNull' => true, + 'autoIncrement' => true, + 'size' => 10, + 'first' => true + ] + ), + new Column( + 'name', + [ + 'type' => Column::TYPE_VARCHAR, + 'notNull' => true, + 'size' => 45, + 'after' => 'id' + ] + ), + new Column( + 'created_at', + [ + 'type' => Column::TYPE_DATETIME, + 'notNull' => true, + 'size' => 1, + 'after' => 'name' + ] + ), + new Column( + 'active', + [ + 'type' => Column::TYPE_INTEGER, + 'notNull' => true, + 'size' => 1, + 'after' => 'created_at' + ] + ) + ], + 'indexes' => [ + new Index('PRIMARY', ['id'], 'PRIMARY') + ], + 'options' => [ + 'TABLE_TYPE' => 'BASE TABLE', + 'AUTO_INCREMENT' => '1', + 'ENGINE' => 'InnoDB', + 'TABLE_COLLATION' => 'utf8_general_ci' + ], + ] + ); + } + + /** + * Run the migrations + * + * @return void + */ + public function up() + { + + } + + /** + * Reverse the migrations + * + * @return void + */ + public function down() + { + + } + +} diff --git a/tests/_data/console/app/test_many_running/migrations/1.0.8/test_many_running.php b/tests/_data/console/app/test_many_running/migrations/1.0.8/test_many_running.php new file mode 100644 index 000000000..12f966440 --- /dev/null +++ b/tests/_data/console/app/test_many_running/migrations/1.0.8/test_many_running.php @@ -0,0 +1,94 @@ +morphTable('test_many_running', [ + 'columns' => [ + new Column( + 'id', + [ + 'type' => Column::TYPE_INTEGER, + 'unsigned' => true, + 'notNull' => true, + 'autoIncrement' => true, + 'size' => 10, + 'first' => true + ] + ), + new Column( + 'name', + [ + 'type' => Column::TYPE_VARCHAR, + 'notNull' => true, + 'size' => 45, + 'after' => 'id' + ] + ), + new Column( + 'created_at', + [ + 'type' => Column::TYPE_DATETIME, + 'notNull' => true, + 'size' => 1, + 'after' => 'name' + ] + ), + new Column( + 'active', + [ + 'type' => Column::TYPE_INTEGER, + 'notNull' => true, + 'size' => 1, + 'after' => 'created_at' + ] + ) + ], + 'indexes' => [ + new Index('PRIMARY', ['id'], 'PRIMARY') + ], + 'options' => [ + 'TABLE_TYPE' => 'BASE TABLE', + 'AUTO_INCREMENT' => '1', + 'ENGINE' => 'InnoDB', + 'TABLE_COLLATION' => 'utf8_general_ci' + ], + ] + ); + } + + /** + * Run the migrations + * + * @return void + */ + public function up() + { + + } + + /** + * Reverse the migrations + * + * @return void + */ + public function down() + { + + } + +} diff --git a/tests/_data/console/app/test_many_running/migrations/1.0.9/test_many_running.php b/tests/_data/console/app/test_many_running/migrations/1.0.9/test_many_running.php new file mode 100644 index 000000000..ea1bcb18f --- /dev/null +++ b/tests/_data/console/app/test_many_running/migrations/1.0.9/test_many_running.php @@ -0,0 +1,94 @@ +morphTable('test_many_running', [ + 'columns' => [ + new Column( + 'id', + [ + 'type' => Column::TYPE_INTEGER, + 'unsigned' => true, + 'notNull' => true, + 'autoIncrement' => true, + 'size' => 10, + 'first' => true + ] + ), + new Column( + 'name', + [ + 'type' => Column::TYPE_VARCHAR, + 'notNull' => true, + 'size' => 45, + 'after' => 'id' + ] + ), + new Column( + 'created_at', + [ + 'type' => Column::TYPE_DATETIME, + 'notNull' => true, + 'size' => 1, + 'after' => 'name' + ] + ), + new Column( + 'active', + [ + 'type' => Column::TYPE_INTEGER, + 'notNull' => true, + 'size' => 1, + 'after' => 'created_at' + ] + ) + ], + 'indexes' => [ + new Index('PRIMARY', ['id'], 'PRIMARY') + ], + 'options' => [ + 'TABLE_TYPE' => 'BASE TABLE', + 'AUTO_INCREMENT' => '1', + 'ENGINE' => 'InnoDB', + 'TABLE_COLLATION' => 'utf8_general_ci' + ], + ] + ); + } + + /** + * Run the migrations + * + * @return void + */ + public function up() + { + + } + + /** + * Reverse the migrations + * + * @return void + */ + public function down() + { + + } + +} diff --git a/tests/_data/schemas/mysql/dump.sql b/tests/_data/schemas/mysql/dump.sql index 1631e851d..320921e4e 100644 --- a/tests/_data/schemas/mysql/dump.sql +++ b/tests/_data/schemas/mysql/dump.sql @@ -24,3 +24,12 @@ INSERT into `test_migrations` SET `active` = 1, `created_at` = NOW(), `updated_at` = NOW(); + +DROP TABLE IF EXISTS `test_many_running`; +CREATE TABLE `test_many_running` ( + `id` int(10) unsigned NOT NULL AUTO_INCREMENT, + `name` varchar(45) NOT NULL, + `created_at` datetime NOT NULL, + `active` tinyint(1) NOT NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; diff --git a/tests/_data/schemas/postgresql/dump.sql b/tests/_data/schemas/postgresql/dump.sql index ee069266f..966a814e8 100644 --- a/tests/_data/schemas/postgresql/dump.sql +++ b/tests/_data/schemas/postgresql/dump.sql @@ -30,7 +30,7 @@ SET default_with_oids = false; CREATE USER phalcon_user WITH PASSWORD '1234'; -GRANT ALL PRIVILEGES ON DATABASE devtools TO devtools; +GRANT ALL PRIVILEGES ON DATABASE devtools TO phalcon_user; -- -- Tables for testing describeReferences() @@ -56,3 +56,25 @@ CREATE TABLE foreign_key_child ( ); ALTER TABLE public.foreign_key_child OWNER TO postgres; + +-- +-- Table for testing generating migrations and methods batchInsert(), batchDelete() +-- +-- Table: test_insert_delete +CREATE TABLE test_insert_delete ( + id SERIAL, + login_type_id INTEGER NOT NULL, + username VARCHAR(255) NOT NULL, + name VARCHAR(255) NOT NULL, + email_addr VARCHAR(255) NOT NULL, + password VARCHAR(255) NOT NULL, + phone_num BIGINT NULL, + token_id BIGINT NULL, + PRIMARY KEY (id), + UNIQUE (username) +); + +ALTER TABLE public.test_insert_delete OWNER TO postgres; + +INSERT INTO public.test_insert_delete VALUES + (3, 3, 'superadmin3', 'First name Last name', 'email@test.com', '', NULL, null); diff --git a/tests/_support/Helper/Utils/NullifyTrait.php b/tests/_support/Helper/Utils/NullifyTrait.php new file mode 100644 index 000000000..db80a86ff --- /dev/null +++ b/tests/_support/Helper/Utils/NullifyTrait.php @@ -0,0 +1,40 @@ + + * @package Helper\Utils + * + * The contents of this file are subject to the New BSD License that is + * bundled with this package in the file LICENSE.txt + * + * If you did not receive a copy of the license and are unable to obtain it + * through the world-wide-web, please send an email to license@phalconphp.com + * so that we can send you a copy immediately. + */ + +trait NullifyTrait +{ + protected function getInvokeData() + { + return [ + 0 => [1, 'test' , 'NULL', null], + 1 => ['foo', 'bar', 'null', NULL], + 2 => [1, 'foo', 'Null'] + ]; + } + + protected function getInvokeExpected () + { + return [ + 0 => [1, 'test', null, null], + 1 => ['foo', 'bar', null, null], + 2 => [1, 'foo', null] + ]; + } +} diff --git a/tests/_support/helpers.php b/tests/_support/helpers.php index e1ba7cdd3..23caa2462 100755 --- a/tests/_support/helpers.php +++ b/tests/_support/helpers.php @@ -45,3 +45,14 @@ function tests_path($path = '') return dirname(__DIR__) . DIRECTORY_SEPARATOR . ($path ? DIRECTORY_SEPARATOR . $path : $path); } } + +if (!function_exists('env')) { + function env($key, $default = null) + { + if (defined($key)) { + return constant($key); + } + + return getenv($key) ?: $default; + } +} diff --git a/tests/console.suite.yml b/tests/console.suite.yml index cab84060d..8106b097b 100755 --- a/tests/console.suite.yml +++ b/tests/console.suite.yml @@ -10,3 +10,5 @@ modules: - Filesystem - Asserts - Cli + Phalcon: + bootstrap: 'tests/_bootstrap.php' diff --git a/tests/console/GenerateMysqlMigrationCept.php b/tests/console/GenerateMysqlMigrationCept.php index 3ca5a89b9..6ce1d52c7 100644 --- a/tests/console/GenerateMysqlMigrationCept.php +++ b/tests/console/GenerateMysqlMigrationCept.php @@ -14,6 +14,7 @@ $I->amInPath(dirname(app_path())); +$I->deleteDir(app_path('migrations/1.0.2/')); $I->dontSeeFileFound(app_path('migrations/1.0.2/test_migrations.php')); $I->dontSeeFileFound(app_path('migrations/1.0.2/test_migrations.dat')); diff --git a/tests/console/GeneratePostgresqlMigrationCept.php b/tests/console/GeneratePostgresqlMigrationCept.php new file mode 100644 index 000000000..3d5e8c54f --- /dev/null +++ b/tests/console/GeneratePostgresqlMigrationCept.php @@ -0,0 +1,26 @@ +wantToTest('Generating migration for Postgresql database'); + +$output=<<amInPath(dirname(app_path())); + +$I->deleteDir(app_path('test_insert_delete/migrations/1.0.3/')); +$I->dontSeeFileFound(app_path('test_insert_delete/migrations/1.0.3/test_insert_delete.php')); +$I->dontSeeFileFound(app_path('test_insert_delete/migrations/1.0.3/test_insert_delete.dat')); + +$I->runShellCommand('phalcon migration --action=generate --migrations=app/test_insert_delete/migrations --table=test_insert_delete --data=always --config=app/postgresql/config.php'); + +$I->seeInShellOutput($output); + +$I->seeFileFound(app_path('test_insert_delete/migrations/1.0.3/test_insert_delete.php')); +$I->seeFileFound(app_path('test_insert_delete/migrations/1.0.3/test_insert_delete.dat')); diff --git a/tests/console/RunMigrationCept.php b/tests/console/RunMigrationCept.php new file mode 100644 index 000000000..2b2c1f159 --- /dev/null +++ b/tests/console/RunMigrationCept.php @@ -0,0 +1,23 @@ +wantToTest('Running migration using migration file from .phalcon folder'); + +$I->amInPath(dirname(app_path())); +$I->cleanDir(tests_path('_data/console/.phalcon')); + +$I->runShellCommand('phalcon migration --action=run --config=app/mysql/config.php --migrations=app/test_many_running/migrations --table=test_many_running'); +$I->seeInShellOutput('Success: Version 1.0.12 was successfully migrated'); + +$I->runShellCommand('phalcon migration --action=run --config=app/mysql/config.php --migrations=app/test_many_running/migrations --table=test_many_running --version=1.0.9'); +$I->seeInShellOutput('Success: Version 1.0.10 was successfully rolled back'); + +$I->runShellCommand('phalcon migration --action=run --config=app/mysql/config.php --migrations=app/test_many_running/migrations --table=test_many_running --version=1.0.12'); +$I->seeInShellOutput('Success: Version 1.0.12 was successfully migrated'); + +$I->cleanDir(tests_path('_data/console/.phalcon')); diff --git a/tests/console/RunMysqlMigrationCept.php b/tests/console/RunMysqlMigrationCept.php index b41af1b2a..ebd3221bc 100644 --- a/tests/console/RunMysqlMigrationCept.php +++ b/tests/console/RunMysqlMigrationCept.php @@ -8,11 +8,8 @@ $I->wantToTest('Running migration for MySQL database'); -//$output=<<amInPath(dirname(app_path())); +$I->cleanDir(tests_path('_data/console/.phalcon')); $I->seeFileFound(app_path('migrations/1.0.2/test_migrations.php')); $I->seeFileFound(app_path('migrations/1.0.2/test_migrations.dat')); @@ -31,8 +28,10 @@ $I->runShellCommand('phalcon migration --action=run --version=1.0.2'); $I->seeInShellOutput('Success: Version 1.0.2 was successfully migrated'); +$I->runShellCommand('phalcon migration --action=run --version=1.0.0'); $I->deleteDir(app_path('migrations/1.0.2/')); $I->dontSeeFileFound(app_path('migrations/1.0.2/test_migrations.php')); $I->dontSeeFileFound(app_path('migrations/1.0.2/test_migrations.dat')); +$I->cleanDir(tests_path('_data/console/.phalcon')); diff --git a/tests/console/RunPostgresqlMigrationCept.php b/tests/console/RunPostgresqlMigrationCept.php new file mode 100644 index 000000000..3e6501b7e --- /dev/null +++ b/tests/console/RunPostgresqlMigrationCept.php @@ -0,0 +1,34 @@ +wantToTest('Running migration for Posqgresql database'); + +$I->amInPath(dirname(app_path())); +$I->cleanDir(tests_path('_data/console/.phalcon')); + +$I->seeFileFound(app_path('test_insert_delete/migrations/1.0.3/test_insert_delete.php')); +$I->seeFileFound(app_path('test_insert_delete/migrations/1.0.3/test_insert_delete.dat')); + +$I->runShellCommand('phalcon migration --action=run --config=app/postgresql/config.php --migrations=app/test_insert_delete/migrations/ --version=1.0.0'); + +$I->runShellCommand('phalcon migration --action=run --config=app/postgresql/config.php --migrations=app/test_insert_delete/migrations/ --version=1.0.1'); +$I->seeInShellOutput('Success: Version 1.0.1 was successfully migrated'); + +$I->runShellCommand('phalcon migration --action=run --config=app/postgresql/config.php --migrations=app/test_insert_delete/migrations/ --version=1.0.2'); +$I->seeInShellOutput('Success: Version 1.0.2 was successfully migrated'); + +$I->runShellCommand('phalcon migration --action=run --config=app/postgresql/config.php --migrations=app/test_insert_delete/migrations/ --version=1.0.3'); +$I->seeInShellOutput('Success: Version 1.0.3 was successfully migrated'); + +$I->runShellCommand('phalcon migration --action=run --config=app/postgresql/config.php --migrations=app/test_insert_delete/migrations/ --version=1.0.0'); +$I->seeInShellOutput('Success: Version 1.0.3 was successfully rolled back'); + +$I->deleteDir(app_path('test_insert_delete/migrations/1.0.3/')); +$I->dontSeeFileFound(app_path('test_insert_delete/migrations/1.0.3/test_insert_delete.php')); +$I->dontSeeFileFound(app_path('test_insert_delete/migrations/1.0.3/test_insert_delete.dat')); +$I->cleanDir(tests_path('_data/console/.phalcon')); diff --git a/tests/unit/Utils/NullifyTest.php b/tests/unit/Utils/NullifyTest.php new file mode 100644 index 000000000..f01184858 --- /dev/null +++ b/tests/unit/Utils/NullifyTest.php @@ -0,0 +1,55 @@ + | + +------------------------------------------------------------------------+ +*/ + +class NullifyTest extends UnitTest +{ + use NullifyTrait; + + /** + * Tests Nullify::__invoke + * + * @test + * @issue 988 + * @author Sergii Svyrydenko + * @since 2017-09-06 + */ + public function shouldTestInvoke() + { + $this->specify( + '', + function ($data, $expected) { + $nullify = new Nullify(); + foreach ($data as $key => $value) { + expect($nullify($value))->equals($expected[$key]); + } + }, + [ + 'examples' => [ + [$this->getInvokeData(), $this->getInvokeExpected()] + ] + ] + ); + } +}