Skip to content

Commit

Permalink
Fix describeReferences
Browse files Browse the repository at this point in the history
  • Loading branch information
Jurigag committed Sep 28, 2016
1 parent 0081fc8 commit 139b7c8
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ env:
- TEST_DB_MYSQL_PASSWD=""
- TEST_DB_MYSQL_NAME="phalcon_test"
- TEST_DB_MYSQL_CHARSET="utf8"
- TEST_DB_POSTGRESQL_HOST="127.0.0.1"
- TEST_DB_POSTGRESQL_PORT="5432"
- TEST_DB_POSTGRESQL_USER="postgres"
- TEST_DB_POSTGRESQL_PASSWD=""
- TEST_DB_POSTGRESQL_NAME="phalcon_test"
- TEST_DB_MYSQL_DSN="mysql:host=127.0.0.1;dbname=phalcon_test"
- TEST_DB_MONGO_HOST="127.0.0.1"
- TEST_DB_MONGO_PORT="27017"
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
- Fixed `Phalcon\Debug::getVersion` to provide valid link to the latest Phalcon major version [#12215](https://github.com/phalcon/cphalcon/issues/12215)
- 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


# [3.0.1](https://github.com/phalcon/cphalcon/releases/tag/v3.0.1) (2016-08-24)
Expand Down
2 changes: 1 addition & 1 deletion phalcon/db/dialect/mysql.zep
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@ class Mysql extends Dialect
*/
public function describeReferences(string! table, string schema = null) -> string
{
var sql = "SELECT KCU.TABLE_NAME, KCU.COLUMN_NAME, KCU.CONSTRAINT_NAME, KCU.REFERENCED_TABLE_SCHEMA, KCU.REFERENCED_TABLE_NAME, KCU.REFERENCED_COLUMN_NAME, RC.UPDATE_RULE, RC.DELETE_RULE FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS KCU LEFT JOIN INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS AS RC ON RC.CONSTRAINT_NAME = KCU.CONSTRAINT_NAME WHERE KCU.REFERENCED_TABLE_NAME IS NOT NULL AND ";
var sql = "SELECT DISTINCT KCU.TABLE_NAME, KCU.COLUMN_NAME, KCU.CONSTRAINT_NAME, KCU.REFERENCED_TABLE_SCHEMA, KCU.REFERENCED_TABLE_NAME, KCU.REFERENCED_COLUMN_NAME, RC.UPDATE_RULE, RC.DELETE_RULE FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS KCU LEFT JOIN INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS AS RC ON RC.CONSTRAINT_NAME = KCU.CONSTRAINT_NAME WHERE KCU.REFERENCED_TABLE_NAME IS NOT NULL AND ";
if schema {
let sql .= "KCU.CONSTRAINT_SCHEMA = '" . schema . "' AND KCU.TABLE_NAME = '" . table . "'";
} else {
Expand Down
2 changes: 1 addition & 1 deletion phalcon/db/dialect/postgresql.zep
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,7 @@ class Postgresql extends Dialect
*/
public function describeReferences(string! table, string schema = null) -> string
{
var sql = "SELECT tc.table_name as TABLE_NAME, kcu.column_name as COLUMN_NAME, tc.constraint_name as CONSTRAINT_NAME, tc.table_catalog as REFERENCED_TABLE_SCHEMA, ccu.table_name AS REFERENCED_TABLE_NAME, ccu.column_name AS REFERENCED_COLUMN_NAME FROM information_schema.table_constraints AS tc JOIN information_schema.key_column_usage AS kcu ON tc.constraint_name = kcu.constraint_name JOIN information_schema.constraint_column_usage AS ccu ON ccu.constraint_name = tc.constraint_name WHERE constraint_type = 'FOREIGN KEY' AND ";
var sql = "SELECT DISTINCT tc.table_name as TABLE_NAME, kcu.column_name as COLUMN_NAME, tc.constraint_name as CONSTRAINT_NAME, tc.table_catalog as REFERENCED_TABLE_SCHEMA, ccu.table_name AS REFERENCED_TABLE_NAME, ccu.column_name AS REFERENCED_COLUMN_NAME FROM information_schema.table_constraints AS tc JOIN information_schema.key_column_usage AS kcu ON tc.constraint_name = kcu.constraint_name JOIN information_schema.constraint_column_usage AS ccu ON ccu.constraint_name = tc.constraint_name WHERE constraint_type = 'FOREIGN KEY' AND ";

if schema {
let sql .= "tc.table_schema = '" . schema . "' AND tc.table_name='" . table . "'";
Expand Down
8 changes: 8 additions & 0 deletions tests/_bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@
defined('TEST_DB_MYSQL_NAME') || define('TEST_DB_MYSQL_NAME', getenv('TEST_DB_MYSQL_NAME') ?: 'phalcon_test');
defined('TEST_DB_MYSQL_CHARSET') || define('TEST_DB_MYSQL_CHARSET', getenv('TEST_DB_MYSQL_CHARSET') ?: 'utf8');

// Postgresql
defined('TEST_DB_POSTGRESQL_HOST') || define('TEST_DB_POSTGRESQL_HOST', getenv('TEST_DB_POSTGRESQL_HOST') ?: '127.0.0.1');
defined('TEST_DB_POSTGRESQL_PORT') || define('TEST_DB_POSTGRESQL_PORT', getenv('TEST_DB_POSTGRESQL_PORT') ?: 5432);
defined('TEST_DB_POSTGRESQL_USER') || define('TEST_DB_POSTGRESQL_USER', getenv('TEST_DB_POSTGRESQL_USER') ?: 'postgres');
defined('TEST_DB_POSTGRESQL_PASSWD') || define('TEST_DB_POSTGRESQL_PASSWD', getenv('TEST_DB_POSTGRESQL_PASSWD') ?: '');
defined('TEST_DB_POSTGRESQL_NAME') || define('TEST_DB_POSTGRESQL_NAME', getenv('TEST_DB_POSTGRESQL_NAME') ?: 'phalcon_test');
defined('TEST_DB_POSTGRESQL_SCHEMA') || define('TEST_DB_POSTGRESQL_SCHEMA', getenv('TEST_DB_POSTGRESQL_SCHEMA') ?: 'public');

// Mongo
defined('TEST_DB_MONGO_HOST') || define('TEST_DB_MONGO_HOST', getenv('TEST_DB_MONGO_HOST') ?: '127.0.0.1');
defined('TEST_DB_MONGO_PORT') || define('TEST_DB_MONGO_PORT', getenv('TEST_DB_MONGO_PORT') ?: 27017);
Expand Down
21 changes: 21 additions & 0 deletions tests/unit/Db/Adapter/Pdo/MysqlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Phalcon\Test\Unit\Db\Adapter\Pdo;

use Phalcon\Db\Reference;
use Phalcon\Test\Module\UnitTest;
use Phalcon\Db\Adapter\Pdo\Mysql;

Expand Down Expand Up @@ -87,4 +88,24 @@ function () {
}
);
}

/**
* Tests Mysql::describeReferences
*
* @author Wojciechj Ślawski <[email protected]>
* @since 2016-09-28
*/
public function testDescribeReferencesColumnsCount()
{
$this->specify(
'Mysql::describeReferences return wrong number of columns in Phalcon\Db\Reference',
function() {
$references = $this->connection->describeReferences("robots_parts", TEST_DB_MYSQL_NAME);
/** @var Reference $reference */
foreach($references as $reference) {
expect($reference->getColumns())->count(1);
}
}
);
}
}
67 changes: 67 additions & 0 deletions tests/unit/Db/Adapter/Pdo/PostgresqlTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace Phalcon\Test\Unit\Db\Adapter\Pdo;

use Phalcon\Db\Adapter\Pdo\Postgresql;
use Phalcon\Db\Reference;
use Phalcon\Test\Module\UnitTest;

/**
* \Phalcon\Test\Unit\Db\Adapter\Pdo\Postgresql
* Tests the \Phalcon\Db\Adapter\Pdo\Postgresql component
*
* @copyright (c) 2011-2016 Phalcon Team
* @link http://www.phalconphp.com
* @author Andres Gutierrez <[email protected]>
* @author Serghei Iakovlev <[email protected]>
* @author Wojciech Ślawski <[email protected]>
* @package Phalcon\Test\Unit\Db\Adapter\Pdo
*
* 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 [email protected]
* so that we can send you a copy immediately.
*/
class PostgresqlTest extends UnitTest
{
/**
* @var Postgresql
*/
protected $connection;

public function _before()
{
parent::_before();

$this->connection = new Postgresql([
'host' => TEST_DB_POSTGRESQL_HOST,
'username' => TEST_DB_POSTGRESQL_USER,
'password' => TEST_DB_POSTGRESQL_PASSWD,
'dbname' => TEST_DB_POSTGRESQL_NAME,
'port' => TEST_DB_POSTGRESQL_PORT,
'schema' => TEST_DB_POSTGRESQL_SCHEMA
]);
}

/**
* Tests Postgresql::describeReferences
*
* @author Wojciech Ślawski <[email protected]>
* @since 2016-09-28
*/
public function testDescribeReferencesColumnsCount()
{
$this->specify(
'Postgresql::describeReferences return wrong number of columns in Phalcon\Db\Reference',
function() {
$references = $this->connection->describeReferences("robots_parts", TEST_DB_POSTGRESQL_NAME);
/** @var Reference $reference */
foreach($references as $reference) {
expect($reference->getColumns())->count(1);
}
}
);
}
}
4 changes: 2 additions & 2 deletions tests/unit/ValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ protected function _before()
*/
public function testWithEntityAndFilter()
{
$this->specify('Tests validate method with entity and filters', function () {
$this->specify('Validation filters are not working with entity', function () {
$users = new Users([
'name' => ' '
]);
Expand Down Expand Up @@ -86,7 +86,7 @@ public function testWithEntityAndFilter()
*/
public function testFilteringEntity()
{
$this->specify('Tests if validate method filters entity', function() {
$this->specify('Validation filters are not saving value to entity', function() {
$users = new Users([
'name' => 'SomeName '
]);
Expand Down

0 comments on commit 139b7c8

Please sign in to comment.