From 52b19274f9de79d9989ffefd95bf37a432176019 Mon Sep 17 00:00:00 2001 From: Serghei Iakovlev Date: Thu, 29 Sep 2016 00:34:47 +0300 Subject: [PATCH] Fixed Column::hasDefault --- CHANGELOG.md | 2 +- phalcon/db/column.zep | 6 +- tests/README.md | 28 +++++++- .../_data/schemas/postgresql/phalcon_test.sql | 12 ++++ tests/unit/Db/Adapter/Pdo/PostgresqlTest.php | 70 +++++++++++++++++-- tests/unit/Db/Column/PostgresqlTest.php | 63 +++++++++++++++++ unit-tests/DbDescribeTest.php | 20 +++--- 7 files changed, 181 insertions(+), 20 deletions(-) create mode 100644 tests/unit/Db/Column/PostgresqlTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index f03873bff71..d29342586ab 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,7 +14,7 @@ - Fixed `Phalcon\Session\Adapter\Libmemcached::read`, `Phalcon\Session\Adapter\Libmemcached::destroy`, `Phalcon\Session\Adapter\Memcache::read`, `Phalcon\Session\Adapter\Memcache::destroy`, `Phalcon\Session\Adapter\Redis::read` and `Phalcon\Session\Adapter\Redis::destroy` in accordance with the `session_set_save_handler` API [#12206](https://github.com/phalcon/cphalcon/pull/12206) - Fixed `Phalcon\Validation::getValue()` to use filters when having entity - Fixed `Phalcon\Db\Dialect\Mysql::describeReferences()` and `Phalcon\Db\Dialect\Postgresql::describeReferences()` to return proper sql - +- Fixed `Phalcon\Db\Column::hasDefault` to return `false` for autoincrement columns [phalcon/phalcon-devtools#853](https://github.com/phalcon/phalcon-devtools/issues/853) # [3.0.1](https://github.com/phalcon/cphalcon/releases/tag/v3.0.1) (2016-08-24) - Fixed `Phalcon\Cache\Backend\Redis::flush` in order to flush cache correctly diff --git a/phalcon/db/column.zep b/phalcon/db/column.zep index 9c2431982c5..db3656c67e2 100644 --- a/phalcon/db/column.zep +++ b/phalcon/db/column.zep @@ -3,7 +3,7 @@ +------------------------------------------------------------------------+ | Phalcon Framework | +------------------------------------------------------------------------+ - | Copyright (c) 2011-2016 Phalcon Team (https://phalconphp.com) | + | Copyright (c) 2011-2016 Phalcon Team (https://phalconphp.com) | +------------------------------------------------------------------------+ | This source file is subject to the New BSD License that is bundled | | with this package in the file docs/LICENSE.txt. | @@ -569,6 +569,10 @@ class Column implements ColumnInterface */ public function hasDefault() -> boolean { + if this->isAutoIncrement() { + return false; + } + return this->_default !== null; } } diff --git a/tests/README.md b/tests/README.md index eb9d0ea9ff4..1d1e8b900eb 100644 --- a/tests/README.md +++ b/tests/README.md @@ -29,14 +29,21 @@ First you need to re-generate base classes for all suites: $ vendor/bin/codecept build ``` -A MySQL database is also bundled in this suite. -The SQL file is required for several tests. You can create a database as follows: +A MySQL/PostgreSQL databases is also bundled in this suite. You can create a databases as follows: +*MySQL* ```sh $ echo 'create database phalcon_test charset=utf8mb4 collate=utf8mb4_unicode_ci;' | mysql -u root +$ mysql -uroot phalcon_test < tests/_data/schemas/mysql/mysql.dump.sql ``` -**Note:** For these tests we use the user `root` without a password. +*PostgreSQL* +``` +psql -c 'create database phalcon_test;' -U postgres +psql -U postgres phalcon_test -q -f tests/_data/schemas/postgresql/phalcon_test.sql +``` + +**Note:** For these MySQL-related we use the user `root` without a password. You may need to change this in `codeception.yml` file. Obviously, Beanstalk-tests use Beanstalk, Memcached-tests use Memcached, etc. @@ -62,6 +69,14 @@ We use the following settings of these services: * DB Name: `phalcon_test` * Charset: `utf8` +**PostgreSQL** + +* Host: `127.0.0.1` +* Port: `5432` +* Username: `postgres` +* Password: `''` (empty string) +* DB Name: `phalcon_test` + **Mongo** * Host: `127.0.0.1` @@ -95,6 +110,13 @@ export TEST_DB_MYSQL_PASSWD="" export TEST_DB_MYSQL_NAME="phalcon_test" export TEST_DB_MYSQL_CHARSET="utf8" +# Postgresql +export TEST_DB_POSTGRESQL_HOST="127.0.0.1" +export TEST_DB_POSTGRESQL_PORT="5432" +export TEST_DB_POSTGRESQL_USER="postgres" +export TEST_DB_POSTGRESQL_PASSWD="" +export TEST_DB_POSTGRESQL_NAME="phalcon_test" + # Mongo export TEST_DB_MONGO_HOST="127.0.0.1" export TEST_DB_MONGO_PORT="27017" diff --git a/tests/_data/schemas/postgresql/phalcon_test.sql b/tests/_data/schemas/postgresql/phalcon_test.sql index b4ad0e745a6..5320e296588 100644 --- a/tests/_data/schemas/postgresql/phalcon_test.sql +++ b/tests/_data/schemas/postgresql/phalcon_test.sql @@ -70,6 +70,18 @@ CREATE TABLE parts ( ALTER TABLE public.parts OWNER TO postgres; +-- +-- Name: images; Type: TABLE; Schema: public; Owner: postgres; Tablespace: +-- + +CREATE TABLE images ( + id BIGSERIAL, + base64 TEXT +); + + +ALTER TABLE public.images OWNER TO postgres; + -- -- Name: personas; Type: TABLE; Schema: public; Owner: postgres; Tablespace: -- diff --git a/tests/unit/Db/Adapter/Pdo/PostgresqlTest.php b/tests/unit/Db/Adapter/Pdo/PostgresqlTest.php index 43c9109df51..7a4b05988a0 100644 --- a/tests/unit/Db/Adapter/Pdo/PostgresqlTest.php +++ b/tests/unit/Db/Adapter/Pdo/PostgresqlTest.php @@ -2,12 +2,13 @@ namespace Phalcon\Test\Unit\Db\Adapter\Pdo; -use Phalcon\Db\Adapter\Pdo\Postgresql; +use Phalcon\Db\Column; use Phalcon\Db\Reference; use Phalcon\Test\Module\UnitTest; +use Phalcon\Db\Adapter\Pdo\Postgresql; /** - * \Phalcon\Test\Unit\Db\Adapter\Pdo\Postgresql + * \Phalcon\Test\Unit\Db\Adapter\Pdo\PostgresqlTest * Tests the \Phalcon\Db\Adapter\Pdo\Postgresql component * * @copyright (c) 2011-2016 Phalcon Team @@ -54,9 +55,11 @@ public function _before() public function testDescribeReferencesColumnsCount() { $this->specify( - 'Postgresql::describeReferences return wrong number of columns in Phalcon\Db\Reference', + 'The table references list contains wrong number of columns', function() { - $references = $this->connection->describeReferences("robots_parts", TEST_DB_POSTGRESQL_NAME); + $references = $this->connection->describeReferences('robots_parts', TEST_DB_POSTGRESQL_SCHEMA); + expect($references)->count(2); + /** @var Reference $reference */ foreach($references as $reference) { expect($reference->getColumns())->count(1); @@ -64,4 +67,61 @@ function() { } ); } -} \ No newline at end of file + + /** + * Tests Postgresql::describeColumns for Postgresql autoincrement column + * + * @issue https://github.com/phalcon/phalcon-devtools/issues/853 + * @author Serghei Iakovlev + * @since 2016-09-28 + */ + public function testDescribeAutoIncrementColumns() + { + $this->specify( + 'The table columns array contains incorrect initialized objects', + function () { + $columns = [ + Column::__set_state([ + '_columnName' => 'id', + '_schemaName' => null, + '_type' => 14, + '_typeReference' => -1, + '_typeValues' => null, + '_isNumeric' => true, + '_size' => 0, + '_scale' => 0, + '_default' => "nextval('images_id_seq'::regclass)", + '_unsigned' => false, + '_notNull' => true, + '_primary' => false, + '_autoIncrement' => true, + '_first' => true, + '_after' => null, + '_bindType' => 1, + ]), + Column::__set_state([ + '_columnName' => 'base64', + '_schemaName' => null, + '_type' => 6, + '_typeReference' => -1, + '_typeValues' => null, + '_isNumeric' => false, + '_size' => null, + '_scale' => 0, + '_default' => null, + '_unsigned' => false, + '_notNull' => false, + '_primary' => false, + '_autoIncrement' => false, + '_first' => false, + '_after' => 'id', + '_bindType' => 2, + ]), + ]; + + expect($this->connection->describeColumns('images', null))->equals($columns); + expect($this->connection->describeColumns('images', TEST_DB_POSTGRESQL_SCHEMA))->equals($columns); + } + ); + } +} diff --git a/tests/unit/Db/Column/PostgresqlTest.php b/tests/unit/Db/Column/PostgresqlTest.php new file mode 100644 index 00000000000..39d931e597b --- /dev/null +++ b/tests/unit/Db/Column/PostgresqlTest.php @@ -0,0 +1,63 @@ + + * @author Serghei Iakovlev + * @package Phalcon\Test\Unit\Db\Column + * + * The contents of this file are subject to the New BSD License that is + * bundled with this package in the file docs/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. + */ +class PostgresqlTest extends UnitTest +{ + /** + * Tests Postgresql::hasDefault for autoincrement fields + * + * @issue https://github.com/phalcon/phalcon-devtools/issues/853 + * @author Serghei Iakovlev + * @since 2016-09-28 + */ + public function testHasAutoIncrementDefault() + { + $this->specify( + 'The autoincrement column has default value', + function () { + $column = Column::__set_state([ + '_columnName' => 'id', + '_schemaName' => null, + '_type' => 14, + '_typeReference' => -1, + '_typeValues' => null, + '_isNumeric' => true, + '_size' => 0, + '_scale' => 0, + '_default' => "nextval('images_id_seq'::regclass)", + '_unsigned' => false, + '_notNull' => true, + '_primary' => false, + '_autoIncrement' => true, + '_first' => true, + '_after' => null, + '_bindType' => 1, + ]); + + expect($column->hasDefault())->false(); + expect($column->isAutoIncrement())->true(); + } + ); + } +} diff --git a/unit-tests/DbDescribeTest.php b/unit-tests/DbDescribeTest.php index 84371249584..7f37842cacb 100644 --- a/unit-tests/DbDescribeTest.php +++ b/unit-tests/DbDescribeTest.php @@ -649,7 +649,6 @@ public function testDbMysql() public function testDbPostgresql() { - require 'unit-tests/config.db.php'; if (empty($configPostgresql)) { $this->markTestSkipped("Skipped"); @@ -660,15 +659,16 @@ public function testDbPostgresql() //List tables $expectedTables = array ( - 0 => 'customers', - 1 => 'parts', - 2 => 'personas', - 3 => 'personnes', - 4 => 'prueba', - 5 => 'robots', - 6 => 'robots_parts', - 7 => 'subscriptores', - 8 => 'tipo_documento', + 'customers', + 'images', + 'parts', + 'personas', + 'personnes', + 'prueba', + 'robots', + 'robots_parts', + 'subscriptores', + 'tipo_documento', ); $tables = $connection->listTables();