Unit test fixes

This commit is contained in:
Dennis Eichhorn 2016-01-16 19:21:30 +01:00
parent 6ddd592297
commit 0b9c78144c
24 changed files with 206 additions and 164 deletions

View File

@ -15,8 +15,6 @@
*/ */
namespace phpOMS\Auth; namespace phpOMS\Auth;
use phpOMS\Config\OptionsInterface;
use phpOMS\Config\OptionsTrait;
use phpOMS\DataStorage\Database\Connection\ConnectionAbstract; use phpOMS\DataStorage\Database\Connection\ConnectionAbstract;
use phpOMS\DataStorage\Database\DatabaseType; use phpOMS\DataStorage\Database\DatabaseType;
use phpOMS\DataStorage\Session\SessionInterface; use phpOMS\DataStorage\Session\SessionInterface;
@ -34,10 +32,8 @@ use phpOMS\DataStorage\Session\SessionInterface;
* @link http://orange-management.com * @link http://orange-management.com
* @since 1.0.0 * @since 1.0.0
*/ */
class Auth implements OptionsInterface class Auth
{ {
use OptionsTrait;
/** /**
* Session instance. * Session instance.
* *
@ -157,7 +153,7 @@ class Auth implements OptionsInterface
* @since 1.0.0 * @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com> * @author Dennis Eichhorn <d.eichhorn@oms.com>
*/ */
public function logout(\int $uid) public function logout(\int $uid = null)
{ {
// TODO: logout other users? If admin wants to kick a user for updates etc. // TODO: logout other users? If admin wants to kick a user for updates etc.
$this->session->remove('UID'); $this->session->remove('UID');

View File

@ -38,7 +38,7 @@ trait OptionsTrait
*/ */
public function exists($key) public function exists($key)
{ {
return array_key_exists($key, $this->options); return isset($this->options[$key]);
} }
/** /**
@ -55,7 +55,7 @@ trait OptionsTrait
public function setOption($key, $value, \bool $overwrite = true) : \bool public function setOption($key, $value, \bool $overwrite = true) : \bool
{ {
if ($overwrite || !array_key_exists($key, $this->options)) { if ($overwrite || !array_key_exists($key, $this->options)) {
$this->options[$key] = [$value, $overwrite]; $this->options[$key] = $value;
return true; return true;
} }

View File

@ -95,7 +95,7 @@ abstract class SettingsAbstract implements OptionsInterface
switch ($this->connection->getType()) { switch ($this->connection->getType()) {
case DatabaseType::MYSQL: case DatabaseType::MYSQL:
$query = new Builder($this->connection); $query = new Builder($this->connection);
$sql = $query->select(static::$columns[0], 'settings_content') $sql = $query->select(...static::$columns)
->from($this->connection->prefix . static::$table) ->from($this->connection->prefix . static::$table)
->where(static::$columns[0], 'in', $columns) ->where(static::$columns[0], 'in', $columns)
->toSql(); ->toSql();
@ -115,7 +115,6 @@ abstract class SettingsAbstract implements OptionsInterface
* Set option by key. * Set option by key.
* *
* @param \string[] $options Column values for filtering * @param \string[] $options Column values for filtering
* @param \bool $overwrite Overwrite existing settings
* @param \bool $store Save this Setting immediately to database * @param \bool $store Save this Setting immediately to database
* *
* @return mixed Option value * @return mixed Option value
@ -123,9 +122,9 @@ abstract class SettingsAbstract implements OptionsInterface
* @since 1.0.0 * @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com> * @author Dennis Eichhorn <d.eichhorn@oms.com>
*/ */
public function set(array $options, \bool $overwrite = true, \bool $store = false) public function set(array $options, \bool $store = false)
{ {
$this->setOptions($options, $overwrite); $this->setOptions($options);
if($store) { if($store) {
// save to db // save to db

View File

@ -85,4 +85,17 @@ abstract class BuilderAbstract
{ {
return $this->prefix; return $this->prefix;
} }
/**
* Get query type.
*
* @return \int
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function getType() : \int
{
return $this->type;
}
} }

View File

@ -17,7 +17,7 @@ namespace phpOMS\DataStorage\Database\Connection;
use phpOMS\DataStorage\Database\DatabaseStatus; use phpOMS\DataStorage\Database\DatabaseStatus;
use phpOMS\DataStorage\Database\Query\Grammar\Grammar; use phpOMS\DataStorage\Database\Query\Grammar\Grammar;
use phpOMS\DataStorage\Database\Schema\Query\Grammar\Grammar as SchemaGrammar; use phpOMS\DataStorage\Database\Schema\Grammar\Grammar as SchemaGrammar;
/** /**
* Database handler. * Database handler.

View File

@ -16,7 +16,7 @@
namespace phpOMS\DataStorage\Database\Connection; namespace phpOMS\DataStorage\Database\Connection;
use phpOMS\DataStorage\Database\Query\Grammar\Grammar; use phpOMS\DataStorage\Database\Query\Grammar\Grammar;
use phpOMS\DataStorage\Database\Schema\Query\Grammar\Grammar as SchemaGrammar; use phpOMS\DataStorage\Database\Schema\Grammar\Grammar as SchemaGrammar;
/** /**
* Database connection interface. * Database connection interface.

View File

@ -40,7 +40,7 @@ abstract class GrammarAbstract
* @var \string * @var \string
* @since 1.0.0 * @since 1.0.0
*/ */
public $systemIdentifier = '"'; protected $systemIdentifier = '"';
/** /**
* And operator. * And operator.
@ -63,7 +63,7 @@ abstract class GrammarAbstract
/** /**
* Compile to query. * Compile to query.
* *
* @param Builder $query Builder * @param BuilderAbstract $query Builder
* *
* @return \string * @return \string
* *
@ -72,7 +72,7 @@ abstract class GrammarAbstract
* @since 1.0.0 * @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com> * @author Dennis Eichhorn <d.eichhorn@oms.com>
*/ */
public function compileQuery($query) : \string public function compileQuery(BuilderAbstract $query) : \string
{ {
return trim( return trim(
implode(' ', implode(' ',
@ -86,6 +86,8 @@ abstract class GrammarAbstract
) . ';'; ) . ';';
} }
abstract protected function compileComponents(BuilderAbstract $query) : array;
/** /**
* Expressionize elements. * Expressionize elements.
* *
@ -118,6 +120,26 @@ abstract class GrammarAbstract
return rtrim($expression, ', '); return rtrim($expression, ', ');
} }
/**
* Compile system.
*
* @param array|\string $system System
* @param \string $prefix Prefix for table
*
* @return \string
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
protected function compileSystem($system, \string $prefix = '') : \string
{
if (count($split = explode('.', $system)) == 2) {
return $this->compileSystem($prefix . $split[0]) . '.' . $this->compileSystem($split[1]);
} else {
return $this->systemIdentifier . $prefix . $system . $this->systemIdentifier;
}
}
public function getDateFormat() : \string public function getDateFormat() : \string
{ {
return 'Y-m-d H:i:s'; return 'Y-m-d H:i:s';

View File

@ -788,19 +788,6 @@ class Builder extends BuilderAbstract
} }
/**
* Get query type.
*
* @return \int
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function getType() : \int
{
return $this->type;
}
/** /**
* Merging query. * Merging query.
* *

View File

@ -16,6 +16,7 @@
namespace phpOMS\DataStorage\Database\Query\Grammar; namespace phpOMS\DataStorage\Database\Query\Grammar;
use phpOMS\DataStorage\Database\BuilderAbstract;
use phpOMS\DataStorage\Database\GrammarAbstract; use phpOMS\DataStorage\Database\GrammarAbstract;
use phpOMS\DataStorage\Database\Query\Builder; use phpOMS\DataStorage\Database\Query\Builder;
use phpOMS\DataStorage\Database\Query\Column; use phpOMS\DataStorage\Database\Query\Column;
@ -82,7 +83,7 @@ class Grammar extends GrammarAbstract
/** /**
* Compile components. * Compile components.
* *
* @param Builder $query Builder * @param BuilderAbstract $query Builder
* *
* @return \string[] * @return \string[]
* *
@ -91,7 +92,7 @@ class Grammar extends GrammarAbstract
* @since 1.0.0 * @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com> * @author Dennis Eichhorn <d.eichhorn@oms.com>
*/ */
protected function compileComponents(Builder $query) : array protected function compileComponents(BuilderAbstract $query) : array
{ {
$sql = []; $sql = [];
@ -257,26 +258,6 @@ class Grammar extends GrammarAbstract
} }
} }
/**
* Compile system.
*
* @param array|\string $system System
* @param \string $prefix Prefix for table
*
* @return \string
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
protected function compileSystem($system, \string $prefix = '') : \string
{
if (count($split = explode('.', $system)) == 2) {
return $this->compileSystem($prefix . $split[0]) . '.' . $this->compileSystem($split[1]);
} else {
return $this->systemIdentifier . $prefix . $system . $this->systemIdentifier;
}
}
/** /**
* Compile limit. * Compile limit.
* *

View File

@ -25,5 +25,5 @@ class MysqlGrammar extends Grammar
* @var \string * @var \string
* @since 1.0.0 * @since 1.0.0
*/ */
public $systemIdentifier = '`'; protected $systemIdentifier = '`';
} }

View File

@ -14,12 +14,11 @@
* @link http://orange-management.com * @link http://orange-management.com
*/ */
namespace phpOMS\DataStorage\Database\Schema\Query; namespace phpOMS\DataStorage\Database\Schema;
use phpOMS\DataStorage\Database\BuilderAbstract; use phpOMS\DataStorage\Database\BuilderAbstract;
use phpOMS\DataStorage\Database\Connection\ConnectionAbstract; use phpOMS\DataStorage\Database\Connection\ConnectionAbstract;
use phpOMS\DataStorage\Database\Query; use phpOMS\DataStorage\Database\Query;
use phpOMS\DataStorage\Database\Schema\QueryType;
/** /**
* Database query builder. * Database query builder.
@ -34,9 +33,9 @@ use phpOMS\DataStorage\Database\Schema\QueryType;
*/ */
class Builder extends BuilderAbstract class Builder extends BuilderAbstract
{ {
private $type = QueryType::SELECT; public $table = [];
private $table = []; public $drop = [];
/** /**
* Constructor. * Constructor.
@ -59,9 +58,11 @@ class Builder extends BuilderAbstract
$this->table = array_unique($this->table); $this->table = array_unique($this->table);
} }
public function drop(\string $table) public function drop(...$table)
{ {
$this->type = QueryType::DROP;
$this->drop += $table;
$this->drop = array_unique($this->drop);
} }
public function create(\string $table) public function create(\string $table)
@ -73,4 +74,17 @@ class Builder extends BuilderAbstract
{ {
} }
/**
* Parsing to string.
*
* @return \string
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function toSql() : \string
{
return $this->grammar->compileQuery($this);
}
} }

View File

@ -16,7 +16,10 @@
namespace phpOMS\DataStorage\Database\Schema\Grammar; namespace phpOMS\DataStorage\Database\Schema\Grammar;
use phpOMS\DataStorage\Database\BuilderAbstract;
use phpOMS\DataStorage\Database\GrammarAbstract; use phpOMS\DataStorage\Database\GrammarAbstract;
use phpOMS\DataStorage\Database\Schema\Builder;
use phpOMS\DataStorage\Database\Schema\QueryType;
/** /**
* Database query grammar. * Database query grammar.
@ -41,4 +44,47 @@ class Grammar extends GrammarAbstract
'selects', 'selects',
'from', 'from',
]; ];
/**
* Select components.
*
* @var \string[]
* @since 1.0.0
*/
protected $dropComponents = [
'drop',
];
public function compileComponents(BuilderAbstract $query) : array
{
$sql = [];
switch ($query->getType()) {
case QueryType::DROP:
$components = $this->dropComponents;
break;
default:
throw new \InvalidArgumentException('Unknown query type.');
}
/* Loop all possible query components and if they exist compile them. */
foreach ($components as $component) {
if (isset($query->{$component}) && !empty($query->{$component})) {
$sql[$component] = $this->{'compile' . ucfirst($component)}($query, $query->{$component});
}
}
return $sql;
}
protected function compileDrop(Builder $query, array $tables) : \string
{
$expression = $this->expressionizeTableColumn($tables, $query->getPrefix());
if ($expression === '') {
$expression = '*';
}
return 'DROP TABLE ' . $expression;
}
} }

View File

@ -31,6 +31,14 @@ use phpOMS\DataStorage\Database\Query\Builder;
*/ */
class MysqlGrammar extends Grammar class MysqlGrammar extends Grammar
{ {
/**
* System identifier.
*
* @var \string
* @since 1.0.0
*/
protected $systemIdentifier = '`';
/** /**
* Compile select. * Compile select.
* *

View File

@ -45,7 +45,7 @@ class ConsoleSession implements SessionInterface
* @since 1.0.0 * @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com> * @author Dennis Eichhorn <d.eichhorn@oms.com>
*/ */
public function __construct($sid) public function __construct($sid = false)
{ {
$this->sid = $sid; $this->sid = $sid;
} }

View File

@ -45,7 +45,7 @@ class SocketSession implements SessionInterface
* @since 1.0.0 * @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com> * @author Dennis Eichhorn <d.eichhorn@oms.com>
*/ */
public function __construct($sid) public function __construct($sid = false)
{ {
$this->sid = $sid; $this->sid = $sid;
} }

View File

@ -38,4 +38,5 @@ abstract class TagType extends Enum
const SELECT = 5; /* <select> */ const SELECT = 5; /* <select> */
const LABEL = 6; /* <label> */ const LABEL = 6; /* <label> */
const ULIST = 7; /* <ul> */ const ULIST = 7; /* <ul> */
const OLIST = 8; /* <ul> */
} }

View File

@ -44,5 +44,6 @@ class UninstallAbstract
*/ */
public static function uninstall(Pool $dbPool, array $info) public static function uninstall(Pool $dbPool, array $info)
{ {
} }
} }

View File

@ -1,13 +0,0 @@
# Tests #
Unit tests
## License ##
* Orange Management
* (c) 2013 - Dennis Eichhorn
## Contact ##
* Customer: spl1nes.com@googlemail.com
* Developer: spl1nes.com@googlemail.com

View File

@ -63,11 +63,7 @@ class UriFactory
*/ */
public static function getQuery(\string $key) public static function getQuery(\string $key)
{ {
if (isset(self::$uri[$key])) { return self::$uri[$key] ?? false;
return self::$uri[$key];
}
return false;
} }
/** /**
@ -77,16 +73,20 @@ class UriFactory
* @param \string $value Replacement value * @param \string $value Replacement value
* @param \bool $overwrite Overwrite if already exists * @param \bool $overwrite Overwrite if already exists
* *
* @return void * @return bool
* *
* @since 1.0.0 * @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com> * @author Dennis Eichhorn <d.eichhorn@oms.com>
*/ */
public static function setQuery(\string $key, \string $value, \bool $overwrite = true) public static function setQuery(\string $key, \string $value, \bool $overwrite = true) : \bool
{ {
if ($overwrite || !isset(self::$uri[$key])) { if ($overwrite || !isset(self::$uri[$key])) {
self::$uri[$key] = $value; self::$uri[$key] = $value;
return true;
} }
return false;
} }
/** /**

View File

@ -116,26 +116,21 @@ class ArrayUtils
* *
* @param mixed $needle Needle for search * @param mixed $needle Needle for search
* @param array $haystack Haystack for search * @param array $haystack Haystack for search
* @param mixed $id ID for search
* *
* @return \bool * @return \bool
* *
* @since 1.0.0 * @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com> * @author Dennis Eichhorn <d.eichhorn@oms.com>
*/ */
public static function inArrayRecursive($needle, array $haystack, $id = null) : \bool public static function inArrayRecursive($needle, array $haystack) : \bool
{ {
$found = false; $found = false;
if (isset($id) && isset($haystack[$id]) && $haystack[$id] === $needle) {
return true;
}
foreach ($haystack as $item) { foreach ($haystack as $item) {
if ($item === $needle) { if ($item === $needle) {
return true; return true;
} elseif (is_array($item)) { } elseif (is_array($item)) {
$found = self::inArrayRecursive($needle, $item, $id); $found = self::inArrayRecursive($needle, $item);
if ($found) { if ($found) {
break; break;
@ -203,16 +198,17 @@ class ArrayUtils
* @param array $data Data to convert * @param array $data Data to convert
* @param \string $delimiter Delim to use * @param \string $delimiter Delim to use
* @param \string $enclosure Enclosure to use * @param \string $enclosure Enclosure to use
* @param \string $escape Escape to use
* *
* @return \string * @return \string
* *
* @since 1.0.0 * @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com> * @author Dennis Eichhorn <d.eichhorn@oms.com>
*/ */
public static function arrayToCSV(array $data, \string $delimiter = ';', \string $enclosure = '"') : \string public static function arrayToCSV(array $data, \string $delimiter = ';', \string $enclosure = '"', \string $escape = '\\') : \string
{ {
$outstream = fopen('php://output', 'r+'); $outstream = fopen('php://memory', 'r+');
fputcsv($outstream, $data, $delimiter, $enclosure); fputcsv($outstream, $data, $delimiter, $enclosure, $escape);
rewind($outstream); rewind($outstream);
$csv = fgets($outstream); $csv = fgets($outstream);
fclose($outstream); fclose($outstream);

View File

@ -26,7 +26,7 @@ namespace phpOMS\Utils;
* @link http://orange-management.com * @link http://orange-management.com
* @since 1.0.0 * @since 1.0.0
*/ */
class MultiMap class MultiMap implements \Countable
{ {
/** /**
@ -58,21 +58,57 @@ class MultiMap
/** /**
* Add data. * Add data.
* *
* @param array $keys Keys for value * @param array $keys Keys for value
* @param mixed $value Value to store * @param mixed $value Value to store
* @param \bool $overwrite Add value if key exists
* *
* @return void * @return bool
* *
* @since 1.0.0 * @since 1.0.0
* @author Dennis Eichhorn * @author Dennis Eichhorn
*/ */
public function add(array $keys, $value) public function add(array $keys, $value, \bool $overwrite = true) : \bool
{ {
$this->values[] = $value; $id = count($this->values);
$id = count($this->values) - 1; $inserted = false;
foreach ($keys as $key) { foreach ($keys as $key) {
$this->keys[$key] = $id; if ($overwrite || !isset($this->keys[$key])) {
$id = $this->keys[$key] ?? $id;
$this->keys[$key] = $id;
$inserted = true;
}
}
if ($inserted) {
$this->values[$id] = $value;
}
// todo: is this really required???? - i don't think so!
$this->garbageCollect();
return $inserted;
}
/**
* Garbage collect unreferenced values/keys
*
* @since 1.0.0
* @author Dennis Eichhorn
*/
private function garbageCollect()
{
foreach ($this->keys as $key => $keyValue) {
if (!isset($this->values[$keyValue])) {
unset($this->keys[$key]);
}
}
foreach ($this->values as $valueKey => $value) {
if (!in_array($valueKey, $this->keys)) {
unset($this->values[$valueKey]);
}
} }
} }
@ -88,7 +124,7 @@ class MultiMap
*/ */
public function get($key) public function get($key)
{ {
return $this->values[$this->keys[$key]] ?? null; return isset($this->keys[$key]) ? $this->values[$this->keys[$key]] ?? null : null;
} }
/** /**
@ -128,14 +164,10 @@ class MultiMap
if (isset($this->keys[$key])) { if (isset($this->keys[$key])) {
$id = $this->keys[$key]; $id = $this->keys[$key];
foreach ($this->keys as $key => $ref) {
if ($ref === $id) {
unset($this->keys[$key]);
}
}
unset($this->values[$id]); unset($this->values[$id]);
$this->garbageCollect();
return true; return true;
} }
@ -160,6 +192,8 @@ class MultiMap
if (isset($this->keys[$old]) && isset($this->keys[$new])) { if (isset($this->keys[$old]) && isset($this->keys[$new])) {
$this->keys[$old] = $this->keys[$new]; $this->keys[$old] = $this->keys[$new];
$this->garbageCollect();
return true; return true;
} }
@ -181,22 +215,9 @@ class MultiMap
public function removeKey($key) : \bool public function removeKey($key) : \bool
{ {
if (isset($this->keys[$key])) { if (isset($this->keys[$key])) {
$id = $this->keys[$key];
unset($this->keys[$key]); unset($this->keys[$key]);
$unreferencd = true; $this->garbageCollect();
foreach ($this->keys as $key => $value) {
if ($value === $id) {
$unreferencd = false;
break;
}
}
if ($unreferencd) {
unset($this->values[$id]);
}
return true; return true;
} }

View File

@ -41,14 +41,14 @@ class StringUtils
* @since 1.0.0 * @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com> * @author Dennis Eichhorn <d.eichhorn@oms.com>
*/ */
public static function generateString($min = 10, $max = 10, $charset = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ') public static function generateString(\int $min = 10, \int $max = 10, \string $charset = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ') : \string
{ {
$length = rand($min, $max); $length = mt_rand($min, $max);
$charactersLength = strlen($charset); $charactersLength = strlen($charset);
$randomString = ''; $randomString = '';
for ($i = 0; $i < $length; $i++) { for ($i = 0; $i < $length; $i++) {
$randomString .= $charset[rand(0, $charactersLength - 1)]; $randomString .= $charset[mt_rand(0, $charactersLength - 1)];
} }
return $randomString; return $randomString;

View File

@ -46,6 +46,16 @@ class Email extends ValidatorAbstract
*/ */
public static function isValid(\string $value) : \bool public static function isValid(\string $value) : \bool
{ {
return filter_var($value, FILTER_VALIDATE_EMAIL) === false ? false : true; if (filter_var($value, FILTER_VALIDATE_EMAIL) === false) {
self::$msg = 'Invalid Email by filter_var standards';
self::$error = 1;
return false;
}
self::$msg = '';
self::$error = 0;
return true;
} }
} }

View File

@ -43,46 +43,6 @@ class Version
{ {
} }
/**
* Save version file.
*
* @param \string $type Lib or tool name
* @param \string $version Version
* @param \string $path Path to version file
*
* @return void
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public static function setVersion(\string $type, \string $version, \string $path)
{
$versions = self::getVersion($path);
$versions[$type] = $version;
file_put_contents($path, json_encode($versions));
}
/**
* Loading version file.
*
* @param \string $jpath Path to version file
*
* @return \string[]
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public static function getVersion(\string $jpath) : array
{
$path = realpath($jpath);
if($path === false || Validator::startsWith($path, ROOT_PATH) === false || strpos($path, 'config.php') !== false) {
throw new FilePathException($jpath);
}
return json_decode(file_get_contents($path), true);
}
/** /**
* Comparing two versions. * Comparing two versions.
* *