From 2bcf6351fd849fba590998dc071b3db8c11dae16 Mon Sep 17 00:00:00 2001 From: Joanhey Date: Tue, 22 Oct 2019 20:43:59 +0200 Subject: [PATCH 001/148] Update Metadata --- Metadata/Metadata.php | 43 +++++++++++++++++-------------------- Metadata/MysqlMetadata.php | 21 +++++++++--------- Metadata/PgsqlMetadata.php | 16 ++++++-------- Metadata/SqlsrvMetadata.php | 29 ++++++++++++------------- 4 files changed, 51 insertions(+), 58 deletions(-) diff --git a/Metadata/Metadata.php b/Metadata/Metadata.php index 60d5c8f..d99a65e 100644 --- a/Metadata/Metadata.php +++ b/Metadata/Metadata.php @@ -70,11 +70,10 @@ abstract class Metadata /** * Metadata de la tabla. * - * @param string $type tipo de controlador - * @param string $database - * @param string $table - * @param string $schema - * + * @param string $type tipo de controlador + * @param string $database + * @param string $table + * @param string $schema * @return Metadata */ public static function get($type, $database, $table, $schema = null) @@ -90,26 +89,25 @@ public static function get($type, $database, $table, $schema = null) * Obtiene la metadata de la tabla * Y la cachea si esta en producción. * - * @param string $type tipo de controlador - * @param string $database - * @param string $table - * @param string $schema - * + * @param string $type tipo de controlador + * @param string $database + * @param string $table + * @param string $schema * @return Metadata */ private static function getMetadata($type, $database, $table, $schema) { - if (PRODUCTION && !(self::$instances["$database.$table.$schema"] = \Cache::driver()->get("$database.$table.$schema", 'ActiveRecord.Metadata'))) { + if (\PRODUCTION && ! (self::$instances["$database.$table.$schema"] = \Cache::driver()->get("$database.$table.$schema", 'ActiveRecord.Metadata'))) { return self::$instances["$database.$table.$schema"]; } - $class = ucwords($type).'Metadata'; + $class = \ucwords($type).'Metadata'; $class = __NAMESPACE__."\\$class"; self::$instances["$database.$table.$schema"] = new $class($database, $table, $schema); - // Cachea los metadatos - if (PRODUCTION) { + // Cachea los metadatos + if (\PRODUCTION) { \Cache::driver()->save( self::$instances["$database.$table.$schema"], \Config::get('config.application.metadata_lifetime'), @@ -128,17 +126,17 @@ private static function getMetadata($type, $database, $table, $schema) * @param string $table tabla * @param string $schema squema */ - private function __construct($database, $table, $schema = null) + private function __construct($database, $table, $schema = \null) { - $this->fields = $this->queryFields($database, $table, $schema); + $this->fields = $this->queryFields($database, $table, $schema); $this->fieldsList = \array_keys($this->fields); } /** * Permite el filtrado de columna en PK, por Defecto y Autogenerado. * - * @param $m información de la columna - * @param $field nombre de la columna + * @param $m información de la columna + * @param $field nombre de la columna */ protected function filterCol($m, $field) { @@ -154,13 +152,12 @@ protected function filterCol($m, $field) /** * Consultar los campos de la tabla en la base de datos. * - * @param string $database base de datos - * @param string $table tabla - * @param string $schema squema - * + * @param string $database base de datos + * @param string $table tabla + * @param string $schema squema * @return array */ - abstract protected function queryFields($database, $table, $schema = null); + abstract protected function queryFields($database, $table, $schema = \null); /** * Obtiene la descripción de los campos. diff --git a/Metadata/MysqlMetadata.php b/Metadata/MysqlMetadata.php index 60e97a6..68152bf 100644 --- a/Metadata/MysqlMetadata.php +++ b/Metadata/MysqlMetadata.php @@ -21,7 +21,7 @@ namespace Kumbia\ActiveRecord\Metadata; use Kumbia\ActiveRecord\Db; -use PDO; +use \PDO; /** * Adaptador de Metadata para Mysql. @@ -31,26 +31,25 @@ class MysqlMetadata extends Metadata /** * Consultar los campos de la tabla en la base de datos. * - * @param string $database base de datos - * @param string $table tabla - * @param string $schema squema - * + * @param string $database base de datos + * @param string $table tabla + * @param string $schema squema * @return array */ - protected function queryFields($database, $table, $schema = null) + protected function queryFields($database, $table, $schema = \null) { - $sql = $schema ? "DESCRIBE `$schema`.`$table`" : "DESCRIBE `$table`"; + $sql = $schema ? "DESCRIBE `$schema`.`$table`" : "DESCRIBE `$table`"; $describe = Db::get($database)->query($sql); $fields = []; // TODO mejorar este código - while (($value = $describe->fetch(PDO::FETCH_OBJ))) { + while ($value = $describe->fetch(\PDO::FETCH_OBJ)) { $fields[$value->Field] = [ 'Type' => $value->Type, - 'Null' => $value->Null != 'NO', + 'Null' => $value->Null !== 'NO', 'Key' => $value->Key, - 'Default' => $value->Default != '', - 'Auto' => $value->Extra == 'auto_increment', + 'Default' => $value->Default !== '', + 'Auto' => $value->Extra === 'auto_increment' ]; $this->filterCol($fields[$value->Field], $value->Field); } diff --git a/Metadata/PgsqlMetadata.php b/Metadata/PgsqlMetadata.php index 32afc4d..43fda78 100644 --- a/Metadata/PgsqlMetadata.php +++ b/Metadata/PgsqlMetadata.php @@ -29,10 +29,9 @@ class PgsqlMetadata extends Metadata /** * Consultar los campos de la tabla en la base de datos. * - * @param string $database base de datos - * @param string $table tabla - * @param string $schema squema - * + * @param string $database base de datos + * @param string $table tabla + * @param string $schema squema * @return array */ protected function queryFields($database, $table, $schema = 'public') @@ -64,8 +63,7 @@ protected function queryFields($database, $table, $schema = 'public') /** * Genera la metadata. * - * @param \PDOStatement $describe - * + * @param \PDOStatement $describe * @return array */ private static function describe(\PDOStatement $describe) @@ -75,10 +73,10 @@ private static function describe(\PDOStatement $describe) foreach ($describe as $value) { $fields[$value['field']] = [ 'Type' => $value['type'], - 'Null' => $value['null'] != 'NO', - 'Default' => $value['default'] != '', + 'Null' => $value['null'] !== 'NO', + 'Default' => $value['default'] !== '', 'Key' => \substr($value['key'], 0, 3), - 'Auto' => \preg_match('/^nextval\(/', $value['default']), + 'Auto' => \preg_match('/^nextval\(/', $value['default']) ]; } diff --git a/Metadata/SqlsrvMetadata.php b/Metadata/SqlsrvMetadata.php index 2120d06..73d812e 100644 --- a/Metadata/SqlsrvMetadata.php +++ b/Metadata/SqlsrvMetadata.php @@ -15,13 +15,14 @@ * @category Kumbia * @package ActiveRecord * @subpackage Metadata + * * @copyright 2005 - 2016 Kumbia Team (http://www.kumbiaphp.com) * @license http://wiki.kumbiaphp.com/Licencia New BSD License */ namespace Kumbia\ActiveRecord\Metadata; use Kumbia\ActiveRecord\Db; -use PDO; +use \PDO; /** * Adaptador de Metadata para Sqlsrv @@ -32,10 +33,9 @@ class SqlsrvMetadata extends Metadata /** * Consultar los campos de la tabla en la base de datos * - * @param string $database base de datos - * @param string $table tabla - * @param string $schema squema - * + * @param string $database base de datos + * @param string $table tabla + * @param string $schema squema * @return array */ protected function queryFields($database, $table, $schema = 'dbo') @@ -60,36 +60,35 @@ protected function queryFields($database, $table, $schema = 'dbo') /** * Optiene el PK * - * @param string $database base de datos - * @param string $table tabla - * + * @param string $database base de datos + * @param string $table tabla * @return string */ private static function pk($database, $table) { $pk = Db::get($database)->query("exec sp_pkeys @table_name='$table'"); - $pk = $pk->fetch(PDO::FETCH_OBJ); + $pk = $pk->fetch(\PDO::FETCH_OBJ); + return $pk->COLUMN_NAME; } /** * Genera la metadata * - * @param \PDOStatement $describe SQL result - * @param string $pk Primary key - * + * @param \PDOStatement $describe SQL result + * @param string $pk Primary key * @return array */ protected function describe(\PDOStatement $describe, $pk) { // TODO Mejorar $fields = []; - while (( $value = $describe->fetch(PDO::FETCH_OBJ))) : + while (($value = $describe->fetch( \PDO::FETCH_OBJ))): $fields[$value->field_name] = [ 'Type' => $value->type_field, 'Null' => ($value->is_nullable), - 'Key' => ($value->field_name == $pk) ? 'PRI' : '', - 'Default' => str_replace("''", "'", trim($value->default_value, "(')")), + 'Key' => ($value->field_name === $pk) ? 'PRI' : '', + 'Default' => \str_replace("''", "'", \trim($value->default_value, "(')")), 'Auto' => ($value->is_auto_increment) ]; $this->filterCol($fields[$value->field_name], $value->field_name); From bd4d0e567aa819fe6509575aa227834651dbf800 Mon Sep 17 00:00:00 2001 From: Joanhey Date: Tue, 22 Oct 2019 20:44:19 +0200 Subject: [PATCH 002/148] Update queries --- Query/mysql_last_insert_id.php | 11 +++++------ Query/mysql_limit.php | 11 ++++------- Query/pgsql_last_insert_id.php | 9 ++++----- Query/pgsql_limit.php | 15 ++++++--------- Query/sqlsrv_limit.php | 15 +++++++-------- 5 files changed, 26 insertions(+), 35 deletions(-) diff --git a/Query/mysql_last_insert_id.php b/Query/mysql_last_insert_id.php index 17a5d3d..6331f32 100644 --- a/Query/mysql_last_insert_id.php +++ b/Query/mysql_last_insert_id.php @@ -22,14 +22,13 @@ /** * Obtiene el último id generado en mysql. * - * @param \PDO $dbh conexion pdo - * @param string $pk campo clave primaria - * @param string $table nombre de tabla - * @param string $schema esquema - * + * @param \PDO $dbh conexion pdo + * @param string $pk campo clave primaria + * @param string $table nombre de tabla + * @param string $schema esquema * @return string */ -function mysql_last_insert_id(\PDO $dbh, $pk, $table, $schema = null) +function mysql_last_insert_id(\PDO $dbh, $pk, $table, $schema = \null) { return $dbh->lastInsertId(); } diff --git a/Query/mysql_limit.php b/Query/mysql_limit.php index e5fa63c..33bfba3 100644 --- a/Query/mysql_limit.php +++ b/Query/mysql_limit.php @@ -23,15 +23,12 @@ /** * Adiciona limit y offset a la consulta sql en mysql. * - * @param string $sql consulta select - * @param string $limit valor limit - * @param string $offset valor offset - * + * @param string $sql consulta select + * @param int $limit valor limit + * @param int $offset valor offset * @return string */ function mysql_limit($sql, int $limit, int $offset = 0) { - $sql .= " LIMIT $offset, $limit"; - - return $sql; + return $sql." LIMIT $offset, $limit"; } diff --git a/Query/pgsql_last_insert_id.php b/Query/pgsql_last_insert_id.php index fb3568f..2daae2c 100644 --- a/Query/pgsql_last_insert_id.php +++ b/Query/pgsql_last_insert_id.php @@ -22,11 +22,10 @@ /** * Obtiene el último id generado en pgsql. * - * @param \PDO $dbh conexion pdo - * @param string $pk campo clave primaria - * @param string $table nombre de tabla - * @param string $schema esquema - * + * @param \PDO $dbh conexion pdo + * @param string $pk campo clave primaria + * @param string $table nombre de tabla + * @param string $schema esquema * @return string */ function pgsql_last_insert_id(\PDO $dbh, $pk, $table, $schema = '') diff --git a/Query/pgsql_limit.php b/Query/pgsql_limit.php index f269ea8..dff71ed 100644 --- a/Query/pgsql_limit.php +++ b/Query/pgsql_limit.php @@ -22,21 +22,18 @@ /** * Adiciona limit y offset a la consulta sql en pgsql. * - * @param string $sql consulta select - * @param string $limit valor limit - * @param string $offset valor offset - * + * @param string $sql consulta select + * @param int $limit valor limit + * @param int $offset valor offset * @return string */ -function pgsql_limit($sql, $limit = null, $offset = null) +function pgsql_limit($sql, int $limit = \null, int $offset = \null) { - if ($limit !== null) { - $limit = (int) $limit; + if ($limit !== \null) { $sql .= " LIMIT $limit"; } - if ($offset !== null) { - $offset = (int) $offset; + if ($offset !== \null) { $sql .= " OFFSET $offset"; } diff --git a/Query/sqlsrv_limit.php b/Query/sqlsrv_limit.php index 6626cc7..999aeb9 100644 --- a/Query/sqlsrv_limit.php +++ b/Query/sqlsrv_limit.php @@ -15,6 +15,7 @@ * @category Kumbia * @package ActiveRecord * @subpackage Query + * * @copyright Copyright (c) 2005-2016 Kumbia Team (http://www.kumbiaphp.com) * @license http://wiki.kumbiaphp.com/Licencia New BSD License */ @@ -24,20 +25,18 @@ /** * Adiciona limit y offset a la consulta sql en mssql * - * @param string $sql consulta select - * @param string $limit valor limit - * @param string $offset valor offset + * @param string $sql consulta select + * @param int $limit valor limit + * @param int $offset valor offset * @return string */ -function sqlsrv_limit($sql, $limit = null, $offset = null) +function sqlsrv_limit($sql, int $limit = \null, int $offset = \null) { if ($limit !== null) { - $limit = (int) $limit; - $sql = preg_replace('/(DELETE|INSERT|SELECT|UPDATE)/i', '${1} TOP ' . $limit, $sql); + $sql = \preg_replace('/(DELETE|INSERT|SELECT|UPDATE)/i', '${1} TOP '.$limit, $sql); } - if ($offset !== null) { - $offset = (int) $offset; + if ($offset !== \null) { $sql .= " OFFSET $offset"; } From 262e3848ac89f8a7ce1aa0245e7363a1d13b19df Mon Sep 17 00:00:00 2001 From: Joanhey Date: Tue, 22 Oct 2019 20:44:49 +0200 Subject: [PATCH 003/148] Update paginator --- BaseRecord.php | 2 +- Paginator.php | 35 +++++++++++++++++------------------ 2 files changed, 18 insertions(+), 19 deletions(-) diff --git a/BaseRecord.php b/BaseRecord.php index f8ad142..3a03ada 100644 --- a/BaseRecord.php +++ b/BaseRecord.php @@ -19,7 +19,7 @@ */ namespace Kumbia\ActiveRecord; -use KumbiaException; +use \KumbiaException; /** * Base del ORM ActiveRecord. diff --git a/Paginator.php b/Paginator.php index 3cd0bd6..de64ded 100644 --- a/Paginator.php +++ b/Paginator.php @@ -84,16 +84,16 @@ class Paginator implements \IteratorAggregate, \Countable, \JsonSerializable /** * Constructor. * - * @param string $model nombre de clase de modelo - * @param string $sql consulta select sql - * @param int $page numero de pagina - * @param int $perPage cantidad de items por pagina - * @param mixed $values valores + * @param string $model nombre de clase de modelo + * @param string $sql consulta select sql + * @param int $page numero de pagina + * @param int $perPage cantidad de items por pagina + * @param mixed $values valores */ - public function __construct($model, $sql, int $page, int $perPage, $values = null) + public function __construct($model, $sql, int $page, int $perPage, $values = \null) { $this->perPage = $perPage; - $this->page = $page; + $this->page = $page; /*validacion*/ $this->validPage(); @@ -101,14 +101,14 @@ public function __construct($model, $sql, int $page, int $perPage, $values = nul $this->model = $model; // Valores para consulta - $this->values = ($values !== null && !is_array($values)) ? - array_slice(func_get_args(), 4) : $values; + $this->values = ($values !== \null && ! \is_array($values)) ? + \array_slice(\func_get_args(), 4) : $values; - $this->count = $this->countQuery($model, $sql); - $this->totalPages = (int) max(1, ceil($this->count / $this->perPage)); + $this->count = $this->countQuery($model, $sql); + $this->totalPages = (int) \max(1, \ceil($this->count / $this->perPage)); $this->validCurrent(); // Establece el limit y offset - $this->sql = QueryGenerator::query($model::getDriver(), 'limit', $sql, $perPage, ($page - 1) * $perPage); + $this->sql = QueryGenerator::query($model::getDriver(), 'limit', $sql, $perPage, ($page - 1) * $perPage); $this->items = $model::query($this->sql, $this->values)->fetchAll(); } @@ -155,10 +155,9 @@ public function getIterator() /** * Cuenta el número de resultados totales. * - * @param string $model - * @param string $sql - * - * @return int total de resultados + * @param string $model + * @param string $sql + * @return int total de resultados */ protected function countQuery($model, $sql) { @@ -194,7 +193,7 @@ public function totalPages() */ public function nextPage() { - return ($this->totalPages > $this->page) ? ($this->page + 1) : null; + return $this->totalPages > $this->page ? $this->page + 1 : 0; } /** @@ -204,7 +203,7 @@ public function nextPage() */ public function prevPage() { - return ($this->page > 1) ? ($this->page - 1) : null; + return $this->page > 1 ? $this->page - 1 : 0; } /** From 2b4e08edb39a55106d2d2b5a8ff886016757487d Mon Sep 17 00:00:00 2001 From: Joanhey Date: Tue, 22 Oct 2019 20:45:40 +0200 Subject: [PATCH 004/148] Update query generator --- QueryGenerator.php | 146 +++++++++++++++++++++------------------------ 1 file changed, 68 insertions(+), 78 deletions(-) diff --git a/QueryGenerator.php b/QueryGenerator.php index a9db8fc..8b4787e 100644 --- a/QueryGenerator.php +++ b/QueryGenerator.php @@ -27,7 +27,6 @@ class QueryGenerator /** * Construye una consulta select desde una lista de parametros. * - * @param array $params parametros de consulta select * where: condiciones where * order: criterio de ordenamiento * fields: lista de campos @@ -36,9 +35,9 @@ class QueryGenerator * having: condiciones de grupo * limit: valor limit * offset: valor offset - * @param string $source - * @param string $type - * + * @param array $params parametros de consulta select + * @param string $source + * @param string $type * @return string */ public static function select($source, $type, array $params) @@ -46,18 +45,18 @@ public static function select($source, $type, array $params) $params = array_merge([ 'fields' => '*', 'join' => '', - 'limit' => null, - 'offset' => null, - 'where' => null, - 'group' => null, - 'having' => null, - 'order' => null, + 'limit' => \null, + 'offset' => \null, + 'where' => \null, + 'group' => \null, + 'having' => \null, + 'order' => \null ], $params); list($where, $group, $having, $order) = static::prepareParam($params); - $sql = "SELECT {$params['fields']} FROM $source {$params['join']} $where $group $having $order"; + $sql = "SELECT {$params['fields']} FROM $source {$params['join']} $where $group $having $order"; - if (!is_null($params['limit']) || !is_null($params['offset'])) { + if ( ! \is_null($params['limit']) || ! \is_null($params['offset'])) { $sql = self::query($type, 'limit', $sql, $params['limit'], $params['offset']); } @@ -68,7 +67,7 @@ public static function select($source, $type, array $params) * Permite construir el WHERE, GROUP BY, HAVING y ORDER BY de una consulta SQL * en base a los parámetros $params. * - * @param array $params + * @param array $params * @return array */ protected static function prepareParam(array $params) @@ -77,68 +76,67 @@ protected static function prepareParam(array $params) static::where($params['where']), static::group($params['group']), static::having($params['having']), - static::order($params['order']), + static::order($params['order']) ]; } /** * Genera una sentencia where. * - * @param $where + * @param string $where * @return string */ protected static function where($where) { - return empty($where) ? '' : "WHERE $where"; + return \empty($where) ? '' : "WHERE $where"; } /** * Genera una sentencia GROUP. * - * @param $group + * @param string $group * @return string */ protected static function group($group) { - return empty($group) ? '' : "GROUP BY $group"; + return \empty($group) ? '' : "GROUP BY $group"; } /** * Genera una sentencia HAVING. * - * @param $having + * @param string $having * @return string */ protected static function having($having) { - return empty($having) ? '' : "HAVING $having"; + return \empty($having) ? '' : "HAVING $having"; } /** * Genera una sentencia ORDER BY. * - * @param $order + * @param string $order * @return string */ protected static function order($order) { - return empty($order) ? '' : "ORDER BY $order"; + return \empty($order) ? '' : "ORDER BY $order"; } /** * Construye una consulta INSERT. * - * @param \Kumbia\ActiveRecord\LiteRecord $model Modelo a actualizar - * @param array $data Datos pasados a la consulta preparada - * + * @param LiteRecord $model Modelo a actualizar + * @param array $data Datos pasados a la consulta preparada * @return string */ - public static function insert(\Kumbia\ActiveRecord\LiteRecord $model, &$data) + public static function insert(LiteRecord $model, array &$data) { - $meta = $model::metadata(); - $data = []; + $meta = $model::metadata(); + $data = []; $columns = []; - $values = []; + $values = []; // Preparar consulta foreach ($meta->getFieldsList() as $field) { @@ -146,8 +144,8 @@ public static function insert(\Kumbia\ActiveRecord\LiteRecord $model, &$data) static::insertField($field, $model, $data, $values); } $columns = \implode(',', $columns); - $values = \implode(',', $values); - $source = $model::getSource(); + $values = \implode(',', $values); + $source = $model::getSource(); return "INSERT INTO $source ($columns) VALUES ($values)"; } @@ -155,22 +153,22 @@ public static function insert(\Kumbia\ActiveRecord\LiteRecord $model, &$data) /** * Agrega un campo a para generar una consulta preparada para un INSERT. * - * @param string $field Nombre del campo - * @param LiteRecord $model valor del campo - * @param array $data array de datos - * @param array $values array de valores - * + * @param string $field Nombre del campo + * @param LiteRecord $model valor del campo + * @param array $data array de datos + * @param array $values array de valores * @return void */ protected static function insertField($field, LiteRecord $model, array &$data, array &$values) { - $meta = $model::metadata(); + $meta = $model::metadata(); $withDefault = $meta->getWithDefault(); - $autoFields = $meta->getAutoFields(); + $autoFields = $meta->getAutoFields(); if (self::haveValue($model, $field)) { $data[":$field"] = $model->$field; - $values[] = ":$field"; - } else{//if (!\in_array($field, $withDefault) && !\in_array($field, $autoFields)) { + $values[] = ":$field"; + } else { +//if (!\in_array($field, $withDefault) && !\in_array($field, $autoFields)) { $values[] = 'NULL'; } } @@ -178,9 +176,8 @@ protected static function insertField($field, LiteRecord $model, array &$data, a /** * Permite conocer si la columna debe definirse como nula. * - * @param LiteRecord $model - * @param string $field - * + * @param LiteRecord $model + * @param string $field * @return bool */ protected static function haveValue(LiteRecord $model, $field) @@ -191,23 +188,22 @@ protected static function haveValue(LiteRecord $model, $field) /** * Construye una consulta UPDATE. * - * @param \Kumbia\ActiveRecord\LiteRecord $model Modelo a actualizar - * @param array $data Datos pasados a la consulta preparada - * + * @param LiteRecord $model Modelo a actualizar + * @param array $data Datos pasados a la consulta preparada * @return string */ - public static function update(\Kumbia\ActiveRecord\LiteRecord $model, array &$data) + public static function update(LiteRecord $model, array &$data) { $set = []; - $pk = $model::getPK(); + $pk = $model::getPK(); /*elimina la clave primaria*/ - $list = array_diff($model::metadata()->getFieldsList(), [$pk]); + $list = \array_diff($model::metadata()->getFieldsList(), [$pk]); foreach ($list as $field) { - $value = isset($model->$field) ? $model->$field : null; + $value = isset($model->$field) ? $model->$field : \null; static::updateField($field, $value, $data, $set); } - $set = \implode(', ', $set); - $source = $model::getSource(); + $set = \implode(', ', $set); + $source = $model::getSource(); $data[":$pk"] = $model->$pk; return "UPDATE $source SET $set WHERE $pk = :$pk"; @@ -216,9 +212,8 @@ public static function update(\Kumbia\ActiveRecord\LiteRecord $model, array &$da /** * Generate SQL for DELETE sentence. * - * @param string $source source - * @param string $where condition - * + * @param string $source source + * @param string $where condition * @return string SQL */ public static function deleteAll($source, $where) @@ -229,9 +224,8 @@ public static function deleteAll($source, $where) /** * Generate SQL for COUNT sentence. * - * @param string $source source - * @param string $where condition - * + * @param string $source source + * @param string $where condition * @return string SQL */ public static function count($source, $where) @@ -242,18 +236,17 @@ public static function count($source, $where) /** * Agrega un campo a para generar una consulta preparada para un UPDATE. * - * @param string $field Nombre del campo - * @param mixed $value valor - * @param array $data array de datos - * @param array $set array de valores - * + * @param string $field Nombre del campo + * @param mixed $value valor + * @param array $data array de datos + * @param array $set array de valores * @return void */ protected static function updateField($field, $value, array &$data, array &$set) { - if (!empty($value)) { + if ( ! empty($value)) { $data[":$field"] = $value; - $set[] = "$field = :$field"; + $set[] = "$field = :$field"; } else { $set[] = "$field = NULL"; } @@ -262,13 +255,11 @@ protected static function updateField($field, $value, array &$data, array &$set) /** * Construye una consulta UPDATE. * - * @param string $model nombre del modelo a actualizar - * @param array $fields campos a actualizar - * @param array $data Datos pasados a la consulta preparada - * @param string|null $where - * * @todo ¿Hay que escapar los nombres de los campos? - * + * @param string $model nombre del modelo a actualizar + * @param array $fields campos a actualizar + * @param array $data Datos pasados a la consulta preparada + * @param string|null $where * @return string */ public static function updateAll($model, array $fields, array &$data, $where) @@ -279,9 +270,9 @@ public static function updateAll($model, array $fields, array &$data, $where) foreach ($fields as $field => $value) { static::updateField($field, $value, $data, $set); } - $set = \implode(', ', $set); + $set = \implode(', ', $set); $source = $model::getSource(); - $where = static::where($where); + $where = static::where($where); return "UPDATE $source SET $set $where"; } @@ -289,11 +280,10 @@ public static function updateAll($model, array $fields, array &$data, $where) /** * Ejecuta una consulta. * - * @param string $type tipo de driver - * @param string $query_function nombre de funcion - * - * @return mixed * @thow KumbiaException + * @param string $type tipo de driver + * @param string $query_function nombre de funcion + * @return mixed */ public static function query($type, $query_function) { @@ -303,6 +293,6 @@ public static function query($type, $query_function) $args = \array_slice(\func_get_args(), 2); - return call_user_func_array(__NAMESPACE__."\\Query\\$query_function", $args); + return \call_user_func_array(__NAMESPACE__."\\Query\\$query_function", $args); } } From d329594a2288f6cf8d67227c0bcff447091156c3 Mon Sep 17 00:00:00 2001 From: Joanhey Date: Tue, 22 Oct 2019 20:49:48 +0200 Subject: [PATCH 005/148] Fix typo --- QueryGenerator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/QueryGenerator.php b/QueryGenerator.php index 8b4787e..9448243 100644 --- a/QueryGenerator.php +++ b/QueryGenerator.php @@ -121,7 +121,7 @@ protected static function having($having) */ protected static function order($order) { - return \empty($order) ? '' : "ORDER BY $order"; + return empty($order) ? '' : "ORDER BY $order"; } /** From fee413a5b2afd74cfe9fe408a2983bee6e856a48 Mon Sep 17 00:00:00 2001 From: Joanhey Date: Wed, 23 Oct 2019 09:47:29 +0200 Subject: [PATCH 006/148] Update metadata --- Metadata/MysqlMetadata.php | 6 +++--- Metadata/PgsqlMetadata.php | 4 ++-- Metadata/SqlsrvMetadata.php | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Metadata/MysqlMetadata.php b/Metadata/MysqlMetadata.php index 68152bf..a2fcda2 100644 --- a/Metadata/MysqlMetadata.php +++ b/Metadata/MysqlMetadata.php @@ -46,10 +46,10 @@ protected function queryFields($database, $table, $schema = \null) while ($value = $describe->fetch(\PDO::FETCH_OBJ)) { $fields[$value->Field] = [ 'Type' => $value->Type, - 'Null' => $value->Null !== 'NO', + 'Null' => $value->Null != 'NO', 'Key' => $value->Key, - 'Default' => $value->Default !== '', - 'Auto' => $value->Extra === 'auto_increment' + 'Default' => $value->Default != '', + 'Auto' => $value->Extra == 'auto_increment' ]; $this->filterCol($fields[$value->Field], $value->Field); } diff --git a/Metadata/PgsqlMetadata.php b/Metadata/PgsqlMetadata.php index 43fda78..0e3df9e 100644 --- a/Metadata/PgsqlMetadata.php +++ b/Metadata/PgsqlMetadata.php @@ -73,8 +73,8 @@ private static function describe(\PDOStatement $describe) foreach ($describe as $value) { $fields[$value['field']] = [ 'Type' => $value['type'], - 'Null' => $value['null'] !== 'NO', - 'Default' => $value['default'] !== '', + 'Null' => $value['null'] != 'NO', + 'Default' => $value['default'] != '', 'Key' => \substr($value['key'], 0, 3), 'Auto' => \preg_match('/^nextval\(/', $value['default']) ]; diff --git a/Metadata/SqlsrvMetadata.php b/Metadata/SqlsrvMetadata.php index 73d812e..632748f 100644 --- a/Metadata/SqlsrvMetadata.php +++ b/Metadata/SqlsrvMetadata.php @@ -87,7 +87,7 @@ protected function describe(\PDOStatement $describe, $pk) $fields[$value->field_name] = [ 'Type' => $value->type_field, 'Null' => ($value->is_nullable), - 'Key' => ($value->field_name === $pk) ? 'PRI' : '', + 'Key' => ($value->field_name == $pk) ? 'PRI' : '', 'Default' => \str_replace("''", "'", \trim($value->default_value, "(')")), 'Auto' => ($value->is_auto_increment) ]; From 854c84d8f4955a41f6a1941b3d168de4bb1a2e6b Mon Sep 17 00:00:00 2001 From: Joanhey Date: Wed, 23 Oct 2019 09:53:29 +0200 Subject: [PATCH 007/148] Fix typo --- QueryGenerator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/QueryGenerator.php b/QueryGenerator.php index 9448243..37b5d0f 100644 --- a/QueryGenerator.php +++ b/QueryGenerator.php @@ -88,7 +88,7 @@ protected static function prepareParam(array $params) */ protected static function where($where) { - return \empty($where) ? '' : "WHERE $where"; + return empty($where) ? '' : "WHERE $where"; } /** From 23e5a8eb662040d93061173fa23210724a28baa7 Mon Sep 17 00:00:00 2001 From: Joanhey Date: Wed, 23 Oct 2019 09:56:55 +0200 Subject: [PATCH 008/148] Fix more typos --- QueryGenerator.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/QueryGenerator.php b/QueryGenerator.php index 37b5d0f..69a4e0d 100644 --- a/QueryGenerator.php +++ b/QueryGenerator.php @@ -99,7 +99,7 @@ protected static function where($where) */ protected static function group($group) { - return \empty($group) ? '' : "GROUP BY $group"; + return empty($group) ? '' : "GROUP BY $group"; } /** @@ -110,7 +110,7 @@ protected static function group($group) */ protected static function having($having) { - return \empty($having) ? '' : "HAVING $having"; + return empty($having) ? '' : "HAVING $having"; } /** From 6d424ce24ee6f2f193546360d3992b326901e163 Mon Sep 17 00:00:00 2001 From: Joanhey Date: Wed, 23 Oct 2019 10:00:39 +0200 Subject: [PATCH 009/148] Update phpdoc --- QueryGenerator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/QueryGenerator.php b/QueryGenerator.php index 69a4e0d..cfaf701 100644 --- a/QueryGenerator.php +++ b/QueryGenerator.php @@ -68,7 +68,7 @@ public static function select($source, $type, array $params) * en base a los parámetros $params. * * @param array $params - * @return array + * @return string[] */ protected static function prepareParam(array $params) { From a5f066145aa9d17b6b68d3201e40addce15ebef0 Mon Sep 17 00:00:00 2001 From: Joanhey Date: Wed, 23 Oct 2019 10:08:36 +0200 Subject: [PATCH 010/148] Update query generator --- QueryGenerator.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/QueryGenerator.php b/QueryGenerator.php index cfaf701..4d0f428 100644 --- a/QueryGenerator.php +++ b/QueryGenerator.php @@ -161,14 +161,12 @@ public static function insert(LiteRecord $model, array &$data) */ protected static function insertField($field, LiteRecord $model, array &$data, array &$values) { - $meta = $model::metadata(); - $withDefault = $meta->getWithDefault(); - $autoFields = $meta->getAutoFields(); + //$meta = $model::metadata(); if (self::haveValue($model, $field)) { $data[":$field"] = $model->$field; $values[] = ":$field"; } else { -//if (!\in_array($field, $withDefault) && !\in_array($field, $autoFields)) { + //if (!\in_array($field, $meta->getWithDefault()) && !\in_array($field, $meta->getAutoFields())) { $values[] = 'NULL'; } } From bcca66fda886fe433630e906434181574b0bdfba Mon Sep 17 00:00:00 2001 From: Joanhey Date: Wed, 23 Oct 2019 10:27:25 +0200 Subject: [PATCH 011/148] Add constant VERSION in BaseRecord, Lite and Active extend from it --- BaseRecord.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/BaseRecord.php b/BaseRecord.php index 3a03ada..c083951 100644 --- a/BaseRecord.php +++ b/BaseRecord.php @@ -26,6 +26,9 @@ */ class BaseRecord { + + const VERSION = '0.4.0'; + /** * Database por defecto usa default. * From 48352cdd437c81d864acfc3f8288192b037c164a Mon Sep 17 00:00:00 2001 From: Joanhey Date: Wed, 23 Oct 2019 10:51:24 +0200 Subject: [PATCH 012/148] Update baserecord --- BaseRecord.php | 62 ++++++++++++++++++++++++++------------------------ 1 file changed, 32 insertions(+), 30 deletions(-) diff --git a/BaseRecord.php b/BaseRecord.php index c083951..acb6b41 100644 --- a/BaseRecord.php +++ b/BaseRecord.php @@ -19,6 +19,8 @@ */ namespace Kumbia\ActiveRecord; +use \PDO; +use \PDOStatement; use \KumbiaException; /** @@ -48,16 +50,16 @@ class BaseRecord * * @param array $data */ - public function __construct(array $data = []) + public function __construct(array $data = []) : void { $this->dump($data); } /** * Get the Primary Key value for the object - * @return mixed + * @return string */ - public function pk() + public function pk() : string { $pk = static::getPK(); @@ -69,7 +71,7 @@ public function pk() * * @param array $data */ - public function dump(array $data = []) + public function dump(array $data = []) : void { foreach ($data as $k => $v) { $this->$k = $v; @@ -79,9 +81,9 @@ public function dump(array $data = []) /** * Listado de los campos. * - * @return array + * @return string[] */ - public function getFields() + public function getFields() : array { return \array_keys(\get_object_vars($this)); } @@ -89,9 +91,9 @@ public function getFields() /** * Alias de los campos. * - * @return array + * @return string[] */ - public function getAlias() + public function getAlias() : array { return \array_map('ucwords', $this->getFields()); } @@ -114,7 +116,7 @@ protected function hasPK() * * @return string */ - public static function getPK() + public static function getPK() : string { if (static::$pk) { return static::$pk; @@ -128,7 +130,7 @@ public static function getPK() * * @return string smallcase del nombre de la clase */ - public static function getTable() + public static function getTable() : string { $split = \explode('\\', \get_called_class()); $table = \preg_replace('/[A-Z]/', '_$0', \lcfirst(\end($split))); @@ -141,7 +143,7 @@ public static function getTable() * * @return string */ - public static function getSchema() + public static function getSchema() : string { return ''; } @@ -151,7 +153,7 @@ public static function getSchema() * * @return string */ - public static function getSource() + public static function getSource() : string { $source = static::getTable(); if ($schema = static::getSchema()) { @@ -166,7 +168,7 @@ public static function getSource() * * @return string */ - public static function getDatabase() + public static function getDatabase() : string { return static::$database; } @@ -176,7 +178,7 @@ public static function getDatabase() * * @return Metadata\Metadata */ - public static function metadata() + public static function metadata() : Metadata\Metadata { return Metadata\Metadata::get( static::getDriver(), @@ -190,9 +192,9 @@ public static function metadata() * Obtiene manejador de conexion a la base de datos. * * @param bool $force forzar nueva conexion PDO - * @return \PDO + * @return PDO */ - protected static function dbh($force = \false) + protected static function dbh(bool $force = \false) : PDO { return Db::get(static::getDatabase(), $force); } @@ -202,9 +204,9 @@ protected static function dbh($force = \false) * * @param string $sql * @throws \PDOException - * @return \PDOStatement + * @return PDOStatement */ - public static function prepare($sql) + public static function prepare(string $sql) : PDOStatement { $sth = self::dbh()->prepare($sql); $sth->setFetchMode(\PDO::FETCH_CLASS, \get_called_class()); @@ -217,7 +219,7 @@ public static function prepare($sql) * * @return string */ - public static function lastInsertId() + public static function lastInsertId() : string { return self::dbh()->lastInsertId(); } @@ -227,9 +229,9 @@ public static function lastInsertId() * * @param string $sql * @throws \PDOException - * @return \PDOStatement + * @return PDOStatement */ - public static function sql($sql) + public static function sql(string $sql) : PDOStatement { $sth = self::dbh()->query($sql); $sth->setFetchMode(\PDO::FETCH_CLASS, \get_called_class()); @@ -241,10 +243,10 @@ public static function sql($sql) * Ejecuta consulta sql. * * @param string $sql - * @param array | string $values valores + * @param array|string $values valores * @return bool|\PDOStatement */ - public static function query($sql, $values = null) + public static function query(string $sql, $values = \null) { if (\func_num_args() === 1) { return self::sql($sql); @@ -264,7 +266,7 @@ public static function query($sql, $values = null) * @param string $pk valor para clave primaria * @return bool */ - public static function exists($pk) + public static function exists($pk) : bool { $source = static::getSource(); $pkField = static::getPK(); @@ -281,9 +283,9 @@ public static function exists($pk) * @param array $values valores * @return Paginator */ - public static function paginateQuery($sql, $page, $perPage, $values = []) + public static function paginateQuery(string $sql, int $page, int $perPage, $values = []) : Paginator { - return new Paginator(\get_called_class(), $sql, (int) $page, (int) $perPage, $values); + return new Paginator(\get_called_class(), $sql, $page, $perPage, $values); } /** @@ -291,7 +293,7 @@ public static function paginateQuery($sql, $page, $perPage, $values = []) * * @return string */ - public static function getDriver() + public static function getDriver() : string { return self::dbh()->getAttribute(\PDO::ATTR_DRIVER_NAME); } @@ -301,7 +303,7 @@ public static function getDriver() * * @return bool */ - public static function begin() + public static function begin() : bool { return self::dbh()->beginTransaction(); } @@ -311,7 +313,7 @@ public static function begin() * * @return bool */ - public static function rollback() + public static function rollback() : bool { return self::dbh()->rollBack(); } @@ -321,7 +323,7 @@ public static function rollback() * * @return bool */ - public static function commit() + public static function commit() : bool { return self::dbh()->commit(); } From 582d86a342557c94c6712f52a7b81349fe26f100 Mon Sep 17 00:00:00 2001 From: Joanhey Date: Wed, 23 Oct 2019 16:07:43 +0200 Subject: [PATCH 013/148] Update type hints --- BaseRecord.php | 42 +++++++++++++++++++++--------------------- LiteRecord.php | 32 ++++++++++++++++---------------- 2 files changed, 37 insertions(+), 37 deletions(-) diff --git a/BaseRecord.php b/BaseRecord.php index acb6b41..e5d0704 100644 --- a/BaseRecord.php +++ b/BaseRecord.php @@ -50,7 +50,7 @@ class BaseRecord * * @param array $data */ - public function __construct(array $data = []) : void + public function __construct(array $data = []) { $this->dump($data); } @@ -59,7 +59,7 @@ public function __construct(array $data = []) : void * Get the Primary Key value for the object * @return string */ - public function pk() : string + public function pk(): string { $pk = static::getPK(); @@ -71,7 +71,7 @@ public function pk() : string * * @param array $data */ - public function dump(array $data = []) : void + public function dump(array $data = []): void { foreach ($data as $k => $v) { $this->$k = $v; @@ -83,7 +83,7 @@ public function dump(array $data = []) : void * * @return string[] */ - public function getFields() : array + public function getFields(): array { return \array_keys(\get_object_vars($this)); } @@ -93,7 +93,7 @@ public function getFields() : array * * @return string[] */ - public function getAlias() : array + public function getAlias(): array { return \array_map('ucwords', $this->getFields()); } @@ -116,7 +116,7 @@ protected function hasPK() * * @return string */ - public static function getPK() : string + public static function getPK(): string { if (static::$pk) { return static::$pk; @@ -130,7 +130,7 @@ public static function getPK() : string * * @return string smallcase del nombre de la clase */ - public static function getTable() : string + public static function getTable(): string { $split = \explode('\\', \get_called_class()); $table = \preg_replace('/[A-Z]/', '_$0', \lcfirst(\end($split))); @@ -143,7 +143,7 @@ public static function getTable() : string * * @return string */ - public static function getSchema() : string + public static function getSchema(): string { return ''; } @@ -153,7 +153,7 @@ public static function getSchema() : string * * @return string */ - public static function getSource() : string + public static function getSource(): string { $source = static::getTable(); if ($schema = static::getSchema()) { @@ -168,7 +168,7 @@ public static function getSource() : string * * @return string */ - public static function getDatabase() : string + public static function getDatabase(): string { return static::$database; } @@ -178,7 +178,7 @@ public static function getDatabase() : string * * @return Metadata\Metadata */ - public static function metadata() : Metadata\Metadata + public static function metadata(): Metadata\Metadata { return Metadata\Metadata::get( static::getDriver(), @@ -194,7 +194,7 @@ public static function metadata() : Metadata\Metadata * @param bool $force forzar nueva conexion PDO * @return PDO */ - protected static function dbh(bool $force = \false) : PDO + protected static function dbh(bool $force = \false): PDO { return Db::get(static::getDatabase(), $force); } @@ -206,7 +206,7 @@ protected static function dbh(bool $force = \false) : PDO * @throws \PDOException * @return PDOStatement */ - public static function prepare(string $sql) : PDOStatement + public static function prepare(string $sql): PDOStatement { $sth = self::dbh()->prepare($sql); $sth->setFetchMode(\PDO::FETCH_CLASS, \get_called_class()); @@ -219,7 +219,7 @@ public static function prepare(string $sql) : PDOStatement * * @return string */ - public static function lastInsertId() : string + public static function lastInsertId(): string { return self::dbh()->lastInsertId(); } @@ -231,7 +231,7 @@ public static function lastInsertId() : string * @throws \PDOException * @return PDOStatement */ - public static function sql(string $sql) : PDOStatement + public static function sql(string $sql): PDOStatement { $sth = self::dbh()->query($sql); $sth->setFetchMode(\PDO::FETCH_CLASS, \get_called_class()); @@ -266,7 +266,7 @@ public static function query(string $sql, $values = \null) * @param string $pk valor para clave primaria * @return bool */ - public static function exists($pk) : bool + public static function exists($pk): bool { $source = static::getSource(); $pkField = static::getPK(); @@ -283,7 +283,7 @@ public static function exists($pk) : bool * @param array $values valores * @return Paginator */ - public static function paginateQuery(string $sql, int $page, int $perPage, $values = []) : Paginator + public static function paginateQuery(string $sql, int $page, int $perPage, $values = []): Paginator { return new Paginator(\get_called_class(), $sql, $page, $perPage, $values); } @@ -293,7 +293,7 @@ public static function paginateQuery(string $sql, int $page, int $perPage, $valu * * @return string */ - public static function getDriver() : string + public static function getDriver(): string { return self::dbh()->getAttribute(\PDO::ATTR_DRIVER_NAME); } @@ -303,7 +303,7 @@ public static function getDriver() : string * * @return bool */ - public static function begin() : bool + public static function begin(): bool { return self::dbh()->beginTransaction(); } @@ -313,7 +313,7 @@ public static function begin() : bool * * @return bool */ - public static function rollback() : bool + public static function rollback(): bool { return self::dbh()->rollBack(); } @@ -323,7 +323,7 @@ public static function rollback() : bool * * @return bool */ - public static function commit() : bool + public static function commit(): bool { return self::dbh()->commit(); } diff --git a/LiteRecord.php b/LiteRecord.php index e1e1d45..a00baec 100644 --- a/LiteRecord.php +++ b/LiteRecord.php @@ -30,7 +30,7 @@ class LiteRecord extends BaseRecord * @param string $id valor para clave primaria * @return self */ - public function __invoke($id) + public function __invoke($id): self { return self::get($id); } @@ -39,9 +39,9 @@ public function __invoke($id) * Invoca el callback. * * @param string $callback - * @return mixed + * @return bool */ - protected function callback($callback) + protected function callback(string $callback): bool { if (\method_exists($this, $callback)) { return $this->$callback(); @@ -55,7 +55,7 @@ protected function callback($callback) * @throws \PDOException * @return bool */ - public function create(array $data = []) + public function create(array $data = []): bool { $this->dump($data); @@ -95,7 +95,7 @@ public function create(array $data = []) * @throws \KumbiaException * @return bool */ - public function update(array $data = []) + public function update(array $data = []): bool { $this->dump($data); // Callback antes de actualizar @@ -121,7 +121,7 @@ public function update(array $data = []) * @param array $data * @return bool */ - public function save(array $data = []) + public function save(array $data = []): bool { $this->dump($data); @@ -146,7 +146,7 @@ public function save(array $data = []) * * @return string */ - protected function saveMethod() + protected function saveMethod(): string { $pk = static::getPK(); @@ -157,15 +157,15 @@ protected function saveMethod() /** * Eliminar registro por pk. * - * @param int $pk valor para clave primaria + * @param string $pk valor para clave primaria * @return bool */ - public static function delete($pk) + public static function delete($pk): bool { $source = static::getSource(); $pkField = static::getPK(); - return static::query("DELETE FROM $source WHERE $pkField = ?", (int) $pk)->rowCount() > 0; + return static::query("DELETE FROM $source WHERE $pkField = ?", $pk)->rowCount() > 0; } /** @@ -173,9 +173,9 @@ public static function delete($pk) * * @param string $pk valor para clave primaria * @param string $fields campos que se desean obtener separados por coma - * @return LiteRecord + * @return self */ - public static function get($pk, $fields = '*') + public static function get($pk, $fields = '*'): self { $source = static::getSource(); $pkField = static::getPK(); @@ -192,7 +192,7 @@ public static function get($pk, $fields = '*') * @param string | array $values * @return array */ - public static function all($sql = '', $values = \null) + public static function all($sql = '', $values = \null): array { if ( ! $sql) { $source = static::getSource(); @@ -205,11 +205,11 @@ public static function all($sql = '', $values = \null) /** * Obtiene el primer registro de la consulta sql. * - * @param string $sql - * @param string | array $values + * @param string $sql + * @param string|array $values * @return array */ - public static function first($sql, $values = \null) + public static function first($sql, $values = \null): array { return static::query($sql, $values)->fetch(); } From b5165551da3eb33e30479187668f3a2601a91b91 Mon Sep 17 00:00:00 2001 From: Joanhey Date: Sun, 27 Oct 2019 15:45:56 +0100 Subject: [PATCH 014/148] Use static::class php5.5 --- ActiveRecord.php | 8 ++++---- BaseRecord.php | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/ActiveRecord.php b/ActiveRecord.php index bf55cc8..c9f4ae6 100644 --- a/ActiveRecord.php +++ b/ActiveRecord.php @@ -118,7 +118,7 @@ public function populate($rel) */ public static function pagination($params = [], $values = [], $page = 1, $per_page = 10) { - $model = get_called_class(); + $model = static::class; unset($params['limit'], $params['offset']); $sql = QueryGenerator::select($model::getSource(), $model::getDriver(), $params); @@ -139,7 +139,7 @@ public static function updateAll(array $fields, $where = null, array $values = [ if ($values !== null && !is_array($values)) { $values = \array_slice(\func_get_args(), 2); } - $sql = QueryGenerator::updateAll(\get_called_class(), $fields, $values, $where); + $sql = QueryGenerator::updateAll(static::class, $fields, $values, $where); $sth = self::prepare($sql); $sth->execute($values); @@ -353,7 +353,7 @@ public static function count($where = null, $values = null) * * @return Paginator */ - public static function paginate(array $params, $page, $perPage, $values = null) + public static function paginate(array $params, int $page, int $perPage, $values = null) { unset($params['limit'], $params['offset']); $sql = QueryGenerator::select(static::getSource(), static::getDriver(), $params); @@ -363,7 +363,7 @@ public static function paginate(array $params, $page, $perPage, $values = null) $values = \array_slice(func_get_args(), 3); } - return new Paginator(\get_called_class(), $sql, (int) $page, (int) $perPage, $values); + return new Paginator(static::class, $sql, $page, $perPage, $values); } /** diff --git a/BaseRecord.php b/BaseRecord.php index e5d0704..21c70d7 100644 --- a/BaseRecord.php +++ b/BaseRecord.php @@ -120,7 +120,7 @@ public static function getPK(): string { if (static::$pk) { return static::$pk; - } + } return self::metadata()->getPK(); } @@ -209,7 +209,7 @@ protected static function dbh(bool $force = \false): PDO public static function prepare(string $sql): PDOStatement { $sth = self::dbh()->prepare($sql); - $sth->setFetchMode(\PDO::FETCH_CLASS, \get_called_class()); + $sth->setFetchMode(\PDO::FETCH_CLASS, static::class); return $sth; } @@ -234,7 +234,7 @@ public static function lastInsertId(): string public static function sql(string $sql): PDOStatement { $sth = self::dbh()->query($sql); - $sth->setFetchMode(\PDO::FETCH_CLASS, \get_called_class()); + $sth->setFetchMode(\PDO::FETCH_CLASS, static::class); return $sth; } @@ -285,7 +285,7 @@ public static function exists($pk): bool */ public static function paginateQuery(string $sql, int $page, int $perPage, $values = []): Paginator { - return new Paginator(\get_called_class(), $sql, $page, $perPage, $values); + return new Paginator(static::class, $sql, $page, $perPage, $values); } /** From dd5117fd01011c77ab6f5a24ce47cae93ca8ffa2 Mon Sep 17 00:00:00 2001 From: Joanhey Date: Sun, 27 Oct 2019 15:49:36 +0100 Subject: [PATCH 015/148] Update ucwords --- BaseRecord.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/BaseRecord.php b/BaseRecord.php index 21c70d7..d998d5b 100644 --- a/BaseRecord.php +++ b/BaseRecord.php @@ -95,7 +95,7 @@ public function getFields(): array */ public function getAlias(): array { - return \array_map('ucwords', $this->getFields()); + return \array_map('\ucwords', $this->getFields()); } /** From d9c7aaac3f5638b2c00e2d2c211662caebff0b79 Mon Sep 17 00:00:00 2001 From: Nelson Rojas Date: Mon, 3 Feb 2020 15:46:39 -0300 Subject: [PATCH 016/148] cambio en cabecera de licencia 2020 --- ActiveRecord.php | 2 +- Autoloader.php | 2 +- Db.php | 2 +- Metadata/Metadata.php | 2 +- Metadata/MysqlMetadata.php | 2 +- Metadata/PgsqlMetadata.php | 2 +- Metadata/SqlsrvMetadata.php | 2 +- Paginator.php | 2 +- Query/mysql_last_insert_id.php | 2 +- Query/mysql_limit.php | 2 +- Query/pgsql_last_insert_id.php | 2 +- Query/pgsql_limit.php | 2 +- QueryGenerator.php | 2 +- 13 files changed, 13 insertions(+), 13 deletions(-) diff --git a/ActiveRecord.php b/ActiveRecord.php index bf55cc8..29b6262 100644 --- a/ActiveRecord.php +++ b/ActiveRecord.php @@ -14,7 +14,7 @@ * * @category Kumbia * - * @copyright 2005 - 2016 Kumbia Team (http://www.kumbiaphp.com) + * @copyright 2005 - 2020 Kumbia Team (http://www.kumbiaphp.com) * @license http://wiki.kumbiaphp.com/Licencia New BSD License */ diff --git a/Autoloader.php b/Autoloader.php index 4050269..ed39c53 100644 --- a/Autoloader.php +++ b/Autoloader.php @@ -14,7 +14,7 @@ * * @category Kumbia * - * @copyright 2005 - 2016 Kumbia Team (http://www.kumbiaphp.com) + * @copyright 2005 - 2020 Kumbia Team (http://www.kumbiaphp.com) * @license http://wiki.kumbiaphp.com/Licencia New BSD License */ namespace Kumbia\ActiveRecord; diff --git a/Db.php b/Db.php index 5164bd1..ab84d39 100644 --- a/Db.php +++ b/Db.php @@ -14,7 +14,7 @@ * * @category Kumbia * - * @copyright 2005 - 2016 Kumbia Team (http://www.kumbiaphp.com) + * @copyright 2005 - 2020 Kumbia Team (http://www.kumbiaphp.com) * @license http://wiki.kumbiaphp.com/Licencia New BSD License */ namespace Kumbia\ActiveRecord; diff --git a/Metadata/Metadata.php b/Metadata/Metadata.php index d99a65e..55c672e 100644 --- a/Metadata/Metadata.php +++ b/Metadata/Metadata.php @@ -15,7 +15,7 @@ * * @category Kumbia * - * @copyright 2005 - 2016 Kumbia Team (http://www.kumbiaphp.com) + * @copyright 2005 - 2020 Kumbia Team (http://www.kumbiaphp.com) * @license http://wiki.kumbiaphp.com/Licencia New BSD License */ namespace Kumbia\ActiveRecord\Metadata; diff --git a/Metadata/MysqlMetadata.php b/Metadata/MysqlMetadata.php index a2fcda2..748caba 100644 --- a/Metadata/MysqlMetadata.php +++ b/Metadata/MysqlMetadata.php @@ -15,7 +15,7 @@ * * @category Kumbia * - * @copyright 2005 - 2016 Kumbia Team (http://www.kumbiaphp.com) + * @copyright 2005 - 2020 Kumbia Team (http://www.kumbiaphp.com) * @license http://wiki.kumbiaphp.com/Licencia New BSD License */ namespace Kumbia\ActiveRecord\Metadata; diff --git a/Metadata/PgsqlMetadata.php b/Metadata/PgsqlMetadata.php index 0e3df9e..dd3baa0 100644 --- a/Metadata/PgsqlMetadata.php +++ b/Metadata/PgsqlMetadata.php @@ -14,7 +14,7 @@ * * @category Kumbia * - * @copyright 2005 - 2016 Kumbia Team (http://www.kumbiaphp.com) + * @copyright 2005 - 2020 Kumbia Team (http://www.kumbiaphp.com) * @license http://wiki.kumbiaphp.com/Licencia New BSD License */ namespace Kumbia\ActiveRecord\Metadata; diff --git a/Metadata/SqlsrvMetadata.php b/Metadata/SqlsrvMetadata.php index 632748f..ec018f0 100644 --- a/Metadata/SqlsrvMetadata.php +++ b/Metadata/SqlsrvMetadata.php @@ -16,7 +16,7 @@ * @package ActiveRecord * @subpackage Metadata * - * @copyright 2005 - 2016 Kumbia Team (http://www.kumbiaphp.com) + * @copyright 2005 - 2020 Kumbia Team (http://www.kumbiaphp.com) * @license http://wiki.kumbiaphp.com/Licencia New BSD License */ namespace Kumbia\ActiveRecord\Metadata; diff --git a/Paginator.php b/Paginator.php index de64ded..4b93b3b 100644 --- a/Paginator.php +++ b/Paginator.php @@ -14,7 +14,7 @@ * * @category Kumbia * - * @copyright 2005 - 2016 Kumbia Team (http://www.kumbiaphp.com) + * @copyright 2005 - 2020 Kumbia Team (http://www.kumbiaphp.com) * @license http://wiki.kumbiaphp.com/Licencia New BSD License */ diff --git a/Query/mysql_last_insert_id.php b/Query/mysql_last_insert_id.php index 6331f32..8e521bf 100644 --- a/Query/mysql_last_insert_id.php +++ b/Query/mysql_last_insert_id.php @@ -14,7 +14,7 @@ * * @category Kumbia * - * @copyright 2005 - 2016 Kumbia Team (http://www.kumbiaphp.com) + * @copyright 2005 - 2020 Kumbia Team (http://www.kumbiaphp.com) * @license http://wiki.kumbiaphp.com/Licencia New BSD License */ namespace Kumbia\ActiveRecord\Query; diff --git a/Query/mysql_limit.php b/Query/mysql_limit.php index 33bfba3..c07618f 100644 --- a/Query/mysql_limit.php +++ b/Query/mysql_limit.php @@ -14,7 +14,7 @@ * * @category Kumbia * - * @copyright 2005 - 2016 Kumbia Team (http://www.kumbiaphp.com) + * @copyright 2005 - 2020 Kumbia Team (http://www.kumbiaphp.com) * @license http://wiki.kumbiaphp.com/Licencia New BSD License */ diff --git a/Query/pgsql_last_insert_id.php b/Query/pgsql_last_insert_id.php index 2daae2c..9a29686 100644 --- a/Query/pgsql_last_insert_id.php +++ b/Query/pgsql_last_insert_id.php @@ -14,7 +14,7 @@ * * @category Kumbia * - * @copyright 2005 - 2016 Kumbia Team (http://www.kumbiaphp.com) + * @copyright 2005 - 2020 Kumbia Team (http://www.kumbiaphp.com) * @license http://wiki.kumbiaphp.com/Licencia New BSD License */ namespace Kumbia\ActiveRecord\Query; diff --git a/Query/pgsql_limit.php b/Query/pgsql_limit.php index dff71ed..7e54369 100644 --- a/Query/pgsql_limit.php +++ b/Query/pgsql_limit.php @@ -14,7 +14,7 @@ * * @category Kumbia * - * @copyright 2005 - 2016 Kumbia Team (http://www.kumbiaphp.com) + * @copyright 2005 - 2020 Kumbia Team (http://www.kumbiaphp.com) * @license http://wiki.kumbiaphp.com/Licencia New BSD License */ namespace Kumbia\ActiveRecord\Query; diff --git a/QueryGenerator.php b/QueryGenerator.php index 4d0f428..92795bf 100644 --- a/QueryGenerator.php +++ b/QueryGenerator.php @@ -14,7 +14,7 @@ * * @category Kumbia * - * @copyright 2005 - 2016 Kumbia Team (http://www.kumbiaphp.com) + * @copyright 2005 - 2020 Kumbia Team (http://www.kumbiaphp.com) * @license http://wiki.kumbiaphp.com/Licencia New BSD License */ namespace Kumbia\ActiveRecord; From 1e4df8efc41317bac83149616f4ff2eb74e239cf Mon Sep 17 00:00:00 2001 From: Joanhey Date: Sat, 28 Mar 2020 13:57:31 +0100 Subject: [PATCH 017/148] Update LiteRecord --- LiteRecord.php | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/LiteRecord.php b/LiteRecord.php index a00baec..2d37767 100644 --- a/LiteRecord.php +++ b/LiteRecord.php @@ -39,6 +39,7 @@ public function __invoke($id): self * Invoca el callback. * * @param string $callback + * * @return bool */ protected function callback(string $callback): bool @@ -52,6 +53,7 @@ protected function callback(string $callback): bool * Crear registro. * * @param array $data + * * @throws \PDOException * @return bool */ @@ -71,7 +73,7 @@ public function create(array $data = []): bool } // Verifica si la PK es autogenerada - $pk = static::getPK(); + $pk = static::$pk; if ( ! isset($this->$pk)) { $this->$pk = QueryGenerator::query( static::getDriver(), @@ -92,6 +94,7 @@ public function create(array $data = []): bool * Actualizar registro. * * @param array $data + * * @throws \KumbiaException * @return bool */ @@ -119,6 +122,7 @@ public function update(array $data = []): bool * Guardar registro. * * @param array $data + * * @return bool */ public function save(array $data = []): bool @@ -148,39 +152,35 @@ public function save(array $data = []): bool */ protected function saveMethod(): string { - $pk = static::getPK(); - - return (empty($this->$pk) || ! static::exists($this->$pk)) ? - 'create' : 'update'; + return static::hasPK() ? 'create' : 'update'; } /** * Eliminar registro por pk. * * @param string $pk valor para clave primaria + * * @return bool */ public static function delete($pk): bool { $source = static::getSource(); - $pkField = static::getPK(); - + $pkField = static::$pk; + // use pdo->execute() return static::query("DELETE FROM $source WHERE $pkField = ?", $pk)->rowCount() > 0; } /** * Buscar por clave primaria. * - * @param string $pk valor para clave primaria + * @param string $pk valor para clave primaria * @param string $fields campos que se desean obtener separados por coma + * * @return self */ public static function get($pk, $fields = '*'): self { - $source = static::getSource(); - $pkField = static::getPK(); - - $sql = "SELECT $fields FROM $source WHERE $pkField = ?"; + $sql = "SELECT $fields FROM ".static::getSource().' WHERE '.static::$pk.' = ?'; return static::query($sql, $pk)->fetch(); } @@ -188,15 +188,15 @@ public static function get($pk, $fields = '*'): self /** * Obtiene todos los registros de la consulta sql. * - * @param string $sql - * @param string | array $values + * @param string $sql + * @param string|array $values + * * @return array */ public static function all($sql = '', $values = \null): array { if ( ! $sql) { - $source = static::getSource(); - $sql = "SELECT * FROM $source"; + $sql = 'SELECT * FROM '.static::getSource(); } return static::query($sql, $values)->fetchAll(); @@ -207,9 +207,10 @@ public static function all($sql = '', $values = \null): array * * @param string $sql * @param string|array $values + * * @return array */ - public static function first($sql, $values = \null): array + public static function first($sql, $values = \null): self { return static::query($sql, $values)->fetch(); } From 4da5f648729a455ddca36a9738eb1533a2a97269 Mon Sep 17 00:00:00 2001 From: Joanhey Date: Sat, 28 Mar 2020 13:57:31 +0100 Subject: [PATCH 018/148] Update LiteRecord From 5e1444f54bf540f2655818c4dd626a9703a2f578 Mon Sep 17 00:00:00 2001 From: Joanhey Date: Sat, 28 Mar 2020 14:18:15 +0100 Subject: [PATCH 019/148] Update pk in QueryGenerator --- QueryGenerator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/QueryGenerator.php b/QueryGenerator.php index 92795bf..ec1a5d4 100644 --- a/QueryGenerator.php +++ b/QueryGenerator.php @@ -193,7 +193,7 @@ protected static function haveValue(LiteRecord $model, $field) public static function update(LiteRecord $model, array &$data) { $set = []; - $pk = $model::getPK(); + $pk = $model::$pk; /*elimina la clave primaria*/ $list = \array_diff($model::metadata()->getFieldsList(), [$pk]); foreach ($list as $field) { From bd8c2b3f9ff004743c723a9d8a0400fea1f86daa Mon Sep 17 00:00:00 2001 From: Joanhey Date: Sat, 28 Mar 2020 14:25:27 +0100 Subject: [PATCH 020/148] Test php > 7.2 in travis --- .travis.yml | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/.travis.yml b/.travis.yml index e1d4847..d056125 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,19 +2,14 @@ language: php dist: trusty php: - - 5.4 - - 5.5 - - 5.6 - - 7.0 - - 7.1 - 7.2 - 7.3 + - 7.4 - master matrix: allow_failures: - php: master - - php: 5.5 notifications: slack: kumbiaphp:51JaKQTXASwf52D8b32OyWb9 From f3ed3dcac2f3ca1789c3e254c2773d04885ce20d Mon Sep 17 00:00:00 2001 From: Joanhey Date: Sat, 28 Mar 2020 14:43:08 +0100 Subject: [PATCH 021/148] Update callback return --- LiteRecord.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LiteRecord.php b/LiteRecord.php index 2d37767..2342eb9 100644 --- a/LiteRecord.php +++ b/LiteRecord.php @@ -42,7 +42,7 @@ public function __invoke($id): self * * @return bool */ - protected function callback(string $callback): bool + protected function callback(string $callback): ?bool { if (\method_exists($this, $callback)) { return $this->$callback(); From 77eb1ec258ab2fab6fe778d9754fbd2b191215fa Mon Sep 17 00:00:00 2001 From: Joanhey Date: Sat, 28 Mar 2020 14:58:39 +0100 Subject: [PATCH 022/148] Travis tests php >= 7.3 --- .travis.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index d056125..5576681 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,7 +2,6 @@ language: php dist: trusty php: - - 7.2 - 7.3 - 7.4 - master From 9ea057bc1180b39bfe6e07b5215359a7098819a1 Mon Sep 17 00:00:00 2001 From: Joanhey Date: Sat, 28 Mar 2020 15:02:25 +0100 Subject: [PATCH 023/148] Delete strict return in callback() for now --- LiteRecord.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LiteRecord.php b/LiteRecord.php index 2342eb9..d66c52c 100644 --- a/LiteRecord.php +++ b/LiteRecord.php @@ -42,7 +42,7 @@ public function __invoke($id): self * * @return bool */ - protected function callback(string $callback): ?bool + protected function callback(string $callback) { if (\method_exists($this, $callback)) { return $this->$callback(); From 2642fe410a1d394bd72bcd8827f5063f37fa0c7f Mon Sep 17 00:00:00 2001 From: Joanhey Date: Sat, 28 Mar 2020 15:09:56 +0100 Subject: [PATCH 024/148] QueryGenerator use getPK() --- QueryGenerator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/QueryGenerator.php b/QueryGenerator.php index ec1a5d4..29d891a 100644 --- a/QueryGenerator.php +++ b/QueryGenerator.php @@ -193,7 +193,7 @@ protected static function haveValue(LiteRecord $model, $field) public static function update(LiteRecord $model, array &$data) { $set = []; - $pk = $model::$pk; + $pk = $model::getPK; /*elimina la clave primaria*/ $list = \array_diff($model::metadata()->getFieldsList(), [$pk]); foreach ($list as $field) { From da26a8e595fd79291d3ad9d1db837b5b71de9456 Mon Sep 17 00:00:00 2001 From: Joanhey Date: Sat, 28 Mar 2020 15:10:40 +0100 Subject: [PATCH 025/148] Fix getPK() --- QueryGenerator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/QueryGenerator.php b/QueryGenerator.php index 29d891a..92795bf 100644 --- a/QueryGenerator.php +++ b/QueryGenerator.php @@ -193,7 +193,7 @@ protected static function haveValue(LiteRecord $model, $field) public static function update(LiteRecord $model, array &$data) { $set = []; - $pk = $model::getPK; + $pk = $model::getPK(); /*elimina la clave primaria*/ $list = \array_diff($model::metadata()->getFieldsList(), [$pk]); foreach ($list as $field) { From 996c23532dba2a491303c5e449a5d6d77f548bd2 Mon Sep 17 00:00:00 2001 From: Joanhey Date: Sat, 28 Mar 2020 16:17:16 +0100 Subject: [PATCH 026/148] query(), first() and all() need values in array $sql, [ 1, 2, 3] no more strings --- BaseRecord.php | 11 ++++------- LiteRecord.php | 10 +++++----- 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/BaseRecord.php b/BaseRecord.php index d998d5b..326a97c 100644 --- a/BaseRecord.php +++ b/BaseRecord.php @@ -242,20 +242,17 @@ public static function sql(string $sql): PDOStatement /** * Ejecuta consulta sql. * - * @param string $sql - * @param array|string $values valores + * @param string $sql + * @param array $values valores * @return bool|\PDOStatement */ - public static function query(string $sql, $values = \null) + public static function query(string $sql, array $values = []) { - if (\func_num_args() === 1) { + if (empty($values)) { return self::sql($sql); } $sth = self::prepare($sql); - if ( ! \is_array($values)) { - $values = \array_slice(\func_get_args(), 1); - } return $sth->execute($values) ? $sth : \false; } diff --git a/LiteRecord.php b/LiteRecord.php index d66c52c..cf5c162 100644 --- a/LiteRecord.php +++ b/LiteRecord.php @@ -189,11 +189,11 @@ public static function get($pk, $fields = '*'): self * Obtiene todos los registros de la consulta sql. * * @param string $sql - * @param string|array $values + * @param array $values * * @return array */ - public static function all($sql = '', $values = \null): array + public static function all(string $sql = '', array $values = []): array { if ( ! $sql) { $sql = 'SELECT * FROM '.static::getSource(); @@ -206,11 +206,11 @@ public static function all($sql = '', $values = \null): array * Obtiene el primer registro de la consulta sql. * * @param string $sql - * @param string|array $values + * @param array $values * - * @return array + * @return self */ - public static function first($sql, $values = \null): self + public static function first(string $sql, array $values = []): self { return static::query($sql, $values)->fetch(); } From 70b2efae7f1062e086bd91e7600f44b08cb016c5 Mon Sep 17 00:00:00 2001 From: Joanhey Date: Sat, 28 Mar 2020 16:24:33 +0100 Subject: [PATCH 027/148] Changes to use array or null to send values --- BaseRecord.php | 6 +++--- LiteRecord.php | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/BaseRecord.php b/BaseRecord.php index 326a97c..1195b72 100644 --- a/BaseRecord.php +++ b/BaseRecord.php @@ -243,10 +243,10 @@ public static function sql(string $sql): PDOStatement * Ejecuta consulta sql. * * @param string $sql - * @param array $values valores + * @param array|null $values valores * @return bool|\PDOStatement */ - public static function query(string $sql, array $values = []) + public static function query(string $sql, ?array $values = []) { if (empty($values)) { return self::sql($sql); @@ -268,7 +268,7 @@ public static function exists($pk): bool $source = static::getSource(); $pkField = static::getPK(); - return self::query("SELECT COUNT(*) AS count FROM $source WHERE $pkField = ?", $pk)->fetch()->count > 0; + return self::query("SELECT COUNT(*) AS count FROM $source WHERE $pkField = ?", [$pk])->fetch()->count > 0; } /** diff --git a/LiteRecord.php b/LiteRecord.php index cf5c162..c919807 100644 --- a/LiteRecord.php +++ b/LiteRecord.php @@ -167,7 +167,7 @@ public static function delete($pk): bool $source = static::getSource(); $pkField = static::$pk; // use pdo->execute() - return static::query("DELETE FROM $source WHERE $pkField = ?", $pk)->rowCount() > 0; + return static::query("DELETE FROM $source WHERE $pkField = ?", [$pk])->rowCount() > 0; } /** @@ -182,7 +182,7 @@ public static function get($pk, $fields = '*'): self { $sql = "SELECT $fields FROM ".static::getSource().' WHERE '.static::$pk.' = ?'; - return static::query($sql, $pk)->fetch(); + return static::query($sql, [$pk])->fetch(); } /** From 035c16e26b3b88aca4c4c443526a4b9208567f18 Mon Sep 17 00:00:00 2001 From: Joanhey Date: Sat, 28 Mar 2020 19:42:00 +0100 Subject: [PATCH 028/148] Delete force reload in Db class Never used or necessary --- Db.php | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/Db.php b/Db.php index ab84d39..d7c8f1f 100644 --- a/Db.php +++ b/Db.php @@ -45,21 +45,11 @@ abstract class Db * * @throw KumbiaException * @param string $database base de datos a conectar - * @param bool $force forzar nueva conexion PDO * @return PDO */ - public static function get($database = 'default', $force = \false) + public static function get($database = 'default') { - // Verifica el singleton - if ( ! $force && isset(self::$pool[$database])) { - return self::$pool[$database]; - } - - if ($force) { - return self::connect(self::getConfig($database)); - } - - return self::$pool[$database] = self::connect(self::getConfig($database)); + return self::$pool[$database] ?? self::$pool[$database] = self::connect(self::getConfig($database)); } /** From b1c71f5a0989d489ba3aa4a6cb767340a8970176 Mon Sep 17 00:00:00 2001 From: Joanhey Date: Sat, 28 Mar 2020 19:42:52 +0100 Subject: [PATCH 029/148] Optimize BaseRecord --- BaseRecord.php | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/BaseRecord.php b/BaseRecord.php index 1195b72..90818a8 100644 --- a/BaseRecord.php +++ b/BaseRecord.php @@ -61,9 +61,7 @@ public function __construct(array $data = []) */ public function pk(): string { - $pk = static::getPK(); - - return $this->$pk; + return $this->{static::$pk}; } /** @@ -105,8 +103,7 @@ public function getAlias(): array */ protected function hasPK() { - $pk = static::getPK(); - if (empty($this->$pk)) { + if (empty($this->{static::$pk})) { throw new KumbiaException(_('No se ha especificado valor para la clave primaria')); } } @@ -118,11 +115,7 @@ protected function hasPK() */ public static function getPK(): string { - if (static::$pk) { - return static::$pk; - } - - return self::metadata()->getPK(); + return static::$pk ?? static::$pk = self::metadata()->getPK(); } /** @@ -191,12 +184,11 @@ public static function metadata(): Metadata\Metadata /** * Obtiene manejador de conexion a la base de datos. * - * @param bool $force forzar nueva conexion PDO * @return PDO */ - protected static function dbh(bool $force = \false): PDO + protected static function dbh(): PDO { - return Db::get(static::getDatabase(), $force); + return Db::get(static::getDatabase()); } /** From e5e928790dfed794476a6217919c5710f7236a19 Mon Sep 17 00:00:00 2001 From: Joanhey Date: Sat, 28 Mar 2020 19:45:15 +0100 Subject: [PATCH 030/148] Force types in Db class --- Db.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Db.php b/Db.php index d7c8f1f..d4ee2ee 100644 --- a/Db.php +++ b/Db.php @@ -47,7 +47,7 @@ abstract class Db * @param string $database base de datos a conectar * @return PDO */ - public static function get($database = 'default') + public static function get(string $database = 'default') { return self::$pool[$database] ?? self::$pool[$database] = self::connect(self::getConfig($database)); } @@ -59,7 +59,7 @@ public static function get($database = 'default') * @throws \RuntimeException * @return PDO */ - private static function connect($config) + private static function connect(array $config) { try { return new PDO($config['dsn'], $config['username'], $config['password'], $config['params']); @@ -77,7 +77,7 @@ private static function connect($config) * @throws \RuntimeException * @return array */ - private static function getConfig($database) + private static function getConfig(string $database) { if (empty(self::$config)) { // Leer la configuración de conexión @@ -104,6 +104,6 @@ private static function getConfig($database) */ public static function setConfig(array $value) { - self::$config = [] + self::$config + $value; + self::$config = [] + self::$config + $value; //TODO retornar PDOstatement } } From aab0fb61f7ce6e5396c76363fc8f5d021b39d50a Mon Sep 17 00:00:00 2001 From: Joanhey Date: Sat, 28 Mar 2020 19:48:30 +0100 Subject: [PATCH 031/148] Force return types in Db class --- Db.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Db.php b/Db.php index d4ee2ee..754dd27 100644 --- a/Db.php +++ b/Db.php @@ -19,7 +19,7 @@ */ namespace Kumbia\ActiveRecord; -use PDO; +use \PDO; /** * Manejador de conexiones de base de datos. @@ -47,7 +47,7 @@ abstract class Db * @param string $database base de datos a conectar * @return PDO */ - public static function get(string $database = 'default') + public static function get(string $database = 'default'): PDO { return self::$pool[$database] ?? self::$pool[$database] = self::connect(self::getConfig($database)); } @@ -59,7 +59,7 @@ public static function get(string $database = 'default') * @throws \RuntimeException * @return PDO */ - private static function connect(array $config) + private static function connect(array $config): PDO { try { return new PDO($config['dsn'], $config['username'], $config['password'], $config['params']); @@ -77,7 +77,7 @@ private static function connect(array $config) * @throws \RuntimeException * @return array */ - private static function getConfig(string $database) + private static function getConfig(string $database): array { if (empty(self::$config)) { // Leer la configuración de conexión @@ -104,6 +104,6 @@ private static function getConfig(string $database) */ public static function setConfig(array $value) { - self::$config = [] + self::$config + $value; //TODO retornar PDOstatement + self::$config = [] + self::$config + $value; //TODO retornar PDO } } From 71f708338df10b1a32d5ce4afe53e987da8c2c60 Mon Sep 17 00:00:00 2001 From: Joanhey Date: Sat, 28 Mar 2020 20:01:31 +0100 Subject: [PATCH 032/148] Delete from test Db force --- tests/ActiveRecord/DbTest.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/tests/ActiveRecord/DbTest.php b/tests/ActiveRecord/DbTest.php index 166ef79..75303b2 100644 --- a/tests/ActiveRecord/DbTest.php +++ b/tests/ActiveRecord/DbTest.php @@ -18,9 +18,5 @@ public function testGet() $instance2 = Db::get($GLOBALS['config_database']); $this->assertEquals($instance, $instance2); - - $instance3 = Db::get($GLOBALS['config_database'], true); - - $this->assertFalse($instance === $instance3); } } From f25be26be126a41e884a301cec9f8c7ff3475442 Mon Sep 17 00:00:00 2001 From: Joanhey Date: Sat, 28 Mar 2020 23:17:46 +0100 Subject: [PATCH 033/148] Travis update to ubuntu bionic 18.04 --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 5576681..bfed4ee 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,5 @@ language: php -dist: trusty +dist: bionic php: - 7.3 From cbaaf34420c91fc01a8422932959448fd1e68c00 Mon Sep 17 00:00:00 2001 From: Joanhey Date: Sat, 28 Mar 2020 23:21:51 +0100 Subject: [PATCH 034/148] Add mysql service to travis --- .travis.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.travis.yml b/.travis.yml index bfed4ee..ddc190a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -16,6 +16,8 @@ notifications: # email: # - programador.manuel@gmail.com +services: + - mysql env: - DB=mysql From 2d7085fd895b643580f7e2f66dd5ef01c8e9b955 Mon Sep 17 00:00:00 2001 From: Joanhey Date: Sat, 28 Mar 2020 23:37:12 +0100 Subject: [PATCH 035/148] Join create table in travis --- .travis.yml | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index ddc190a..92dfc1b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -27,8 +27,11 @@ env: before_script: - sh -c "if [ '$DB' = 'pgsql' ]; then psql -c 'DROP DATABASE IF EXISTS test;' -U postgres; fi" - sh -c "if [ '$DB' = 'pgsql' ]; then psql -c 'create database test;' -U postgres; fi" - - sh -c "if [ '$DB' = 'mysql' ]; then mysql -e 'create database IF NOT EXISTS test;'; fi" - - sh -c "if [ '$DB' = 'mysql' ]; then mysql -e 'CREATE TABLE IF NOT EXISTS test.usuario ( id int(11) NOT NULL DEFAULT 0, nombre varchar(50) NOT NULL , email varchar(100) NOT NULL , activo tinyint(1) NULL DEFAULT 1 , PRIMARY KEY (id) );'; fi" + - sh -c "if [ '$DB' = 'mysql' ]; then mysql -e 'create database IF NOT EXISTS test; + CREATE TABLE IF NOT EXISTS test.usuario ( id int(11) NOT NULL DEFAULT 0, nombre varchar(50) NOT NULL , email varchar(100) NOT NULL , activo tinyint(1) NULL DEFAULT 1 , PRIMARY KEY (id) );' + ; fi" + +# - sh -c "if [ '$DB' = 'mysql' ]; then mysql -e 'CREATE TABLE IF NOT EXISTS test.usuario ( id int(11) NOT NULL DEFAULT 0, nombre varchar(50) NOT NULL , email varchar(100) NOT NULL , activo tinyint(1) NULL DEFAULT 1 , PRIMARY KEY (id) );'; fi" script: - phpunit --configuration tests/phpunit_$DB.xml --coverage-text --colors --coverage-clover=coverage.clover From 483cd18b50bef7cb2c5a0bda5f8277502a119839 Mon Sep 17 00:00:00 2001 From: Joanhey Date: Sat, 28 Mar 2020 23:47:40 +0100 Subject: [PATCH 036/148] Add postgres to travis --- .travis.yml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/.travis.yml b/.travis.yml index 92dfc1b..03c3e02 100644 --- a/.travis.yml +++ b/.travis.yml @@ -21,18 +21,18 @@ services: env: - DB=mysql -# - DB=pgsql + - DB=pgsql # - DB=sqlite before_script: - sh -c "if [ '$DB' = 'pgsql' ]; then psql -c 'DROP DATABASE IF EXISTS test;' -U postgres; fi" - - sh -c "if [ '$DB' = 'pgsql' ]; then psql -c 'create database test;' -U postgres; fi" - - sh -c "if [ '$DB' = 'mysql' ]; then mysql -e 'create database IF NOT EXISTS test; + - sh -c "if [ '$DB' = 'pgsql' ]; then psql -c 'CREATE DATABASE test; + CREATE TABLE IF NOT EXISTS test.usuario ( id int(11) NOT NULL DEFAULT 0, nombre varchar(50) NOT NULL , email varchar(100) NOT NULL , activo tinyint(1) NULL DEFAULT 1 , PRIMARY KEY (id) );' -U postgres + ; fi" + - sh -c "if [ '$DB' = 'mysql' ]; then mysql -e 'CREATE DATABASE IF NOT EXISTS test; CREATE TABLE IF NOT EXISTS test.usuario ( id int(11) NOT NULL DEFAULT 0, nombre varchar(50) NOT NULL , email varchar(100) NOT NULL , activo tinyint(1) NULL DEFAULT 1 , PRIMARY KEY (id) );' ; fi" -# - sh -c "if [ '$DB' = 'mysql' ]; then mysql -e 'CREATE TABLE IF NOT EXISTS test.usuario ( id int(11) NOT NULL DEFAULT 0, nombre varchar(50) NOT NULL , email varchar(100) NOT NULL , activo tinyint(1) NULL DEFAULT 1 , PRIMARY KEY (id) );'; fi" - script: - phpunit --configuration tests/phpunit_$DB.xml --coverage-text --colors --coverage-clover=coverage.clover - wget https://scrutinizer-ci.com/ocular.phar From af0a4ec74b6bcdd8e02e1eceef856800e12c06c1 Mon Sep 17 00:00:00 2001 From: Joanhey Date: Sun, 29 Mar 2020 00:07:58 +0100 Subject: [PATCH 037/148] Changes to use pgsql in travis --- .travis.yml | 2 +- tests/phpunit_mysql.xml | 2 +- tests/phpunit_pgsql.xml | 3 ++- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 03c3e02..8ee70aa 100644 --- a/.travis.yml +++ b/.travis.yml @@ -27,7 +27,7 @@ env: before_script: - sh -c "if [ '$DB' = 'pgsql' ]; then psql -c 'DROP DATABASE IF EXISTS test;' -U postgres; fi" - sh -c "if [ '$DB' = 'pgsql' ]; then psql -c 'CREATE DATABASE test; - CREATE TABLE IF NOT EXISTS test.usuario ( id int(11) NOT NULL DEFAULT 0, nombre varchar(50) NOT NULL , email varchar(100) NOT NULL , activo tinyint(1) NULL DEFAULT 1 , PRIMARY KEY (id) );' -U postgres + CREATE TABLE IF NOT EXISTS test.usuario ( id int(11) NOT NULL DEFAULT 0, nombre varchar(50) NOT NULL , email varchar(100) NOT NULL , activo tinyint(1) NULL DEFAULT 1 , PRIMARY KEY (id) );' -U postgres ; fi" - sh -c "if [ '$DB' = 'mysql' ]; then mysql -e 'CREATE DATABASE IF NOT EXISTS test; CREATE TABLE IF NOT EXISTS test.usuario ( id int(11) NOT NULL DEFAULT 0, nombre varchar(50) NOT NULL , email varchar(100) NOT NULL , activo tinyint(1) NULL DEFAULT 1 , PRIMARY KEY (id) );' diff --git a/tests/phpunit_mysql.xml b/tests/phpunit_mysql.xml index 4d0df85..f2936dd 100644 --- a/tests/phpunit_mysql.xml +++ b/tests/phpunit_mysql.xml @@ -2,7 +2,7 @@ - + diff --git a/tests/phpunit_pgsql.xml b/tests/phpunit_pgsql.xml index 3632e3c..394524f 100644 --- a/tests/phpunit_pgsql.xml +++ b/tests/phpunit_pgsql.xml @@ -2,7 +2,8 @@ - + + From c2d08a3f6074c62752ea35a99703f0b21acaee17 Mon Sep 17 00:00:00 2001 From: Joanhey Date: Sun, 29 Mar 2020 00:39:10 +0100 Subject: [PATCH 038/148] Add postgresql service in travis --- .travis.yml | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 8ee70aa..4a01a1f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -18,15 +18,16 @@ notifications: services: - mysql + - postgresql env: - DB=mysql - - DB=pgsql + - DB=postgresql # - DB=sqlite before_script: - - sh -c "if [ '$DB' = 'pgsql' ]; then psql -c 'DROP DATABASE IF EXISTS test;' -U postgres; fi" - - sh -c "if [ '$DB' = 'pgsql' ]; then psql -c 'CREATE DATABASE test; + - sh -c "if [ '$DB' = 'postgresql' ]; then psql -c 'DROP DATABASE IF EXISTS test;' -U postgres; fi" + - sh -c "if [ '$DB' = 'postgresql' ]; then psql -c 'CREATE DATABASE test; CREATE TABLE IF NOT EXISTS test.usuario ( id int(11) NOT NULL DEFAULT 0, nombre varchar(50) NOT NULL , email varchar(100) NOT NULL , activo tinyint(1) NULL DEFAULT 1 , PRIMARY KEY (id) );' -U postgres ; fi" - sh -c "if [ '$DB' = 'mysql' ]; then mysql -e 'CREATE DATABASE IF NOT EXISTS test; From 4e6c44524dbc919945f0b3dd9471610098c16c94 Mon Sep 17 00:00:00 2001 From: Joanhey Date: Sun, 29 Mar 2020 00:57:48 +0100 Subject: [PATCH 039/148] Update postgresql in travis --- .travis.yml | 17 ++++++++++++++--- tests/ActiveRecord/Metadata/MetadataTest.php | 2 +- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index 4a01a1f..f81d41c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -28,10 +28,21 @@ env: before_script: - sh -c "if [ '$DB' = 'postgresql' ]; then psql -c 'DROP DATABASE IF EXISTS test;' -U postgres; fi" - sh -c "if [ '$DB' = 'postgresql' ]; then psql -c 'CREATE DATABASE test; - CREATE TABLE IF NOT EXISTS test.usuario ( id int(11) NOT NULL DEFAULT 0, nombre varchar(50) NOT NULL , email varchar(100) NOT NULL , activo tinyint(1) NULL DEFAULT 1 , PRIMARY KEY (id) );' -U postgres - ; fi" + CREATE TABLE IF NOT EXISTS test.usuario ( + id serial PRIMARY KEY, + nombre varchar(50) NOT NULL , + email varchar(100) NOT NULL , + activo smallint(1) NULL DEFAULT 1 , + PRIMARY KEY (id) );' + -U postgres; + fi" - sh -c "if [ '$DB' = 'mysql' ]; then mysql -e 'CREATE DATABASE IF NOT EXISTS test; - CREATE TABLE IF NOT EXISTS test.usuario ( id int(11) NOT NULL DEFAULT 0, nombre varchar(50) NOT NULL , email varchar(100) NOT NULL , activo tinyint(1) NULL DEFAULT 1 , PRIMARY KEY (id) );' + CREATE TABLE IF NOT EXISTS test.usuario ( + id int(11) NOT NULL DEFAULT 0, + nombre varchar(50) NOT NULL , + email varchar(100) NOT NULL , + activo smallint(1) NULL DEFAULT 1 , + PRIMARY KEY (id) );' ; fi" script: diff --git a/tests/ActiveRecord/Metadata/MetadataTest.php b/tests/ActiveRecord/Metadata/MetadataTest.php index 91c87a6..cd53bcb 100644 --- a/tests/ActiveRecord/Metadata/MetadataTest.php +++ b/tests/ActiveRecord/Metadata/MetadataTest.php @@ -59,7 +59,7 @@ public function testMethod_getFields() $this->fieldData($fields['id'], 'int(11)', false, 'PRI', true, false); $this->fieldData($fields['nombre'], 'varchar(50)', false, '', false, false); $this->fieldData($fields['email'], 'varchar(100)', false, '', false, false); - $this->fieldData($fields['activo'], 'tinyint(1)', true, '', true, false); + $this->fieldData($fields['activo'], 'smallint(1)', true, '', true, false); } protected function fieldData($field, $type, $null, $key, $default, $auto) From e4e98dba5eb92f07b10442b089cadb4468d0af6c Mon Sep 17 00:00:00 2001 From: Joanhey Date: Sun, 29 Mar 2020 01:04:03 +0100 Subject: [PATCH 040/148] Fix create table pgsql in travis --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index f81d41c..6265088 100644 --- a/.travis.yml +++ b/.travis.yml @@ -32,7 +32,7 @@ before_script: id serial PRIMARY KEY, nombre varchar(50) NOT NULL , email varchar(100) NOT NULL , - activo smallint(1) NULL DEFAULT 1 , + activo smallint NULL DEFAULT 1 , PRIMARY KEY (id) );' -U postgres; fi" From a5612a4085941018eb5e0902504e2a9517cc0164 Mon Sep 17 00:00:00 2001 From: Joanhey Date: Sun, 29 Mar 2020 01:09:48 +0100 Subject: [PATCH 041/148] Fix create table pgsql travis --- .travis.yml | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/.travis.yml b/.travis.yml index 6265088..cece38e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -27,13 +27,13 @@ env: before_script: - sh -c "if [ '$DB' = 'postgresql' ]; then psql -c 'DROP DATABASE IF EXISTS test;' -U postgres; fi" - - sh -c "if [ '$DB' = 'postgresql' ]; then psql -c 'CREATE DATABASE test; - CREATE TABLE IF NOT EXISTS test.usuario ( - id serial PRIMARY KEY, - nombre varchar(50) NOT NULL , - email varchar(100) NOT NULL , - activo smallint NULL DEFAULT 1 , - PRIMARY KEY (id) );' + - sh -c "if [ '$DB' = 'postgresql' ]; then psql -c 'CREATE DATABASE test;' -U postgres; fi" + - sh -c "if [ '$DB' = 'postgresql' ]; then psql -c 'CREATE TABLE IF NOT EXISTS test.usuario ( + id serial PRIMARY KEY, + nombre varchar(50) NOT NULL , + email varchar(100) NOT NULL , + activo smallint NULL DEFAULT 1 , + PRIMARY KEY (id) );' -U postgres; fi" - sh -c "if [ '$DB' = 'mysql' ]; then mysql -e 'CREATE DATABASE IF NOT EXISTS test; From 74d14af6de43b80919432a1878d189f223d64125 Mon Sep 17 00:00:00 2001 From: Joanhey Date: Sun, 29 Mar 2020 01:18:48 +0100 Subject: [PATCH 042/148] Pgsql in travis --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index cece38e..af71f8f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -28,7 +28,7 @@ env: before_script: - sh -c "if [ '$DB' = 'postgresql' ]; then psql -c 'DROP DATABASE IF EXISTS test;' -U postgres; fi" - sh -c "if [ '$DB' = 'postgresql' ]; then psql -c 'CREATE DATABASE test;' -U postgres; fi" - - sh -c "if [ '$DB' = 'postgresql' ]; then psql -c 'CREATE TABLE IF NOT EXISTS test.usuario ( + - sh -c "if [ '$DB' = 'postgresql' ]; then psql -c 'CREATE TABLE IF NOT EXISTS usuario ( id serial PRIMARY KEY, nombre varchar(50) NOT NULL , email varchar(100) NOT NULL , From cda38a98c2588f23959d51475860ac5e6ec50469 Mon Sep 17 00:00:00 2001 From: Joanhey Date: Sun, 29 Mar 2020 01:23:58 +0100 Subject: [PATCH 043/148] Fix pgsql travis --- .travis.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index af71f8f..054d610 100644 --- a/.travis.yml +++ b/.travis.yml @@ -32,8 +32,8 @@ before_script: id serial PRIMARY KEY, nombre varchar(50) NOT NULL , email varchar(100) NOT NULL , - activo smallint NULL DEFAULT 1 , - PRIMARY KEY (id) );' + activo smallint NULL DEFAULT 1 + );' -U postgres; fi" - sh -c "if [ '$DB' = 'mysql' ]; then mysql -e 'CREATE DATABASE IF NOT EXISTS test; From a78f63b487e6a4e1c280f50340e4c2cbccc707fd Mon Sep 17 00:00:00 2001 From: Joanhey Date: Sun, 29 Mar 2020 01:29:06 +0100 Subject: [PATCH 044/148] Rename phunit pgsql .xml --- tests/{phpunit_pgsql.xml => phpunit_postggesql.xml} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename tests/{phpunit_pgsql.xml => phpunit_postggesql.xml} (100%) diff --git a/tests/phpunit_pgsql.xml b/tests/phpunit_postggesql.xml similarity index 100% rename from tests/phpunit_pgsql.xml rename to tests/phpunit_postggesql.xml From b0c7ae0a20f60eacb6bb46a0253d56bb85610177 Mon Sep 17 00:00:00 2001 From: Joanhey Date: Sun, 29 Mar 2020 01:33:30 +0100 Subject: [PATCH 045/148] Fix typo in name --- tests/{phpunit_postggesql.xml => phpunit_postgresql.xml} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename tests/{phpunit_postggesql.xml => phpunit_postgresql.xml} (100%) diff --git a/tests/phpunit_postggesql.xml b/tests/phpunit_postgresql.xml similarity index 100% rename from tests/phpunit_postggesql.xml rename to tests/phpunit_postgresql.xml From 18bf3a09ab3209a30d23d6d463221c7533e00f0c Mon Sep 17 00:00:00 2001 From: Joanhey Date: Sun, 29 Mar 2020 01:36:48 +0100 Subject: [PATCH 046/148] Fix dir in postresql config test --- tests/phpunit_postgresql.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit_postgresql.xml b/tests/phpunit_postgresql.xml index 394524f..142f135 100644 --- a/tests/phpunit_postgresql.xml +++ b/tests/phpunit_postgresql.xml @@ -10,7 +10,7 @@ - ./Tests/ + . From 2b19cc5af292ac40f785b6fc7b9176e567ee67e3 Mon Sep 17 00:00:00 2001 From: Joanhey Date: Sun, 29 Mar 2020 01:40:47 +0100 Subject: [PATCH 047/148] Fix path to load boostrap in tests --- tests/phpunit_postgresql.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit_postgresql.xml b/tests/phpunit_postgresql.xml index 142f135..703c0ca 100644 --- a/tests/phpunit_postgresql.xml +++ b/tests/phpunit_postgresql.xml @@ -1,6 +1,6 @@ - + From a88f4c4e15739298e15a3012a33021d7a71c83bb Mon Sep 17 00:00:00 2001 From: Joanhey Date: Sun, 29 Mar 2020 01:44:05 +0100 Subject: [PATCH 048/148] Add pgsql in databases.php config --- tests/config/databases.php | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/tests/config/databases.php b/tests/config/databases.php index 015ea34..9b4e002 100644 --- a/tests/config/databases.php +++ b/tests/config/databases.php @@ -1,24 +1,27 @@ [ 'dsn' => 'mysql:host=127.0.0.1;dbname=test;charset=utf8', 'username' => 'root', 'password' => '', 'params' => [ - //PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8', //UTF8 en PHP < 5.3.6 - PDO::ATTR_PERSISTENT => true, //conexión persistente - PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, - ], -]; + \PDO::ATTR_PERSISTENT => \true, //conexión persistente + \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION + ] + ], + //Conexión a pgsql + 'pgsql' => [ + 'dsn' => 'pgsql:dbname=test;host=localhost', + 'username' => 'postgres', + 'password' => '', + 'params' => [ + \PDO::ATTR_PERSISTENT => \true, //conexión persistente + \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION + ] + ], -////Conexión a sqlite ejemplo -//$databases['pgsql'] = array( -// 'dsn' => "sqlite:{APP_PATH}/temp/mydb.sq3", -// 'username' => NULL, -// 'password' => NULL, -// 'params' => array(PDO::ATTR_PERSISTENT => true) -//); -return $databases; + //Más conexiones +]; \ No newline at end of file From 36f5e58c82ee0b74b6693d5f48c5ae1c45c69628 Mon Sep 17 00:00:00 2001 From: Joanhey Date: Sun, 29 Mar 2020 01:50:54 +0100 Subject: [PATCH 049/148] Add metadata type in pgsql config --- tests/phpunit_postgresql.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/phpunit_postgresql.xml b/tests/phpunit_postgresql.xml index 703c0ca..54a43f6 100644 --- a/tests/phpunit_postgresql.xml +++ b/tests/phpunit_postgresql.xml @@ -4,6 +4,7 @@ + From 73a1ad411e1302386015eb51f9250420d8e45982 Mon Sep 17 00:00:00 2001 From: Joanhey Date: Sun, 29 Mar 2020 01:57:12 +0100 Subject: [PATCH 050/148] Add more metadata in pgsql config tests --- tests/phpunit_postgresql.xml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/phpunit_postgresql.xml b/tests/phpunit_postgresql.xml index 54a43f6..9daf2c1 100644 --- a/tests/phpunit_postgresql.xml +++ b/tests/phpunit_postgresql.xml @@ -5,6 +5,8 @@ + + From 314e25673b77dbd318cc06cbab76201ede178860 Mon Sep 17 00:00:00 2001 From: Joanhey Date: Mon, 30 Mar 2020 12:34:35 +0200 Subject: [PATCH 051/148] PgsqlMetaddata use objects as MysqlMetadata --- Metadata/PgsqlMetadata.php | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/Metadata/PgsqlMetadata.php b/Metadata/PgsqlMetadata.php index dd3baa0..60df544 100644 --- a/Metadata/PgsqlMetadata.php +++ b/Metadata/PgsqlMetadata.php @@ -32,11 +32,11 @@ class PgsqlMetadata extends Metadata * @param string $database base de datos * @param string $table tabla * @param string $schema squema + * * @return array */ - protected function queryFields($database, $table, $schema = 'public') + protected function queryFields(string $database, string $table, string $schema = 'public'): array { - // Nota: Se excluyen claves compuestas $describe = Db::get($database)->query( "SELECT DISTINCT @@ -54,29 +54,32 @@ protected function queryFields($database, $table, $schema = 'public') LEFT OUTER JOIN information_schema.table_constraints tc ON (cu.constraint_name = tc.constraint_name AND tc.constraint_type IN ('PRIMARY KEY', 'UNIQUE')) - WHERE c.table_name = '$table' AND c.table_schema = '$schema';" + WHERE c.table_name = '$table' AND c.table_schema = '$schema';", + + \PDO::FETCH_OBJ ); - return self::describe($describe); + return self::describe($describe->fetchAll()); } /** * Genera la metadata. * - * @param \PDOStatement $describe + * @param array $describe + * * @return array */ - private static function describe(\PDOStatement $describe) + private function describe(array $describe): array { $fields = []; // TODO mejorar este código foreach ($describe as $value) { - $fields[$value['field']] = [ - 'Type' => $value['type'], - 'Null' => $value['null'] != 'NO', - 'Default' => $value['default'] != '', - 'Key' => \substr($value['key'], 0, 3), - 'Auto' => \preg_match('/^nextval\(/', $value['default']) + $fields[$value->Field] = [ + 'Type' => $value->Type, + 'Null' => $value->Null !== 'NO', + 'Default' => $value->Default != '', + 'Key' => \substr($value->Key, 0, 3), + 'Auto' => \preg_match('/^nextval\(/', $value->Default) ]; } From 0e23c0fc6b6961e539cd90e6ad72afceb30ca22c Mon Sep 17 00:00:00 2001 From: Joanhey Date: Mon, 30 Mar 2020 12:35:06 +0200 Subject: [PATCH 052/148] Add method that populate the Medata class --- Metadata/PgsqlMetadata.php | 1 + 1 file changed, 1 insertion(+) diff --git a/Metadata/PgsqlMetadata.php b/Metadata/PgsqlMetadata.php index 60df544..3e806bf 100644 --- a/Metadata/PgsqlMetadata.php +++ b/Metadata/PgsqlMetadata.php @@ -81,6 +81,7 @@ private function describe(array $describe): array 'Key' => \substr($value->Key, 0, 3), 'Auto' => \preg_match('/^nextval\(/', $value->Default) ]; + $this->filterCol($fields[$value->Field], $value->Field); } return $fields; From 704a29e35736349e2e4236bdcdc040612aae719f Mon Sep 17 00:00:00 2001 From: Joanhey Date: Mon, 30 Mar 2020 12:38:42 +0200 Subject: [PATCH 053/148] Clean and faster Metadata class --- Metadata/Metadata.php | 60 +++++++++++++++++++++---------------------- 1 file changed, 29 insertions(+), 31 deletions(-) diff --git a/Metadata/Metadata.php b/Metadata/Metadata.php index 55c672e..8284dee 100644 --- a/Metadata/Metadata.php +++ b/Metadata/Metadata.php @@ -28,21 +28,21 @@ abstract class Metadata /** * Singleton de metadata. * - * @var array + * @var self[] */ private static $instances = []; /** * Descripción de los campos. * - * @var array + * @var string[] */ protected $fields = []; /** * Lista de campos. * - * @var array + * @var string[] */ protected $fieldsList = []; @@ -56,14 +56,14 @@ abstract class Metadata /** * Campos con valor predeterminado. * - * @var array + * @var string[] */ protected $withDefault = []; /** * Campos con valor autogenerado. * - * @var array + * @var string[] */ protected $autoFields = []; @@ -74,15 +74,11 @@ abstract class Metadata * @param string $database * @param string $table * @param string $schema - * @return Metadata + * @return self */ - public static function get($type, $database, $table, $schema = null) + public static function get(string $type, string $database, string $table, string $schema = ''): self { - if (isset(self::$instances["$database.$table.$schema"])) { - return self::$instances["$database.$table.$schema"]; - } - - return self::getMetadata($type, $database, $table, $schema); + return self::$instances["$database.$table.$schema"] ?? self::getMetadata($type, $database, $table, $schema); } /** @@ -93,9 +89,10 @@ public static function get($type, $database, $table, $schema = null) * @param string $database * @param string $table * @param string $schema - * @return Metadata + * + * @return self */ - private static function getMetadata($type, $database, $table, $schema) + private static function getMetadata(string $type, string $database, string $table, string $schema): self { if (\PRODUCTION && ! (self::$instances["$database.$table.$schema"] = \Cache::driver()->get("$database.$table.$schema", 'ActiveRecord.Metadata'))) { return self::$instances["$database.$table.$schema"]; @@ -126,7 +123,7 @@ private static function getMetadata($type, $database, $table, $schema) * @param string $table tabla * @param string $schema squema */ - private function __construct($database, $table, $schema = \null) + private function __construct(string $database, string $table, string $schema = '') { $this->fields = $this->queryFields($database, $table, $schema); $this->fieldsList = \array_keys($this->fields); @@ -135,16 +132,16 @@ private function __construct($database, $table, $schema = \null) /** * Permite el filtrado de columna en PK, por Defecto y Autogenerado. * - * @param $m información de la columna - * @param $field nombre de la columna + * @param array $meta información de la columna + * @param string $field nombre de la columna */ - protected function filterCol($m, $field) + protected function filterCol(array $meta, string $field): void { - if ($m['Key'] == 'PRI') { + if ($meta['Key'] === 'PRI') { $this->pk = $field; - } elseif ($m['Default']) { + } elseif ($meta['Default']) { $this->withDefault[] = $field; - } elseif ($m['Auto']) { + } elseif ($meta['Auto']) { $this->autoFields[] = $field; } } @@ -155,16 +152,17 @@ protected function filterCol($m, $field) * @param string $database base de datos * @param string $table tabla * @param string $schema squema + * * @return array */ - abstract protected function queryFields($database, $table, $schema = \null); + abstract protected function queryFields(string $database, string $table, string $schema = ''): array; /** * Obtiene la descripción de los campos. * - * @return array + * @return string[] */ - public function getFields() + public function getFields(): array { return $this->fields; } @@ -172,9 +170,9 @@ public function getFields() /** * Obtiene la lista de campos. * - * @return array + * @return string[] */ - public function getFieldsList() + public function getFieldsList(): array { return $this->fieldsList; } @@ -184,7 +182,7 @@ public function getFieldsList() * * @return string */ - public function getPK() + public function getPK(): string { return $this->pk; } @@ -192,9 +190,9 @@ public function getPK() /** * Obtiene los campos con valor predeterminado. * - * @return array + * @return string[] */ - public function getWithDefault() + public function getWithDefault(): array { return $this->withDefault; } @@ -202,9 +200,9 @@ public function getWithDefault() /** * Obtiene los campos con valor generado automatico. * - * @return array + * @return string[] */ - public function getAutoFields() + public function getAutoFields(): array { return $this->autoFields; } From 2c222fc86e2bae8a6e7d571b3925418d8f6e150c Mon Sep 17 00:00:00 2001 From: Joanhey Date: Mon, 30 Mar 2020 12:42:59 +0200 Subject: [PATCH 054/148] Update types in MysqlMedata class --- Metadata/MysqlMetadata.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/Metadata/MysqlMetadata.php b/Metadata/MysqlMetadata.php index 748caba..e87510b 100644 --- a/Metadata/MysqlMetadata.php +++ b/Metadata/MysqlMetadata.php @@ -34,22 +34,23 @@ class MysqlMetadata extends Metadata * @param string $database base de datos * @param string $table tabla * @param string $schema squema + * * @return array */ - protected function queryFields($database, $table, $schema = \null) + protected function queryFields(string $database, string $table, string $schema = '') { $sql = $schema ? "DESCRIBE `$schema`.`$table`" : "DESCRIBE `$table`"; - $describe = Db::get($database)->query($sql); + $describe = Db::get($database)->query($sql, \PDO::FETCH_OBJ); $fields = []; // TODO mejorar este código - while ($value = $describe->fetch(\PDO::FETCH_OBJ)) { + while ($value = $describe->fetch()) { $fields[$value->Field] = [ 'Type' => $value->Type, - 'Null' => $value->Null != 'NO', + 'Null' => $value->Null !== 'NO', 'Key' => $value->Key, 'Default' => $value->Default != '', - 'Auto' => $value->Extra == 'auto_increment' + 'Auto' => $value->Extra === 'auto_increment' ]; $this->filterCol($fields[$value->Field], $value->Field); } From b6baa4df03a44c90b2fd2741337a8a289a15b85b Mon Sep 17 00:00:00 2001 From: Joanhey Date: Mon, 30 Mar 2020 12:47:48 +0200 Subject: [PATCH 055/148] Faster Metadata::getTable() and clean the class --- BaseRecord.php | 45 ++++++++++++++++++++++++++------------ tests/config/databases.php | 2 +- 2 files changed, 32 insertions(+), 15 deletions(-) diff --git a/BaseRecord.php b/BaseRecord.php index 90818a8..0f50f6d 100644 --- a/BaseRecord.php +++ b/BaseRecord.php @@ -21,6 +21,7 @@ use \PDO; use \PDOStatement; +use \PDOException; use \KumbiaException; /** @@ -38,6 +39,13 @@ class BaseRecord */ protected static $database = 'default'; + /** + * Nombre de la tabla. + * + * @var string + */ + protected static $table = ''; + /** * PK por defecto, si no existe mira en metadata. * @@ -93,6 +101,7 @@ public function getFields(): array */ public function getAlias(): array { + //$humanize = function () return \array_map('\ucwords', $this->getFields()); } @@ -103,7 +112,7 @@ public function getAlias(): array */ protected function hasPK() { - if (empty($this->{static::$pk})) { + if (! isset($this->{static::$pk})) { throw new KumbiaException(_('No se ha especificado valor para la clave primaria')); } } @@ -125,10 +134,14 @@ public static function getPK(): string */ public static function getTable(): string { + if (static::$table) { + return static::$table; + } + $split = \explode('\\', \get_called_class()); $table = \preg_replace('/[A-Z]/', '_$0', \lcfirst(\end($split))); - return \strtolower($table); + return static::$table = \strtolower($table); } /** @@ -184,9 +197,9 @@ public static function metadata(): Metadata\Metadata /** * Obtiene manejador de conexion a la base de datos. * - * @return PDO + * @return \PDO */ - protected static function dbh(): PDO + protected static function dbh(): \PDO { return Db::get(static::getDatabase()); } @@ -195,8 +208,9 @@ protected static function dbh(): PDO * Consulta sql preparada. * * @param string $sql + * * @throws \PDOException - * @return PDOStatement + * @return \PDOStatement */ public static function prepare(string $sql): PDOStatement { @@ -220,25 +234,25 @@ public static function lastInsertId(): string * Consulta sql. * * @param string $sql + * * @throws \PDOException - * @return PDOStatement + * @return \PDOStatement */ public static function sql(string $sql): PDOStatement { - $sth = self::dbh()->query($sql); - $sth->setFetchMode(\PDO::FETCH_CLASS, static::class); - - return $sth; + return self::dbh()->query($sql, \PDO::FETCH_CLASS, static::class); } /** * Ejecuta consulta sql. * * @param string $sql - * @param array|null $values valores - * @return bool|\PDOStatement + * @param array $values valores + * + * @throws PDOException + * @return bool|PDOStatement */ - public static function query(string $sql, ?array $values = []) + public static function query(string $sql, array $values = []) { if (empty($values)) { return self::sql($sql); @@ -272,7 +286,7 @@ public static function exists($pk): bool * @param array $values valores * @return Paginator */ - public static function paginateQuery(string $sql, int $page, int $perPage, $values = []): Paginator + public static function paginateQuery(string $sql, int $page, int $perPage, array $values = []): Paginator { return new Paginator(static::class, $sql, $page, $perPage, $values); } @@ -290,6 +304,7 @@ public static function getDriver(): string /** * Comienza una trasacción. * + * @throws PDOException If there is already a transaction started or the driver does not support transactions * @return bool */ public static function begin(): bool @@ -300,6 +315,7 @@ public static function begin(): bool /** * Da marcha atrás a una trasacción. * + * @throws PDOException if there is no active transaction. * @return bool */ public static function rollback(): bool @@ -310,6 +326,7 @@ public static function rollback(): bool /** * Realiza el commit de una trasacción. * + * @throws \PDOException if there is no active transaction. * @return bool */ public static function commit(): bool diff --git a/tests/config/databases.php b/tests/config/databases.php index 9b4e002..bab9044 100644 --- a/tests/config/databases.php +++ b/tests/config/databases.php @@ -24,4 +24,4 @@ //Más conexiones -]; \ No newline at end of file +]; From 10a7c2b27b622c51201d67d6fc0f0d13122e5c30 Mon Sep 17 00:00:00 2001 From: Joanhey Date: Mon, 30 Mar 2020 12:48:56 +0200 Subject: [PATCH 056/148] Better phpdoc in Db class --- Db.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Db.php b/Db.php index 754dd27..b6a368a 100644 --- a/Db.php +++ b/Db.php @@ -29,7 +29,7 @@ abstract class Db /** * Pool de conexiones. * - * @var array + * @var PDO[] */ private static $pool = []; @@ -43,8 +43,8 @@ abstract class Db /** * Obtiene manejador de conexión a la base de datos. * - * @throw KumbiaException * @param string $database base de datos a conectar + * * @return PDO */ public static function get(string $database = 'default'): PDO @@ -56,6 +56,7 @@ public static function get(string $database = 'default'): PDO * Conexión a la base de datos. * * @param array $config Config base de datos a conectar + * * @throws \RuntimeException * @return PDO */ @@ -74,6 +75,7 @@ private static function connect(array $config): PDO * Obtiene manejador de conexión a la base de datos. * * @param string $database base de datos a conectar + * * @throws \RuntimeException * @return array */ From 14b3e798c13bd1f87bad5e14d39c72c37df86c22 Mon Sep 17 00:00:00 2001 From: Joanhey Date: Mon, 30 Mar 2020 12:53:35 +0200 Subject: [PATCH 057/148] Add return array in Mysql queryFields() --- Metadata/MysqlMetadata.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Metadata/MysqlMetadata.php b/Metadata/MysqlMetadata.php index e87510b..1ce8afe 100644 --- a/Metadata/MysqlMetadata.php +++ b/Metadata/MysqlMetadata.php @@ -37,7 +37,7 @@ class MysqlMetadata extends Metadata * * @return array */ - protected function queryFields(string $database, string $table, string $schema = '') + protected function queryFields(string $database, string $table, string $schema = ''): array { $sql = $schema ? "DESCRIBE `$schema`.`$table`" : "DESCRIBE `$table`"; $describe = Db::get($database)->query($sql, \PDO::FETCH_OBJ); From 9fd3f2e793934118f854aec241991e57cd710b48 Mon Sep 17 00:00:00 2001 From: Joanhey Date: Mon, 30 Mar 2020 12:59:42 +0200 Subject: [PATCH 058/148] Update AR to return array in query $values --- ActiveRecord.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/ActiveRecord.php b/ActiveRecord.php index b5440b7..da6cd03 100644 --- a/ActiveRecord.php +++ b/ActiveRecord.php @@ -331,13 +331,11 @@ public static function allBy($field, $value, $params = []) * * @return int */ - public static function count($where = null, $values = null) + public static function count(string $where = '', array $values = []) { $source = static::getSource(); $sql = QueryGenerator::count($source, $where); - if ($values !== null && !is_array($values)) { - $values = \array_slice(\func_get_args(), 1); - } + $sth = static::query($sql, $values); return $sth->fetch()->count; From fd3c9cecef58303b4e24c7e27739acd89bd69f5a Mon Sep 17 00:00:00 2001 From: Joanhey Date: Mon, 30 Mar 2020 13:04:05 +0200 Subject: [PATCH 059/148] Delete unused code in ActiveRecord --- ActiveRecord.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/ActiveRecord.php b/ActiveRecord.php index da6cd03..71e97cf 100644 --- a/ActiveRecord.php +++ b/ActiveRecord.php @@ -134,11 +134,8 @@ public static function pagination($params = [], $values = [], $page = 1, $per_pa * * @return int numero de registros actualizados */ - public static function updateAll(array $fields, $where = null, array $values = []) + public static function updateAll(array $fields, string $where = '', array $values = []) { - if ($values !== null && !is_array($values)) { - $values = \array_slice(\func_get_args(), 2); - } $sql = QueryGenerator::updateAll(static::class, $fields, $values, $where); $sth = self::prepare($sql); $sth->execute($values); From d122b5917d2c38fc4aca5dd3a0033b3a73dfec04 Mon Sep 17 00:00:00 2001 From: Joanhey Date: Mon, 30 Mar 2020 13:35:16 +0200 Subject: [PATCH 060/148] Update pgsl get metadata sql --- Metadata/PgsqlMetadata.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Metadata/PgsqlMetadata.php b/Metadata/PgsqlMetadata.php index 3e806bf..a666ca6 100644 --- a/Metadata/PgsqlMetadata.php +++ b/Metadata/PgsqlMetadata.php @@ -40,11 +40,11 @@ protected function queryFields(string $database, string $table, string $schema = // Nota: Se excluyen claves compuestas $describe = Db::get($database)->query( "SELECT DISTINCT - c.column_name AS field, - c.udt_name AS type, - tc.constraint_type AS key, - c.column_default AS default, - c.is_nullable AS null + c.column_name AS Field, + c.udt_name AS Type, + tc.constraint_type AS Key, + c.column_default AS Default, + c.is_nullable AS Null FROM information_schema.columns c LEFT OUTER JOIN information_schema.key_column_usage cu ON ( cu.column_name = c.column_name AND cu.table_name = c.table_name AND ( From 5851f1fd3db811226724692792480d547f982d99 Mon Sep 17 00:00:00 2001 From: Joanhey Date: Mon, 30 Mar 2020 14:12:16 +0200 Subject: [PATCH 061/148] Faster getMedata() --- Metadata/Metadata.php | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/Metadata/Metadata.php b/Metadata/Metadata.php index 8284dee..98f8e3f 100644 --- a/Metadata/Metadata.php +++ b/Metadata/Metadata.php @@ -74,6 +74,7 @@ abstract class Metadata * @param string $database * @param string $table * @param string $schema + * * @return self */ public static function get(string $type, string $database, string $table, string $schema = ''): self @@ -94,26 +95,27 @@ public static function get(string $type, string $database, string $table, string */ private static function getMetadata(string $type, string $database, string $table, string $schema): self { - if (\PRODUCTION && ! (self::$instances["$database.$table.$schema"] = \Cache::driver()->get("$database.$table.$schema", 'ActiveRecord.Metadata'))) { - return self::$instances["$database.$table.$schema"]; + $key = "$database.$table.$schema"; + if (\PRODUCTION && ! (self::$instances[$key] = \Cache::driver()->get($key, 'ActiveRecord.Metadata'))) { + return self::$instances[$key]; } $class = \ucwords($type).'Metadata'; $class = __NAMESPACE__."\\$class"; - self::$instances["$database.$table.$schema"] = new $class($database, $table, $schema); + self::$instances[$key] = new $class($database, $table, $schema); // Cachea los metadatos if (\PRODUCTION) { \Cache::driver()->save( - self::$instances["$database.$table.$schema"], + self::$instances[$key], \Config::get('config.application.metadata_lifetime'), - "$database.$table.$schema", + $key, 'ActiveRecord.Metadata' ); } - return self::$instances["$database.$table.$schema"]; + return self::$instances[$key]; } /** From 53208b926ac433d00fff0ebddbab2bca37977ecd Mon Sep 17 00:00:00 2001 From: Joanhey Date: Mon, 30 Mar 2020 14:29:43 +0200 Subject: [PATCH 062/148] Change ucwords to ucfirst in getMetadata() --- Metadata/Metadata.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Metadata/Metadata.php b/Metadata/Metadata.php index 98f8e3f..71f85fe 100644 --- a/Metadata/Metadata.php +++ b/Metadata/Metadata.php @@ -99,7 +99,7 @@ private static function getMetadata(string $type, string $database, string $tabl if (\PRODUCTION && ! (self::$instances[$key] = \Cache::driver()->get($key, 'ActiveRecord.Metadata'))) { return self::$instances[$key]; } - $class = \ucwords($type).'Metadata'; + $class = \ucfirst($type).'Metadata'; $class = __NAMESPACE__."\\$class"; From 8a08e043bb94163488a94d911c7b9eecf29f612e Mon Sep 17 00:00:00 2001 From: Joanhey Date: Mon, 30 Mar 2020 15:08:15 +0200 Subject: [PATCH 063/148] Update PgsqlMetadata::class --- Metadata/PgsqlMetadata.php | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/Metadata/PgsqlMetadata.php b/Metadata/PgsqlMetadata.php index a666ca6..6e90406 100644 --- a/Metadata/PgsqlMetadata.php +++ b/Metadata/PgsqlMetadata.php @@ -40,11 +40,11 @@ protected function queryFields(string $database, string $table, string $schema = // Nota: Se excluyen claves compuestas $describe = Db::get($database)->query( "SELECT DISTINCT - c.column_name AS Field, - c.udt_name AS Type, - tc.constraint_type AS Key, - c.column_default AS Default, - c.is_nullable AS Null + c.column_name AS field, + c.udt_name AS type, + tc.constraint_type AS key, + c.column_default AS default, + c.is_nullable AS null FROM information_schema.columns c LEFT OUTER JOIN information_schema.key_column_usage cu ON ( cu.column_name = c.column_name AND cu.table_name = c.table_name AND ( @@ -74,14 +74,14 @@ private function describe(array $describe): array $fields = []; // TODO mejorar este código foreach ($describe as $value) { - $fields[$value->Field] = [ - 'Type' => $value->Type, - 'Null' => $value->Null !== 'NO', - 'Default' => $value->Default != '', - 'Key' => \substr($value->Key, 0, 3), - 'Auto' => \preg_match('/^nextval\(/', $value->Default) + $fields[$value->field] = [ + 'Type' => $value->type, + 'Null' => $value->null !== 'NO', + 'Default' => $value->default != '', + 'Key' => \substr($value->key, 0, 3), + 'Auto' => \preg_match('/^nextval\(/', $value->default) ]; - $this->filterCol($fields[$value->Field], $value->Field); + $this->filterCol($fields[$value->field], $value->field); } return $fields; From 79c49869cb32f7faa966f6e44c007e3209872b9c Mon Sep 17 00:00:00 2001 From: Joanhey Date: Mon, 30 Mar 2020 16:10:03 +0200 Subject: [PATCH 064/148] Change phpdoc for Metadata->fields --- Metadata/Metadata.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Metadata/Metadata.php b/Metadata/Metadata.php index 71f85fe..e797494 100644 --- a/Metadata/Metadata.php +++ b/Metadata/Metadata.php @@ -35,7 +35,7 @@ abstract class Metadata /** * Descripción de los campos. * - * @var string[] + * @var array */ protected $fields = []; From c08c923b080edfc35a1a5c1740d94aba7c2e37c3 Mon Sep 17 00:00:00 2001 From: Joanhey Date: Mon, 30 Mar 2020 20:23:43 +0200 Subject: [PATCH 065/148] Use only PSR4 to autoload --- Autoloader.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Autoloader.php b/Autoloader.php index ed39c53..7ea2763 100644 --- a/Autoloader.php +++ b/Autoloader.php @@ -43,7 +43,7 @@ public static function autoload($className) return; } $className = \substr($className, 19); - $fileName = \str_replace(['_', '\\'], \DIRECTORY_SEPARATOR, $className).'.php'; + $fileName = \str_replace('\\', \DIRECTORY_SEPARATOR, $className).'.php'; require __DIR__.$fileName; } } From f0adce25965b990dac0533581d13b35fc6804e5d Mon Sep 17 00:00:00 2001 From: Joanhey Date: Mon, 30 Mar 2020 20:30:11 +0200 Subject: [PATCH 066/148] Clean Autoloader --- Autoloader.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Autoloader.php b/Autoloader.php index 7ea2763..41c6798 100644 --- a/Autoloader.php +++ b/Autoloader.php @@ -42,8 +42,8 @@ public static function autoload($className) if (0 !== \strpos($className, 'Kumbia\\ActiveRecord')) { return; } - $className = \substr($className, 19); - $fileName = \str_replace('\\', \DIRECTORY_SEPARATOR, $className).'.php'; + + $fileName = \str_replace('\\', \DIRECTORY_SEPARATOR, \substr($className, 19)).'.php'; require __DIR__.$fileName; } } From f63d47c2c219c8495d19286648b89bd55a53912e Mon Sep 17 00:00:00 2001 From: Joanhey Date: Mon, 30 Mar 2020 22:50:28 +0200 Subject: [PATCH 067/148] Update composer.json PHP >=4.2.0 psr-4 autoload --- composer.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 92f8e0b..fec4f9c 100644 --- a/composer.json +++ b/composer.json @@ -27,10 +27,10 @@ } ], "require": { - "php": ">=5.4.0" + "php": ">=7.2.0" }, "target-dir": "Kumbia/ActiveRecord", "autoload": { - "psr-0": { "Kumbia\\ActiveRecord": "." } + "psr-4": { "Kumbia\\ActiveRecord": "." } } } From 887c263907cc028a627c2e380218792725328424 Mon Sep 17 00:00:00 2001 From: Joanhey Date: Mon, 30 Mar 2020 23:02:40 +0200 Subject: [PATCH 068/148] Delete target-dir in composer.json Deprecated and not used with psr-4 --- composer.json | 1 - 1 file changed, 1 deletion(-) diff --git a/composer.json b/composer.json index fec4f9c..a4b5e64 100644 --- a/composer.json +++ b/composer.json @@ -29,7 +29,6 @@ "require": { "php": ">=7.2.0" }, - "target-dir": "Kumbia/ActiveRecord", "autoload": { "psr-4": { "Kumbia\\ActiveRecord": "." } } From 79759f503091306bd62b7f8c424d3a973717037f Mon Sep 17 00:00:00 2001 From: Joanhey Date: Mon, 30 Mar 2020 23:06:19 +0200 Subject: [PATCH 069/148] Add config in composer.json --- composer.json | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index a4b5e64..af0b79c 100644 --- a/composer.json +++ b/composer.json @@ -7,7 +7,7 @@ "license": "BSD-3-Clause", "support": { "issues": "https://github.com/KumbiaPHP/ActiveRecord/issues", - "irc": "irc://irc.freenode.org/kumbiaphp" + "slack": "irc://irc.freenode.org/kumbiaphp" }, "authors": [ { @@ -31,5 +31,9 @@ }, "autoload": { "psr-4": { "Kumbia\\ActiveRecord": "." } + }, + "config": { + "optimize-autoloader": true, + "sort-packages": true } } From 08c68a6e2a040d0e62d39ba5f709463bd57df932 Mon Sep 17 00:00:00 2001 From: Joanhey Date: Mon, 30 Mar 2020 23:08:17 +0200 Subject: [PATCH 070/148] Delete support.irc and added slack in composer --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index af0b79c..616cd66 100644 --- a/composer.json +++ b/composer.json @@ -7,7 +7,7 @@ "license": "BSD-3-Clause", "support": { "issues": "https://github.com/KumbiaPHP/ActiveRecord/issues", - "slack": "irc://irc.freenode.org/kumbiaphp" + "slack": "https://slack.kumbiaphp.com" }, "authors": [ { From 6ca9424876854d13bded9ec48a055f4fe34dc2b1 Mon Sep 17 00:00:00 2001 From: Joanhey Date: Mon, 30 Mar 2020 23:34:44 +0200 Subject: [PATCH 071/148] Update README --- Metadata/PgsqlMetadata.php | 2 +- README.md | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Metadata/PgsqlMetadata.php b/Metadata/PgsqlMetadata.php index 6e90406..07a0b00 100644 --- a/Metadata/PgsqlMetadata.php +++ b/Metadata/PgsqlMetadata.php @@ -31,7 +31,7 @@ class PgsqlMetadata extends Metadata * * @param string $database base de datos * @param string $table tabla - * @param string $schema squema + * @param string $schema esquema, por defecto 'public' * * @return array */ diff --git a/README.md b/README.md index c74041c..9303417 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -![KumbiaPHP](https://proto.kumbiaphp.com/img/kumbiaphp.png) +![KumbiaPHP](https://proto.kumbiaphp.com/img/kumbiaphp.svg) [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/KumbiaPHP/ActiveRecord/badges/quality-score.png?s=f7230602070a9e9605d46544197bcdac46166612)](https://scrutinizer-ci.com/g/KumbiaPHP/ActiveRecord/) [![Code Coverage](https://scrutinizer-ci.com/g/KumbiaPHP/ActiveRecord/badges/coverage.png?s=58997633701e84050c0ebd5334f3eb1bb8b7ad42)](https://scrutinizer-ci.com/g/KumbiaPHP/ActiveRecord/) @@ -9,7 +9,7 @@ ESPAÑOL - [ENGLISH](/README.en.md) # ActiveRecord -Nuevo ActiveRecord en desarrollo, requiere PHP 5.4 +Nuevo ActiveRecord en desarrollo. No usar en producción From 32fe08329b124c65803093a5935a9f615da8c48a Mon Sep 17 00:00:00 2001 From: Joanhey Date: Mon, 30 Mar 2020 23:55:42 +0200 Subject: [PATCH 072/148] Update README.en --- README.en.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.en.md b/README.en.md index 7b65923..97ae5c6 100644 --- a/README.en.md +++ b/README.en.md @@ -9,7 +9,7 @@ ENGLISH - [SPANISH](/README.md) # ActiveRecord -New ActiveRecord in development, requires PHP > 5.4 +New ActiveRecord in development Don't use in production From 893931c916a59eda5d575175e58c2f5cfe557477 Mon Sep 17 00:00:00 2001 From: Joanhey Date: Tue, 31 Mar 2020 00:24:16 +0200 Subject: [PATCH 073/148] Travis add php 7.2 --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 054d610..211cc1a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,6 +2,7 @@ language: php dist: bionic php: + - 7.2 - 7.3 - 7.4 - master From ad18d311e96337c95b1fe2e7438a8517ad810a4a Mon Sep 17 00:00:00 2001 From: Joanhey Date: Tue, 31 Mar 2020 17:28:25 +0200 Subject: [PATCH 074/148] Medata::class check the db driver --- BaseRecord.php | 1 - Metadata/Metadata.php | 24 +++++++++++++----------- Metadata/MysqlMetadata.php | 9 ++++----- Metadata/PgsqlMetadata.php | 10 +++++----- Metadata/SqlsrvMetadata.php | 25 ++++++++++++------------- 5 files changed, 34 insertions(+), 35 deletions(-) diff --git a/BaseRecord.php b/BaseRecord.php index 0f50f6d..c2ec43f 100644 --- a/BaseRecord.php +++ b/BaseRecord.php @@ -187,7 +187,6 @@ public static function getDatabase(): string public static function metadata(): Metadata\Metadata { return Metadata\Metadata::get( - static::getDriver(), static::getDatabase(), static::getTable(), static::getSchema() diff --git a/Metadata/Metadata.php b/Metadata/Metadata.php index e797494..2a562c9 100644 --- a/Metadata/Metadata.php +++ b/Metadata/Metadata.php @@ -20,6 +20,8 @@ */ namespace Kumbia\ActiveRecord\Metadata; +use Kumbia\ActiveRecord\Db; + /** * Metadata de tabla. */ @@ -70,40 +72,40 @@ abstract class Metadata /** * Metadata de la tabla. * - * @param string $type tipo de controlador * @param string $database * @param string $table * @param string $schema * * @return self */ - public static function get(string $type, string $database, string $table, string $schema = ''): self + public static function get(string $database, string $table, string $schema = ''): self { - return self::$instances["$database.$table.$schema"] ?? self::getMetadata($type, $database, $table, $schema); + return self::$instances["$database.$table.$schema"] ?? self::getMetadata($database, $table, $schema); } /** * Obtiene la metadata de la tabla * Y la cachea si esta en producción. * - * @param string $type tipo de controlador * @param string $database * @param string $table * @param string $schema * * @return self */ - private static function getMetadata(string $type, string $database, string $table, string $schema): self + private static function getMetadata(string $database, string $table, string $schema): self { $key = "$database.$table.$schema"; + //TODO añadir cache propia if (\PRODUCTION && ! (self::$instances[$key] = \Cache::driver()->get($key, 'ActiveRecord.Metadata'))) { return self::$instances[$key]; } - $class = \ucfirst($type).'Metadata'; + + $pdo = Db::get($database); - $class = __NAMESPACE__."\\$class"; + $driverClass = __NAMESPACE__."\\".\ucfirst($pdo->getAttribute(\PDO::ATTR_DRIVER_NAME)).'Metadata'; - self::$instances[$key] = new $class($database, $table, $schema); + self::$instances[$key] = new $driverClass($pdo, $table, $schema); // Cachea los metadatos if (\PRODUCTION) { @@ -137,7 +139,7 @@ private function __construct(string $database, string $table, string $schema = ' * @param array $meta información de la columna * @param string $field nombre de la columna */ - protected function filterCol(array $meta, string $field): void + protected function filterColumn(array $meta, string $field): void { if ($meta['Key'] === 'PRI') { $this->pk = $field; @@ -151,13 +153,13 @@ protected function filterCol(array $meta, string $field): void /** * Consultar los campos de la tabla en la base de datos. * - * @param string $database base de datos + * @param \PDO $pdo base de datos * @param string $table tabla * @param string $schema squema * * @return array */ - abstract protected function queryFields(string $database, string $table, string $schema = ''): array; + abstract protected function queryFields(\PDO $pdo, string $table, string $schema = ''): array; /** * Obtiene la descripción de los campos. diff --git a/Metadata/MysqlMetadata.php b/Metadata/MysqlMetadata.php index 1ce8afe..7017c17 100644 --- a/Metadata/MysqlMetadata.php +++ b/Metadata/MysqlMetadata.php @@ -20,7 +20,6 @@ */ namespace Kumbia\ActiveRecord\Metadata; -use Kumbia\ActiveRecord\Db; use \PDO; /** @@ -31,16 +30,16 @@ class MysqlMetadata extends Metadata /** * Consultar los campos de la tabla en la base de datos. * - * @param string $database base de datos + * @param \PDO $pdo base de datos * @param string $table tabla * @param string $schema squema * * @return array */ - protected function queryFields(string $database, string $table, string $schema = ''): array + protected function queryFields(\PDO $pdo, string $table, string $schema = ''): array { $sql = $schema ? "DESCRIBE `$schema`.`$table`" : "DESCRIBE `$table`"; - $describe = Db::get($database)->query($sql, \PDO::FETCH_OBJ); + $describe = $pdo->query($sql, \PDO::FETCH_OBJ); $fields = []; // TODO mejorar este código @@ -52,7 +51,7 @@ protected function queryFields(string $database, string $table, string $schema = 'Default' => $value->Default != '', 'Auto' => $value->Extra === 'auto_increment' ]; - $this->filterCol($fields[$value->Field], $value->Field); + $this->filterColumn($fields[$value->Field], $value->Field); } return $fields; diff --git a/Metadata/PgsqlMetadata.php b/Metadata/PgsqlMetadata.php index 07a0b00..1983e48 100644 --- a/Metadata/PgsqlMetadata.php +++ b/Metadata/PgsqlMetadata.php @@ -19,7 +19,7 @@ */ namespace Kumbia\ActiveRecord\Metadata; -use Kumbia\ActiveRecord\Db; +use \PDO; /** * Adaptador de Metadata para Pgsql. @@ -29,16 +29,16 @@ class PgsqlMetadata extends Metadata /** * Consultar los campos de la tabla en la base de datos. * - * @param string $database base de datos + * @param \PDO $pdo base de datos * @param string $table tabla * @param string $schema esquema, por defecto 'public' * * @return array */ - protected function queryFields(string $database, string $table, string $schema = 'public'): array + protected function queryFields(\PDO $pdo, string $table, string $schema = 'public'): array { // Nota: Se excluyen claves compuestas - $describe = Db::get($database)->query( + $describe = $pdo->query( "SELECT DISTINCT c.column_name AS field, c.udt_name AS type, @@ -81,7 +81,7 @@ private function describe(array $describe): array 'Key' => \substr($value->key, 0, 3), 'Auto' => \preg_match('/^nextval\(/', $value->default) ]; - $this->filterCol($fields[$value->field], $value->field); + $this->filterColumn($fields[$value->field], $value->field); } return $fields; diff --git a/Metadata/SqlsrvMetadata.php b/Metadata/SqlsrvMetadata.php index ec018f0..5f437f6 100644 --- a/Metadata/SqlsrvMetadata.php +++ b/Metadata/SqlsrvMetadata.php @@ -21,7 +21,6 @@ */ namespace Kumbia\ActiveRecord\Metadata; -use Kumbia\ActiveRecord\Db; use \PDO; /** @@ -33,14 +32,14 @@ class SqlsrvMetadata extends Metadata /** * Consultar los campos de la tabla en la base de datos * - * @param string $database base de datos + * @param \PDO $pdo base de datos * @param string $table tabla * @param string $schema squema * @return array */ - protected function queryFields($database, $table, $schema = 'dbo') + protected function queryFields(\PDO $pdo, string $table, string $schema = 'dbo'): array { - $describe = Db::get($database)->query( + $describe = $pdo->query( "SELECT c.name AS field_name, c.is_identity AS is_auto_increment, @@ -52,7 +51,7 @@ protected function queryFields($database, $table, $schema = 'dbo') WHERE object_id = object_id('$schema.$table')" ); - $pk = self::pk($database, $table); + $pk = self::pk($pdo, $table); return self::describe($describe, $pk); } @@ -60,13 +59,13 @@ protected function queryFields($database, $table, $schema = 'dbo') /** * Optiene el PK * - * @param string $database base de datos + * @param \PDO $pdo base de datos * @param string $table tabla * @return string */ - private static function pk($database, $table) + private static function pk(\PDO $pdo, string $table): string { - $pk = Db::get($database)->query("exec sp_pkeys @table_name='$table'"); + $pk = $pdo->query("exec sp_pkeys @table_name='$table'"); $pk = $pk->fetch(\PDO::FETCH_OBJ); return $pk->COLUMN_NAME; @@ -79,20 +78,20 @@ private static function pk($database, $table) * @param string $pk Primary key * @return array */ - protected function describe(\PDOStatement $describe, $pk) + protected function describe(\PDOStatement $describe, string $pk): array { // TODO Mejorar $fields = []; - while (($value = $describe->fetch( \PDO::FETCH_OBJ))): + while ($value = $describe->fetch()) { $fields[$value->field_name] = [ 'Type' => $value->type_field, 'Null' => ($value->is_nullable), - 'Key' => ($value->field_name == $pk) ? 'PRI' : '', + 'Key' => ($value->field_name === $pk) ? 'PRI' : '', 'Default' => \str_replace("''", "'", \trim($value->default_value, "(')")), 'Auto' => ($value->is_auto_increment) ]; - $this->filterCol($fields[$value->field_name], $value->field_name); - endwhile; + $this->filterColumn($fields[$value->field_name], $value->field_name); + } return $fields; } From b138dcd5a41e60569cc1880cb2551aa520d1630a Mon Sep 17 00:00:00 2001 From: Joanhey Date: Tue, 31 Mar 2020 17:30:05 +0200 Subject: [PATCH 075/148] Simplfiy tests To unify all db driver tests --- tests/ActiveRecord/Metadata/MetadataTest.php | 16 ++++++++-------- tests/phpunit_mysql.xml | 2 -- tests/phpunit_postgresql.xml | 2 -- 3 files changed, 8 insertions(+), 12 deletions(-) diff --git a/tests/ActiveRecord/Metadata/MetadataTest.php b/tests/ActiveRecord/Metadata/MetadataTest.php index cd53bcb..2da0768 100644 --- a/tests/ActiveRecord/Metadata/MetadataTest.php +++ b/tests/ActiveRecord/Metadata/MetadataTest.php @@ -1,5 +1,7 @@ assertInstanceOf('\\Kumbia\\ActiveRecord\\Metadata\\Metadata', $metadata); } - public function testMethod_getPK() + public function testGetPK() { $metadata = $this->createClass(); $pk = $metadata->getPK(); @@ -32,7 +32,7 @@ public function testMethod_getPK() $this->assertEquals('id', $pk); } - public function testMethod_getWithDefault() + public function testGetWithDefault() { $metadata = $this->createClass(); $withDefault = $metadata->getWithDefault(); @@ -42,7 +42,7 @@ public function testMethod_getWithDefault() $this->assertEquals('activo', $withDefault[0]); } - public function testMethod_getFields() + public function testGetFields() { $metadata = $this->createClass(); $fields = $metadata->getFields(); @@ -71,7 +71,7 @@ protected function fieldData($field, $type, $null, $key, $default, $auto) $this->assertEquals($auto, $field['Auto']); } - public function testMethod_getFieldsList() + public function testGetFieldsList() { $metadata = $this->createClass(); $fields = $metadata->getFieldsList(); @@ -82,7 +82,7 @@ public function testMethod_getFieldsList() $this->assertEquals(['id', 'nombre', 'email', 'activo'], $fields); } - public function testMethod_getAutoFields() + public function testGetAutoFields() { $metadata = $this->createClass(); $fields = $metadata->getAutoFields(); diff --git a/tests/phpunit_mysql.xml b/tests/phpunit_mysql.xml index f2936dd..25bff07 100644 --- a/tests/phpunit_mysql.xml +++ b/tests/phpunit_mysql.xml @@ -3,8 +3,6 @@ - - diff --git a/tests/phpunit_postgresql.xml b/tests/phpunit_postgresql.xml index 9daf2c1..270986d 100644 --- a/tests/phpunit_postgresql.xml +++ b/tests/phpunit_postgresql.xml @@ -3,8 +3,6 @@ - - From 5497f0e549d41b8cc5c610568931d84949ccee45 Mon Sep 17 00:00:00 2001 From: Joanhey Date: Tue, 31 Mar 2020 17:38:43 +0200 Subject: [PATCH 076/148] Update Medata::class --- Metadata/Metadata.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Metadata/Metadata.php b/Metadata/Metadata.php index 2a562c9..5b1ce9f 100644 --- a/Metadata/Metadata.php +++ b/Metadata/Metadata.php @@ -123,13 +123,13 @@ private static function getMetadata(string $database, string $table, string $sch /** * Constructor. * - * @param string $database base de datos + * @param \PDO $pdo base de datos * @param string $table tabla * @param string $schema squema */ - private function __construct(string $database, string $table, string $schema = '') + private function __construct(\PDO $pdo, string $table, string $schema = '') { - $this->fields = $this->queryFields($database, $table, $schema); + $this->fields = $this->queryFields($pdo, $table, $schema); $this->fieldsList = \array_keys($this->fields); } From ac96e9960532fd56fdc5ae33eb2d8bf592e02ccc Mon Sep 17 00:00:00 2001 From: Joanhey Date: Tue, 31 Mar 2020 17:39:10 +0200 Subject: [PATCH 077/148] Fix typo in MetadataTest --- tests/ActiveRecord/Metadata/MetadataTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ActiveRecord/Metadata/MetadataTest.php b/tests/ActiveRecord/Metadata/MetadataTest.php index 2da0768..4fd56c6 100644 --- a/tests/ActiveRecord/Metadata/MetadataTest.php +++ b/tests/ActiveRecord/Metadata/MetadataTest.php @@ -13,7 +13,7 @@ protected function createClass() $tableName = $GLOBALS['metadata_table']; $schemaName = $GLOBALS['metadata_schema']; - return Medatada::get($databaseName, $tableName, $schemaName); + return Metadada::get($databaseName, $tableName, $schemaName); } public function testInstanceOf() From 7d99bb84b567ba451f9c9e10285bed653789d869 Mon Sep 17 00:00:00 2001 From: Joanhey Date: Tue, 31 Mar 2020 18:10:01 +0200 Subject: [PATCH 078/148] Fix typo and add use --- tests/ActiveRecord/Metadata/MetadataTest.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/ActiveRecord/Metadata/MetadataTest.php b/tests/ActiveRecord/Metadata/MetadataTest.php index 4fd56c6..ea5426c 100644 --- a/tests/ActiveRecord/Metadata/MetadataTest.php +++ b/tests/ActiveRecord/Metadata/MetadataTest.php @@ -1,8 +1,9 @@ Date: Tue, 31 Mar 2020 18:10:25 +0200 Subject: [PATCH 079/148] Add use --- tests/ActiveRecord/DbTest.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/ActiveRecord/DbTest.php b/tests/ActiveRecord/DbTest.php index 75303b2..408c9cb 100644 --- a/tests/ActiveRecord/DbTest.php +++ b/tests/ActiveRecord/DbTest.php @@ -1,8 +1,9 @@ Date: Tue, 31 Mar 2020 18:10:46 +0200 Subject: [PATCH 080/148] Updata databases config --- tests/config/databases.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/config/databases.php b/tests/config/databases.php index bab9044..b924c0b 100644 --- a/tests/config/databases.php +++ b/tests/config/databases.php @@ -1,7 +1,7 @@ [ 'dsn' => 'mysql:host=127.0.0.1;dbname=test;charset=utf8', 'username' => 'root', @@ -11,7 +11,7 @@ \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION ] ], - //Conexión a pgsql + //Pgsql 'pgsql' => [ 'dsn' => 'pgsql:dbname=test;host=localhost', 'username' => 'postgres', @@ -23,5 +23,5 @@ ], - //Más conexiones + //More connections ]; From 9c8b97ab21c2da8261250b67219a151a6db0129a Mon Sep 17 00:00:00 2001 From: Joanhey Date: Tue, 31 Mar 2020 18:29:51 +0200 Subject: [PATCH 081/148] Use DB env --- .travis.yml | 8 ++++---- tests/ActiveRecord/Metadata/MetadataTest.php | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/.travis.yml b/.travis.yml index 211cc1a..6ba3c1c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -23,13 +23,13 @@ services: env: - DB=mysql - - DB=postgresql + - DB=pgsql # - DB=sqlite before_script: - - sh -c "if [ '$DB' = 'postgresql' ]; then psql -c 'DROP DATABASE IF EXISTS test;' -U postgres; fi" - - sh -c "if [ '$DB' = 'postgresql' ]; then psql -c 'CREATE DATABASE test;' -U postgres; fi" - - sh -c "if [ '$DB' = 'postgresql' ]; then psql -c 'CREATE TABLE IF NOT EXISTS usuario ( + - sh -c "if [ '$DB' = 'psql' ]; then psql -c 'DROP DATABASE IF EXISTS test;' -U postgres; fi" + - sh -c "if [ '$DB' = 'psql' ]; then psql -c 'CREATE DATABASE test;' -U postgres; fi" + - sh -c "if [ '$DB' = 'psql' ]; then psql -c 'CREATE TABLE IF NOT EXISTS usuario ( id serial PRIMARY KEY, nombre varchar(50) NOT NULL , email varchar(100) NOT NULL , diff --git a/tests/ActiveRecord/Metadata/MetadataTest.php b/tests/ActiveRecord/Metadata/MetadataTest.php index ea5426c..99a7564 100644 --- a/tests/ActiveRecord/Metadata/MetadataTest.php +++ b/tests/ActiveRecord/Metadata/MetadataTest.php @@ -10,7 +10,7 @@ class MetadataTest extends TestCase */ protected function createClass() { - $databaseName = $GLOBALS['config_database']; + $databaseName = getenv('DB'); $tableName = $GLOBALS['metadata_table']; $schemaName = $GLOBALS['metadata_schema']; From 47ce52e381bb3959d7854320930edb172bec818e Mon Sep 17 00:00:00 2001 From: Joanhey Date: Tue, 31 Mar 2020 18:36:06 +0200 Subject: [PATCH 082/148] Travis use only 1 phpunit.xml for all db drivers --- .travis.yml | 2 +- tests/phpunit_postgresql.xml | 26 ----------------------- tests/{phpunit_mysql.xml => phpunitl.xml} | 3 +-- 3 files changed, 2 insertions(+), 29 deletions(-) delete mode 100644 tests/phpunit_postgresql.xml rename tests/{phpunit_mysql.xml => phpunitl.xml} (83%) diff --git a/.travis.yml b/.travis.yml index 6ba3c1c..62326ca 100644 --- a/.travis.yml +++ b/.travis.yml @@ -47,6 +47,6 @@ before_script: ; fi" script: - - phpunit --configuration tests/phpunit_$DB.xml --coverage-text --colors --coverage-clover=coverage.clover + - phpunit --configuration tests/phpunit.xml --coverage-text --colors --coverage-clover=coverage.clover - wget https://scrutinizer-ci.com/ocular.phar - php ocular.phar code-coverage:upload --format=php-clover coverage.clover diff --git a/tests/phpunit_postgresql.xml b/tests/phpunit_postgresql.xml deleted file mode 100644 index 270986d..0000000 --- a/tests/phpunit_postgresql.xml +++ /dev/null @@ -1,26 +0,0 @@ - - - - - - - - - - - - - - . - - - - - - . - - ./tests - - - - diff --git a/tests/phpunit_mysql.xml b/tests/phpunitl.xml similarity index 83% rename from tests/phpunit_mysql.xml rename to tests/phpunitl.xml index 25bff07..b0f991b 100644 --- a/tests/phpunit_mysql.xml +++ b/tests/phpunitl.xml @@ -2,13 +2,12 @@ - - + . From e01cc3398057c2e760f899c7aa9f08fcbd36b866 Mon Sep 17 00:00:00 2001 From: Joanhey Date: Tue, 31 Mar 2020 18:40:46 +0200 Subject: [PATCH 083/148] Fix typo in name --- tests/{phpunitl.xml => phpunit.xml} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename tests/{phpunitl.xml => phpunit.xml} (100%) diff --git a/tests/phpunitl.xml b/tests/phpunit.xml similarity index 100% rename from tests/phpunitl.xml rename to tests/phpunit.xml From ce02179472c59524258edbcf20a13d295e3b50ad Mon Sep 17 00:00:00 2001 From: Joanhey Date: Tue, 31 Mar 2020 18:43:49 +0200 Subject: [PATCH 084/148] DbTest use DB env too --- tests/ActiveRecord/DbTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/ActiveRecord/DbTest.php b/tests/ActiveRecord/DbTest.php index 408c9cb..b043dbb 100644 --- a/tests/ActiveRecord/DbTest.php +++ b/tests/ActiveRecord/DbTest.php @@ -7,16 +7,16 @@ class DbTest extends TestCase { public function testGetInstance() { - $instance = Db::get($GLOBALS['config_database']); + $instance = Db::get(getenv('DB');); $this->assertInstanceOf('PDO', $instance); } public function testGet() { - $instance = Db::get($GLOBALS['config_database']); + $instance = Db::get(getenv('DB')); - $instance2 = Db::get($GLOBALS['config_database']); + $instance2 = Db::get(getenv('DB')); $this->assertEquals($instance, $instance2); } From 3e556b09fa633e2cd787e5a9059fc622832f2bcc Mon Sep 17 00:00:00 2001 From: Joanhey Date: Tue, 31 Mar 2020 18:47:25 +0200 Subject: [PATCH 085/148] Simplify before_script in travis --- .travis.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index 62326ca..278c5d6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -27,9 +27,9 @@ env: # - DB=sqlite before_script: - - sh -c "if [ '$DB' = 'psql' ]; then psql -c 'DROP DATABASE IF EXISTS test;' -U postgres; fi" - - sh -c "if [ '$DB' = 'psql' ]; then psql -c 'CREATE DATABASE test;' -U postgres; fi" - - sh -c "if [ '$DB' = 'psql' ]; then psql -c 'CREATE TABLE IF NOT EXISTS usuario ( + - if [[ "$DB" == "pgsql" ]]; then psql -c "DROP DATABASE IF EXISTS test;" -U postgres; fi + - if [[ "$DB" == "pgsql" ]]; then psql -c 'CREATE DATABASE test;' -U postgres; fi" + - if [[ "$DB" == "pgsql" ]]; then psql -c 'CREATE TABLE IF NOT EXISTS usuario ( id serial PRIMARY KEY, nombre varchar(50) NOT NULL , email varchar(100) NOT NULL , @@ -37,7 +37,7 @@ before_script: );' -U postgres; fi" - - sh -c "if [ '$DB' = 'mysql' ]; then mysql -e 'CREATE DATABASE IF NOT EXISTS test; + - if [[ "$DB" == "mysql" ]]; then mysql -e 'CREATE DATABASE IF NOT EXISTS test; CREATE TABLE IF NOT EXISTS test.usuario ( id int(11) NOT NULL DEFAULT 0, nombre varchar(50) NOT NULL , From 7e68295e65605f6098db514ad0bc37886b562b09 Mon Sep 17 00:00:00 2001 From: Joanhey Date: Tue, 31 Mar 2020 18:48:19 +0200 Subject: [PATCH 086/148] Fix typo in DbTest --- tests/ActiveRecord/DbTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ActiveRecord/DbTest.php b/tests/ActiveRecord/DbTest.php index b043dbb..79ec2a2 100644 --- a/tests/ActiveRecord/DbTest.php +++ b/tests/ActiveRecord/DbTest.php @@ -7,7 +7,7 @@ class DbTest extends TestCase { public function testGetInstance() { - $instance = Db::get(getenv('DB');); + $instance = Db::get(getenv('DB')); $this->assertInstanceOf('PDO', $instance); } From d904138a40a2bc620ba8e26ecc359f692cac29b6 Mon Sep 17 00:00:00 2001 From: Joanhey Date: Tue, 31 Mar 2020 18:51:10 +0200 Subject: [PATCH 087/148] Fix travis.yml --- .travis.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 278c5d6..f42bcd3 100644 --- a/.travis.yml +++ b/.travis.yml @@ -28,7 +28,7 @@ env: before_script: - if [[ "$DB" == "pgsql" ]]; then psql -c "DROP DATABASE IF EXISTS test;" -U postgres; fi - - if [[ "$DB" == "pgsql" ]]; then psql -c 'CREATE DATABASE test;' -U postgres; fi" + - if [[ "$DB" == "pgsql" ]]; then psql -c 'CREATE DATABASE test;' -U postgres; fi - if [[ "$DB" == "pgsql" ]]; then psql -c 'CREATE TABLE IF NOT EXISTS usuario ( id serial PRIMARY KEY, nombre varchar(50) NOT NULL , @@ -36,7 +36,7 @@ before_script: activo smallint NULL DEFAULT 1 );' -U postgres; - fi" + fi - if [[ "$DB" == "mysql" ]]; then mysql -e 'CREATE DATABASE IF NOT EXISTS test; CREATE TABLE IF NOT EXISTS test.usuario ( id int(11) NOT NULL DEFAULT 0, @@ -44,7 +44,7 @@ before_script: email varchar(100) NOT NULL , activo smallint(1) NULL DEFAULT 1 , PRIMARY KEY (id) );' - ; fi" + ; fi script: - phpunit --configuration tests/phpunit.xml --coverage-text --colors --coverage-clover=coverage.clover From 6722f480175ff6cd5a337731f74e33290c09c36f Mon Sep 17 00:00:00 2001 From: Joanhey Date: Tue, 31 Mar 2020 19:11:35 +0200 Subject: [PATCH 088/148] Travis add php nightly (PHP 8) --- .travis.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index f42bcd3..c69fe1c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,11 +5,11 @@ php: - 7.2 - 7.3 - 7.4 - - master + - nightly matrix: allow_failures: - - php: master + - php: nightly notifications: slack: kumbiaphp:51JaKQTXASwf52D8b32OyWb9 From 066506866d165f1dd550ca32c88aadce03ab9580 Mon Sep 17 00:00:00 2001 From: Joanhey Date: Tue, 31 Mar 2020 19:19:37 +0200 Subject: [PATCH 089/148] Add test driver db Metadata class --- tests/ActiveRecord/Metadata/MetadataTest.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/tests/ActiveRecord/Metadata/MetadataTest.php b/tests/ActiveRecord/Metadata/MetadataTest.php index 99a7564..1e55ace 100644 --- a/tests/ActiveRecord/Metadata/MetadataTest.php +++ b/tests/ActiveRecord/Metadata/MetadataTest.php @@ -17,13 +17,21 @@ protected function createClass() return Metadata::get($databaseName, $tableName, $schemaName); } - public function testInstanceOf() + public function testInstanceOfMetadata() { $metadata = $this->createClass(); $this->assertInstanceOf('\\Kumbia\\ActiveRecord\\Metadata\\Metadata', $metadata); } + public function testInstanceOfDriverDb() + { + $metadata = $this->createClass(); + $dbDriverClass = \ucfirst(getenv($DB)).'Metadata'; + + $this->assertInstanceOf('\\Kumbia\\ActiveRecord\\Metadata\\'.$dbDriverClass, $metadata); + } + public function testGetPK() { $metadata = $this->createClass(); From ab354d1ee2fe6fad2a0e869d540a6bd4d90189d8 Mon Sep 17 00:00:00 2001 From: Joanhey Date: Tue, 31 Mar 2020 19:22:17 +0200 Subject: [PATCH 090/148] Fix typo --- tests/ActiveRecord/Metadata/MetadataTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ActiveRecord/Metadata/MetadataTest.php b/tests/ActiveRecord/Metadata/MetadataTest.php index 1e55ace..ebf6709 100644 --- a/tests/ActiveRecord/Metadata/MetadataTest.php +++ b/tests/ActiveRecord/Metadata/MetadataTest.php @@ -27,7 +27,7 @@ public function testInstanceOfMetadata() public function testInstanceOfDriverDb() { $metadata = $this->createClass(); - $dbDriverClass = \ucfirst(getenv($DB)).'Metadata'; + $dbDriverClass = \ucfirst(getenv('DB')).'Metadata'; $this->assertInstanceOf('\\Kumbia\\ActiveRecord\\Metadata\\'.$dbDriverClass, $metadata); } From 36fdfadd555952986d6e181087e2117bb4f820e3 Mon Sep 17 00:00:00 2001 From: Joanhey Date: Tue, 31 Mar 2020 19:32:08 +0200 Subject: [PATCH 091/148] Delete __NAMESPACE__ to simplify getMetadata() --- Metadata/Metadata.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Metadata/Metadata.php b/Metadata/Metadata.php index 5b1ce9f..5620ebc 100644 --- a/Metadata/Metadata.php +++ b/Metadata/Metadata.php @@ -103,7 +103,7 @@ private static function getMetadata(string $database, string $table, string $sch $pdo = Db::get($database); - $driverClass = __NAMESPACE__."\\".\ucfirst($pdo->getAttribute(\PDO::ATTR_DRIVER_NAME)).'Metadata'; + $driverClass = \ucfirst($pdo->getAttribute(\PDO::ATTR_DRIVER_NAME)).'Metadata'; self::$instances[$key] = new $driverClass($pdo, $table, $schema); From b9555683ce8acbfbb85d89313206ae5296053e06 Mon Sep 17 00:00:00 2001 From: Joanhey Date: Tue, 31 Mar 2020 19:58:03 +0200 Subject: [PATCH 092/148] Use namespace\ in getMetadata() --- Metadata/Metadata.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Metadata/Metadata.php b/Metadata/Metadata.php index 5620ebc..3d6432a 100644 --- a/Metadata/Metadata.php +++ b/Metadata/Metadata.php @@ -103,7 +103,7 @@ private static function getMetadata(string $database, string $table, string $sch $pdo = Db::get($database); - $driverClass = \ucfirst($pdo->getAttribute(\PDO::ATTR_DRIVER_NAME)).'Metadata'; + $driverClass = 'namespace\\'.\ucfirst($pdo->getAttribute(\PDO::ATTR_DRIVER_NAME)).'Metadata'; self::$instances[$key] = new $driverClass($pdo, $table, $schema); From 2c92ff388f788dd56a656bc77ed87a098363aedc Mon Sep 17 00:00:00 2001 From: Joanhey Date: Tue, 31 Mar 2020 20:00:43 +0200 Subject: [PATCH 093/148] Use again __NAMESPACE__ --- Metadata/Metadata.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Metadata/Metadata.php b/Metadata/Metadata.php index 3d6432a..5b1ce9f 100644 --- a/Metadata/Metadata.php +++ b/Metadata/Metadata.php @@ -103,7 +103,7 @@ private static function getMetadata(string $database, string $table, string $sch $pdo = Db::get($database); - $driverClass = 'namespace\\'.\ucfirst($pdo->getAttribute(\PDO::ATTR_DRIVER_NAME)).'Metadata'; + $driverClass = __NAMESPACE__."\\".\ucfirst($pdo->getAttribute(\PDO::ATTR_DRIVER_NAME)).'Metadata'; self::$instances[$key] = new $driverClass($pdo, $table, $schema); From 4db9a1fc49b4a8fbd9ed797f47e7da244887bc23 Mon Sep 17 00:00:00 2001 From: Joanhey Date: Tue, 31 Mar 2020 23:32:20 +0200 Subject: [PATCH 094/148] Update MetadataTest --- tests/ActiveRecord/Metadata/MetadataTest.php | 26 ++++++++++++-------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/tests/ActiveRecord/Metadata/MetadataTest.php b/tests/ActiveRecord/Metadata/MetadataTest.php index ebf6709..e44f2c1 100644 --- a/tests/ActiveRecord/Metadata/MetadataTest.php +++ b/tests/ActiveRecord/Metadata/MetadataTest.php @@ -5,10 +5,15 @@ class MetadataTest extends TestCase { - /** - * @return \Kumbia\ActiveRecord\Metadata\Metadata - */ - protected function createClass() + protected $dbName; + + public function setUp() + { + $this->dbName = getenv('DB'); + + } + + protected function createClass(): Metadata { $databaseName = getenv('DB'); $tableName = $GLOBALS['metadata_table']; @@ -27,7 +32,7 @@ public function testInstanceOfMetadata() public function testInstanceOfDriverDb() { $metadata = $this->createClass(); - $dbDriverClass = \ucfirst(getenv('DB')).'Metadata'; + $dbDriverClass = \ucfirst($this->dbName).'Metadata'; $this->assertInstanceOf('\\Kumbia\\ActiveRecord\\Metadata\\'.$dbDriverClass, $metadata); } @@ -59,11 +64,12 @@ public function testGetFields() $this->assertTrue(is_array($fields), 'Debe retornar un array'); $this->assertEquals(4, count($fields)); - $this->assertEquals(['id', 'nombre', 'email', 'activo'], array_keys($fields)); - $this->assertEquals(['Type', 'Null', 'Key', 'Default', 'Auto'], array_keys($fields['id'])); - $this->assertEquals(['Type', 'Null', 'Key', 'Default', 'Auto'], array_keys($fields['nombre'])); - $this->assertEquals(['Type', 'Null', 'Key', 'Default', 'Auto'], array_keys($fields['email'])); - $this->assertEquals(['Type', 'Null', 'Key', 'Default', 'Auto'], array_keys($fields['activo'])); + $fieldList = array_keys($fields); + $this->assertEquals(['id', 'nombre', 'email', 'activo'], $fieldList); + + foreach($fieldList as $fieldName) { + $this->assertEquals(['Type', 'Null', 'Key', 'Default', 'Auto'], array_keys($fields[$fieldName])); + } $this->fieldData($fields['id'], 'int(11)', false, 'PRI', true, false); $this->fieldData($fields['nombre'], 'varchar(50)', false, '', false, false); From 23e4b19425cd7fd85c8281d9440448029c441a01 Mon Sep 17 00:00:00 2001 From: Joanhey Date: Tue, 31 Mar 2020 23:36:01 +0200 Subject: [PATCH 095/148] Add return void --- tests/ActiveRecord/Metadata/MetadataTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ActiveRecord/Metadata/MetadataTest.php b/tests/ActiveRecord/Metadata/MetadataTest.php index e44f2c1..2652aa9 100644 --- a/tests/ActiveRecord/Metadata/MetadataTest.php +++ b/tests/ActiveRecord/Metadata/MetadataTest.php @@ -7,7 +7,7 @@ class MetadataTest extends TestCase { protected $dbName; - public function setUp() + public function setUp(): void { $this->dbName = getenv('DB'); From 6fe2714a12aedc87df25b32a6c2e2269b17907ee Mon Sep 17 00:00:00 2001 From: Joanhey Date: Tue, 31 Mar 2020 23:40:34 +0200 Subject: [PATCH 096/148] Delete testInstanceOfMetadata The createClass must return Metadata::class --- tests/ActiveRecord/Metadata/MetadataTest.php | 7 ------- 1 file changed, 7 deletions(-) diff --git a/tests/ActiveRecord/Metadata/MetadataTest.php b/tests/ActiveRecord/Metadata/MetadataTest.php index 2652aa9..1b6285a 100644 --- a/tests/ActiveRecord/Metadata/MetadataTest.php +++ b/tests/ActiveRecord/Metadata/MetadataTest.php @@ -22,13 +22,6 @@ protected function createClass(): Metadata return Metadata::get($databaseName, $tableName, $schemaName); } - public function testInstanceOfMetadata() - { - $metadata = $this->createClass(); - - $this->assertInstanceOf('\\Kumbia\\ActiveRecord\\Metadata\\Metadata', $metadata); - } - public function testInstanceOfDriverDb() { $metadata = $this->createClass(); From 2f001f20dd45aa73f1d65f2c512a8b5f98456584 Mon Sep 17 00:00:00 2001 From: Joanhey Date: Tue, 31 Mar 2020 23:54:08 +0200 Subject: [PATCH 097/148] Move config to setup() --- tests/ActiveRecord/Metadata/MetadataTest.php | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/tests/ActiveRecord/Metadata/MetadataTest.php b/tests/ActiveRecord/Metadata/MetadataTest.php index 1b6285a..f689b83 100644 --- a/tests/ActiveRecord/Metadata/MetadataTest.php +++ b/tests/ActiveRecord/Metadata/MetadataTest.php @@ -6,20 +6,22 @@ class MetadataTest extends TestCase { protected $dbName; + + protected $tableName; + + protected $schemaName; public function setUp(): void { - $this->dbName = getenv('DB'); - + $this->dbName = getenv('DB'); + + $this->tableName = $GLOBALS['metadata_table']; + $this->schemaName = $GLOBALS['metadata_schema']; } protected function createClass(): Metadata { - $databaseName = getenv('DB'); - $tableName = $GLOBALS['metadata_table']; - $schemaName = $GLOBALS['metadata_schema']; - - return Metadata::get($databaseName, $tableName, $schemaName); + return Metadata::get($this->dbName, $this->tableName, $this->schemaName); } public function testInstanceOfDriverDb() From 42cdeab7490fd5fc61a6e27e22c2a16593be2b99 Mon Sep 17 00:00:00 2001 From: Joanhey Date: Wed, 1 Apr 2020 00:13:11 +0200 Subject: [PATCH 098/148] Delete unnecessary test type return The class have return typehints --- tests/ActiveRecord/Metadata/MetadataTest.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/tests/ActiveRecord/Metadata/MetadataTest.php b/tests/ActiveRecord/Metadata/MetadataTest.php index f689b83..699c9a3 100644 --- a/tests/ActiveRecord/Metadata/MetadataTest.php +++ b/tests/ActiveRecord/Metadata/MetadataTest.php @@ -14,7 +14,7 @@ class MetadataTest extends TestCase public function setUp(): void { $this->dbName = getenv('DB'); - + $this->tableName = $GLOBALS['metadata_table']; $this->schemaName = $GLOBALS['metadata_schema']; } @@ -37,7 +37,6 @@ public function testGetPK() $metadata = $this->createClass(); $pk = $metadata->getPK(); - $this->assertTrue(is_string($pk), 'Debe retornar un string'); $this->assertEquals('id', $pk); } @@ -46,7 +45,6 @@ public function testGetWithDefault() $metadata = $this->createClass(); $withDefault = $metadata->getWithDefault(); - $this->assertTrue(is_array($withDefault), 'Debe retornar un array'); $this->assertEquals(1, count($withDefault)); $this->assertEquals('activo', $withDefault[0]); } @@ -56,7 +54,6 @@ public function testGetFields() $metadata = $this->createClass(); $fields = $metadata->getFields(); - $this->assertTrue(is_array($fields), 'Debe retornar un array'); $this->assertEquals(4, count($fields)); $fieldList = array_keys($fields); @@ -86,7 +83,6 @@ public function testGetFieldsList() $metadata = $this->createClass(); $fields = $metadata->getFieldsList(); - $this->assertTrue(is_array($fields), 'Debe retornar un array'); $this->assertEquals(4, count($fields)); $this->assertEquals(['id', 'nombre', 'email', 'activo'], $fields); From 3a3f6c4c814b71547b9e9c49735efc39a27b645d Mon Sep 17 00:00:00 2001 From: Joanhey Date: Wed, 1 Apr 2020 00:13:50 +0200 Subject: [PATCH 099/148] Fix testGetAutoFields() --- .travis.yml | 2 +- tests/ActiveRecord/Metadata/MetadataTest.php | 9 ++++----- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/.travis.yml b/.travis.yml index c69fe1c..f7782f7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -39,7 +39,7 @@ before_script: fi - if [[ "$DB" == "mysql" ]]; then mysql -e 'CREATE DATABASE IF NOT EXISTS test; CREATE TABLE IF NOT EXISTS test.usuario ( - id int(11) NOT NULL DEFAULT 0, + id int(11) NOT NULL AUTO_INCREMENT DEFAULT 0, nombre varchar(50) NOT NULL , email varchar(100) NOT NULL , activo smallint(1) NULL DEFAULT 1 , diff --git a/tests/ActiveRecord/Metadata/MetadataTest.php b/tests/ActiveRecord/Metadata/MetadataTest.php index 699c9a3..ff29f15 100644 --- a/tests/ActiveRecord/Metadata/MetadataTest.php +++ b/tests/ActiveRecord/Metadata/MetadataTest.php @@ -93,10 +93,9 @@ public function testGetAutoFields() $metadata = $this->createClass(); $fields = $metadata->getAutoFields(); - $this->assertTrue(is_array($fields), 'Debe retornar un array'); - // @TODO: Revisar, está devolviendo un array vacio -// $this->assertEquals(4, count($fields)); -// -// $this->assertEquals(array('id', 'nombre', 'email', 'activo'), $fields); + + $this->assertEquals(1, count($fields)); + + $this->assertEquals(['id'], $fields); } } From b51faca3238f4307ae0ffbf45f8af9b492e2661b Mon Sep 17 00:00:00 2001 From: Joanhey Date: Wed, 1 Apr 2020 00:19:08 +0200 Subject: [PATCH 100/148] Fix mysql create table --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index f7782f7..8104628 100644 --- a/.travis.yml +++ b/.travis.yml @@ -39,7 +39,7 @@ before_script: fi - if [[ "$DB" == "mysql" ]]; then mysql -e 'CREATE DATABASE IF NOT EXISTS test; CREATE TABLE IF NOT EXISTS test.usuario ( - id int(11) NOT NULL AUTO_INCREMENT DEFAULT 0, + id int NOT NULL AUTO_INCREMENT, nombre varchar(50) NOT NULL , email varchar(100) NOT NULL , activo smallint(1) NULL DEFAULT 1 , From 750c2566019ccdc0c1a5980c4c9f025fb64905a9 Mon Sep 17 00:00:00 2001 From: Joanhey Date: Wed, 1 Apr 2020 00:28:11 +0200 Subject: [PATCH 101/148] Update MetadataTest to reflect changes in sql --- Metadata/MysqlMetadata.php | 2 +- tests/ActiveRecord/Metadata/MetadataTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Metadata/MysqlMetadata.php b/Metadata/MysqlMetadata.php index 7017c17..27d7cc0 100644 --- a/Metadata/MysqlMetadata.php +++ b/Metadata/MysqlMetadata.php @@ -49,7 +49,7 @@ protected function queryFields(\PDO $pdo, string $table, string $schema = ''): a 'Null' => $value->Null !== 'NO', 'Key' => $value->Key, 'Default' => $value->Default != '', - 'Auto' => $value->Extra === 'auto_increment' + 'Auto' => \strtolower($value->Extra) === 'auto_increment' ]; $this->filterColumn($fields[$value->Field], $value->Field); } diff --git a/tests/ActiveRecord/Metadata/MetadataTest.php b/tests/ActiveRecord/Metadata/MetadataTest.php index ff29f15..5e7a279 100644 --- a/tests/ActiveRecord/Metadata/MetadataTest.php +++ b/tests/ActiveRecord/Metadata/MetadataTest.php @@ -63,7 +63,7 @@ public function testGetFields() $this->assertEquals(['Type', 'Null', 'Key', 'Default', 'Auto'], array_keys($fields[$fieldName])); } - $this->fieldData($fields['id'], 'int(11)', false, 'PRI', true, false); + $this->fieldData($fields['id'], 'int', false, 'PRI', false, true); $this->fieldData($fields['nombre'], 'varchar(50)', false, '', false, false); $this->fieldData($fields['email'], 'varchar(100)', false, '', false, false); $this->fieldData($fields['activo'], 'smallint(1)', true, '', true, false); From f41bb56a870739e237075152f6066cf21945c4cd Mon Sep 17 00:00:00 2001 From: Joanhey Date: Wed, 1 Apr 2020 00:38:07 +0200 Subject: [PATCH 102/148] Change mysql create table --- .travis.yml | 2 +- tests/ActiveRecord/Metadata/MetadataTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 8104628..bd45d1b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -39,7 +39,7 @@ before_script: fi - if [[ "$DB" == "mysql" ]]; then mysql -e 'CREATE DATABASE IF NOT EXISTS test; CREATE TABLE IF NOT EXISTS test.usuario ( - id int NOT NULL AUTO_INCREMENT, + id BIGINT(20) NOT NULL AUTO_INCREMENT, nombre varchar(50) NOT NULL , email varchar(100) NOT NULL , activo smallint(1) NULL DEFAULT 1 , diff --git a/tests/ActiveRecord/Metadata/MetadataTest.php b/tests/ActiveRecord/Metadata/MetadataTest.php index 5e7a279..6a7f2fd 100644 --- a/tests/ActiveRecord/Metadata/MetadataTest.php +++ b/tests/ActiveRecord/Metadata/MetadataTest.php @@ -63,7 +63,7 @@ public function testGetFields() $this->assertEquals(['Type', 'Null', 'Key', 'Default', 'Auto'], array_keys($fields[$fieldName])); } - $this->fieldData($fields['id'], 'int', false, 'PRI', false, true); + $this->fieldData($fields['id'], 'bigint(20)', false, 'PRI', false, true); $this->fieldData($fields['nombre'], 'varchar(50)', false, '', false, false); $this->fieldData($fields['email'], 'varchar(100)', false, '', false, false); $this->fieldData($fields['activo'], 'smallint(1)', true, '', true, false); From 087993c06afab0873aa498289b4ec3f7cfcfaa79 Mon Sep 17 00:00:00 2001 From: Joanhey Date: Wed, 1 Apr 2020 00:44:45 +0200 Subject: [PATCH 103/148] Mysql describe table return auto_increment in lower case --- Metadata/MysqlMetadata.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Metadata/MysqlMetadata.php b/Metadata/MysqlMetadata.php index 27d7cc0..7017c17 100644 --- a/Metadata/MysqlMetadata.php +++ b/Metadata/MysqlMetadata.php @@ -49,7 +49,7 @@ protected function queryFields(\PDO $pdo, string $table, string $schema = ''): a 'Null' => $value->Null !== 'NO', 'Key' => $value->Key, 'Default' => $value->Default != '', - 'Auto' => \strtolower($value->Extra) === 'auto_increment' + 'Auto' => $value->Extra === 'auto_increment' ]; $this->filterColumn($fields[$value->Field], $value->Field); } From 71b4dad65011b933fb8cf4a81162401c033353a1 Mon Sep 17 00:00:00 2001 From: Joanhey Date: Wed, 1 Apr 2020 00:55:16 +0200 Subject: [PATCH 104/148] Delete extra variables --- tests/ActiveRecord/Metadata/MetadataTest.php | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/tests/ActiveRecord/Metadata/MetadataTest.php b/tests/ActiveRecord/Metadata/MetadataTest.php index 6a7f2fd..ca05bf4 100644 --- a/tests/ActiveRecord/Metadata/MetadataTest.php +++ b/tests/ActiveRecord/Metadata/MetadataTest.php @@ -34,16 +34,15 @@ public function testInstanceOfDriverDb() public function testGetPK() { - $metadata = $this->createClass(); - $pk = $metadata->getPK(); + $pk = $this->createClass()->getPK(); $this->assertEquals('id', $pk); } public function testGetWithDefault() { - $metadata = $this->createClass(); - $withDefault = $metadata->getWithDefault(); + + $withDefault = $this->createClass()->getWithDefault(); $this->assertEquals(1, count($withDefault)); $this->assertEquals('activo', $withDefault[0]); @@ -51,8 +50,7 @@ public function testGetWithDefault() public function testGetFields() { - $metadata = $this->createClass(); - $fields = $metadata->getFields(); + $fields = $this->createClass()->getFields(); $this->assertEquals(4, count($fields)); @@ -80,8 +78,7 @@ protected function fieldData($field, $type, $null, $key, $default, $auto) public function testGetFieldsList() { - $metadata = $this->createClass(); - $fields = $metadata->getFieldsList(); + $fields = $this->createClass()->getFieldsList(); $this->assertEquals(4, count($fields)); @@ -90,9 +87,7 @@ public function testGetFieldsList() public function testGetAutoFields() { - $metadata = $this->createClass(); - $fields = $metadata->getAutoFields(); - + $fields = $this->createClass()->getAutoFields(); $this->assertEquals(1, count($fields)); From a2d82dea4267f584d620dc0f42084b87dd1dd2dd Mon Sep 17 00:00:00 2001 From: Joanhey Date: Wed, 1 Apr 2020 00:59:36 +0200 Subject: [PATCH 105/148] Change createClass() to getMetadata() --- tests/ActiveRecord/Metadata/MetadataTest.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/ActiveRecord/Metadata/MetadataTest.php b/tests/ActiveRecord/Metadata/MetadataTest.php index ca05bf4..0ab9adb 100644 --- a/tests/ActiveRecord/Metadata/MetadataTest.php +++ b/tests/ActiveRecord/Metadata/MetadataTest.php @@ -19,14 +19,14 @@ public function setUp(): void $this->schemaName = $GLOBALS['metadata_schema']; } - protected function createClass(): Metadata + protected function getMetadata(): Metadata { return Metadata::get($this->dbName, $this->tableName, $this->schemaName); } public function testInstanceOfDriverDb() { - $metadata = $this->createClass(); + $metadata = $this->getMetdata(); $dbDriverClass = \ucfirst($this->dbName).'Metadata'; $this->assertInstanceOf('\\Kumbia\\ActiveRecord\\Metadata\\'.$dbDriverClass, $metadata); @@ -34,7 +34,7 @@ public function testInstanceOfDriverDb() public function testGetPK() { - $pk = $this->createClass()->getPK(); + $pk = $this->getMetdata()->getPK(); $this->assertEquals('id', $pk); } @@ -42,7 +42,7 @@ public function testGetPK() public function testGetWithDefault() { - $withDefault = $this->createClass()->getWithDefault(); + $withDefault = $this->getMetdata()->getWithDefault(); $this->assertEquals(1, count($withDefault)); $this->assertEquals('activo', $withDefault[0]); @@ -50,7 +50,7 @@ public function testGetWithDefault() public function testGetFields() { - $fields = $this->createClass()->getFields(); + $fields = $this->getMetdata()->getFields(); $this->assertEquals(4, count($fields)); @@ -78,7 +78,7 @@ protected function fieldData($field, $type, $null, $key, $default, $auto) public function testGetFieldsList() { - $fields = $this->createClass()->getFieldsList(); + $fields = $this->getMetdata()->getFieldsList(); $this->assertEquals(4, count($fields)); @@ -87,7 +87,7 @@ public function testGetFieldsList() public function testGetAutoFields() { - $fields = $this->createClass()->getAutoFields(); + $fields = $this->getMetdata()->getAutoFields(); $this->assertEquals(1, count($fields)); From 5359f5df20c5f741d285c2108e80011c5d61eca0 Mon Sep 17 00:00:00 2001 From: Joanhey Date: Wed, 1 Apr 2020 01:02:55 +0200 Subject: [PATCH 106/148] Fix another typo :( --- tests/ActiveRecord/Metadata/MetadataTest.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/ActiveRecord/Metadata/MetadataTest.php b/tests/ActiveRecord/Metadata/MetadataTest.php index 0ab9adb..057179b 100644 --- a/tests/ActiveRecord/Metadata/MetadataTest.php +++ b/tests/ActiveRecord/Metadata/MetadataTest.php @@ -26,7 +26,7 @@ protected function getMetadata(): Metadata public function testInstanceOfDriverDb() { - $metadata = $this->getMetdata(); + $metadata = $this->getMetadata(); $dbDriverClass = \ucfirst($this->dbName).'Metadata'; $this->assertInstanceOf('\\Kumbia\\ActiveRecord\\Metadata\\'.$dbDriverClass, $metadata); @@ -34,7 +34,7 @@ public function testInstanceOfDriverDb() public function testGetPK() { - $pk = $this->getMetdata()->getPK(); + $pk = $this->getMetadata()->getPK(); $this->assertEquals('id', $pk); } @@ -42,7 +42,7 @@ public function testGetPK() public function testGetWithDefault() { - $withDefault = $this->getMetdata()->getWithDefault(); + $withDefault = $this->getMetadata()->getWithDefault(); $this->assertEquals(1, count($withDefault)); $this->assertEquals('activo', $withDefault[0]); @@ -50,7 +50,7 @@ public function testGetWithDefault() public function testGetFields() { - $fields = $this->getMetdata()->getFields(); + $fields = $this->getMetadata()->getFields(); $this->assertEquals(4, count($fields)); @@ -78,7 +78,7 @@ protected function fieldData($field, $type, $null, $key, $default, $auto) public function testGetFieldsList() { - $fields = $this->getMetdata()->getFieldsList(); + $fields = $this->getMetadata()->getFieldsList(); $this->assertEquals(4, count($fields)); @@ -87,7 +87,7 @@ public function testGetFieldsList() public function testGetAutoFields() { - $fields = $this->getMetdata()->getAutoFields(); + $fields = $this->getMetadata()->getAutoFields(); $this->assertEquals(1, count($fields)); From 0e35c909756fe0974d07c985b342204b48be9c9d Mon Sep 17 00:00:00 2001 From: Joanhey Date: Wed, 1 Apr 2020 01:07:18 +0200 Subject: [PATCH 107/148] Fix filterColumn() --- Metadata/Metadata.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Metadata/Metadata.php b/Metadata/Metadata.php index 5b1ce9f..458e82e 100644 --- a/Metadata/Metadata.php +++ b/Metadata/Metadata.php @@ -143,9 +143,11 @@ protected function filterColumn(array $meta, string $field): void { if ($meta['Key'] === 'PRI') { $this->pk = $field; - } elseif ($meta['Default']) { + } + if ($meta['Default']) { $this->withDefault[] = $field; - } elseif ($meta['Auto']) { + } + if ($meta['Auto']) { $this->autoFields[] = $field; } } From aefb87ae84f5c46c45d98900b248b16b99a89840 Mon Sep 17 00:00:00 2001 From: Joanhey Date: Wed, 1 Apr 2020 01:18:54 +0200 Subject: [PATCH 108/148] Delete unnecessary validations So will be easy to pass parameters --- tests/ActiveRecord/Metadata/MetadataTest.php | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/tests/ActiveRecord/Metadata/MetadataTest.php b/tests/ActiveRecord/Metadata/MetadataTest.php index 057179b..2cb94aa 100644 --- a/tests/ActiveRecord/Metadata/MetadataTest.php +++ b/tests/ActiveRecord/Metadata/MetadataTest.php @@ -44,16 +44,13 @@ public function testGetWithDefault() $withDefault = $this->getMetadata()->getWithDefault(); - $this->assertEquals(1, count($withDefault)); - $this->assertEquals('activo', $withDefault[0]); + $this->assertEquals(['activo'], $withDefault); } public function testGetFields() { $fields = $this->getMetadata()->getFields(); - $this->assertEquals(4, count($fields)); - $fieldList = array_keys($fields); $this->assertEquals(['id', 'nombre', 'email', 'activo'], $fieldList); @@ -80,8 +77,6 @@ public function testGetFieldsList() { $fields = $this->getMetadata()->getFieldsList(); - $this->assertEquals(4, count($fields)); - $this->assertEquals(['id', 'nombre', 'email', 'activo'], $fields); } @@ -89,8 +84,6 @@ public function testGetAutoFields() { $fields = $this->getMetadata()->getAutoFields(); - $this->assertEquals(1, count($fields)); - $this->assertEquals(['id'], $fields); } } From 8d24d843b28ee2c222fa476a4d2afc7b2ced75ca Mon Sep 17 00:00:00 2001 From: Joanhey Date: Wed, 1 Apr 2020 13:41:47 +0200 Subject: [PATCH 109/148] Now use database kumbia_test, table user In english and the database name is less generic to avoid colissions --- .travis.yml | 10 +++++----- tests/config/databases.php | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/.travis.yml b/.travis.yml index bd45d1b..c5a5cf1 100644 --- a/.travis.yml +++ b/.travis.yml @@ -27,9 +27,9 @@ env: # - DB=sqlite before_script: - - if [[ "$DB" == "pgsql" ]]; then psql -c "DROP DATABASE IF EXISTS test;" -U postgres; fi - - if [[ "$DB" == "pgsql" ]]; then psql -c 'CREATE DATABASE test;' -U postgres; fi - - if [[ "$DB" == "pgsql" ]]; then psql -c 'CREATE TABLE IF NOT EXISTS usuario ( + - if [[ "$DB" == "pgsql" ]]; then psql -c "DROP DATABASE IF EXISTS kumbia_test;" -U postgres; fi + - if [[ "$DB" == "pgsql" ]]; then psql -c 'CREATE DATABASE kumbia_test;' -U postgres; fi + - if [[ "$DB" == "pgsql" ]]; then psql -c 'CREATE TABLE IF NOT EXISTS user ( id serial PRIMARY KEY, nombre varchar(50) NOT NULL , email varchar(100) NOT NULL , @@ -37,8 +37,8 @@ before_script: );' -U postgres; fi - - if [[ "$DB" == "mysql" ]]; then mysql -e 'CREATE DATABASE IF NOT EXISTS test; - CREATE TABLE IF NOT EXISTS test.usuario ( + - if [[ "$DB" == "mysql" ]]; then mysql -e 'CREATE DATABASE IF NOT EXISTS kumbia_test; + CREATE TABLE IF NOT EXISTS kumbia_test.user ( id BIGINT(20) NOT NULL AUTO_INCREMENT, nombre varchar(50) NOT NULL , email varchar(100) NOT NULL , diff --git a/tests/config/databases.php b/tests/config/databases.php index b924c0b..dfaa0aa 100644 --- a/tests/config/databases.php +++ b/tests/config/databases.php @@ -3,7 +3,7 @@ return [ //Mysql 'mysql' => [ - 'dsn' => 'mysql:host=127.0.0.1;dbname=test;charset=utf8', + 'dsn' => 'mysql:host=127.0.0.1;dbname=kumbia_test;charset=utf8', 'username' => 'root', 'password' => '', 'params' => [ @@ -13,7 +13,7 @@ ], //Pgsql 'pgsql' => [ - 'dsn' => 'pgsql:dbname=test;host=localhost', + 'dsn' => 'pgsql:dbname=kumbia_test;host=localhost', 'username' => 'postgres', 'password' => '', 'params' => [ From cae66bd6202c4da55bc6cb18ec8562a3acd4b7b4 Mon Sep 17 00:00:00 2001 From: Joanhey Date: Wed, 1 Apr 2020 13:42:56 +0200 Subject: [PATCH 110/148] Change to table user in phpunit.xml --- tests/phpunit.xml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/phpunit.xml b/tests/phpunit.xml index b0f991b..2879d3f 100644 --- a/tests/phpunit.xml +++ b/tests/phpunit.xml @@ -2,7 +2,7 @@ - + @@ -17,6 +17,7 @@ . ./tests + ./temp From de07c031021e2f6e0db5dcb27d4440c98fd93b96 Mon Sep 17 00:00:00 2001 From: Joanhey Date: Wed, 1 Apr 2020 13:44:00 +0200 Subject: [PATCH 111/148] Add SQLite metadata class --- Metadata/SqliteMetadata.php | 55 +++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/Metadata/SqliteMetadata.php b/Metadata/SqliteMetadata.php index b3d9bbc..2d7242f 100644 --- a/Metadata/SqliteMetadata.php +++ b/Metadata/SqliteMetadata.php @@ -1 +1,56 @@ query("PRAGMA table_info($table)", \PDO::FETCH_OBJ); + //var_dump($results); die(); + $fields = []; + foreach ($describe as $value) { + $fields[$value->name] = [ + 'Type' => $value->type, + 'Null' => $value->notnull !== 'NO', + 'Default' => $value->dflt_value, + 'Key' => $value->pk === 1 ? 'PRI' : '', + 'Auto' => ($value->type === 'int' && $value->pk === 1) + ]; + } + + return $fields; + } +} From 3479dc668a3e8a59145348f7aa65a977f50c0dff Mon Sep 17 00:00:00 2001 From: Joanhey Date: Wed, 1 Apr 2020 14:07:02 +0200 Subject: [PATCH 112/148] Change table name to test kumbia_test.test --- .travis.yml | 4 ++-- tests/phpunit.xml | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index c5a5cf1..0183bd2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -29,7 +29,7 @@ env: before_script: - if [[ "$DB" == "pgsql" ]]; then psql -c "DROP DATABASE IF EXISTS kumbia_test;" -U postgres; fi - if [[ "$DB" == "pgsql" ]]; then psql -c 'CREATE DATABASE kumbia_test;' -U postgres; fi - - if [[ "$DB" == "pgsql" ]]; then psql -c 'CREATE TABLE IF NOT EXISTS user ( + - if [[ "$DB" == "pgsql" ]]; then psql -c 'CREATE TABLE IF NOT EXISTS test ( id serial PRIMARY KEY, nombre varchar(50) NOT NULL , email varchar(100) NOT NULL , @@ -38,7 +38,7 @@ before_script: -U postgres; fi - if [[ "$DB" == "mysql" ]]; then mysql -e 'CREATE DATABASE IF NOT EXISTS kumbia_test; - CREATE TABLE IF NOT EXISTS kumbia_test.user ( + CREATE TABLE IF NOT EXISTS kumbia_test.test ( id BIGINT(20) NOT NULL AUTO_INCREMENT, nombre varchar(50) NOT NULL , email varchar(100) NOT NULL , diff --git a/tests/phpunit.xml b/tests/phpunit.xml index 2879d3f..660ebe5 100644 --- a/tests/phpunit.xml +++ b/tests/phpunit.xml @@ -2,7 +2,7 @@ - + From 72abb95c5b20caf4bbd9e497066bf61ad30f770b Mon Sep 17 00:00:00 2001 From: Joanhey Date: Wed, 1 Apr 2020 14:07:44 +0200 Subject: [PATCH 113/148] Add funding to composer.json --- composer.json | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index 616cd66..9f1bd00 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,11 @@ "name": "kumbia/activerecord", "type": "library", "description": "Fast ActiveRecord", - "keywords": ["ActiveRecord","database","ORM"], + "keywords": [ + "ActiveRecord", + "database", + "ORM" + ], "homepage": "https://github.com/KumbiaPHP/ActiveRecord", "license": "BSD-3-Clause", "support": { @@ -30,10 +34,18 @@ "php": ">=7.2.0" }, "autoload": { - "psr-4": { "Kumbia\\ActiveRecord": "." } + "psr-4": { + "Kumbia\\ActiveRecord": "." + } }, "config": { "optimize-autoloader": true, "sort-packages": true - } + }, + "funding": [ + { + "type": "Open Collective", + "url": "https://opencollective.com/kumbiaphp" + } + ] } From 4ada44445bcc1739c529897dbbaed960cc0b2959 Mon Sep 17 00:00:00 2001 From: Joanhey Date: Wed, 1 Apr 2020 14:21:17 +0200 Subject: [PATCH 114/148] Add PrivateUtil::callMethod() To test private and protected methods easily. Not allways necessary, but useful to check hidden bugs. --- tests/Utils/PrivateUtil.php | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 tests/Utils/PrivateUtil.php diff --git a/tests/Utils/PrivateUtil.php b/tests/Utils/PrivateUtil.php new file mode 100644 index 0000000..356ce0e --- /dev/null +++ b/tests/Utils/PrivateUtil.php @@ -0,0 +1,22 @@ +setAccessible(true); + + return $method->invokeArgs($obj, $args); + } +} From f856c49cd68647bcbd4e30783f96ed6ea5682d9a Mon Sep 17 00:00:00 2001 From: Joanhey Date: Wed, 1 Apr 2020 14:24:02 +0200 Subject: [PATCH 115/148] Check if extension installed pdo_xxx To skip the tests and show an informative message. --- tests/ActiveRecord/Metadata/MetadataTest.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/ActiveRecord/Metadata/MetadataTest.php b/tests/ActiveRecord/Metadata/MetadataTest.php index 2cb94aa..5cc5c33 100644 --- a/tests/ActiveRecord/Metadata/MetadataTest.php +++ b/tests/ActiveRecord/Metadata/MetadataTest.php @@ -15,6 +15,12 @@ public function setUp(): void { $this->dbName = getenv('DB'); + if (!extension_loaded('pdo_'.$this->dbName)) { + $this->markTestSkipped( + 'The pdo_'.$this->dbName.' extension is not available.' + ); + } + $this->tableName = $GLOBALS['metadata_table']; $this->schemaName = $GLOBALS['metadata_schema']; } From 69f4fe7c1dea14030792bde6cb3a648ce7b5c9c1 Mon Sep 17 00:00:00 2001 From: Joanhey Date: Wed, 1 Apr 2020 15:04:55 +0200 Subject: [PATCH 116/148] Travis force create table in public schema --- .travis.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 0183bd2..003fdf3 100644 --- a/.travis.yml +++ b/.travis.yml @@ -27,8 +27,8 @@ env: # - DB=sqlite before_script: - - if [[ "$DB" == "pgsql" ]]; then psql -c "DROP DATABASE IF EXISTS kumbia_test;" -U postgres; fi - - if [[ "$DB" == "pgsql" ]]; then psql -c 'CREATE DATABASE kumbia_test;' -U postgres; fi + - if [[ "$DB" == "pgsql" ]]; then psql -c "DROP DATABASE IF EXISTS public.kumbia_test;" -U postgres; fi + - if [[ "$DB" == "pgsql" ]]; then psql -c 'CREATE DATABASE public.kumbia_test;' -U postgres; fi - if [[ "$DB" == "pgsql" ]]; then psql -c 'CREATE TABLE IF NOT EXISTS test ( id serial PRIMARY KEY, nombre varchar(50) NOT NULL , From 36391467e8f98edd6d2886d898f71e5475317b18 Mon Sep 17 00:00:00 2001 From: Joanhey Date: Wed, 1 Apr 2020 15:09:10 +0200 Subject: [PATCH 117/148] Revert last commit --- .travis.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 003fdf3..0183bd2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -27,8 +27,8 @@ env: # - DB=sqlite before_script: - - if [[ "$DB" == "pgsql" ]]; then psql -c "DROP DATABASE IF EXISTS public.kumbia_test;" -U postgres; fi - - if [[ "$DB" == "pgsql" ]]; then psql -c 'CREATE DATABASE public.kumbia_test;' -U postgres; fi + - if [[ "$DB" == "pgsql" ]]; then psql -c "DROP DATABASE IF EXISTS kumbia_test;" -U postgres; fi + - if [[ "$DB" == "pgsql" ]]; then psql -c 'CREATE DATABASE kumbia_test;' -U postgres; fi - if [[ "$DB" == "pgsql" ]]; then psql -c 'CREATE TABLE IF NOT EXISTS test ( id serial PRIMARY KEY, nombre varchar(50) NOT NULL , From fefb503944f47fbb355a25c94b4be6ffaae3d548 Mon Sep 17 00:00:00 2001 From: Joanhey Date: Wed, 1 Apr 2020 17:09:24 +0200 Subject: [PATCH 118/148] Only assert exact array in testGetFields() Faster tests, more informative and easy to adapt-configure per driver --- tests/ActiveRecord/Metadata/MetadataTest.php | 43 ++++++++++++++++++-- 1 file changed, 39 insertions(+), 4 deletions(-) diff --git a/tests/ActiveRecord/Metadata/MetadataTest.php b/tests/ActiveRecord/Metadata/MetadataTest.php index 5cc5c33..4d03452 100644 --- a/tests/ActiveRecord/Metadata/MetadataTest.php +++ b/tests/ActiveRecord/Metadata/MetadataTest.php @@ -53,11 +53,46 @@ public function testGetWithDefault() $this->assertEquals(['activo'], $withDefault); } + protected static function ExpectedGetFields(): array + { + return [ + [activo] => [ + [Type] => 'smallint(1)' + [Null] => true + [Default] => true + [Key] => '' + [Auto] => false + ] + [email] => [ + [Type] => 'varchar(100)' + [Null] => false + [Default] => false + [Key] => '' + [Auto] => false + ] + [id] => [ + [Type] => 'bigint(20)' + [Null] => false + [Default] => false + [Key] => 'PRI' + [Auto] => true + ] + [nombre] => [ + [Type] => 'varchar(50)' + [Null] => false + [Default] => false + [Key] => '' + [Auto] => false + ] + ]; + } public function testGetFields() { $fields = $this->getMetadata()->getFields(); - $fieldList = array_keys($fields); + $this->assertsEquals(self::ExpectedGetFields(), $fields); + + /* $fieldList = array_keys($fields); $this->assertEquals(['id', 'nombre', 'email', 'activo'], $fieldList); foreach($fieldList as $fieldName) { @@ -67,17 +102,17 @@ public function testGetFields() $this->fieldData($fields['id'], 'bigint(20)', false, 'PRI', false, true); $this->fieldData($fields['nombre'], 'varchar(50)', false, '', false, false); $this->fieldData($fields['email'], 'varchar(100)', false, '', false, false); - $this->fieldData($fields['activo'], 'smallint(1)', true, '', true, false); + $this->fieldData($fields['activo'], 'smallint(1)', true, '', true, false); */ } - protected function fieldData($field, $type, $null, $key, $default, $auto) + /* protected function fieldData($field, $type, $null, $key, $default, $auto) { $this->assertEquals($type, $field['Type']); $this->assertEquals($null, $field['Null']); $this->assertEquals($key, $field['Key']); $this->assertEquals($default, $field['Default']); $this->assertEquals($auto, $field['Auto']); - } + } */ public function testGetFieldsList() { From 50abb159069dd273283c1e5d690a6e7ff022176c Mon Sep 17 00:00:00 2001 From: Joanhey Date: Wed, 1 Apr 2020 17:19:35 +0200 Subject: [PATCH 119/148] Fix expectedGefFields array --- tests/ActiveRecord/Metadata/MetadataTest.php | 54 ++++++++++---------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/tests/ActiveRecord/Metadata/MetadataTest.php b/tests/ActiveRecord/Metadata/MetadataTest.php index 4d03452..b545476 100644 --- a/tests/ActiveRecord/Metadata/MetadataTest.php +++ b/tests/ActiveRecord/Metadata/MetadataTest.php @@ -56,33 +56,33 @@ public function testGetWithDefault() protected static function ExpectedGetFields(): array { return [ - [activo] => [ - [Type] => 'smallint(1)' - [Null] => true - [Default] => true - [Key] => '' - [Auto] => false - ] - [email] => [ - [Type] => 'varchar(100)' - [Null] => false - [Default] => false - [Key] => '' - [Auto] => false - ] - [id] => [ - [Type] => 'bigint(20)' - [Null] => false - [Default] => false - [Key] => 'PRI' - [Auto] => true - ] - [nombre] => [ - [Type] => 'varchar(50)' - [Null] => false - [Default] => false - [Key] => '' - [Auto] => false + 'activo' => [ + 'Type' => 'smallint(1)', + 'Null' => true, + 'Default' => true, + 'Key' => '', + 'Auto' => false + ], + 'email' => [ + 'Type' => 'varchar(100)', + 'Null' => false, + 'Default' => false, + 'Key' => '', + 'Auto' => false, + ], + 'id' => [ + 'Type' => 'bigint(20)', + 'Null' => false, + 'Default' => false, + 'Key' => 'PRI', + 'Auto' => true, + ], + 'nombre' => [ + 'Type' => 'varchar(50)', + 'Null' => false, + 'Default' => false, + 'Key' => '', + 'Auto' => false, ] ]; } From c5408ff51c02539033e2e1eaa48b4ac361d849b1 Mon Sep 17 00:00:00 2001 From: Joanhey Date: Wed, 1 Apr 2020 17:22:50 +0200 Subject: [PATCH 120/148] Fix typo --- tests/ActiveRecord/Metadata/MetadataTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ActiveRecord/Metadata/MetadataTest.php b/tests/ActiveRecord/Metadata/MetadataTest.php index b545476..fd9340e 100644 --- a/tests/ActiveRecord/Metadata/MetadataTest.php +++ b/tests/ActiveRecord/Metadata/MetadataTest.php @@ -90,7 +90,7 @@ public function testGetFields() { $fields = $this->getMetadata()->getFields(); - $this->assertsEquals(self::ExpectedGetFields(), $fields); + $this->assertEquals(self::ExpectedGetFields(), $fields); /* $fieldList = array_keys($fields); $this->assertEquals(['id', 'nombre', 'email', 'activo'], $fieldList); From 1676383f1a2ed7e9f6c7207602b6e89933bae7e5 Mon Sep 17 00:00:00 2001 From: Joanhey Date: Wed, 1 Apr 2020 17:49:38 +0200 Subject: [PATCH 121/148] Delete unused code --- tests/ActiveRecord/Metadata/MetadataTest.php | 21 -------------------- 1 file changed, 21 deletions(-) diff --git a/tests/ActiveRecord/Metadata/MetadataTest.php b/tests/ActiveRecord/Metadata/MetadataTest.php index fd9340e..bd3427a 100644 --- a/tests/ActiveRecord/Metadata/MetadataTest.php +++ b/tests/ActiveRecord/Metadata/MetadataTest.php @@ -91,29 +91,8 @@ public function testGetFields() $fields = $this->getMetadata()->getFields(); $this->assertEquals(self::ExpectedGetFields(), $fields); - - /* $fieldList = array_keys($fields); - $this->assertEquals(['id', 'nombre', 'email', 'activo'], $fieldList); - - foreach($fieldList as $fieldName) { - $this->assertEquals(['Type', 'Null', 'Key', 'Default', 'Auto'], array_keys($fields[$fieldName])); - } - - $this->fieldData($fields['id'], 'bigint(20)', false, 'PRI', false, true); - $this->fieldData($fields['nombre'], 'varchar(50)', false, '', false, false); - $this->fieldData($fields['email'], 'varchar(100)', false, '', false, false); - $this->fieldData($fields['activo'], 'smallint(1)', true, '', true, false); */ } - /* protected function fieldData($field, $type, $null, $key, $default, $auto) - { - $this->assertEquals($type, $field['Type']); - $this->assertEquals($null, $field['Null']); - $this->assertEquals($key, $field['Key']); - $this->assertEquals($default, $field['Default']); - $this->assertEquals($auto, $field['Auto']); - } */ - public function testGetFieldsList() { $fields = $this->getMetadata()->getFieldsList(); From f742fedaff4ec7c1bf8118066acec6788f3169f5 Mon Sep 17 00:00:00 2001 From: Joanhey Date: Wed, 1 Apr 2020 17:51:15 +0200 Subject: [PATCH 122/148] Testing small change error in expectedGetFields() --- tests/ActiveRecord/Metadata/MetadataTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/ActiveRecord/Metadata/MetadataTest.php b/tests/ActiveRecord/Metadata/MetadataTest.php index bd3427a..0cc237a 100644 --- a/tests/ActiveRecord/Metadata/MetadataTest.php +++ b/tests/ActiveRecord/Metadata/MetadataTest.php @@ -53,11 +53,11 @@ public function testGetWithDefault() $this->assertEquals(['activo'], $withDefault); } - protected static function ExpectedGetFields(): array + protected static function expectedGetFields(): array { return [ 'activo' => [ - 'Type' => 'smallint(1)', + 'Type' => 'smallint', 'Null' => true, 'Default' => true, 'Key' => '', @@ -90,7 +90,7 @@ public function testGetFields() { $fields = $this->getMetadata()->getFields(); - $this->assertEquals(self::ExpectedGetFields(), $fields); + $this->assertEquals(self::expectedGetFields(), $fields); } public function testGetFieldsList() From 22ea2732979f965194d9d02794f42f11b5cfcb8e Mon Sep 17 00:00:00 2001 From: Joanhey Date: Wed, 1 Apr 2020 18:00:42 +0200 Subject: [PATCH 123/148] Revert to the correct array expected The informative error was completely clear --- tests/ActiveRecord/Metadata/MetadataTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ActiveRecord/Metadata/MetadataTest.php b/tests/ActiveRecord/Metadata/MetadataTest.php index 0cc237a..718d9b2 100644 --- a/tests/ActiveRecord/Metadata/MetadataTest.php +++ b/tests/ActiveRecord/Metadata/MetadataTest.php @@ -57,7 +57,7 @@ protected static function expectedGetFields(): array { return [ 'activo' => [ - 'Type' => 'smallint', + 'Type' => 'smallint(1)', 'Null' => true, 'Default' => true, 'Key' => '', From d0e6bd8de59e948210427a4712535df2fd62b6b6 Mon Sep 17 00:00:00 2001 From: Joanhey Date: Wed, 1 Apr 2020 20:58:41 +0200 Subject: [PATCH 124/148] Fix PgsqlMetdata --- Metadata/PgsqlMetadata.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Metadata/PgsqlMetadata.php b/Metadata/PgsqlMetadata.php index 1983e48..ec82d26 100644 --- a/Metadata/PgsqlMetadata.php +++ b/Metadata/PgsqlMetadata.php @@ -35,8 +35,9 @@ class PgsqlMetadata extends Metadata * * @return array */ - protected function queryFields(\PDO $pdo, string $table, string $schema = 'public'): array + protected function queryFields(\PDO $pdo, string $table, string $schema = ''): array { + $schema = $schema === '' ? 'public' : $schema; // default to public // Nota: Se excluyen claves compuestas $describe = $pdo->query( "SELECT DISTINCT From af2e76afc94e0b456272151db7d42852793f90dc Mon Sep 17 00:00:00 2001 From: Joanhey Date: Wed, 1 Apr 2020 21:11:03 +0200 Subject: [PATCH 125/148] Test PgsqlMetadata --- Metadata/PgsqlMetadata.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Metadata/PgsqlMetadata.php b/Metadata/PgsqlMetadata.php index ec82d26..447a645 100644 --- a/Metadata/PgsqlMetadata.php +++ b/Metadata/PgsqlMetadata.php @@ -55,7 +55,7 @@ protected function queryFields(\PDO $pdo, string $table, string $schema = ''): a LEFT OUTER JOIN information_schema.table_constraints tc ON (cu.constraint_name = tc.constraint_name AND tc.constraint_type IN ('PRIMARY KEY', 'UNIQUE')) - WHERE c.table_name = '$table' AND c.table_schema = '$schema';", + WHERE c.table_name = '$table';", \PDO::FETCH_OBJ ); From b5a0aba15136be98171a30772e736fe299dc4590 Mon Sep 17 00:00:00 2001 From: Joanhey Date: Wed, 1 Apr 2020 23:16:01 +0200 Subject: [PATCH 126/148] Revert schema in sql --- Metadata/PgsqlMetadata.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Metadata/PgsqlMetadata.php b/Metadata/PgsqlMetadata.php index 447a645..3314fd6 100644 --- a/Metadata/PgsqlMetadata.php +++ b/Metadata/PgsqlMetadata.php @@ -55,7 +55,8 @@ protected function queryFields(\PDO $pdo, string $table, string $schema = ''): a LEFT OUTER JOIN information_schema.table_constraints tc ON (cu.constraint_name = tc.constraint_name AND tc.constraint_type IN ('PRIMARY KEY', 'UNIQUE')) - WHERE c.table_name = '$table';", + WHERE c.table_name = '$table' AND c.table_schema = '$schema' + ;", \PDO::FETCH_OBJ ); From 4a373a29d2a416487e264e8c3e679ff192826cd6 Mon Sep 17 00:00:00 2001 From: Joanhey Date: Wed, 1 Apr 2020 23:17:17 +0200 Subject: [PATCH 127/148] Delete DISTINCT in pgsql describe sql --- Metadata/PgsqlMetadata.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Metadata/PgsqlMetadata.php b/Metadata/PgsqlMetadata.php index 3314fd6..eecabb5 100644 --- a/Metadata/PgsqlMetadata.php +++ b/Metadata/PgsqlMetadata.php @@ -40,7 +40,7 @@ protected function queryFields(\PDO $pdo, string $table, string $schema = ''): a $schema = $schema === '' ? 'public' : $schema; // default to public // Nota: Se excluyen claves compuestas $describe = $pdo->query( - "SELECT DISTINCT + "SELECT c.column_name AS field, c.udt_name AS type, tc.constraint_type AS key, From 6607e8fcb9f67b6e5c483fcb4ecf1fac86e0879c Mon Sep 17 00:00:00 2001 From: Joanhey Date: Wed, 1 Apr 2020 23:17:50 +0200 Subject: [PATCH 128/148] Force bool in pgsql Auto fields --- Metadata/PgsqlMetadata.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Metadata/PgsqlMetadata.php b/Metadata/PgsqlMetadata.php index eecabb5..c91b15d 100644 --- a/Metadata/PgsqlMetadata.php +++ b/Metadata/PgsqlMetadata.php @@ -81,7 +81,7 @@ private function describe(array $describe): array 'Null' => $value->null !== 'NO', 'Default' => $value->default != '', 'Key' => \substr($value->key, 0, 3), - 'Auto' => \preg_match('/^nextval\(/', $value->default) + 'Auto' => (bool) \preg_match('/^nextval\(/', $value->default) ]; $this->filterColumn($fields[$value->field], $value->field); } From ab41dbec72300513cff9f3b77b1f27d3c114e74d Mon Sep 17 00:00:00 2001 From: Joanhey Date: Thu, 2 Apr 2020 01:54:46 +0200 Subject: [PATCH 129/148] Reorder expectedGetFields array --- tests/ActiveRecord/Metadata/MetadataTest.php | 57 ++++++++++---------- 1 file changed, 29 insertions(+), 28 deletions(-) diff --git a/tests/ActiveRecord/Metadata/MetadataTest.php b/tests/ActiveRecord/Metadata/MetadataTest.php index 718d9b2..503bca9 100644 --- a/tests/ActiveRecord/Metadata/MetadataTest.php +++ b/tests/ActiveRecord/Metadata/MetadataTest.php @@ -5,6 +5,7 @@ class MetadataTest extends TestCase { + protected $dbName; protected $tableName; @@ -17,7 +18,7 @@ public function setUp(): void if (!extension_loaded('pdo_'.$this->dbName)) { $this->markTestSkipped( - 'The pdo_'.$this->dbName.' extension is not available.' + "The pdo_{$this->dbName} extension is not available." ); } @@ -56,33 +57,33 @@ public function testGetWithDefault() protected static function expectedGetFields(): array { return [ - 'activo' => [ - 'Type' => 'smallint(1)', - 'Null' => true, - 'Default' => true, - 'Key' => '', - 'Auto' => false - ], - 'email' => [ - 'Type' => 'varchar(100)', - 'Null' => false, - 'Default' => false, - 'Key' => '', - 'Auto' => false, - ], - 'id' => [ - 'Type' => 'bigint(20)', - 'Null' => false, - 'Default' => false, - 'Key' => 'PRI', - 'Auto' => true, - ], - 'nombre' => [ - 'Type' => 'varchar(50)', - 'Null' => false, - 'Default' => false, - 'Key' => '', - 'Auto' => false, + 'id' => [ + 'Type' => 'bigint(20)', + 'Null' => false, + 'Default' => false, + 'Key' => 'PRI', + 'Auto' => true, + ], + 'nombre' => [ + 'Type' => 'varchar(50)', + 'Null' => false, + 'Default' => false, + 'Key' => '', + 'Auto' => false, + ], + 'email' => [ + 'Type' => 'varchar(100)', + 'Null' => false, + 'Default' => false, + 'Key' => '', + 'Auto' => false, + ], + 'activo' => [ + 'Type' => 'smallint(1)', + 'Null' => true, + 'Default' => true, + 'Key' => '', + 'Auto' => false ] ]; } From 884fe2e7ae7274696698733602f820c09b73c3bc Mon Sep 17 00:00:00 2001 From: Joanhey Date: Thu, 2 Apr 2020 04:17:01 +0200 Subject: [PATCH 130/148] Delete create table in travis --- .travis.yml | 24 +++++------------------- 1 file changed, 5 insertions(+), 19 deletions(-) diff --git a/.travis.yml b/.travis.yml index 0183bd2..95cb1ae 100644 --- a/.travis.yml +++ b/.travis.yml @@ -27,25 +27,11 @@ env: # - DB=sqlite before_script: - - if [[ "$DB" == "pgsql" ]]; then psql -c "DROP DATABASE IF EXISTS kumbia_test;" -U postgres; fi - - if [[ "$DB" == "pgsql" ]]; then psql -c 'CREATE DATABASE kumbia_test;' -U postgres; fi - - if [[ "$DB" == "pgsql" ]]; then psql -c 'CREATE TABLE IF NOT EXISTS test ( - id serial PRIMARY KEY, - nombre varchar(50) NOT NULL , - email varchar(100) NOT NULL , - activo smallint NULL DEFAULT 1 - );' - -U postgres; - fi - - if [[ "$DB" == "mysql" ]]; then mysql -e 'CREATE DATABASE IF NOT EXISTS kumbia_test; - CREATE TABLE IF NOT EXISTS kumbia_test.test ( - id BIGINT(20) NOT NULL AUTO_INCREMENT, - nombre varchar(50) NOT NULL , - email varchar(100) NOT NULL , - activo smallint(1) NULL DEFAULT 1 , - PRIMARY KEY (id) );' - ; fi - + - "mysql -e 'DROP DATABASE IF EXISTS kumbia_test;'" + - "mysql -e 'create database kumbia_test;'" + - "psql -c 'DROP DATABASE IF EXISTS kumbia_test;' -U postgres" + - "psql -c 'create database kumbia_test;' -U postgres" + script: - phpunit --configuration tests/phpunit.xml --coverage-text --colors --coverage-clover=coverage.clover - wget https://scrutinizer-ci.com/ocular.phar From 3c3754e3c443ebc89bc8b7ef4aa0b4908bb48229 Mon Sep 17 00:00:00 2001 From: Joanhey Date: Thu, 2 Apr 2020 04:17:43 +0200 Subject: [PATCH 131/148] Delete DB env in travis --- .travis.yml | 5 ----- 1 file changed, 5 deletions(-) diff --git a/.travis.yml b/.travis.yml index 95cb1ae..4fdb175 100644 --- a/.travis.yml +++ b/.travis.yml @@ -21,11 +21,6 @@ services: - mysql - postgresql -env: - - DB=mysql - - DB=pgsql -# - DB=sqlite - before_script: - "mysql -e 'DROP DATABASE IF EXISTS kumbia_test;'" - "mysql -e 'create database kumbia_test;'" From 0f6e0088719cbed8579a2d643eb09728c1db4bd5 Mon Sep 17 00:00:00 2001 From: Joanhey Date: Thu, 2 Apr 2020 12:18:37 +0200 Subject: [PATCH 132/148] ExpectedGetFiels is now in an atribute --- tests/ActiveRecord/Metadata/MetadataTest.php | 88 +++++++++----------- 1 file changed, 41 insertions(+), 47 deletions(-) diff --git a/tests/ActiveRecord/Metadata/MetadataTest.php b/tests/ActiveRecord/Metadata/MetadataTest.php index 503bca9..41393fd 100644 --- a/tests/ActiveRecord/Metadata/MetadataTest.php +++ b/tests/ActiveRecord/Metadata/MetadataTest.php @@ -3,7 +3,7 @@ use PHPUnit\Framework\TestCase; use Kumbia\ActiveRecord\Metadata\Metadata; -class MetadataTest extends TestCase +abstract class MetadataTest extends TestCase { protected $dbName; @@ -11,19 +11,44 @@ class MetadataTest extends TestCase protected $tableName; protected $schemaName; - - public function setUp(): void - { - $this->dbName = getenv('DB'); - if (!extension_loaded('pdo_'.$this->dbName)) { - $this->markTestSkipped( - "The pdo_{$this->dbName} extension is not available." - ); - } + protected $expectedGetFields = [ + 'id' => [ + 'Type' => 'int', + 'Null' => false, + 'Default' => false, + 'Key' => 'PRI', + 'Auto' => true, + ], + 'nombre' => [ + 'Type' => 'varchar(50)', + 'Null' => false, + 'Default' => false, + 'Key' => '', + 'Auto' => false, + ], + 'email' => [ + 'Type' => 'varchar(100)', + 'Null' => false, + 'Default' => false, + 'Key' => '', + 'Auto' => false, + ], + 'activo' => [ + 'Type' => 'smallint(1)', + 'Null' => true, + 'Default' => true, + 'Key' => '', + 'Auto' => false + ] + ]; + + - $this->tableName = $GLOBALS['metadata_table']; - $this->schemaName = $GLOBALS['metadata_schema']; + public function setUp(): void + { + $this->tableName = getenv('metadata_table'); + $this->schemaName = getenv('metadata_schema'); } protected function getMetadata(): Metadata @@ -48,50 +73,17 @@ public function testGetPK() public function testGetWithDefault() { - $withDefault = $this->getMetadata()->getWithDefault(); $this->assertEquals(['activo'], $withDefault); } - protected static function expectedGetFields(): array - { - return [ - 'id' => [ - 'Type' => 'bigint(20)', - 'Null' => false, - 'Default' => false, - 'Key' => 'PRI', - 'Auto' => true, - ], - 'nombre' => [ - 'Type' => 'varchar(50)', - 'Null' => false, - 'Default' => false, - 'Key' => '', - 'Auto' => false, - ], - 'email' => [ - 'Type' => 'varchar(100)', - 'Null' => false, - 'Default' => false, - 'Key' => '', - 'Auto' => false, - ], - 'activo' => [ - 'Type' => 'smallint(1)', - 'Null' => true, - 'Default' => true, - 'Key' => '', - 'Auto' => false - ] - ]; - } + public function testGetFields() { $fields = $this->getMetadata()->getFields(); - $this->assertEquals(self::expectedGetFields(), $fields); + $this->assertEquals($this->expectedGetFields, $fields); } public function testGetFieldsList() @@ -108,3 +100,5 @@ public function testGetAutoFields() $this->assertEquals(['id'], $fields); } } +//TODO add validation when don't connect to bd and don't get the metadata +// now fail silently From 173eec77ef1b48790f2114de622777b6102ae977 Mon Sep 17 00:00:00 2001 From: Joanhey Date: Thu, 2 Apr 2020 12:19:19 +0200 Subject: [PATCH 133/148] Add MysqlMetadatTest::class --- .../Metadata/MysqlMetadataTest.php | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 tests/ActiveRecord/Metadata/MysqlMetadataTest.php diff --git a/tests/ActiveRecord/Metadata/MysqlMetadataTest.php b/tests/ActiveRecord/Metadata/MysqlMetadataTest.php new file mode 100644 index 0000000..d346f75 --- /dev/null +++ b/tests/ActiveRecord/Metadata/MysqlMetadataTest.php @@ -0,0 +1,27 @@ +query(' + CREATE TABLE IF NOT EXISTS kumbia_test.test ( + id INT NOT NULL AUTO_INCREMENT, + nombre varchar(50) NOT NULL , + email varchar(100) NOT NULL , + activo smallint(1) NULL DEFAULT 1 , + PRIMARY KEY (id) );' + ); + } +} From 392e7c9bfcdb06c6b69b3bca27e0d1e98ffab965 Mon Sep 17 00:00:00 2001 From: Joanhey Date: Thu, 2 Apr 2020 12:19:55 +0200 Subject: [PATCH 134/148] Add PgsqlMetadataTest class --- .../Metadata/PgsqlMetadataTest.php | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 tests/ActiveRecord/Metadata/PgsqlMetadataTest.php diff --git a/tests/ActiveRecord/Metadata/PgsqlMetadataTest.php b/tests/ActiveRecord/Metadata/PgsqlMetadataTest.php new file mode 100644 index 0000000..f4d4ac7 --- /dev/null +++ b/tests/ActiveRecord/Metadata/PgsqlMetadataTest.php @@ -0,0 +1,67 @@ + [ + 'Type' => 'int4', // int + 'Null' => false, + 'Default' => true, //false + 'Key' => 'PRI', + 'Auto' => true, + ], + 'nombre' => [ + 'Type' => 'varchar', + 'Null' => false, + 'Default' => false, + 'Key' => '', + 'Auto' => false, + ], + 'email' => [ + 'Type' => 'varchar', + 'Null' => false, + 'Default' => false, + 'Key' => '', + 'Auto' => false, + ], + 'activo' => [ + 'Type' => 'int2', // smallint(1) + 'Null' => true, + 'Default' => true, + 'Key' => '', + 'Auto' => false + ] + ]; + + /** + * @beforeClass + */ + public static function setUpCreateTable() + { + Db::get('pgsql')->query(' + CREATE TABLE IF NOT EXISTS test ( + id serial PRIMARY KEY, + nombre varchar(50) NOT NULL , + email varchar(100) NOT NULL , + activo smallint NULL DEFAULT 1 + );' + ); + } + + //TODO Fix it to delete it + public function testGetWithDefault() + { + $withDefault = $this->getMetadata()->getWithDefault(); + + $this->assertEquals(['id', 'activo'], $withDefault); + } +} From f18c9614a03146070b25684f37347e64984ae9de Mon Sep 17 00:00:00 2001 From: Joanhey Date: Thu, 2 Apr 2020 12:21:06 +0200 Subject: [PATCH 135/148] Add describe to Sqlite database --- Metadata/SqliteMetadata.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/Metadata/SqliteMetadata.php b/Metadata/SqliteMetadata.php index 2d7242f..d80eb8a 100644 --- a/Metadata/SqliteMetadata.php +++ b/Metadata/SqliteMetadata.php @@ -43,12 +43,13 @@ protected function queryFields(\PDO $pdo, string $table, string $schema = ''): a $fields = []; foreach ($describe as $value) { $fields[$value->name] = [ - 'Type' => $value->type, - 'Null' => $value->notnull !== 'NO', - 'Default' => $value->dflt_value, - 'Key' => $value->pk === 1 ? 'PRI' : '', - 'Auto' => ($value->type === 'int' && $value->pk === 1) + 'Type' => \strtolower(\str_replace(' ', '', $value->type)), + 'Null' => $value->notnull == 0, + 'Default' => (bool) $value->dflt_value, + 'Key' => $value->pk == 1 ? 'PRI' : '', + 'Auto' => (\strtolower($value->type) == 'int' && $value->pk == 1) ]; + $this->filterColumn($fields[$value->name], $value->name); } return $fields; From 80f6e29c795372f9b75ed2e91f6beaa546a33872 Mon Sep 17 00:00:00 2001 From: Joanhey Date: Thu, 2 Apr 2020 12:23:09 +0200 Subject: [PATCH 136/148] Add tests to SqliteMetadata --- .travis.yml | 2 +- .../Metadata/SqliteMetadataTest.php | 27 +++++++++++++++++++ tests/phpunit.xml | 4 +-- 3 files changed, 30 insertions(+), 3 deletions(-) create mode 100644 tests/ActiveRecord/Metadata/SqliteMetadataTest.php diff --git a/.travis.yml b/.travis.yml index 4fdb175..6f1cad9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -15,7 +15,7 @@ notifications: slack: kumbiaphp:51JaKQTXASwf52D8b32OyWb9 # irc: "irc.freenode.org#kumbiaphp" # email: -# - programador.manuel@gmail.com +# - xxxxx@gmail.com services: - mysql diff --git a/tests/ActiveRecord/Metadata/SqliteMetadataTest.php b/tests/ActiveRecord/Metadata/SqliteMetadataTest.php new file mode 100644 index 0000000..3268092 --- /dev/null +++ b/tests/ActiveRecord/Metadata/SqliteMetadataTest.php @@ -0,0 +1,27 @@ +query(' + CREATE TABLE IF NOT EXISTS test ( + id int PRIMARY KEY NOT NULL, + nombre VARCHAR(50) NOT NULL, + email VARCHAR(100) NOT NULL, + activo SMALLINT(1) DEFAULT (1) + );' + ); + } +} diff --git a/tests/phpunit.xml b/tests/phpunit.xml index 660ebe5..f2ade9b 100644 --- a/tests/phpunit.xml +++ b/tests/phpunit.xml @@ -2,8 +2,8 @@ - - + + From 0ed7c7b0529bffa5853f412d521fd837ea8f95cf Mon Sep 17 00:00:00 2001 From: Joanhey Date: Thu, 2 Apr 2020 12:24:31 +0200 Subject: [PATCH 137/148] Add Sqlite in config/databases By default the Db cllass add PDO::ERRMODE_EXCEPTION --- tests/config/databases.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/config/databases.php b/tests/config/databases.php index dfaa0aa..6ab82ce 100644 --- a/tests/config/databases.php +++ b/tests/config/databases.php @@ -21,6 +21,11 @@ \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION ] ], + 'sqlite' => [ + 'dsn' => 'sqlite::memory:', + 'username' => '', + 'password' => '', + ] //More connections From 0ab32555fba28a46bcab0c3edb8822a4e1b702a2 Mon Sep 17 00:00:00 2001 From: Joanhey Date: Thu, 2 Apr 2020 12:28:40 +0200 Subject: [PATCH 138/148] Update DbTests to work without envs --- tests/ActiveRecord/DbTest.php | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/tests/ActiveRecord/DbTest.php b/tests/ActiveRecord/DbTest.php index 79ec2a2..3ec9a24 100644 --- a/tests/ActiveRecord/DbTest.php +++ b/tests/ActiveRecord/DbTest.php @@ -5,19 +5,35 @@ class DbTest extends TestCase { + /** + * @requires extension pdo_sqlite + */ public function testGetInstance() { - $instance = Db::get(getenv('DB')); + + $instance = Db::get('sqlite'); $this->assertInstanceOf('PDO', $instance); } + /** + * @requires extension pdo_mysql + */ public function testGet() { - $instance = Db::get(getenv('DB')); + $instance = Db::get('mysql'); - $instance2 = Db::get(getenv('DB')); + $instance2 = Db::get('mysql'); $this->assertEquals($instance, $instance2); } + + public function testGetThatDontExistInConfig() + { + $this->expectException(RuntimeException::class); + $this->expectExceptionMessageRegExp('/No existen datos de conexión para la bd$/'); + + $instance = Db::get('no_exist'); + + } } From 26997ceb0c05f06a09bd21fc9f4dcfe5e5e53f5d Mon Sep 17 00:00:00 2001 From: Joanhey Date: Thu, 2 Apr 2020 12:46:01 +0200 Subject: [PATCH 139/148] Fix regexp in test database thta don't exist in config/databases --- tests/ActiveRecord/DbTest.php | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/tests/ActiveRecord/DbTest.php b/tests/ActiveRecord/DbTest.php index 3ec9a24..ce03ab9 100644 --- a/tests/ActiveRecord/DbTest.php +++ b/tests/ActiveRecord/DbTest.php @@ -31,9 +31,18 @@ public function testGet() public function testGetThatDontExistInConfig() { $this->expectException(RuntimeException::class); - $this->expectExceptionMessageRegExp('/No existen datos de conexión para la bd$/'); + $this->expectExceptionMessageRegExp('/^No existen datos de conexión para la bd/'); $instance = Db::get('no_exist'); } +/* + public function testGetWithBadCredentialsInConfig() + { + //$this->expectException(RuntimeException::class); + //$this->expectExceptionMessageRegExp('/No existen datos de conexión para la bd$/'); + + $instance = Db::get('bad_credentials'); + + }*/ } From 88db6a18bef6fb70a522195f246b9b4df56c10fc Mon Sep 17 00:00:00 2001 From: Joanhey Date: Thu, 2 Apr 2020 13:26:59 +0200 Subject: [PATCH 140/148] Fix SqliteMetdata --- Metadata/SqliteMetadata.php | 2 +- .../Metadata/SqliteMetadataTest.php | 34 ++++++++++++++++++- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/Metadata/SqliteMetadata.php b/Metadata/SqliteMetadata.php index d80eb8a..1427d9b 100644 --- a/Metadata/SqliteMetadata.php +++ b/Metadata/SqliteMetadata.php @@ -47,7 +47,7 @@ protected function queryFields(\PDO $pdo, string $table, string $schema = ''): a 'Null' => $value->notnull == 0, 'Default' => (bool) $value->dflt_value, 'Key' => $value->pk == 1 ? 'PRI' : '', - 'Auto' => (\strtolower($value->type) == 'int' && $value->pk == 1) + 'Auto' => (\strtolower($value->type) == 'int' && $value->pk == 1) // using rowid ]; $this->filterColumn($fields[$value->name], $value->name); } diff --git a/tests/ActiveRecord/Metadata/SqliteMetadataTest.php b/tests/ActiveRecord/Metadata/SqliteMetadataTest.php index 3268092..7256c5b 100644 --- a/tests/ActiveRecord/Metadata/SqliteMetadataTest.php +++ b/tests/ActiveRecord/Metadata/SqliteMetadataTest.php @@ -10,13 +10,45 @@ class SqliteMetadataTest extends MetadataTest protected $dbName = 'sqlite'; + protected $expectedGetFields = [ + 'id' => [ + 'Type' => 'int', //int(11) + 'Null' => false, + 'Default' => false, + 'Key' => 'PRI', + 'Auto' => true, + ], + 'nombre' => [ + 'Type' => 'varchar(50)', + 'Null' => false, + 'Default' => false, + 'Key' => '', + 'Auto' => false, + ], + 'email' => [ + 'Type' => 'varchar(100)', + 'Null' => false, + 'Default' => false, + 'Key' => '', + 'Auto' => false, + ], + 'activo' => [ + 'Type' => 'smallint(1)', + 'Null' => true, + 'Default' => true, + 'Key' => '', + 'Auto' => false + ] + ]; + /** * @beforeClass */ public static function setUpCreateTable() { Db::get('sqlite')->query(' - CREATE TABLE IF NOT EXISTS test ( + CREATE TABLE IF NOT EXISTS test + ( id int PRIMARY KEY NOT NULL, nombre VARCHAR(50) NOT NULL, email VARCHAR(100) NOT NULL, From 524463b0ef79aba479825eae2e333d0c75860128 Mon Sep 17 00:00:00 2001 From: Joanhey Date: Thu, 2 Apr 2020 13:29:57 +0200 Subject: [PATCH 141/148] Fix Mysql create table --- tests/ActiveRecord/Metadata/MysqlMetadataTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ActiveRecord/Metadata/MysqlMetadataTest.php b/tests/ActiveRecord/Metadata/MysqlMetadataTest.php index d346f75..0815ab0 100644 --- a/tests/ActiveRecord/Metadata/MysqlMetadataTest.php +++ b/tests/ActiveRecord/Metadata/MysqlMetadataTest.php @@ -17,7 +17,7 @@ public static function setUpCreateTable() { Db::get('mysql')->query(' CREATE TABLE IF NOT EXISTS kumbia_test.test ( - id INT NOT NULL AUTO_INCREMENT, + id INT(11) NOT NULL AUTO_INCREMENT, nombre varchar(50) NOT NULL , email varchar(100) NOT NULL , activo smallint(1) NULL DEFAULT 1 , From 1e492abe2d7d87c9446262c878da71793bbe49c8 Mon Sep 17 00:00:00 2001 From: Joanhey Date: Thu, 2 Apr 2020 13:33:11 +0200 Subject: [PATCH 142/148] Add change in sql --- tests/ActiveRecord/Metadata/MetadataTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ActiveRecord/Metadata/MetadataTest.php b/tests/ActiveRecord/Metadata/MetadataTest.php index 41393fd..d11848c 100644 --- a/tests/ActiveRecord/Metadata/MetadataTest.php +++ b/tests/ActiveRecord/Metadata/MetadataTest.php @@ -14,7 +14,7 @@ abstract class MetadataTest extends TestCase protected $expectedGetFields = [ 'id' => [ - 'Type' => 'int', + 'Type' => 'int(11)', 'Null' => false, 'Default' => false, 'Key' => 'PRI', From 8619653bc7a37e3dc1078e2897ace12a9f9ff71e Mon Sep 17 00:00:00 2001 From: Joanhey Date: Thu, 2 Apr 2020 14:39:09 +0200 Subject: [PATCH 143/148] Delete throw exception in hasPK() now returns bool --- BaseRecord.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/BaseRecord.php b/BaseRecord.php index c2ec43f..68f963d 100644 --- a/BaseRecord.php +++ b/BaseRecord.php @@ -108,13 +108,11 @@ public function getAlias(): array /** * Verifica que PK este seteado. * - * @throws KumbiaException + * @return bool */ - protected function hasPK() + protected function hasPK(): bool { - if (! isset($this->{static::$pk})) { - throw new KumbiaException(_('No se ha especificado valor para la clave primaria')); - } + return isset($this->{static::$pk}); } /** From 9cecb0b89dab94ef92e743e6e1b615c930354af7 Mon Sep 17 00:00:00 2001 From: Joanhey Date: Thu, 2 Apr 2020 14:43:09 +0200 Subject: [PATCH 144/148] Update phpdoc in LiteRecord --- LiteRecord.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/LiteRecord.php b/LiteRecord.php index c919807..7f7cdb8 100644 --- a/LiteRecord.php +++ b/LiteRecord.php @@ -191,7 +191,7 @@ public static function get($pk, $fields = '*'): self * @param string $sql * @param array $values * - * @return array + * @return static[] */ public static function all(string $sql = '', array $values = []): array { @@ -208,9 +208,9 @@ public static function all(string $sql = '', array $values = []): array * @param string $sql * @param array $values * - * @return self + * @return static */ - public static function first(string $sql, array $values = []): self + public static function first(string $sql, array $values = [])//: static in php 8 { return static::query($sql, $values)->fetch(); } From 077ff90858bb8cc4b302eae926358ea329d9d608 Mon Sep 17 00:00:00 2001 From: Joanhey Date: Fri, 3 Apr 2020 05:05:32 +0200 Subject: [PATCH 145/148] Add require ext-pdo in composer.json --- composer.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 9f1bd00..cdefe52 100644 --- a/composer.json +++ b/composer.json @@ -31,7 +31,8 @@ } ], "require": { - "php": ">=7.2.0" + "php": ">=7.2.0", + "ext-pdo": "*" }, "autoload": { "psr-4": { From c0c0b5649d0cf88e2a54151b7a15a6460a563cde Mon Sep 17 00:00:00 2001 From: Joanhey Date: Fri, 3 Apr 2020 13:52:23 +0200 Subject: [PATCH 146/148] Add test to Db::setConfig() --- tests/ActiveRecord/DbTest.php | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/tests/ActiveRecord/DbTest.php b/tests/ActiveRecord/DbTest.php index ce03ab9..f2ec874 100644 --- a/tests/ActiveRecord/DbTest.php +++ b/tests/ActiveRecord/DbTest.php @@ -34,9 +34,8 @@ public function testGetThatDontExistInConfig() $this->expectExceptionMessageRegExp('/^No existen datos de conexión para la bd/'); $instance = Db::get('no_exist'); - } -/* + public function testGetWithBadCredentialsInConfig() { //$this->expectException(RuntimeException::class); @@ -44,5 +43,22 @@ public function testGetWithBadCredentialsInConfig() $instance = Db::get('bad_credentials'); - }*/ + public function testSetConfig() + { + $config = ['dynamic' => [ + 'dsn' => 'pgsql:dbname=kumbia_test;host=localhost', + 'username' => 'postgres', + 'password' => '', + 'params' => [ + \PDO::ATTR_PERSISTENT => \true, //conexión persistente + \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION + ] + ] + ]; + + Db::setConfig($config); + $instance = Db::get('dynamic'); + + $this->assertInstanceOf('PDO', $instance); + } } From 67d928b7a59c43cd7839a6de645eca9f74624a74 Mon Sep 17 00:00:00 2001 From: Joanhey Date: Fri, 3 Apr 2020 13:56:57 +0200 Subject: [PATCH 147/148] Add bad credentials test in DbTest --- tests/ActiveRecord/DbTest.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/ActiveRecord/DbTest.php b/tests/ActiveRecord/DbTest.php index f2ec874..44e2297 100644 --- a/tests/ActiveRecord/DbTest.php +++ b/tests/ActiveRecord/DbTest.php @@ -38,10 +38,11 @@ public function testGetThatDontExistInConfig() public function testGetWithBadCredentialsInConfig() { - //$this->expectException(RuntimeException::class); + $this->expectException(RuntimeException::class); //$this->expectExceptionMessageRegExp('/No existen datos de conexión para la bd$/'); $instance = Db::get('bad_credentials'); + } public function testSetConfig() { From 5c38c32b430272468cc4b9cc9de9c9a6d9e5a625 Mon Sep 17 00:00:00 2001 From: Joanhey Date: Fri, 3 Apr 2020 15:42:10 +0200 Subject: [PATCH 148/148] Add more errors in tests config databases --- tests/ActiveRecord/DbTest.php | 6 +++--- tests/config/databases.php | 15 ++++++++++++++- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/tests/ActiveRecord/DbTest.php b/tests/ActiveRecord/DbTest.php index 44e2297..cfaeee7 100644 --- a/tests/ActiveRecord/DbTest.php +++ b/tests/ActiveRecord/DbTest.php @@ -36,12 +36,12 @@ public function testGetThatDontExistInConfig() $instance = Db::get('no_exist'); } - public function testGetWithBadCredentialsInConfig() + public function testGetConfigWithoutPassword() { $this->expectException(RuntimeException::class); - //$this->expectExceptionMessageRegExp('/No existen datos de conexión para la bd$/'); + $this->expectExceptionMessageRegExp('/No se pudo realizar la conexión con/'); - $instance = Db::get('bad_credentials'); + $instance = Db::get('no_password'); } public function testSetConfig() diff --git a/tests/config/databases.php b/tests/config/databases.php index 6ab82ce..6f1e8d9 100644 --- a/tests/config/databases.php +++ b/tests/config/databases.php @@ -15,16 +15,29 @@ 'pgsql' => [ 'dsn' => 'pgsql:dbname=kumbia_test;host=localhost', 'username' => 'postgres', - 'password' => '', + 'password' => '414141', 'params' => [ \PDO::ATTR_PERSISTENT => \true, //conexión persistente \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION ] ], + //Sqlite 'sqlite' => [ 'dsn' => 'sqlite::memory:', 'username' => '', 'password' => '', + ], + + // bad connections to tests errors + 'no_dsn' => [ + 'dsn' => '' + ], + 'no_password' => [ + 'dsn' => 'pgsql:dbname=no_exist;host=localhost' + ], + 'bad_credentials' => [ + 'dsn' => 'pgsql:dbname=no_exist;host=localhost', + 'password' => 'as' ]