Unit test fixes

This commit is contained in:
Dennis Eichhorn 2016-01-20 21:37:02 +01:00
parent 4c9fca4f62
commit d796cc05e1
16 changed files with 122 additions and 233 deletions

View File

@ -86,7 +86,7 @@ class Pool
public function get($key = 'core')
{
if (!isset($this->pool[$key])) {
return false;
return false; /* todo: return nullconnection */
}
return $this->pool[$key];
@ -119,18 +119,20 @@ class Pool
* @param mixed $key Database key
* @param array $config Database config data
*
* @return void
* @return \bool
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function create($key, array $config)
public function create($key, array $config) : \bool
{
if (isset($this->pool[$key])) {
return;
return false;
}
$this->pool[$key] = ConnectionFactory::create($config);
return true;
}
}

View File

@ -37,7 +37,7 @@ class Address implements JsonableInterface
* @var \string
* @since 1.0.0
*/
private $recipient = null;
private $recipient = '';
/**
* Sub of the address.
@ -45,7 +45,7 @@ class Address implements JsonableInterface
* @var \string
* @since 1.0.0
*/
private $fao = null;
private $fao = '';
/**
* Location.

View File

@ -31,14 +31,6 @@ namespace phpOMS\Datatypes;
abstract class Enum
{
/**
* Caching constant values.
*
* @var array
* @since 1.0.0
*/
private static $constCache = null;
/**
* Checking enum name.
*
@ -55,7 +47,7 @@ abstract class Enum
{
$constants = self::getConstants();
return array_key_exists($name, $constants);
return isset($constants[$name]);
}
/**
@ -68,12 +60,9 @@ abstract class Enum
*/
public static function getConstants() : array
{
if (!isset(self::$constCache)) {
$reflect = new \ReflectionClass(get_called_class());
self::$constCache = $reflect->getConstants();
}
$reflect = new \ReflectionClass(get_called_class());
return self::$constCache;
return $reflect->getConstants();
}
/**
@ -92,7 +81,7 @@ abstract class Enum
{
$values = array_values(self::getConstants());
return in_array($value, $values, $strict = true);
return in_array($value, $values, true);
}
}

View File

@ -47,7 +47,7 @@ abstract class EnumArray
{
$constants = self::getConstants();
return array_key_exists($name, $constants);
return isset($constants[$name]);
}
/**
@ -80,7 +80,7 @@ abstract class EnumArray
{
$constants = self::getConstants();
return in_array($value, $constants, $strict = true);
return in_array($value, $constants, true);
}
/**
@ -97,7 +97,7 @@ abstract class EnumArray
*/
public static function get($key)
{
$constants = self::getConstants()[$key];
$constants = self::getConstants();
if (!isset($constants[$key])) {
throw new \OutOfBoundsException('Key "' . $key . '" is not valid.');

View File

@ -37,7 +37,7 @@ class Location implements JsonableInterface
* @var \string
* @since 1.0.0
*/
private $postal = null;
private $postal = '';
/**
* Name of city.
@ -45,7 +45,7 @@ class Location implements JsonableInterface
* @var \string
* @since 1.0.0
*/
private $city = null;
private $city = '';
/**
* Name of the country.
@ -53,7 +53,7 @@ class Location implements JsonableInterface
* @var \string
* @since 1.0.0
*/
private $country = null;
private $country = '';
/**
* Street & district.
@ -61,7 +61,7 @@ class Location implements JsonableInterface
* @var \string
* @since 1.0.0
*/
private $address = null;
private $address = '';
/**
* State.
@ -69,7 +69,7 @@ class Location implements JsonableInterface
* @var \string
* @since 1.0.0
*/
private $state = null;
private $state = '';
/**
* Geo coordinates.
@ -77,7 +77,7 @@ class Location implements JsonableInterface
* @var \float[]
* @since 1.0.0
*/
private $geo = null;
private $geo = ['lat' => 0, 'long' => 0];
/**
* Constructor.

View File

@ -70,16 +70,18 @@ class SmartDateTime extends \DateTime
* @param \int $d Day
* @param \int $calendar Calendar
*
* @return void
* @return SmartDateTime
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function smartModify(\int $y, \int $m = 0, \int $d = 0, \int $calendar = CAL_GREGORIAN)
public function smartModify(\int $y, \int $m = 0, \int $d = 0, \int $calendar = CAL_GREGORIAN) : SmartDateTime
{
$y_new = (int) $this->format('Y') + $y;
$y_change = floor(((int) $this->format('m') + $m) / 12);
$y_change = ((int) $this->format('m') + $m) < 0 && ((int) $this->format('m') + $m) % 12 === 0 ? $y_change - 1 : $y_change;
$y_new = (int) $this->format('Y') + $y + $y_change;
$m_new = ((int) $this->format('m') + $m) % 12;
$m_new = $m_new === 0 ? 12 : $m_new;
$m_new = $m_new === 0 ? 12 : $m_new < 0 ? 12 + $m_new : $m_new;
$d_month_old = cal_days_in_month($calendar, (int) $this->format('m'), (int) $this->format('Y'));
$d_month_new = cal_days_in_month($calendar, $m_new, $y_new);
$d_old = (int) $this->format('d');
@ -97,6 +99,8 @@ class SmartDateTime extends \DateTime
if ($d !== 0) {
$this->modify($d . ' day');
}
return $this;
}
}

View File

@ -19,6 +19,7 @@ use phpOMS\ApplicationAbstract;
use phpOMS\Message\RequestAbstract;
use phpOMS\Message\ResponseAbstract;
use phpOMS\Module\ModuleAbstract;
use phpOMS\System\FilePathException;
use phpOMS\Views\ViewLayout;
/**
@ -84,7 +85,7 @@ class Dispatcher
$views = [];
$type = ViewLayout::UNDEFINED;
if (isset($controller['type'])) {
if (is_array($controller) && isset($controller['type'])) {
$type = $controller['type'];
$controller = $controller['dest'];
}
@ -119,20 +120,22 @@ class Dispatcher
*
* @param \string $controller Controller string
*
* @return \bool
* @return mixed
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function get(\string $controller) : \bool
public function get(\string $controller)
{
if (!isset($this->controllers[$controller])) {
$this->controllers[$controller] = new $controller($this->app);
if (realpath($path = ROOT_PATH . '/' . str_replace('\\', '/', $controller) . '.php') === false) {
throw new FilePathException($path);
}
return true;
$this->controllers[$controller] = new $controller($this->app);
}
return false;
return $this->controllers[$controller];
}
/**

View File

View File

@ -1,55 +0,0 @@
<?php
/**
* Orange Management
*
* PHP Version 7.0
*
* @category TBD
* @package TBD
* @author OMS Development Team <dev@oms.com>
* @author Dennis Eichhorn <d.eichhorn@oms.com>
* @copyright 2013 Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
*/
namespace phpOMS\Install;
/**
* Dummy data factory.
*
* Used in order to install dummy data of modules
*
* @category Install
* @package Framework
* @author OMS Development Team <dev@oms.com>
* @author Dennis Eichhorn <d.eichhorn@oms.com>
* @license OMS License 1.0
* @link http://orange-management.com
* @since 1.0.0
*/
class DummyFactory
{
/**
* Generate dummy data.
*
* @param \phpOMS\DataStorage\Database\Pool $db Database instance
* @param \string $module Module name (= directory name)
* @param \int $amount Amount of dummy data
*
* @return void
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public static function generate($db, $module, $amount = 9997)
{
if (file_exists(__DIR__ . '/../../Modules/' . $module . '/Admin/Dummy.class.php')) {
/** @var \phpOMS\Install\DummyInterface $class */
$class = '\\Modules\\' . $module . '\\Admin\\Dummy';
$class::generate($db, $amount);
}
}
}

View File

@ -1,47 +0,0 @@
<?php
/**
* Orange Management
*
* PHP Version 7.0
*
* @category TBD
* @package TBD
* @author OMS Development Team <dev@oms.com>
* @author Dennis Eichhorn <d.eichhorn@oms.com>
* @copyright 2013 Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
*/
namespace phpOMS\Install;
/**
* Dummy data interface.
*
* Used in order to install dummy data of modules
*
* @category Install
* @package Framework
* @author OMS Development Team <dev@oms.com>
* @author Dennis Eichhorn <d.eichhorn@oms.com>
* @license OMS License 1.0
* @link http://orange-management.com
* @since 1.0.0
*/
interface DummyInterface
{
/**
* Generate dummy data.
*
* @param \phpOMS\DataStorage\Database\Pool $db Database instance
* @param \int $amount Amount of dummy data
*
* @return void
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public static function generate($db, $amount);
}

View File

View File

@ -1 +0,0 @@
# Install #

View File

View File

@ -1,44 +0,0 @@
<?php
/**
* Orange Management
*
* PHP Version 7.0
*
* @category TBD
* @package TBD
* @author OMS Development Team <dev@oms.com>
* @author Dennis Eichhorn <d.eichhorn@oms.com>
* @copyright 2013 Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
*/
namespace phpOMS\Install;
/**
* Update class.
*
* @category Install
* @package Framework
* @author OMS Development Team <dev@oms.com>
* @author Dennis Eichhorn <d.eichhorn@oms.com>
* @license OMS License 1.0
* @link http://orange-management.com
* @since 1.0.0
*/
class Update
{
/* TODO: remove relative elements (security) and only allow paths in this application */
public static function replace($old, $new)
{
unlink($old);
rename('somewhere_in_temp_folder/' . $new, $old);
}
public static function remove($old)
{
unlink($old);
}
}

View File

@ -14,7 +14,6 @@
* @link http://orange-management.com
*/
namespace phpOMS\Localization;
use phpOMS\Datatypes\Exception\InvalidEnumName;
use phpOMS\Datatypes\Exception\InvalidEnumValue;
/**
@ -37,7 +36,7 @@ class Localization
* @var \string
* @since 1.0.0
*/
public $country = null;
private $country = ISO3166Enum::_US;
/**
* Timezone.
@ -45,7 +44,7 @@ class Localization
* @var \string
* @since 1.0.0
*/
public $timezone = null;
private $timezone = 'America/New_York';
/**
* Language ISO code.
@ -53,7 +52,7 @@ class Localization
* @var \string
* @since 1.0.0
*/
public $language = 'en';
private $language = ISO639Enum::_EN;
/**
* Currency.
@ -61,7 +60,7 @@ class Localization
* @var \string
* @since 1.0.0
*/
public $currency = null;
private $currency = ISO4217Enum::C_USD;
/**
* Number format.
@ -69,7 +68,15 @@ class Localization
* @var \string
* @since 1.0.0
*/
public $numberformat = null;
private $decimal = '.';
/**
* Number format.
*
* @var \string
* @since 1.0.0
*/
private $thousands = ',';
/**
* Time format.
@ -77,7 +84,7 @@ class Localization
* @var \string
* @since 1.0.0
*/
public $datetime = null;
private $datetime = 'Y-m-d H:i:s';
/**
* Language array.
@ -85,7 +92,7 @@ class Localization
* @var \string[]
* @since 1.0.0
*/
public $lang = [];
private $lang = [];
/**
* Constructor.
@ -174,8 +181,8 @@ class Localization
*/
public function setLanguage(\string $language)
{
if(!ISO639EnumArray::isValidName($language)) {
throw new InvalidEnumName($language);
if(!ISO639Enum::isValidValue($language)) {
throw new InvalidEnumValue($language);
}
$this->language = $language;
@ -226,8 +233,8 @@ class Localization
*/
public function setCurrency(\string $currency)
{
if(!ISO4217EnumArray::isValidName($currency)) {
throw new InvalidEnumName($currency);
if(!ISO4217Enum::isValidValue($currency)) {
throw new InvalidEnumValue($currency);
}
$this->currency = $currency;
@ -263,21 +270,45 @@ class Localization
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function getNumberformat() : \string
public function getDecimal() : \string
{
return $this->numberformat;
return $this->decimal;
}
/**
* @param \string $numberformat
* @param \string $decimal
*
* @return \string
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function setNumberformat(\string $numberformat) : \string
public function setDecimal(\string $decimal)
{
$this->numberformat = $numberformat;
$this->decimal = $decimal;
}
/**
* @return \string
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function getThousands() : \string
{
return $this->thousands;
}
/**
* @param \string $thousands
*
* @return \string
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function setThousands(\string $thousands)
{
$this->thousands = $thousands;
}
}

View File

@ -50,7 +50,7 @@ class FileLogger implements LoggerInterface
/**
* Instance.
*
* @var \phpOMS\DataStorage\Cache\CacheManager
* @var \phpOMS\DataStorage\Cache\Pool
* @since 1.0.0
*/
protected static $instance = null;
@ -85,25 +85,31 @@ class FileLogger implements LoggerInterface
*/
public function __construct(\string $lpath)
{
if (!file_exists($lpath)) {
mkdir($lpath);
}
$path = realpath($lpath);
if ($path === false || Validator::startsWith($path, ROOT_PATH) === false) {
if ($path !== false && Validator::startsWith($path, ROOT_PATH) === false) {
throw new FilePathException($lpath);
}
if (!file_exists($path)) {
mkdir($path, 0644, true);
if (is_dir($lpath)) {
if (!file_exists($lpath)) {
mkdir($lpath, 0644, true);
}
if (!file_exists($lpath . '/' . date('Y-m-d') . '.log')) {
touch($lpath . '/' . date('Y-m-d') . '.log');
}
$path = realpath($lpath . '/' . date('Y-m-d') . '.log');
} else {
if (!file_exists($lpath)) {
touch($lpath);
}
$path = realpath($lpath);
}
$this->path = $path . '/' . date('Y-m-d') . '.log';
if (!file_exists($path)) {
touch($path);
}
$this->path = $path;
}
/**
@ -237,8 +243,8 @@ class FileLogger implements LoggerInterface
$replace['{backtrace}'] = str_replace(str_replace('\\', '\\\\', ROOT_PATH), '', $backtrace);
$replace['{datetime}'] = sprintf('%--19s', (new \DateTime('NOW'))->format('Y-m-d H:i:s'));
$replace['{level}'] = sprintf('%--12s', $level);
$replace['{path}'] = $_SERVER['REQUEST_URI'];
$replace['{ip}'] = sprintf('%--15s', $_SERVER['REMOTE_ADDR']);
$replace['{path}'] = $_SERVER['REQUEST_URI'] ?? 'REQUEST_URI';
$replace['{ip}'] = sprintf('%--15s', $_SERVER['REMOTE_ADDR'] ?? '0.0.0.0');
$replace['{version}'] = sprintf('%--15s', PHP_VERSION);
$replace['{os}'] = sprintf('%--15s', PHP_OS);
@ -533,15 +539,16 @@ class FileLogger implements LoggerInterface
while (($line = fgetcsv($this->fp, 0, ';')) !== false) {
$id++;
$offset--;
if ($offset > 0) {
$offset--;
continue;
}
if ($limit <= 0) {
$logs = array_reverse($logs, true);
array_pop($logs);
reset($logs);
unset($logs[key($logs)]);
}
foreach ($line as &$value) {
@ -550,11 +557,11 @@ class FileLogger implements LoggerInterface
$logs[$id] = $line;
$limit--;
ksort($logs);
}
fseek($this->fp, 0, SEEK_END);
fclose($this->fp);
asort($logs);
}
return $logs;
@ -583,16 +590,16 @@ class FileLogger implements LoggerInterface
continue;
}
$log['datetime'] = $line[0] ?? '';
$log['level'] = $line[1] ?? '';
$log['ip'] = $line[2] ?? '';
$log['line'] = $line[3] ?? '';
$log['version'] = $line[4] ?? '';
$log['os'] = $line[5] ?? '';
$log['path'] = $line[6] ?? '';
$log['message'] = $line[7] ?? '';
$log['file'] = $line[8] ?? '';
$log['backtrace'] = $line[9] ?? '';
$log['datetime'] = trim($line[0] ?? '');
$log['level'] = trim($line[1] ?? '');
$log['ip'] = trim($line[2] ?? '');
$log['line'] = trim($line[3] ?? '');
$log['version'] = trim($line[4] ?? '');
$log['os'] = trim($line[5] ?? '');
$log['path'] = trim($line[6] ?? '');
$log['message'] = trim($line[7] ?? '');
$log['file'] = trim($line[8] ?? '');
$log['backtrace'] = trim($line[9] ?? '');
break;
}