Started working on renewed Unit Tests.
This commit is contained in:
parent
68b3b402e7
commit
5093f98c4b
|
@ -8,7 +8,7 @@ class Example extends Model{
|
|||
public function __construct(&$core){
|
||||
parent::__construct($core);
|
||||
|
||||
$this->setType('techfuze/databaseutils', 'Model');
|
||||
$this->setType('core/databaseutils', 'Model');
|
||||
$this->table = 'example';
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,13 @@
|
|||
<?php
|
||||
namespace Model;
|
||||
use \FuzeWorks\Model;
|
||||
|
||||
class Table extends Model{
|
||||
|
||||
public function __construct(){
|
||||
|
||||
$this->setType('core/databaseutils', 'Model');
|
||||
$this->fields = '*';
|
||||
$this->table = 'table';
|
||||
}
|
||||
}
|
|
@ -54,7 +54,7 @@ namespace FuzeWorks;
|
|||
*/
|
||||
class Events {
|
||||
|
||||
private static $listeners = array();
|
||||
public static $listeners = array();
|
||||
private static $enabled = true;
|
||||
private static $register;
|
||||
|
||||
|
|
|
@ -127,7 +127,7 @@ class Model {
|
|||
*/
|
||||
public function select(){
|
||||
|
||||
$queryBuilder = new Query($this->core);
|
||||
$queryBuilder = new Query();
|
||||
$queryBuilder->setTable($this->table);
|
||||
call_user_func_array(array($queryBuilder, 'select'), func_get_args());
|
||||
$queryBuilder->from();
|
||||
|
@ -143,7 +143,7 @@ class Model {
|
|||
|
||||
public function update(){
|
||||
|
||||
$queryBuilder = new Query($this->core);
|
||||
$queryBuilder = new Query();
|
||||
$queryBuilder->setTable($this->table);
|
||||
call_user_func_array(array($queryBuilder, 'update'), func_get_args());
|
||||
|
||||
|
@ -158,7 +158,7 @@ class Model {
|
|||
|
||||
public function delete(){
|
||||
|
||||
$queryBuilder = new Query($this->core);
|
||||
$queryBuilder = new Query();
|
||||
$queryBuilder->setTable($this->table);
|
||||
call_user_func_array(array($queryBuilder, 'delete'), func_get_args());
|
||||
|
||||
|
@ -175,7 +175,7 @@ class Model {
|
|||
|
||||
public function insert($array){
|
||||
|
||||
$queryBuilder = new Query($this->core);
|
||||
$queryBuilder = new Query();
|
||||
$queryBuilder->setTable($this->table);
|
||||
call_user_func_array(array($queryBuilder, 'insert'), func_get_args());
|
||||
|
||||
|
|
|
@ -50,14 +50,9 @@ use \FuzeWorks\Logger;
|
|||
class Query extends Module {
|
||||
|
||||
/**
|
||||
* @var string The string containing the query
|
||||
* @var array An array containing all the counted functions
|
||||
*/
|
||||
private $query = '';
|
||||
|
||||
/**
|
||||
* @var array The binds corresponding to the query
|
||||
*/
|
||||
private $binds = array();
|
||||
private $functions = array();
|
||||
|
||||
/**
|
||||
* @var null|string The table used
|
||||
|
@ -70,49 +65,68 @@ class Query extends Module {
|
|||
private $sth = null;
|
||||
|
||||
/**
|
||||
* Default module function on creation
|
||||
* @param string $table The string to set as a table, optional
|
||||
*/
|
||||
public function onLoad() {}
|
||||
public function __construct($table = ''){
|
||||
|
||||
if($table != '')
|
||||
$this->setTable($table);
|
||||
}
|
||||
/**
|
||||
* Each argument is another field to select
|
||||
*
|
||||
* @param string $field,... The field to select, if no field is given, all fields(*) will be selected
|
||||
* @return $this
|
||||
*/
|
||||
public function select()
|
||||
{
|
||||
$this->query .= 'SELECT ' . (func_num_args() == 0 ? '*' : '`' . implode("`, `", func_get_args()) . '`');
|
||||
|
||||
return $this;
|
||||
public function select($field = '*'){
|
||||
|
||||
return $this->__call(__FUNCTION__, func_get_args());
|
||||
}
|
||||
|
||||
private function internalSelect($field = '*')
|
||||
{
|
||||
return array(
|
||||
'sql'=> 'SELECT ' . ($field == '*' ? '*' : implode(", ", func_get_args()) . ''),
|
||||
'binds' => null
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* From
|
||||
*
|
||||
* @param ...$table null|string The table, if it is null, the table set with setTable() will be used
|
||||
* @param $table,... null|string The table, if it is null, the table set with setTable() will be used
|
||||
* @return $this
|
||||
* @throws Exception
|
||||
*/
|
||||
public function from()
|
||||
public function from($table = null){
|
||||
|
||||
return $this->__call(__FUNCTION__, func_get_args());
|
||||
}
|
||||
|
||||
private function internalFrom($table = null)
|
||||
{
|
||||
$tables = func_get_args();
|
||||
|
||||
$this->query .= ' FROM ';
|
||||
$query = 'FROM ';
|
||||
|
||||
if(count($tables) == 0)
|
||||
$tables[] = "`$this->table`";
|
||||
$tables[] = "$this->table";
|
||||
|
||||
else foreach($tables as $i => $t) {
|
||||
|
||||
if ($t == '' && $this->table == '')
|
||||
throw new Exception("No table given");
|
||||
|
||||
$tables[$i] = strpos($t, ' ') === false ? "`$t`" : '`'.implode('` ', explode(' ', $t));
|
||||
$tables[$i] = strpos($t, ' ') === false ? "$t" : ''.implode(' ', explode(' ', $t));
|
||||
}
|
||||
|
||||
$this->query .= implode(', ', $tables);
|
||||
$query .= implode(', ', $tables);
|
||||
|
||||
return $this;
|
||||
return array(
|
||||
'sql' => $query,
|
||||
'binds' => null
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -125,6 +139,11 @@ class Query extends Module {
|
|||
*/
|
||||
public function join($table, $type = ''){
|
||||
|
||||
return $this->__call(__FUNCTION__, func_get_args());
|
||||
}
|
||||
|
||||
private function internalJoin($table, $type = ''){
|
||||
|
||||
if($table === '') {
|
||||
|
||||
if ($this->table == '')
|
||||
|
@ -133,12 +152,17 @@ class Query extends Module {
|
|||
$table = $this->table;
|
||||
}
|
||||
|
||||
$query = '';
|
||||
|
||||
if($type != '')
|
||||
$this->query .= ' '.$type;
|
||||
$query .= $type . ' ';
|
||||
|
||||
$this->query .= ' JOIN ' . (strpos($table, ' ') === false ? "`$table`" : '`'.implode('` ', explode(' ', $table)));
|
||||
$query .= 'JOIN ' . (strpos($table, ' ') === false ? "$table" : ''.implode(' ', explode(' ', $table)));
|
||||
|
||||
return $this;
|
||||
return array(
|
||||
'sql' => $query,
|
||||
'binds' => null
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -193,234 +217,138 @@ class Query extends Module {
|
|||
/**
|
||||
* Where
|
||||
*
|
||||
* @param string $field Field name, or raw SQL
|
||||
* @param string|null $field Field name, or raw SQL, If this is left empty, only the $type will be added to the query
|
||||
* @param string $arg2, The value of the field, or an operator in which case the value is pushed to $arg3
|
||||
* @param null|string $arg3 The value of the field when $arg2 is used for an operator
|
||||
* @param string $type Whether this is an WHERE or ON operation
|
||||
* @return $this
|
||||
*/
|
||||
public function where($field, $arg2 = null, $arg3 = null, $type = 'WHERE'){
|
||||
public function where($field = null, $arg2 = null, $arg3 = null, $type = 'WHERE'){
|
||||
|
||||
return $this->__call(__FUNCTION__, func_get_args());
|
||||
}
|
||||
|
||||
private function internalWhere($field = null, $arg2 = null, $arg3 = null, $type = 'WHERE'){
|
||||
|
||||
if($field == null)
|
||||
return array('sql' => $type, 'binds' => array());
|
||||
|
||||
if($arg2 === null)
|
||||
return $this->sql(' '.$type.' '.$field);
|
||||
return $this->internalSql($type . ' '.$field);
|
||||
|
||||
//The value is the second parameter, unless the second parameter is an operator, in which case it's the third
|
||||
$value = ($arg3 == null ? $arg2 : $arg3);
|
||||
//If the third parameter is not given, the default operator should be used
|
||||
$operator = strtoupper($arg3 == null ? "=" : $arg2);
|
||||
|
||||
$field = $this->formatField($field);
|
||||
$query = '';
|
||||
$binds = array();
|
||||
|
||||
switch($operator){
|
||||
|
||||
case 'IN':
|
||||
|
||||
$this->query .= ' '.$type.' '.$field.' IN (';
|
||||
$query .= $type . ($type == '' ? '' : ' ') . $field.' IN (';
|
||||
|
||||
foreach($value as $k => $v){
|
||||
if(is_array($value)) {
|
||||
foreach ($value as $k => $v) {
|
||||
|
||||
$this->query .= '?,';
|
||||
$this->binds[] = $v;
|
||||
$query .= '?,';
|
||||
$binds[] = $v;
|
||||
}
|
||||
|
||||
//Remove the trailing comma and close it
|
||||
$query = rtrim($query, ",") . ')';
|
||||
|
||||
}elseif(get_class($value) == 'System\Core\Query'){
|
||||
|
||||
|
||||
/** @var Query $value */
|
||||
$data = $this->internalSql($value->getSql(), $value->getBinds());
|
||||
$query .= $data['sql'] . ')';
|
||||
$binds = array_merge($binds, $data['binds']);
|
||||
}
|
||||
|
||||
//Remove the trailing comma and close it
|
||||
$this->query = rtrim($this->query, ",") . ')';
|
||||
|
||||
break;
|
||||
|
||||
case 'BETWEEN':
|
||||
|
||||
$this->query .= ' '.$type.' '.$field.' BETWEEN ? AND ?';
|
||||
$this->binds[] = $value[0];
|
||||
$this->binds[] = $value[1];
|
||||
$query .= $type . ($type == '' ? '' : ' ') . $field.' BETWEEN ? AND ?';
|
||||
$binds[] = $value[0];
|
||||
$binds[] = $value[1];
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
|
||||
$this->query .= ' '.$type.' '.$field.' ' . $operator . ' ?';
|
||||
$this->binds[] = $value;
|
||||
$query .= $type . ($type == '' ? '' : ' ') . $field.' ' . $operator . ' ?';
|
||||
$binds[] = $value;
|
||||
|
||||
break;
|
||||
}
|
||||
return $this;
|
||||
|
||||
return array(
|
||||
'sql' => $query,
|
||||
'binds' => $binds
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*And, keep in mind this is only the OR statement. For A = B call ->where
|
||||
* @return array
|
||||
*/
|
||||
private function internalOr(){
|
||||
|
||||
return array(
|
||||
'sql' => 'OR',
|
||||
'binds' => array()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Where open. Is the start of WHERE(....). To end this call ->close()
|
||||
*
|
||||
* @param string $field Field name, or raw SQL
|
||||
* @param $arg2 string, The value of the field, or an operator in which case the value is pushed to $arg3
|
||||
* @param null|string $arg3 The value of the field when $arg2 is used for an operator
|
||||
* And, keep in mind this is only the AND statement. For A = B call ->where
|
||||
* @return array
|
||||
*/
|
||||
private function internalAnd(){
|
||||
|
||||
return array(
|
||||
'sql' => 'AND',
|
||||
'binds' => array()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* An opening bracket
|
||||
* @return $this
|
||||
*/
|
||||
public function where_open($field, $arg2, $arg3 = null){
|
||||
public function open(){
|
||||
|
||||
$old = $this->query;
|
||||
$this->where($field, $arg2, $arg3);
|
||||
return $this->__call(__FUNCTION__, func_get_args());
|
||||
}
|
||||
|
||||
//Replace the WHERE with WHERE (
|
||||
$this->query = $old . ' WHERE (' . substr($this->query, strlen($old) + 7);
|
||||
private function internalOpen(){
|
||||
|
||||
return $this;
|
||||
return array(
|
||||
'sql' => '(',
|
||||
'binds' => ''
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Or, this function should be called after ->where() or ->having(). Please use ->or as an alias instead of ->_or()
|
||||
*
|
||||
* @param string $field Field name, or raw SQL
|
||||
* @param $arg2 string, The value of the field, or an operator in which case the value is pushed to $arg3
|
||||
* @param null|string $arg3 The value of the field when $arg2 is used for an operator
|
||||
* @return $this
|
||||
*/
|
||||
private function _or($field, $arg2 = null, $arg3 = null){
|
||||
|
||||
if($arg2 === null)
|
||||
return $this->sql(' OR '.$field);
|
||||
|
||||
//The value is the second paramter, unless the second parameter is an operator, in which case it's the third
|
||||
$value = ($arg3 == null ? $arg2 : $arg3);
|
||||
//If the third parameter is not given, the default operator should be used
|
||||
$operator = strtoupper($arg3 == null ? "=" : $arg2);
|
||||
|
||||
$field = $this->formatField($field);
|
||||
|
||||
switch($operator){
|
||||
|
||||
case 'IN':
|
||||
|
||||
$this->query .= ' OR ' . $field .' IN (';
|
||||
|
||||
foreach($value as $k => $v){
|
||||
|
||||
$this->query .= '?,';
|
||||
$this->binds[] = $v;
|
||||
}
|
||||
|
||||
//Remove the trailing comma and close it
|
||||
$this->query = rtrim($this->query, ",") . ')';
|
||||
|
||||
|
||||
break;
|
||||
|
||||
case 'BETWEEN':
|
||||
|
||||
$this->query .= ' OR ' . $field .' BETWEEN ? AND ?';
|
||||
$this->binds[] = $value[0];
|
||||
$this->binds[] = $value[1];
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
|
||||
$this->query .= ' OR ' . $field .' ' . $operator . ' ?';
|
||||
$this->binds[] = $value;
|
||||
|
||||
break;
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Or open. Is the start of OR(....). To end this call ->close()
|
||||
*
|
||||
* @param string $field Field name, or raw SQL
|
||||
* @param $arg2 string, The value of the field, or an operator in which case the value is pushed to $arg3
|
||||
* @param null|string $arg3 The value of the field when $arg2 is used for an operator
|
||||
* @return $this
|
||||
*/
|
||||
public function or_open($field, $arg2, $arg3 = null){
|
||||
|
||||
$old = $this->query;
|
||||
$this->_or($field, $arg2, $arg3);
|
||||
//Replace the OR with OR (
|
||||
$this->query = $old . ' OR (' . substr($this->query, strlen($old) + 4);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* And, this function should be called after ->where() or ->having(). Please use ->and as an alias instead of ->_and()
|
||||
*
|
||||
* @param string $field Field name, or raw SQL
|
||||
* @param $arg2 string, The value of the field, or an operator in which case the value is pushed to $arg3
|
||||
* @param null|string $arg3 The value of the field when $arg2 is used for an operator
|
||||
* @return $this
|
||||
*/
|
||||
private function _and($field, $arg2 = null, $arg3 = null){
|
||||
|
||||
if($arg2 === null)
|
||||
return $this->sql(' AND '.$field);
|
||||
|
||||
//The value is the second paramter, unless the second parameter is an operator, in which case it's the third
|
||||
$value = ($arg3 == null ? $arg2 : $arg3);
|
||||
//If the third parameter is not given, the default operator should be used
|
||||
$operator = strtoupper($arg3 == null ? "=" : $arg2);
|
||||
|
||||
$field = $this->formatField($field);
|
||||
|
||||
switch($operator){
|
||||
|
||||
case 'IN':
|
||||
|
||||
$this->query .= ' AND ' . $field .' IN (';
|
||||
|
||||
foreach($value as $k => $v){
|
||||
|
||||
$this->query .= '?,';
|
||||
$this->binds[] = $v;
|
||||
}
|
||||
|
||||
//Remove the trailing comma and close it
|
||||
$this->query = rtrim($this->query, ",") . ')';
|
||||
|
||||
|
||||
break;
|
||||
|
||||
case 'BETWEEN':
|
||||
|
||||
$this->query .= ' AND ' . $field .' BETWEEN ? AND ?';
|
||||
$this->binds[] = $value[0];
|
||||
$this->binds[] = $value[1];
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
|
||||
$this->query .= ' AND ' . $field .' ' . $operator . ' ?';
|
||||
$this->binds[] = $value;
|
||||
|
||||
break;
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* And open. Is the start of AND(....). To end this call ->close()
|
||||
*
|
||||
* @param $field
|
||||
* @param $arg2 string, The value of the field, or an operator in which case the value is pushed to $arg3
|
||||
* @param null|string $arg3 The value of the field when $arg2 is used for an operator
|
||||
* @return $this
|
||||
*/
|
||||
public function and_open($field, $arg2, $arg3 = null){
|
||||
|
||||
$old = $this->query;
|
||||
$this->_and($field, $arg2, $arg3);
|
||||
//Replace the AND with AND (
|
||||
$this->query = $old . ' AND (' . substr($this->query, strlen($old) + 5);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Closes and ->where_open() or ->having_open()
|
||||
* A closing bracket
|
||||
* @return $this
|
||||
*/
|
||||
public function close(){
|
||||
|
||||
$this->query .= ')';
|
||||
return $this;
|
||||
return $this->__call(__FUNCTION__, func_get_args());
|
||||
}
|
||||
|
||||
private function internalClose(){
|
||||
|
||||
return array(
|
||||
'sql' => ')',
|
||||
'binds' => null
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -428,16 +356,22 @@ class Query extends Module {
|
|||
*
|
||||
* Each argument is another order. If you put a minus in front of the name, the order will be DESC instead of ASC
|
||||
*
|
||||
* @param string $field,... The field to order by, add a minus in front of the name and the order will be DESC
|
||||
* @return $this
|
||||
*/
|
||||
public function order(){
|
||||
public function order($field){
|
||||
|
||||
$this->query .= ' ORDER BY';
|
||||
return $this->__call(__FUNCTION__, func_get_args());
|
||||
}
|
||||
|
||||
private function internalOrder($field){
|
||||
|
||||
$query = 'ORDER BY';
|
||||
|
||||
foreach(func_get_args() as $field){
|
||||
|
||||
if(substr($this->query, -2) != 'BY')
|
||||
$this->query .= ",";
|
||||
if(substr($query, -2) != 'BY')
|
||||
$query .= ",";
|
||||
|
||||
$mode = 'ASC';
|
||||
|
||||
|
@ -448,12 +382,49 @@ class Query extends Module {
|
|||
|
||||
}
|
||||
|
||||
$field = $this->formatField($field);
|
||||
|
||||
$this->query .= ' ' . $field . ' ' . $mode;
|
||||
$query .= ' ' . $field . ' ' . $mode;
|
||||
}
|
||||
|
||||
return $this;
|
||||
return array(
|
||||
'sql' => $query,
|
||||
'binds' => null
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Group by
|
||||
*
|
||||
* Each argument is another field to group by.
|
||||
*
|
||||
* @param string $field,... The field to group by
|
||||
* @return $this
|
||||
*/
|
||||
public function groupBy($field){
|
||||
|
||||
return $this->__call(__FUNCTION__, func_get_args());
|
||||
}
|
||||
|
||||
private function internalGroupBy($field){
|
||||
|
||||
$query = 'GROUP BY ';
|
||||
|
||||
$first = true;
|
||||
|
||||
foreach(func_get_args() as $field){
|
||||
|
||||
if(!$first){
|
||||
$query .= ', ';
|
||||
}
|
||||
|
||||
$first = false;
|
||||
|
||||
$query .= $field;
|
||||
}
|
||||
|
||||
return array(
|
||||
'sql' => $query,
|
||||
'binds' => null,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -465,9 +436,15 @@ class Query extends Module {
|
|||
*/
|
||||
public function limit($limit, $offset = 0){
|
||||
|
||||
$this->query .= ' LIMIT ' . $offset . ', ' . $limit;
|
||||
return $this->__call(__FUNCTION__, func_get_args());
|
||||
}
|
||||
|
||||
return $this;
|
||||
private function internalLimit($limit, $offset = 0){
|
||||
|
||||
return array(
|
||||
'sql' => 'LIMIT ' . $offset . ', ' . $limit,
|
||||
'binds' => null
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -478,68 +455,9 @@ class Query extends Module {
|
|||
* @param null|string $arg3 The value of the field when $arg2 is used for an operator
|
||||
* @return $this
|
||||
*/
|
||||
public function having($field, $arg2, $arg3 = null){
|
||||
public function having($field = null, $arg2 = null, $arg3 = null){
|
||||
|
||||
//The value is the second paramter, unless the second parameter is an operator, in which case it's the third
|
||||
$value = ($arg3 == null ? $arg2 : $arg3);
|
||||
//If the third parameter is not given, the default operator should be used
|
||||
$operator = strtoupper($arg3 == null ? "=" : $arg2);
|
||||
|
||||
$field = $this->formatField($field);
|
||||
|
||||
switch($operator){
|
||||
|
||||
case 'IN':
|
||||
|
||||
$this->query .= ' HAVING ' . $field .' IN (';
|
||||
|
||||
foreach($value as $k => $v){
|
||||
|
||||
$this->query .= '?,';
|
||||
$this->binds[] = $v;
|
||||
}
|
||||
|
||||
//Remove the trailing comma and close it
|
||||
$this->query = rtrim($this->query, ",") . ')';
|
||||
|
||||
|
||||
break;
|
||||
|
||||
case 'BETWEEN':
|
||||
|
||||
$this->query .= ' HAVING ' . $field .' BETWEEN ? AND ?';
|
||||
$this->binds[] = $value[0];
|
||||
$this->binds[] = $value[1];
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
|
||||
$this->query .= ' HAVING ' . $field .' ' . $operator . ' ?';
|
||||
$this->binds[] = $value;
|
||||
|
||||
break;
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Having open. Is the start of HAVING(....). To end this call ->close()
|
||||
*
|
||||
* @param $field
|
||||
* @param $arg2 string, The value of the field, or an operator in which case the value is pushed to $arg3
|
||||
* @param null|string $arg3 The value of the field when $arg2 is used for an operator
|
||||
* @return $this
|
||||
*/
|
||||
|
||||
public function having_open($field, $arg2, $arg3 = null){
|
||||
|
||||
$old = $this->query;
|
||||
$this->having($field, $arg2, $arg3);
|
||||
//Replace the WHERE with WHERE (
|
||||
$this->query = $old . ' HAVING (' . substr($this->query, strlen($old) + 8);
|
||||
|
||||
return $this;
|
||||
return $this->where($field, $arg2, $arg3, 'HAVING');
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -549,8 +467,12 @@ class Query extends Module {
|
|||
* @return $this
|
||||
* @throws Exception
|
||||
*/
|
||||
public function update($table = ''){
|
||||
|
||||
public function update($table = '')
|
||||
return $this->__call(__FUNCTION__, func_get_args());
|
||||
}
|
||||
|
||||
private function internalUpdate($table = '')
|
||||
{
|
||||
if($table === ''){
|
||||
|
||||
|
@ -560,9 +482,10 @@ class Query extends Module {
|
|||
$table = $this->table;
|
||||
}
|
||||
|
||||
$this->query .= 'UPDATE `' . $table . '`';
|
||||
|
||||
return $this;
|
||||
return array(
|
||||
'sql' => 'UPDATE ' . $table,
|
||||
'binds' => array()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -571,27 +494,36 @@ class Query extends Module {
|
|||
* @param $data array|string Key value, $field => $value or raw SQL
|
||||
* @return $this
|
||||
*/
|
||||
public function set($data)
|
||||
public function set($data){
|
||||
|
||||
return $this->__call(__FUNCTION__, func_get_args());
|
||||
}
|
||||
|
||||
private function internalSet($data)
|
||||
{
|
||||
if(is_string($data))
|
||||
return $this->sql(' SET '.$data);
|
||||
return $this->internalSql('SET '.$data);
|
||||
|
||||
$this->query .= ' SET';
|
||||
$query = 'SET';
|
||||
$binds = array();
|
||||
|
||||
$first = true;
|
||||
|
||||
foreach($data as $field => $value){
|
||||
|
||||
if(!$first)
|
||||
$this->query .= ',';
|
||||
$query .= ',';
|
||||
|
||||
$first = false;
|
||||
$this->query .= ' `' . $field . '`=?';
|
||||
$query .= ' ' . $field . '=?';
|
||||
|
||||
$this->binds[] = $value;
|
||||
$binds[] = $value;
|
||||
}
|
||||
|
||||
return $this;
|
||||
return array(
|
||||
'sql' => $query,
|
||||
'binds' => $binds
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -599,12 +531,17 @@ class Query extends Module {
|
|||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function delete()
|
||||
public function delete(){
|
||||
|
||||
return $this->__call(__FUNCTION__, func_get_args());
|
||||
}
|
||||
|
||||
private function internalDelete()
|
||||
{
|
||||
|
||||
$this->query .= 'DELETE';
|
||||
|
||||
return $this;
|
||||
return array(
|
||||
'sql' => 'DELETE',
|
||||
'binds' => array()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -617,6 +554,11 @@ class Query extends Module {
|
|||
*/
|
||||
public function insert($array, $table = ''){
|
||||
|
||||
return $this->__call(__FUNCTION__, func_get_args());
|
||||
}
|
||||
|
||||
private function internalInsert($array, $table = ''){
|
||||
|
||||
if($table === ''){
|
||||
|
||||
if($this->table == '')
|
||||
|
@ -625,66 +567,153 @@ class Query extends Module {
|
|||
$table = $this->table;
|
||||
}
|
||||
|
||||
$query = '';
|
||||
$binds = array();
|
||||
|
||||
//Implode the array to get a list with the fields
|
||||
$this->query .= 'INSERT INTO `' . $table . '` (`' . implode('`,`', array_keys($array)) . '`) VALUES (';
|
||||
$query .= 'INSERT INTO ' . $table . ' (' . implode(',', array_keys($array)) . ') VALUES (';
|
||||
|
||||
//Add all the values as ? and add them to the binds
|
||||
foreach($array as $field => $value){
|
||||
$this->query .= '?,';
|
||||
$query .= '?,';
|
||||
|
||||
$this->binds[] = $value;
|
||||
$binds[] = $value;
|
||||
}
|
||||
|
||||
$this->query = rtrim($this->query, ',') . ')';
|
||||
$query = rtrim($query, ',') . ')';
|
||||
|
||||
return $this;
|
||||
return array(
|
||||
'sql' => $query,
|
||||
'binds' => $binds
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace
|
||||
*
|
||||
* @param $array array Key value, $field => $value
|
||||
* @param $table string|null Table name, if it is null, the table set with setTable() will be used
|
||||
* @return $this
|
||||
* @throws Exception
|
||||
*/
|
||||
public function replace($array, $table = ''){
|
||||
|
||||
return $this->__call(__FUNCTION__, func_get_args());
|
||||
}
|
||||
|
||||
private function internalReplace($array, $table = ''){
|
||||
|
||||
if($table === ''){
|
||||
|
||||
if($this->table == '')
|
||||
throw new DatabaseException("No table given");
|
||||
|
||||
$table = $this->table;
|
||||
}
|
||||
|
||||
$query = '';
|
||||
$binds = array();
|
||||
|
||||
//Implode the array to get a list with the fields
|
||||
$query .= 'REPLACE INTO ' . $table . ' (' . implode(',', array_keys($array)) . ') VALUES (';
|
||||
|
||||
//Add all the values as ? and add them to the binds
|
||||
foreach($array as $field => $value){
|
||||
$query .= '?,';
|
||||
|
||||
$binds[] = $value;
|
||||
}
|
||||
|
||||
$query = rtrim($query, ',') . ')';
|
||||
|
||||
return array(
|
||||
'sql' => $query,
|
||||
'binds' => $binds
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add raw SQL to the query string
|
||||
*
|
||||
* @param string $sql The SQL to add
|
||||
* @return string
|
||||
* @param array $binds The optional binds to add
|
||||
* @return $this
|
||||
*/
|
||||
public function sql($sql){
|
||||
public function sql($sql, $binds = array()){
|
||||
|
||||
$this->query .= $sql;
|
||||
return $this->__call(__FUNCTION__, func_get_args());
|
||||
}
|
||||
|
||||
return $this;
|
||||
private function internalSql($sql, $binds = array()){
|
||||
|
||||
return array(
|
||||
'sql' => $sql,
|
||||
'binds' => $binds
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats the given field
|
||||
*
|
||||
* @param string $field The field to format
|
||||
* @return string The formatted field
|
||||
* Builds The SQL query and its binds.
|
||||
* @return array
|
||||
*/
|
||||
private function formatField($field){
|
||||
public function build(){
|
||||
|
||||
if(strpos($field, '.') === false)
|
||||
$field = "`$field`";
|
||||
$sql = array();
|
||||
$binds = array();
|
||||
|
||||
return $field;
|
||||
foreach($this->functions as $i => $function){
|
||||
|
||||
//The current function is a WHERE or HAVING,
|
||||
//and it after a or, and or open, this means we do not have to
|
||||
//add the WHERE/HAVING statement again
|
||||
if((
|
||||
$function['name'] == 'internalWhere' ||
|
||||
$function['name'] == 'internalHaving'
|
||||
)&&
|
||||
(
|
||||
$this->functions[$i - 1]['name'] == 'internalOpen' ||
|
||||
$this->functions[$i - 1]['name'] == 'internalAnd' ||
|
||||
$this->functions[$i - 1]['name'] == 'internalOr'
|
||||
)){
|
||||
|
||||
if(!isset($function['arguments'][2]))
|
||||
$function['arguments'][2] = null;
|
||||
|
||||
$function['arguments'][3] = '';
|
||||
}
|
||||
|
||||
|
||||
$data = call_user_func_array(array($this, $function['name']), $function['arguments']);
|
||||
|
||||
$sql[] = $data['sql'];
|
||||
|
||||
if($data['binds'] != null)
|
||||
$binds = array_merge($binds, $data['binds']);
|
||||
}
|
||||
|
||||
return array(
|
||||
'sql' => implode(' ', $sql),
|
||||
'binds' => $binds
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the query
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getQuery(){
|
||||
|
||||
return $this->query;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the binds that corresponds with the binds
|
||||
* Returns the query as an array
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getSql(){
|
||||
|
||||
return $this->build()['sql'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the built SQL query
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getBinds(){
|
||||
|
||||
return $this->binds;
|
||||
return $this->build()['binds'];
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -746,17 +775,17 @@ class Query extends Module {
|
|||
|
||||
|
||||
if(Config::get('database')->debug)
|
||||
Logger::log("Generated query: ".$this->query, 'QueryBuilder');
|
||||
Logger::log(get_class($this).': '.$this->getSql());
|
||||
|
||||
try{
|
||||
|
||||
$this->sth = Modules::get('core/database')->prepare($this->query);
|
||||
if(count($this->binds) === 0){
|
||||
$this->sth = Modules::get('core/database')->getConnection()->prepare($this->getSql());
|
||||
if(count($this->getBinds()) === 0){
|
||||
|
||||
$this->sth->execute();
|
||||
}else{
|
||||
|
||||
$this->sth->execute($this->binds);
|
||||
$this->sth->execute($this->getBinds());
|
||||
}
|
||||
}catch (\PDOException $e) {
|
||||
|
||||
|
@ -766,30 +795,23 @@ class Query extends Module {
|
|||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the results of the query as an associative array. ->execute() must be called first.
|
||||
* Returns the results of the query in the given type. All the arguments of this function will be passed onto
|
||||
* fetchAll
|
||||
* @param int $type The default type is \PDO::FETCH_ASSOC, all the types that are possible for fetchAll() are valid
|
||||
* @return array
|
||||
* @throws DatabaseException
|
||||
*/
|
||||
public function getArray(){
|
||||
public function getResults($type = \PDO::FETCH_ASSOC){
|
||||
|
||||
if(!isset($this->sth))
|
||||
$this->execute();
|
||||
|
||||
return $this->sth->fetchAll(\PDO::FETCH_ASSOC);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the results of the query as an array containing objects. ->execute() must be called first.
|
||||
* @return object[]
|
||||
* @throws DatabaseException
|
||||
*/
|
||||
public function getObject(){
|
||||
|
||||
if(!isset($this->sth))
|
||||
$this->execute();
|
||||
|
||||
return $this->sth->fetchAll(\PDO::FETCH_OBJ);
|
||||
return call_user_func_array(array(
|
||||
$this->sth,
|
||||
'fetchAll'
|
||||
), func_get_args());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -810,26 +832,30 @@ class Query extends Module {
|
|||
*/
|
||||
public function getLastInsertId(){
|
||||
|
||||
return Modules::get('core/database')->lastInsertId();
|
||||
return Modules::get('core/database')->getConnection()->lastInsertId();
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* PHP does not allow to use "or" and "and" as function name, by using __call we can redirect them to _or and _and
|
||||
* All the called functions are saved in an array, so when build is called, the query is built from all the functions
|
||||
*
|
||||
* @param $name string
|
||||
* @param $arguments array
|
||||
* @return mixed
|
||||
* @return $this
|
||||
* @throws DatabaseException
|
||||
*/
|
||||
public function __call($name, $arguments){
|
||||
|
||||
switch($name){
|
||||
$name = "internal" . ucfirst($name);
|
||||
|
||||
case "or":
|
||||
return call_user_func_array(array($this, "_or"), $arguments);
|
||||
if(!method_exists($this, $name))
|
||||
throw new DatabaseException('Unknown function "' . $name . '"');
|
||||
|
||||
case "and":
|
||||
return call_user_func_array(array($this, "_and"), $arguments);
|
||||
}
|
||||
$this->functions[] = array(
|
||||
'name' => $name,
|
||||
'arguments' => $arguments
|
||||
);
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
<?php
|
||||
use FuzeWorks\Events;
|
||||
|
||||
/**
|
||||
* Class CoreTestAbstract
|
||||
*
|
||||
* Provides the event tests with some basic functionality
|
||||
*/
|
||||
abstract class CoreTestAbstract extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* Remove all listeners before the next test starts
|
||||
*/
|
||||
public function tearDown(){
|
||||
|
||||
Events::$listeners = array();
|
||||
}
|
||||
}
|
|
@ -1,7 +1,19 @@
|
|||
<?php
|
||||
// Load the abstract
|
||||
require_once("class.abstract.coreTestAbstract.php");
|
||||
|
||||
use \FuzeWorks\Config;
|
||||
use \FuzeWorks\Core;
|
||||
|
||||
require_once "abstract.coreTestAbstract.php";
|
||||
require_once( "Core/System/class.core.php");
|
||||
|
||||
ob_start();
|
||||
Core::init();
|
||||
|
||||
// Disable debugger
|
||||
$cfg = Config::get('error');
|
||||
$cfg->debug = false;
|
||||
$cfg->error_reporting = false;
|
||||
$cfg->commit();
|
||||
|
||||
?>
|
|
@ -1,23 +0,0 @@
|
|||
<?php
|
||||
/**
|
||||
* Class CoreTestAbstract
|
||||
*
|
||||
* Provides the event tests with some basic functionality
|
||||
*/
|
||||
abstract class CoreTestAbstract extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* This function provides the framework
|
||||
* @returns \FuzeWorks\Core
|
||||
*/
|
||||
static function createCore()
|
||||
{
|
||||
$core = new FuzeWorks\Core();
|
||||
$core->init();
|
||||
|
||||
//Disable debugging
|
||||
$core->mods->logger->disable();
|
||||
|
||||
return $core;
|
||||
}
|
||||
}
|
|
@ -1,4 +1,5 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Class CoreTest
|
||||
*
|
||||
|
@ -8,13 +9,12 @@ class CoreTest extends CoreTestAbstract
|
|||
{
|
||||
public function testCanLoadStartupFiles(){
|
||||
|
||||
$core = $this->createCore();
|
||||
|
||||
// Assert
|
||||
$this->assertInstanceOf('\FuzeWorks\Config', $core->mods->config);
|
||||
$this->assertInstanceOf('\FuzeWorks\Logger', $core->mods->logger);
|
||||
$this->assertInstanceOf('\FuzeWorks\Events', $core->mods->events);
|
||||
$this->assertInstanceOf('\FuzeWorks\Layout', $core->mods->layout);
|
||||
$this->assertInstanceOf('\FuzeWorks\Models', $core->mods->models);
|
||||
$this->assertTrue(class_exists('\FuzeWorks\Config'));
|
||||
$this->assertTrue(class_exists('\FuzeWorks\Logger'));
|
||||
$this->assertTrue(class_exists('\FuzeWorks\Events'));
|
||||
$this->assertTrue(class_exists('\FuzeWorks\Router'));
|
||||
$this->assertTrue(class_exists('\FuzeWorks\Layout'));
|
||||
$this->assertTrue(class_exists('\FuzeWorks\Models'));
|
||||
}
|
||||
}
|
|
@ -1,23 +0,0 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Class ModelTest
|
||||
*
|
||||
* Core model testing suite, will test model parent class
|
||||
*
|
||||
*/
|
||||
class ModelTest extends CoreTestAbstract
|
||||
{
|
||||
|
||||
/**
|
||||
* Tests wether FuzeWorks is able to load a simple Dummy Model
|
||||
*/
|
||||
public function testModelLoading() {
|
||||
$core = $this->createCore();
|
||||
|
||||
$core->mods->models->loadModel('dummy', 'tests/models/testModelLoading/');
|
||||
$this->assertInstanceOf('\Model\Dummy', $core->mods->models->dummy);
|
||||
}
|
||||
|
||||
// PARENT INTERACTION VIA A DUMMY MODELSERVER IN DUMMY MODULE
|
||||
}
|
|
@ -1,85 +0,0 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Class ModuleTest
|
||||
*
|
||||
* Core module testing suite, will test basic module functionality
|
||||
*
|
||||
*/
|
||||
class ModuleTest extends CoreTestAbstract
|
||||
{
|
||||
|
||||
/**
|
||||
* Tests the loading of a single module located in a folder
|
||||
*/
|
||||
public function testFolderLoading(){
|
||||
|
||||
$core = $this->createCore();
|
||||
|
||||
$core->addMod('tests/modules/testFolderLoading/moduleInfo.php');
|
||||
$mod = $core->loadMod('ci/folderloading');
|
||||
$this->assertInstanceOf('\Module\FolderLoading\Main', $mod);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the enabling and disabling of modules
|
||||
*/
|
||||
public function testModuleEnabling(){
|
||||
|
||||
$core = $this->createCore();
|
||||
|
||||
$core->addMod('tests/modules/testFolderLoading/moduleInfo.php');
|
||||
|
||||
//Enable a module
|
||||
$core->mods->modules->enableModule('ci/folderloading');
|
||||
$cfg = (object) require('tests/modules/testFolderLoading/moduleInfo.php');
|
||||
$this->assertEquals(true, $cfg->enabled);
|
||||
|
||||
//Disable a module
|
||||
$core->mods->modules->disableModule('ci/folderloading');
|
||||
$cfg = (object) require('tests/modules/testFolderLoading/moduleInfo.php');
|
||||
$this->assertEquals(false, $cfg->enabled);
|
||||
|
||||
$core->mods->modules->enableModule('ci/folderloading');
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if a reloaded module is the same Id
|
||||
*/
|
||||
public function testDuplicateInstance() {
|
||||
|
||||
$core = $this->createCore();
|
||||
|
||||
// The 2 instances which should be the same
|
||||
$mod = $core->loadMod('mycorp/example');
|
||||
$mod2 = $core->loadMod('mycorp/example');
|
||||
|
||||
$this->assertEquals(spl_object_hash($mod), spl_object_hash($mod2));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if the retrieved module info is correct
|
||||
*/
|
||||
public function testModuleInfo() {
|
||||
$core = $this->createCore();
|
||||
$mod = $core->loadMod('mycorp/example');
|
||||
|
||||
// The name
|
||||
$this->assertEquals('mycorp/example', $mod->getModuleName());
|
||||
|
||||
// The directory
|
||||
$this->assertEquals('Modules/example/', $mod->getModulePath());
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the loading of a moduleInfo in an unknown directory
|
||||
* @throws ModuleException
|
||||
* @expectedException \FuzeWorks\moduleException
|
||||
*/
|
||||
public function testLoadingUnknownModuleInfoDirectory(){
|
||||
|
||||
$core = $this->createCore();
|
||||
|
||||
$core->addMod("tests/moduleInfo.php");
|
||||
}
|
||||
}
|
|
@ -1,23 +1,30 @@
|
|||
<?php
|
||||
use Module\DatabaseUtils\Query;
|
||||
use \FuzeWorks\Modules;
|
||||
use \Module\DatabaseUtils\Query;
|
||||
|
||||
class QueryTests extends CoreTestAbstract {
|
||||
class QueryTests extends \CoreTestAbstract {
|
||||
|
||||
/**
|
||||
* @var Query
|
||||
*/
|
||||
public $query;
|
||||
|
||||
public $core;
|
||||
|
||||
/**
|
||||
* @before
|
||||
*/
|
||||
public function initQuery(){
|
||||
|
||||
$core = $this->createCore();
|
||||
$core->loadMod('techfuze/databaseutils');
|
||||
$this->query = new Query($core);
|
||||
protected function setUp(){
|
||||
Modules::get('core/databaseutils');
|
||||
$this->query = new Query();
|
||||
}
|
||||
|
||||
public function testConstructor(){
|
||||
|
||||
$query = new Query('table');
|
||||
$this->assertEquals('table', $query->getTable());
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
* Select
|
||||
*/
|
||||
|
@ -25,43 +32,43 @@ class QueryTests extends CoreTestAbstract {
|
|||
public function testSelectSimple()
|
||||
{
|
||||
$this->query->select()->from('table');
|
||||
$this->assertEquals('SELECT * FROM `table`', $this->query->getQuery());
|
||||
$this->assertEquals('SELECT * FROM table', $this->query->getSql());
|
||||
}
|
||||
|
||||
public function testSelectSimpleDefaultTable(){
|
||||
|
||||
$this->query->setTable("table")->select()->from();
|
||||
$this->assertEquals('SELECT * FROM `table`', $this->query->getQuery());
|
||||
$this->assertEquals('SELECT * FROM table', $this->query->getSql());
|
||||
}
|
||||
|
||||
public function testSelectSimpleComboTable()
|
||||
{
|
||||
$this->query->select()->from('table', 'table2');
|
||||
$this->assertEquals('SELECT * FROM `table`, `table2`', $this->query->getQuery());
|
||||
$this->assertEquals('SELECT * FROM table, table2', $this->query->getSql());
|
||||
}
|
||||
|
||||
public function testSelectSimpleAlias()
|
||||
{
|
||||
$this->query->select()->from('table t');
|
||||
$this->assertEquals('SELECT * FROM `table` t', $this->query->getQuery());
|
||||
$this->assertEquals('SELECT * FROM table t', $this->query->getSql());
|
||||
}
|
||||
|
||||
public function testSelectSimpleComboAlias()
|
||||
{
|
||||
$this->query->select()->from('table t', 'table2 t2');
|
||||
$this->assertEquals('SELECT * FROM `table` t, `table2` t2', $this->query->getQuery());
|
||||
$this->assertEquals('SELECT * FROM table t, table2 t2', $this->query->getSql());
|
||||
}
|
||||
|
||||
public function testSelectSimpleOneField(){
|
||||
|
||||
$this->query->select('field1')->from('table');
|
||||
$this->assertEquals('SELECT `field1` FROM `table`', $this->query->getQuery());
|
||||
$this->assertEquals('SELECT field1 FROM table', $this->query->getSql());
|
||||
}
|
||||
|
||||
public function testSelectSimpleTwoFields(){
|
||||
|
||||
$this->query->select('field1', 'field2')->from('table');
|
||||
$this->assertEquals('SELECT `field1`, `field2` FROM `table`', $this->query->getQuery());
|
||||
$this->assertEquals('SELECT field1, field2 FROM table', $this->query->getSql());
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -71,132 +78,146 @@ class QueryTests extends CoreTestAbstract {
|
|||
public function testSelectWhere(){
|
||||
|
||||
$this->query->select()->from('table')->where("field", "value");
|
||||
$this->assertEquals('SELECT * FROM `table` WHERE `field` = ?', $this->query->getQuery());
|
||||
$this->assertEquals('SELECT * FROM table WHERE field = ?', $this->query->getSql());
|
||||
$this->assertEquals(array('value'), $this->query->getBinds());
|
||||
}
|
||||
|
||||
public function testSelectWhereLike(){
|
||||
|
||||
$this->query->select()->from('table')->where("field", "like", '%value%');
|
||||
$this->assertEquals('SELECT * FROM `table` WHERE `field` LIKE ?', $this->query->getQuery());
|
||||
$this->assertEquals('SELECT * FROM table WHERE field LIKE ?', $this->query->getSql());
|
||||
$this->assertEquals(array('%value%'), $this->query->getBinds());
|
||||
}
|
||||
|
||||
public function testSelectWhereBetween(){
|
||||
|
||||
$this->query->select()->from('table')->where("field", "between", array(2, 4));
|
||||
$this->assertEquals('SELECT * FROM `table` WHERE `field` BETWEEN ? AND ?', $this->query->getQuery());
|
||||
$this->assertEquals('SELECT * FROM table WHERE field BETWEEN ? AND ?', $this->query->getSql());
|
||||
$this->assertEquals(array(2, 4), $this->query->getBinds());
|
||||
}
|
||||
|
||||
public function testSelectWhereIn(){
|
||||
|
||||
$this->query->select()->from('table')->where("field", "in", array(2, 3, 4));
|
||||
$this->assertEquals('SELECT * FROM `table` WHERE `field` IN (?,?,?)', $this->query->getQuery());
|
||||
$this->assertEquals('SELECT * FROM table WHERE field IN (?,?,?)', $this->query->getSql());
|
||||
$this->assertEquals(array(2, 3, 4), $this->query->getBinds());
|
||||
}
|
||||
|
||||
/**
|
||||
* @todo Make this work!
|
||||
* @return [type] [description]
|
||||
*/
|
||||
/*public function testSelectWhereInSubQuery(){
|
||||
|
||||
$subQuery = new Query();
|
||||
$subQuery->select()->from('table2')->where('field', 'value');
|
||||
|
||||
$this->query->select()->from('table')->where("field", "in", $subQuery);
|
||||
$this->assertEquals('SELECT * FROM table WHERE field IN (SELECT * FROM table2 WHERE field = ?)', $this->query->getSql());
|
||||
$this->assertEquals(array('value'), $this->query->getBinds());
|
||||
}*/
|
||||
|
||||
public function testSelectWhereNot(){
|
||||
|
||||
$this->query->select()->from('table')->where("field", "<>", "value");
|
||||
$this->assertEquals('SELECT * FROM `table` WHERE `field` <> ?', $this->query->getQuery());
|
||||
$this->assertEquals('SELECT * FROM table WHERE field <> ?', $this->query->getSql());
|
||||
$this->assertEquals(array('value'), $this->query->getBinds());
|
||||
}
|
||||
|
||||
|
||||
public function testSelectWhereGreater(){
|
||||
|
||||
$this->query->select()->from('table')->where("field", ">", "value");
|
||||
$this->assertEquals('SELECT * FROM `table` WHERE `field` > ?', $this->query->getQuery());
|
||||
$this->assertEquals('SELECT * FROM table WHERE field > ?', $this->query->getSql());
|
||||
$this->assertEquals(array('value'), $this->query->getBinds());
|
||||
}
|
||||
|
||||
|
||||
public function testSelectWhereGreaterEqual(){
|
||||
|
||||
$this->query->select()->from('table')->where("field", ">=", "value");
|
||||
$this->assertEquals('SELECT * FROM `table` WHERE `field` >= ?', $this->query->getQuery());
|
||||
$this->assertEquals('SELECT * FROM table WHERE field >= ?', $this->query->getSql());
|
||||
$this->assertEquals(array('value'), $this->query->getBinds());
|
||||
}
|
||||
|
||||
|
||||
public function testSelectWhereSmaller(){
|
||||
|
||||
$this->query->select()->from('table')->where("field", "<", "value");
|
||||
$this->assertEquals('SELECT * FROM `table` WHERE `field` < ?', $this->query->getQuery());
|
||||
$this->assertEquals('SELECT * FROM table WHERE field < ?', $this->query->getSql());
|
||||
$this->assertEquals(array('value'), $this->query->getBinds());
|
||||
}
|
||||
|
||||
|
||||
public function testSelectWhereSmallerEqual(){
|
||||
|
||||
$this->query->select()->from('table')->where("field", "<=", "value");
|
||||
$this->assertEquals('SELECT * FROM `table` WHERE `field` <= ?', $this->query->getQuery());
|
||||
$this->assertEquals('SELECT * FROM table WHERE field <= ?', $this->query->getSql());
|
||||
$this->assertEquals(array('value'), $this->query->getBinds());
|
||||
}
|
||||
|
||||
|
||||
public function testSelectWhereAnd(){
|
||||
|
||||
$this->query->select()->from('table')->where("field1", "value1")->and("field2", "value2");
|
||||
$this->assertEquals('SELECT * FROM `table` WHERE `field1` = ? AND `field2` = ?', $this->query->getQuery());
|
||||
$this->query->select()->from('table')->where("field1", "value1")->and()->where("field2", "value2");
|
||||
$this->assertEquals('SELECT * FROM table WHERE field1 = ? AND field2 = ?', $this->query->getSql());
|
||||
$this->assertEquals(array('value1', 'value2'), $this->query->getBinds());
|
||||
}
|
||||
|
||||
public function testSelectWhereAndOpen(){
|
||||
|
||||
$this->query->select()->from('table')->where("field1", "value1")->and_open("field2", "value2")->or("field3", "value3")->close();
|
||||
$this->assertEquals('SELECT * FROM `table` WHERE `field1` = ? AND (`field2` = ? OR `field3` = ?)', $this->query->getQuery());
|
||||
$this->query->select()->from('table')->where("field1", "value1")->and()->open()->where("field2", "value2")->or()->where("field3", "value3")->close();
|
||||
$this->assertEquals('SELECT * FROM table WHERE field1 = ? AND ( field2 = ? OR field3 = ? )', $this->query->getSql());
|
||||
$this->assertEquals(array('value1', 'value2', 'value3'), $this->query->getBinds());
|
||||
}
|
||||
|
||||
public function testSelectWhereOr(){
|
||||
|
||||
$this->query->select()->from('table')->where("field1", "value1")->or("field2", "value2");
|
||||
$this->assertEquals('SELECT * FROM `table` WHERE `field1` = ? OR `field2` = ?', $this->query->getQuery());
|
||||
$this->query->select()->from('table')->where("field1", "value1")->or()->where("field2", "value2");
|
||||
$this->assertEquals('SELECT * FROM table WHERE field1 = ? OR field2 = ?', $this->query->getSql());
|
||||
$this->assertEquals(array('value1', 'value2'), $this->query->getBinds());
|
||||
}
|
||||
|
||||
public function testSelectWhereOrOpen(){
|
||||
|
||||
$this->query->select()->from('table')->where("field1", "value1")->or_open("field2", "value2")->and("field3", "value3")->close();
|
||||
$this->assertEquals('SELECT * FROM `table` WHERE `field1` = ? OR (`field2` = ? AND `field3` = ?)', $this->query->getQuery());
|
||||
$this->query->select()->from('table')->where("field1", "value1")->or()->open()->where("field2", "value2")->and()->where("field3", "value3")->close();
|
||||
$this->assertEquals('SELECT * FROM table WHERE field1 = ? OR ( field2 = ? AND field3 = ? )', $this->query->getSql());
|
||||
$this->assertEquals(array('value1', 'value2', 'value3'), $this->query->getBinds());
|
||||
}
|
||||
public function testSelectWhereOpen(){
|
||||
|
||||
$this->query->select()->from('table')
|
||||
->where_open("field1", "value1")->and("field2", "value2")->close()
|
||||
->or('field3', 'value3');
|
||||
$this->assertEquals('SELECT * FROM `table` WHERE (`field1` = ? AND `field2` = ?) OR `field3` = ?', $this->query->getQuery());
|
||||
->where()->open()->where("field1", "value1")->and()->where("field2", "value2")->close()
|
||||
->or()->where('field3', 'value3');
|
||||
$this->assertEquals('SELECT * FROM table WHERE ( field1 = ? AND field2 = ? ) OR field3 = ?', $this->query->getSql());
|
||||
$this->assertEquals(array('value1', 'value2', 'value3'), $this->query->getBinds());
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Order by
|
||||
*/
|
||||
|
||||
|
||||
public function testSelectOrderASC(){
|
||||
|
||||
$this->query->select()->from('table')->order('field');
|
||||
$this->assertEquals('SELECT * FROM `table` ORDER BY `field` ASC', $this->query->getQuery());
|
||||
$this->assertEquals('SELECT * FROM table ORDER BY field ASC', $this->query->getSql());
|
||||
}
|
||||
|
||||
public function testSelectOrderDESC(){
|
||||
|
||||
$this->query->select()->from('table')->order('-field');
|
||||
$this->assertEquals('SELECT * FROM `table` ORDER BY `field` DESC', $this->query->getQuery());
|
||||
$this->assertEquals('SELECT * FROM table ORDER BY field DESC', $this->query->getSql());
|
||||
}
|
||||
|
||||
public function testSelectOrderMultiple(){
|
||||
|
||||
$this->query->select()->from('table')->order('field1', '-field2');
|
||||
$this->assertEquals('SELECT * FROM `table` ORDER BY `field1` ASC, `field2` DESC', $this->query->getQuery());
|
||||
$this->assertEquals('SELECT * FROM table ORDER BY field1 ASC, field2 DESC', $this->query->getSql());
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Limit
|
||||
*/
|
||||
|
||||
|
||||
public function testSelectLimit()
|
||||
{
|
||||
$this->query->select()->from('table')->limit(5, 10);
|
||||
$this->assertEquals('SELECT * FROM `table` LIMIT 10, 5', $this->query->getQuery());
|
||||
$this->assertEquals('SELECT * FROM table LIMIT 10, 5', $this->query->getSql());
|
||||
|
||||
}
|
||||
|
||||
|
@ -207,99 +228,99 @@ class QueryTests extends CoreTestAbstract {
|
|||
public function testSelectHaving(){
|
||||
|
||||
$this->query->select()->from('table')->having("field", "value");
|
||||
$this->assertEquals('SELECT * FROM `table` HAVING `field` = ?', $this->query->getQuery());
|
||||
$this->assertEquals('SELECT * FROM table HAVING field = ?', $this->query->getSql());
|
||||
$this->assertEquals(array('value'), $this->query->getBinds());
|
||||
}
|
||||
|
||||
public function testSelectHavingLike(){
|
||||
|
||||
$this->query->select()->from('table')->having("field", "like", '%value%');
|
||||
$this->assertEquals('SELECT * FROM `table` HAVING `field` LIKE ?', $this->query->getQuery());
|
||||
$this->assertEquals('SELECT * FROM table HAVING field LIKE ?', $this->query->getSql());
|
||||
$this->assertEquals(array('%value%'), $this->query->getBinds());
|
||||
}
|
||||
|
||||
public function testSelectHavingBetween(){
|
||||
|
||||
$this->query->select()->from('table')->having("field", "between", array(2, 4));
|
||||
$this->assertEquals('SELECT * FROM `table` HAVING `field` BETWEEN ? AND ?', $this->query->getQuery());
|
||||
$this->assertEquals('SELECT * FROM table HAVING field BETWEEN ? AND ?', $this->query->getSql());
|
||||
$this->assertEquals(array(2, 4), $this->query->getBinds());
|
||||
}
|
||||
|
||||
public function testSelectHavingIn(){
|
||||
|
||||
$this->query->select()->from('table')->having("field", "in", array(2, 3, 4));
|
||||
$this->assertEquals('SELECT * FROM `table` HAVING `field` IN (?,?,?)', $this->query->getQuery());
|
||||
$this->assertEquals('SELECT * FROM table HAVING field IN (?,?,?)', $this->query->getSql());
|
||||
$this->assertEquals(array(2, 3, 4), $this->query->getBinds());
|
||||
}
|
||||
|
||||
public function testSelectHavingNot(){
|
||||
|
||||
$this->query->select()->from('table')->having("field", "<>", "value");
|
||||
$this->assertEquals('SELECT * FROM `table` HAVING `field` <> ?', $this->query->getQuery());
|
||||
$this->assertEquals('SELECT * FROM table HAVING field <> ?', $this->query->getSql());
|
||||
$this->assertEquals(array('value'), $this->query->getBinds());
|
||||
}
|
||||
|
||||
public function testSelectHavingGreater(){
|
||||
|
||||
$this->query->select()->from('table')->having("field", ">", "value");
|
||||
$this->assertEquals('SELECT * FROM `table` HAVING `field` > ?', $this->query->getQuery());
|
||||
$this->assertEquals('SELECT * FROM table HAVING field > ?', $this->query->getSql());
|
||||
$this->assertEquals(array('value'), $this->query->getBinds());
|
||||
}
|
||||
|
||||
public function testSelectHavingGreaterEqual(){
|
||||
|
||||
$this->query->select()->from('table')->having("field", ">=", "value");
|
||||
$this->assertEquals('SELECT * FROM `table` HAVING `field` >= ?', $this->query->getQuery());
|
||||
$this->assertEquals('SELECT * FROM table HAVING field >= ?', $this->query->getSql());
|
||||
$this->assertEquals(array('value'), $this->query->getBinds());
|
||||
}
|
||||
|
||||
public function testSelectHavingSmaller(){
|
||||
|
||||
$this->query->select()->from('table')->having("field", "<", "value");
|
||||
$this->assertEquals('SELECT * FROM `table` HAVING `field` < ?', $this->query->getQuery());
|
||||
$this->assertEquals('SELECT * FROM table HAVING field < ?', $this->query->getSql());
|
||||
$this->assertEquals(array('value'), $this->query->getBinds());
|
||||
}
|
||||
|
||||
public function testSelectHavingSmallerEqual(){
|
||||
|
||||
$this->query->select()->from('table')->having("field", "<=", "value");
|
||||
$this->assertEquals('SELECT * FROM `table` HAVING `field` <= ?', $this->query->getQuery());
|
||||
$this->assertEquals('SELECT * FROM table HAVING field <= ?', $this->query->getSql());
|
||||
$this->assertEquals(array('value'), $this->query->getBinds());
|
||||
}
|
||||
|
||||
public function testSelectHavingAnd(){
|
||||
|
||||
$this->query->select()->from('table')->having("field1", "value1")->and("field2", "value2");
|
||||
$this->assertEquals('SELECT * FROM `table` HAVING `field1` = ? AND `field2` = ?', $this->query->getQuery());
|
||||
$this->query->select()->from('table')->having("field1", "value1")->and()->where("field2", "value2");
|
||||
$this->assertEquals('SELECT * FROM table HAVING field1 = ? AND field2 = ?', $this->query->getSql());
|
||||
$this->assertEquals(array('value1', 'value2'), $this->query->getBinds());
|
||||
}
|
||||
|
||||
public function testSelectHavingAndOpen(){
|
||||
|
||||
$this->query->select()->from('table')->having("field1", "value1")->and_open("field2", "value2")->or("field3", "value3")->close();
|
||||
$this->assertEquals('SELECT * FROM `table` HAVING `field1` = ? AND (`field2` = ? OR `field3` = ?)', $this->query->getQuery());
|
||||
$this->query->select()->from('table')->having("field1", "value1")->and()->open()->where("field2", "value2")->or()->where("field3", "value3")->close();
|
||||
$this->assertEquals('SELECT * FROM table HAVING field1 = ? AND ( field2 = ? OR field3 = ? )', $this->query->getSql());
|
||||
$this->assertEquals(array('value1', 'value2', 'value3'), $this->query->getBinds());
|
||||
}
|
||||
|
||||
public function testSelectHavingOr(){
|
||||
|
||||
$this->query->select()->from('table')->having("field1", "value1")->or("field2", "value2");
|
||||
$this->assertEquals('SELECT * FROM `table` HAVING `field1` = ? OR `field2` = ?', $this->query->getQuery());
|
||||
$this->query->select()->from('table')->having("field1", "value1")->or()->where("field2", "value2");
|
||||
$this->assertEquals('SELECT * FROM table HAVING field1 = ? OR field2 = ?', $this->query->getSql());
|
||||
$this->assertEquals(array('value1', 'value2'), $this->query->getBinds());
|
||||
}
|
||||
|
||||
public function testSelectHavingOrOpen(){
|
||||
|
||||
$this->query->select()->from('table')->having("field1", "value1")->or_open("field2", "value2")->and("field3", "value3")->close();
|
||||
$this->assertEquals('SELECT * FROM `table` HAVING `field1` = ? OR (`field2` = ? AND `field3` = ?)', $this->query->getQuery());
|
||||
$this->query->select()->from('table')->having("field1", "value1")->or()->open()->where("field2", "value2")->and()->where("field3", "value3")->close();
|
||||
$this->assertEquals('SELECT * FROM table HAVING field1 = ? OR ( field2 = ? AND field3 = ? )', $this->query->getSql());
|
||||
$this->assertEquals(array('value1', 'value2', 'value3'), $this->query->getBinds());
|
||||
}
|
||||
public function testSelectHavingOpen(){
|
||||
|
||||
$this->query->select()->from('table')
|
||||
->having_open("field1", "value1")->and("field2", "value2")->close()
|
||||
->or('field3', 'value3');
|
||||
$this->assertEquals('SELECT * FROM `table` HAVING (`field1` = ? AND `field2` = ?) OR `field3` = ?', $this->query->getQuery());
|
||||
->having()->open()->where("field1", "value1")->and()->where("field2", "value2")->close()
|
||||
->or()->where('field3', 'value3');
|
||||
$this->assertEquals('SELECT * FROM table HAVING ( field1 = ? AND field2 = ? ) OR field3 = ?', $this->query->getSql());
|
||||
$this->assertEquals(array('value1', 'value2', 'value3'), $this->query->getBinds());
|
||||
}
|
||||
|
||||
|
@ -310,14 +331,14 @@ class QueryTests extends CoreTestAbstract {
|
|||
public function testUpdateSimple(){
|
||||
|
||||
$this->query->update('table')->set(array('field' => 'value'));
|
||||
$this->assertEquals('UPDATE `table` SET `field`=?', $this->query->getQuery());
|
||||
$this->assertEquals('UPDATE table SET field=?', $this->query->getSql());
|
||||
$this->assertEquals(array('value'), $this->query->getBinds());
|
||||
}
|
||||
|
||||
public function testUpdateMultiple(){
|
||||
|
||||
$this->query->update('table')->set(array('field1' => 'value1', "field2" => 'value2'));
|
||||
$this->assertEquals('UPDATE `table` SET `field1`=?, `field2`=?', $this->query->getQuery());
|
||||
$this->assertEquals('UPDATE table SET field1=?, field2=?', $this->query->getSql());
|
||||
$this->assertEquals(array('value1', 'value2'), $this->query->getBinds());
|
||||
}
|
||||
|
||||
|
@ -328,7 +349,7 @@ class QueryTests extends CoreTestAbstract {
|
|||
public function testDeleteSimple(){
|
||||
|
||||
$this->query->delete()->from('table');
|
||||
$this->assertEquals('DELETE FROM `table`', $this->query->getQuery());
|
||||
$this->assertEquals('DELETE FROM table', $this->query->getSql());
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -338,7 +359,7 @@ class QueryTests extends CoreTestAbstract {
|
|||
public function testInsertSimple(){
|
||||
|
||||
$this->query->insert(array('field' => 'value'), 'table');
|
||||
$this->assertEquals('INSERT INTO `table` (`field`) VALUES (?)', $this->query->getQuery());
|
||||
$this->assertEquals('INSERT INTO table (field) VALUES (?)', $this->query->getSql());
|
||||
$this->assertEquals(array('value'), $this->query->getBinds());
|
||||
|
||||
}
|
||||
|
@ -346,7 +367,27 @@ class QueryTests extends CoreTestAbstract {
|
|||
public function testInsertMultiple(){
|
||||
|
||||
$this->query->insert(array('field1' => 'value1', 'field2' => 'value2'), 'table');
|
||||
$this->assertEquals('INSERT INTO `table` (`field1`,`field2`) VALUES (?,?)', $this->query->getQuery());
|
||||
$this->assertEquals('INSERT INTO table (field1,field2) VALUES (?,?)', $this->query->getSql());
|
||||
$this->assertEquals(array('value1', 'value2'), $this->query->getBinds());
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
* Replace
|
||||
*/
|
||||
|
||||
public function testReplaceSimple(){
|
||||
|
||||
$this->query->replace(array('field' => 'value'), 'table');
|
||||
$this->assertEquals('REPLACE INTO table (field) VALUES (?)', $this->query->getSql());
|
||||
$this->assertEquals(array('value'), $this->query->getBinds());
|
||||
|
||||
}
|
||||
|
||||
public function testReplaceMultiple(){
|
||||
|
||||
$this->query->replace(array('field1' => 'value1', 'field2' => 'value2'), 'table');
|
||||
$this->assertEquals('REPLACE INTO table (field1,field2) VALUES (?,?)', $this->query->getSql());
|
||||
$this->assertEquals(array('value1', 'value2'), $this->query->getBinds());
|
||||
|
||||
}
|
||||
|
@ -358,28 +399,28 @@ class QueryTests extends CoreTestAbstract {
|
|||
public function testJoin(){
|
||||
|
||||
$this->query->select()->from('table')->join('other')->on("field", "value")->where("field", "value2");
|
||||
$this->assertEquals('SELECT * FROM `table` JOIN `other` ON `field` = ? WHERE `field` = ?', $this->query->getQuery());
|
||||
$this->assertEquals('SELECT * FROM table JOIN other ON field = ? WHERE field = ?', $this->query->getSql());
|
||||
$this->assertEquals(array('value', 'value2'), $this->query->getBinds());
|
||||
}
|
||||
|
||||
public function testJoinLeft(){
|
||||
|
||||
$this->query->select()->from('table')->left_join('other')->on("field", "value")->where("field", "value2");
|
||||
$this->assertEquals('SELECT * FROM `table` LEFT JOIN `other` ON `field` = ? WHERE `field` = ?', $this->query->getQuery());
|
||||
$this->assertEquals('SELECT * FROM table LEFT JOIN other ON field = ? WHERE field = ?', $this->query->getSql());
|
||||
$this->assertEquals(array('value', 'value2'), $this->query->getBinds());
|
||||
}
|
||||
|
||||
public function testJoinRight(){
|
||||
|
||||
$this->query->select()->from('table')->right_join('other')->on("field", "value")->where("field", "value2");
|
||||
$this->assertEquals('SELECT * FROM `table` RIGHT JOIN `other` ON `field` = ? WHERE `field` = ?', $this->query->getQuery());
|
||||
$this->assertEquals('SELECT * FROM table RIGHT JOIN other ON field = ? WHERE field = ?', $this->query->getSql());
|
||||
$this->assertEquals(array('value', 'value2'), $this->query->getBinds());
|
||||
}
|
||||
|
||||
public function testJoinFull(){
|
||||
|
||||
$this->query->select()->from('table')->full_join('other')->on("field", "value")->where("field", "value2");
|
||||
$this->assertEquals('SELECT * FROM `table` FULL JOIN `other` ON `field` = ? WHERE `field` = ?', $this->query->getQuery());
|
||||
$this->assertEquals('SELECT * FROM table FULL JOIN other ON field = ? WHERE field = ?', $this->query->getSql());
|
||||
$this->assertEquals(array('value', 'value2'), $this->query->getBinds());
|
||||
}
|
||||
|
||||
|
@ -390,7 +431,7 @@ class QueryTests extends CoreTestAbstract {
|
|||
->right_join('other_b')->on("field", "value2")
|
||||
->full_join('other_c')->on("field", "value3")
|
||||
->where("field", "value4");
|
||||
$this->assertEquals('SELECT * FROM `table` LEFT JOIN `other_a` ON `field` = ? RIGHT JOIN `other_b` ON `field` = ? FULL JOIN `other_c` ON `field` = ? WHERE `field` = ?', $this->query->getQuery());
|
||||
$this->assertEquals('SELECT * FROM table LEFT JOIN other_a ON field = ? RIGHT JOIN other_b ON field = ? FULL JOIN other_c ON field = ? WHERE field = ?', $this->query->getSql());
|
||||
$this->assertEquals(array('value', 'value2', 'value3', 'value4'), $this->query->getBinds());
|
||||
}
|
||||
|
||||
|
@ -400,14 +441,14 @@ class QueryTests extends CoreTestAbstract {
|
|||
public function testJoinInline(){
|
||||
|
||||
$this->query->select()->from('table t', 'other o')->where("o.field", "value");
|
||||
$this->assertEquals('SELECT * FROM `table` t, `other` o WHERE o.field = ?', $this->query->getQuery());
|
||||
$this->assertEquals('SELECT * FROM table t, other o WHERE o.field = ?', $this->query->getSql());
|
||||
$this->assertEquals(array('value'), $this->query->getBinds());
|
||||
}
|
||||
|
||||
public function testJoinInlineAdvanced(){
|
||||
|
||||
$this->query->select()->from('table t', 'other o')->where("o.field = t.field");
|
||||
$this->assertEquals('SELECT * FROM `table` t, `other` o WHERE o.field = t.field', $this->query->getQuery());
|
||||
$this->assertEquals('SELECT * FROM table t, other o WHERE o.field = t.field', $this->query->getSql());
|
||||
$this->assertEmpty($this->query->getBinds());
|
||||
}
|
||||
|
||||
|
@ -417,8 +458,18 @@ class QueryTests extends CoreTestAbstract {
|
|||
->from('table t', 'other o')
|
||||
->left_join('third th')->on('th.field = o.field')
|
||||
->where("o.field = t.field")
|
||||
->and('t.thing', '>', 25);
|
||||
$this->assertEquals('SELECT * FROM `table` t, `other` o LEFT JOIN `third` th ON th.field = o.field WHERE o.field = t.field AND t.thing > ?', $this->query->getQuery());
|
||||
->and()->where('t.thing', '>', 25);
|
||||
$this->assertEquals('SELECT * FROM table t, other o LEFT JOIN third th ON th.field = o.field WHERE o.field = t.field AND t.thing > ?', $this->query->getSql());
|
||||
$this->assertEquals(array(25), $this->query->getBinds());
|
||||
}
|
||||
|
||||
/**
|
||||
* Group by
|
||||
*/
|
||||
|
||||
public function testSelectGroupBy(){
|
||||
|
||||
$this->query->groupBy('field1', 'field2');
|
||||
$this->assertEquals('GROUP BY field1, field2', $this->query->getSql());
|
||||
}
|
||||
}
|
|
@ -1,4 +1,7 @@
|
|||
<?php
|
||||
use \FuzeWorks\Core;
|
||||
use \FuzeWorks\Router;
|
||||
|
||||
/**
|
||||
* Class RouterTest
|
||||
*
|
||||
|
@ -8,26 +11,24 @@ class RouterTest extends CoreTestAbstract
|
|||
{
|
||||
public function testParsePath(){
|
||||
|
||||
$core = $this->createCore();
|
||||
|
||||
// Act and assert
|
||||
$core->mods->router->setPath('a/b/c/d/');
|
||||
$this->assertEquals('a/b/c/d', $core->mods->router->getPath());
|
||||
Router::setPath('a/b/c/d/');
|
||||
$this->assertEquals('a/b/c/d', Router::getPath());
|
||||
|
||||
$core->mods->router->setPath('//a//b//c');
|
||||
$this->assertEquals('a/b/c', $core->mods->router->getPath());
|
||||
Router::setPath('//a//b//c');
|
||||
$this->assertEquals('a/b/c', Router::getPath());
|
||||
|
||||
$core->mods->router->setPath('/');
|
||||
$this->assertEquals('', $core->mods->router->getPath());
|
||||
Router::setPath('/');
|
||||
$this->assertEquals('', Router::getPath());
|
||||
|
||||
$core->mods->router->setPath('');
|
||||
$this->assertEquals('', $core->mods->router->getPath());
|
||||
Router::setPath('');
|
||||
$this->assertEquals('', Router::getPath());
|
||||
|
||||
$core->mods->router->setPath(false);
|
||||
$this->assertEquals('', $core->mods->router->getPath());
|
||||
Router::setPath(false);
|
||||
$this->assertEquals('', Router::getPath());
|
||||
|
||||
$core->mods->router->setPath(null);
|
||||
$this->assertEquals('', $core->mods->router->getPath());
|
||||
Router::setPath(null);
|
||||
$this->assertEquals('', Router::getPath());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -35,27 +36,25 @@ class RouterTest extends CoreTestAbstract
|
|||
*/
|
||||
public function testDoRoute(){
|
||||
|
||||
$core = $this->createCore();
|
||||
|
||||
// Act
|
||||
$core->mods->router->setPath('a/b/c/d/');
|
||||
$core->mods->router->route(false);
|
||||
Router::setPath('a/b/c/d/');
|
||||
Router::route(false);
|
||||
|
||||
// Assert
|
||||
// Whole route
|
||||
$this->assertEquals(array('a','b',array('c','d')), array($core->mods->router->getController(), $core->mods->router->getFunction(), $core->mods->router->getParameters()));
|
||||
$this->assertEquals('a', $core->mods->router->getController());
|
||||
$this->assertEquals('d', $core->mods->router->getParameter(-1));
|
||||
$this->assertEquals(null, $core->mods->router->getParameter(5));
|
||||
$this->assertEquals(array('a','b',array('c','d')), array(Router::getController(), Router::getFunction(), Router::getParameters()));
|
||||
$this->assertEquals('a', Router::getController());
|
||||
$this->assertEquals('d', Router::getParameter(-1));
|
||||
$this->assertEquals(null, Router::getParameter(5));
|
||||
|
||||
// Parameters
|
||||
$this->assertEquals(array('c','d'), $core->mods->router->getParameters());
|
||||
$this->assertEquals('c', $core->mods->router->getParameter(0));
|
||||
$this->assertEquals('d', $core->mods->router->getParameter(-1));
|
||||
$this->assertEquals(array('c','d'), Router::getParameters());
|
||||
$this->assertEquals('c', Router::getParameter(0));
|
||||
$this->assertEquals('d', Router::getParameter(-1));
|
||||
|
||||
// Function and controller
|
||||
$this->assertEquals('a', $core->mods->router->getController());
|
||||
$this->assertEquals('b', $core->mods->router->getFunction());
|
||||
$this->assertEquals('a', Router::getController());
|
||||
$this->assertEquals('b', Router::getFunction());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -63,64 +62,53 @@ class RouterTest extends CoreTestAbstract
|
|||
*/
|
||||
public function testOddRoutes(){
|
||||
|
||||
$core = $this->createCore();
|
||||
|
||||
// Empty path
|
||||
$core->mods->router->setPath(null);
|
||||
$core->mods->router->route(false);
|
||||
$this->assertEquals(null, $core->mods->router->getController());
|
||||
Router::setPath(null);
|
||||
Router::route(false);
|
||||
$this->assertEquals(null, Router::getController());
|
||||
|
||||
// Double slashes
|
||||
$core->mods->router->setPath('a///b');
|
||||
$core->mods->router->route(false);
|
||||
$this->assertEquals(array('a','b'), array($core->mods->router->getController(), $core->mods->router->getFunction()));
|
||||
Router::setPath('a///b');
|
||||
Router::route(false);
|
||||
$this->assertEquals(array('a','b'), array(Router::getController(), Router::getFunction()));
|
||||
|
||||
// Escaped path path
|
||||
$core->mods->router->setPath('/a\/b\/c/');
|
||||
$core->mods->router->route(false);
|
||||
$this->assertEquals(array('a\\','b\\','c'), array($core->mods->router->getController(), $core->mods->router->getFunction(), $core->mods->router->getParameter(0)));
|
||||
$this->assertNotEquals('a', $core->mods->router->getController());
|
||||
Router::setPath('/a\/b\/c/');
|
||||
Router::route(false);
|
||||
$this->assertEquals(array('a\\','b\\','c'), array(Router::getController(), Router::getFunction(), Router::getParameter(0)));
|
||||
$this->assertNotEquals('a', Router::getController());
|
||||
}
|
||||
|
||||
public function testCustomRoute(){
|
||||
|
||||
$core = $this->createCore();
|
||||
|
||||
$core->mods->router->addRoute('/test1/test2/', 'callable');
|
||||
$this->assertArraySubset(array('/test1/test2/' => 'callable'), $core->mods->router->getRoutes());
|
||||
|
||||
$core->mods->router->setPath('test1/test2');
|
||||
$core->mods->router->route(false);
|
||||
$this->assertEquals(array('test1', 'test2'), array($core->mods->router->getController(), $core->mods->router->getFunction()));
|
||||
|
||||
Router::addRoute('/test1\/test2/', 'callable');
|
||||
$this->assertArraySubset(array('/test1\/test2/' => 'callable'), Router::getRoutes());
|
||||
}
|
||||
|
||||
public function testCustomRouteWithParameters(){
|
||||
|
||||
$core = $this->createCore();
|
||||
Router::addRoute('/^b\/(?P<controller>[^\/]+)\/?(?P<function>.+?)$/', 'callable');
|
||||
Router::addRoute('/e\/(?P<function>[^\/]+)/', 'callable');
|
||||
Router::addRoute('/b\/b$/', 'callable');
|
||||
|
||||
$core->mods->router->addRoute('/^b\/(?P<controller>[^\/]+)\/?(?P<function>.+?)$/', 'callable');
|
||||
$core->mods->router->addRoute('/e\/(?P<function>[^\/]+)/', 'callable');
|
||||
$core->mods->router->addRoute('/b\/b$/', 'callable');
|
||||
Router::setPath('b/controller_a/function_a');
|
||||
Router::route(false);
|
||||
$this->assertEquals('controller_a', Router::getController());
|
||||
$this->assertEquals('function_a', Router::getFunction());
|
||||
|
||||
$core->mods->router->setPath('b/controller_a/function_a');
|
||||
$core->mods->router->route(false);
|
||||
$this->assertEquals('controller_a', $core->mods->router->getController());
|
||||
$this->assertEquals('function_a', $core->mods->router->getFunction());
|
||||
Router::setPath('e/function_b/c');
|
||||
Router::route(false);
|
||||
$this->assertEquals(null, Router::getController());
|
||||
$this->assertEquals('function_b', Router::getFunction());
|
||||
|
||||
$core->mods->router->setPath('e/function_b/c');
|
||||
$core->mods->router->route(false);
|
||||
$this->assertEquals(null, $core->mods->router->getController());
|
||||
$this->assertEquals('function_b', $core->mods->router->getFunction());
|
||||
Router::setPath('b/b');
|
||||
Router::route(false);
|
||||
$this->assertEquals(null, Router::getController());
|
||||
$this->assertEquals(null, Router::getFunction());
|
||||
|
||||
$core->mods->router->setPath('b/b');
|
||||
$core->mods->router->route(false);
|
||||
$this->assertEquals(null, $core->mods->router->getController());
|
||||
$this->assertEquals(null, $core->mods->router->getFunction());
|
||||
|
||||
$core->mods->router->setPath('a/b');
|
||||
$core->mods->router->route(false);
|
||||
$this->assertEquals('a', $core->mods->router->getController());
|
||||
$this->assertEquals('b', $core->mods->router->getFunction());
|
||||
Router::setPath('a/b');
|
||||
Router::route(false);
|
||||
$this->assertEquals('a', Router::getController());
|
||||
$this->assertEquals('b', Router::getFunction());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
<?php
|
||||
use \FuzeWorks\Core;
|
||||
use \FuzeWorks\Events;
|
||||
use \FuzeWorks\EventPriority;
|
||||
|
||||
/**
|
||||
* Class CoreStartEventTest
|
||||
*/
|
||||
class CoreStartEventTest extends CoreTestAbstract
|
||||
{
|
||||
/**
|
||||
* Check if the event is fired when it should be
|
||||
*/
|
||||
public function testCoreStartEvent(){
|
||||
|
||||
$mock = $this->getMock('MockEvent', array('mockMethod'));
|
||||
$mock->expects($this->once())->method('mockMethod');
|
||||
|
||||
Events::addListener(array($mock, 'mockMethod'), 'coreStartEvent', EventPriority::NORMAL);
|
||||
Core::init();
|
||||
}
|
||||
}
|
|
@ -1,82 +0,0 @@
|
|||
<?php
|
||||
/**
|
||||
* Class RouterLoadCallableEventTest
|
||||
*/
|
||||
class RouterLoadCallableEventTest extends CoreTestAbstract{
|
||||
|
||||
/**
|
||||
* Check if the event is fired when it should be
|
||||
*/
|
||||
public function test_basic(){
|
||||
|
||||
$core = $this->createCore();
|
||||
|
||||
$mock = $this->getMock('MockEvent', array('mockMethod'));
|
||||
$mock->expects($this->once())->method('mockMethod')->with(
|
||||
$this->isInstanceOf('\routerLoadCallableEvent')
|
||||
);
|
||||
|
||||
$core->mods->events->addListener(array($mock, 'mockMethod'), 'routerLoadCallableEvent', \FuzeWorks\EventPriority::NORMAL);
|
||||
//Prevent ouputting HTML
|
||||
ob_start();
|
||||
$core->mods->router->route();
|
||||
ob_end_clean();
|
||||
}
|
||||
|
||||
/**
|
||||
* Intercept and change
|
||||
* @todo Make this test correct
|
||||
*/
|
||||
/*public function test_change(){
|
||||
|
||||
$core = $this->createCore();
|
||||
|
||||
$core->mods->events->addListener(array($this, 'listener_change'), 'routerLoadCallableEvent', \FuzeWorks\EventPriority::NORMAL);
|
||||
$core->mods->router->setPath('x/y/z');
|
||||
ob_start();
|
||||
$core->mods->router->route(true);
|
||||
ob_end_clean();
|
||||
|
||||
$this->assertNotNull($core->mods->router->getCallable());
|
||||
$this->assertInstanceOf('\FuzeWorks\Router', $core->mods->router->getCallable()[0]);
|
||||
}*/
|
||||
|
||||
// Change title from new to other
|
||||
public function listener_change(\routerLoadCallableEvent $event){
|
||||
|
||||
// This controller should not exist
|
||||
$this->assertEquals('x', $event->controller);
|
||||
$this->assertEquals('y', $event->function);
|
||||
|
||||
// It should exist now
|
||||
$event->controller = 'home';
|
||||
$event->function = 'index';
|
||||
}
|
||||
|
||||
/**
|
||||
* Cancel events
|
||||
*/
|
||||
public function test_cancel(){
|
||||
|
||||
// When the callable may execute, the callable will change to the controller
|
||||
// (because '' will trigger the default callable')
|
||||
$core = $this->createCore();
|
||||
$core->mods->router->setPath('');
|
||||
|
||||
$core->mods->events->addListener(array($this, 'listener_cancel'), 'routerLoadCallableEvent', \FuzeWorks\EventPriority::NORMAL);
|
||||
$core->mods->router->route();
|
||||
$this->assertTrue(is_callable($core->mods->router->getCallable()));
|
||||
|
||||
// When disabled, the default controller will be loaded and the callable will be overwritten
|
||||
$core = $this->createCore();
|
||||
$core->mods->router->setPath('');
|
||||
$core->mods->router->route();
|
||||
$this->assertFalse(is_callable($core->mods->router->getCallable()));
|
||||
}
|
||||
|
||||
// Cancel all calls
|
||||
public function listener_cancel(\routerLoadCallableEvent $event){
|
||||
|
||||
$event->setCancelled(true);
|
||||
}
|
||||
}
|
|
@ -1,45 +0,0 @@
|
|||
<?php
|
||||
/**
|
||||
* Class RouterRouteEventTest
|
||||
*/
|
||||
class RouterRouteEventTest extends CoreTestAbstract{
|
||||
|
||||
/**
|
||||
* Check if the event is fired when it should be
|
||||
*/
|
||||
public function test_basic(){
|
||||
|
||||
$core = $this->createCore();
|
||||
|
||||
$mock = $this->getMock('MockEvent', array('mockMethod'));
|
||||
$mock->expects($this->once())->method('mockMethod')->with(
|
||||
$this->isInstanceOf('\routerRouteEvent')
|
||||
);
|
||||
|
||||
$core->mods->events->addListener(array($mock, 'mockMethod'), 'routerRouteEvent', \FuzeWorks\EventPriority::NORMAL);
|
||||
$core->mods->router->setPath('a/b/c');
|
||||
$core->mods->router->route(false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Cancel events
|
||||
*/
|
||||
public function test_cancel(){
|
||||
|
||||
$core = $this->createCore();
|
||||
$core->mods->router->setPath('a/b/c');
|
||||
|
||||
$core->mods->events->addListener(array($this, 'listener_cancel'), 'routerRouteEvent', \FuzeWorks\EventPriority::NORMAL);
|
||||
$core->mods->router->route(false);
|
||||
|
||||
$this->assertNotEquals('a', $core->mods->router->getController());
|
||||
$this->assertNotEquals('b', $core->mods->router->getFunction());
|
||||
$this->assertNotEquals(array('c'), $core->mods->router->getParameters());
|
||||
}
|
||||
|
||||
// Cancel all calls
|
||||
public function listener_cancel(\System\Events\routerRouteEvent $event){
|
||||
|
||||
$event->setCancelled(true);
|
||||
}
|
||||
}
|
|
@ -1,83 +0,0 @@
|
|||
<?php
|
||||
/**
|
||||
* Class RouterSetPathEventTest
|
||||
*/
|
||||
class RouterSetPathEventTest extends CoreTestAbstract{
|
||||
|
||||
/**
|
||||
* Check if the event is fired when it should be
|
||||
*/
|
||||
public function testRouterSetPathEvent(){
|
||||
|
||||
$core = $this->createCore();
|
||||
|
||||
$mock = $this->getMock('MockEvent', array('mockMethod'));
|
||||
$mock->expects($this->once())->method('mockMethod')->with(
|
||||
$this->isInstanceOf('\routerSetPathEvent')
|
||||
);
|
||||
|
||||
$core->mods->events->addListener(array($mock, 'mockMethod'), 'routerSetPathEvent', \FuzeWorks\EventPriority::NORMAL);
|
||||
$core->mods->router->setPath('a/b/c');
|
||||
}
|
||||
|
||||
/**
|
||||
* Intercept and change
|
||||
*/
|
||||
public function testRouterSetPathEvent_change(){
|
||||
|
||||
$core = $this->createCore();
|
||||
|
||||
$core->mods->events->addListener(array($this, 'listener_change'), 'routerSetPathEvent', \FuzeWorks\EventPriority::NORMAL);
|
||||
$core->mods->router->setPath('a/b/c');
|
||||
|
||||
$this->assertEquals('x/y/z', $core->mods->router->getPath());
|
||||
}
|
||||
|
||||
// Change title from new to other
|
||||
public function listener_change(\routerSetPathEvent $event){
|
||||
|
||||
$this->assertEquals('a/b/c', $event->path);
|
||||
$event->path = 'x/y/z';
|
||||
}
|
||||
|
||||
/**
|
||||
* Cancel events
|
||||
*/
|
||||
public function testLayoutFunctionCallEvent_cancel(){
|
||||
|
||||
$core = $this->createCore();
|
||||
$core->mods->router->setPath('a/b/c');
|
||||
|
||||
$core->mods->events->addListener(array($this, 'listener_cancel'), 'routerSetPathEvent', \FuzeWorks\EventPriority::NORMAL);
|
||||
$core->mods->router->setPath('x/y/z');
|
||||
|
||||
$this->assertEquals('a/b/c', $core->mods->router->getPath());
|
||||
}
|
||||
|
||||
// Cancel all calls
|
||||
public function listener_cancel(\routerSetPathEvent $event){
|
||||
|
||||
$event->setCancelled(true);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Do not cancel events
|
||||
*/
|
||||
public function testLayoutFunctionCallEvent_dontcancel(){
|
||||
|
||||
$core = $this->createCore();
|
||||
$core->mods->router->setPath('a/b/c');
|
||||
|
||||
$core->mods->events->addListener(array($this, 'listener_dontcancel'), 'routerSetPathEvent', \FuzeWorks\EventPriority::NORMAL);
|
||||
$core->mods->router->setPath('x/y/z');
|
||||
|
||||
$this->assertEquals('x/y/z', $core->mods->router->getPath());
|
||||
}
|
||||
|
||||
// Cancel all calls
|
||||
public function listener_dontcancel(\routerSetPathEvent $event){
|
||||
|
||||
$event->setCancelled(false);
|
||||
}
|
||||
}
|
|
@ -1,13 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Model;
|
||||
use \FuzeWorks\Model;
|
||||
|
||||
class Dummy extends Model{
|
||||
|
||||
public function __construct(&$core){
|
||||
parent::__construct($core);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -1,13 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Model;
|
||||
use \FuzeWorks\Model;
|
||||
|
||||
class Dummy extends Model{
|
||||
|
||||
public function __construct(&$core){
|
||||
parent::__construct($core);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -1,22 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Module\FolderLoading;
|
||||
use \FuzeWorks\Module;
|
||||
|
||||
/**
|
||||
* Sections module, see usage documentation
|
||||
* @author TechFuze
|
||||
*/
|
||||
class Main extends Module {
|
||||
|
||||
/**
|
||||
* Loads the module and registers the events
|
||||
* @access public
|
||||
*/
|
||||
public function onLoad() {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
|
@ -1,23 +0,0 @@
|
|||
<?php return array (
|
||||
'module_class' => 'Module\\FolderLoading\\Main',
|
||||
'module_file' => 'class.main.php',
|
||||
'module_name' => 'FolderLoading',
|
||||
'abstract' => false,
|
||||
'dependencies' =>
|
||||
array (
|
||||
),
|
||||
'events' =>
|
||||
array (
|
||||
),
|
||||
'sections' =>
|
||||
array (
|
||||
),
|
||||
'name' => 'FuzeWorks Example Module',
|
||||
'description' => 'A descriptive module that functions as an example',
|
||||
'author' => 'ci',
|
||||
'version' => '1.0.0',
|
||||
'website' => 'http://fuzeworks.techfuze.net/',
|
||||
'date_created' => '29-04-2015',
|
||||
'date_updated' => '29-04-2015',
|
||||
'enabled' => true,
|
||||
) ;
|
Loading…
Reference in New Issue