From 38350e9c773f932d84e1f3766ca769789a9bff7f Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Sat, 23 Jul 2016 18:17:25 +0200 Subject: [PATCH] Test fixes --- Account/Account.php | 17 ++---------- Account/AccountManager.php | 10 ++++---- Datatypes/Enum.php | 6 ++--- Datatypes/Iban.php | 10 ++++---- Dispatcher/Dispatcher.php | 4 +-- Localization/ISO3166TwoEnum.php | 4 +-- Localization/Localization.php | 2 +- Message/Http/Request.php | 9 +++---- Message/Http/Response.php | 7 +++-- Module/ModuleFactory.php | 2 +- Module/ModuleManager.php | 44 ++++++++++++++++++++------------ Utils/ArrayUtils.php | 7 ++++- Utils/Compression/LZW.php | 6 ++--- Utils/Permutation.php | 10 ++++---- Utils/StringUtils.php | 26 +++++++++++++++++-- Validation/Base/Iban.php | 8 +++--- Validation/ValidatorAbstract.php | 8 ++++++ 17 files changed, 107 insertions(+), 73 deletions(-) diff --git a/Account/Account.php b/Account/Account.php index 3b7df4661..3484f238b 100644 --- a/Account/Account.php +++ b/Account/Account.php @@ -17,6 +17,7 @@ namespace phpOMS\Account; use phpOMS\Contract\ArrayableInterface; use phpOMS\Localization\Localization; +use phpOMS\Localization\NullLocalization; use phpOMS\Validation\Base\Email; /** @@ -159,6 +160,7 @@ class Account implements ArrayableInterface, \JsonSerializable { $this->createdAt = new \DateTime('now'); $this->id = $id; + $this->l11n = new NullLocalization(); } /** @@ -425,21 +427,6 @@ class Account implements ArrayableInterface, \JsonSerializable $this->lastActive = new \DateTime('NOW'); } - /** - * Set created at. - * - * @param \DateTime $created Created at - * - * @return void - * - * @since 1.0.0 - * @author Dennis Eichhorn - */ - public function setCreatedAt(\DateTime $created) - { - $this->createdAt = $created; - } - /** * {@inheritdoc} */ diff --git a/Account/AccountManager.php b/Account/AccountManager.php index 81c12d142..91460f148 100644 --- a/Account/AccountManager.php +++ b/Account/AccountManager.php @@ -130,24 +130,24 @@ class AccountManager implements \Countable } /** - * Set account. + * Add account. * * @param Account $account Account * - * @return int Account id + * @return bool * * @since 1.0.0 * @author Dennis Eichhorn */ - public function set(Account $account) + public function add(Account $account) : bool { if (!isset($this->accounts[$account->getId()])) { $this->accounts[$account->getId()] = $account; - return $account->getId(); + return true; } - return null; + return false; } /** diff --git a/Datatypes/Enum.php b/Datatypes/Enum.php index 207d36bf9..fc0824230 100644 --- a/Datatypes/Enum.php +++ b/Datatypes/Enum.php @@ -95,10 +95,10 @@ abstract class Enum public static function getByName(string $name) { if (!self::isValidName($name)) { - throw new \Exception('Undefined constant'); + throw new \Exception('Undefined constant "' . $name . '"'); } - return constant($name); + return constant('static::' . $name); } /** @@ -115,7 +115,7 @@ abstract class Enum */ public static function isValidName(string $name) : bool { - return defined($name); + return defined('static::' . $name); } } diff --git a/Datatypes/Iban.php b/Datatypes/Iban.php index dbd5bf271..1d529a752 100644 --- a/Datatypes/Iban.php +++ b/Datatypes/Iban.php @@ -65,11 +65,11 @@ class Iban implements \Serializable */ private function parse(string $iban) { - if (!\phpOMS\Validation\Base\Iban::isValid($iban)) { + $this->iban = self::normalize($iban); + + if (!\phpOMS\Validation\Base\Iban::isValid($this->iban)) { throw new \InvalidArgumentException('Invalid IBAN'); } - - $this->iban = self::normalize($iban); } /** @@ -135,7 +135,7 @@ class Iban implements \Serializable return ''; } - return substr($this->iban, $start, $end - $start); + return substr($this->iban, $start, $end - $start + 1); } /** @@ -226,7 +226,7 @@ class Iban implements \Serializable */ public function getAccount() : string { - return $this->getSequence('n'); + return $this->getSequence('c'); } /** diff --git a/Dispatcher/Dispatcher.php b/Dispatcher/Dispatcher.php index db59345c0..094229c65 100644 --- a/Dispatcher/Dispatcher.php +++ b/Dispatcher/Dispatcher.php @@ -165,12 +165,12 @@ class Dispatcher * @param ResponseAbstract $response Response * @param mixed $data Data * - * @return array + * @return mixed * * @since 1.0.0 * @author Dennis Eichhorn */ - private function dispatchClosure(\Closure $controller, RequestAbstract $request, ResponseAbstract $response, $data = null) : array + private function dispatchClosure(\Closure $controller, RequestAbstract $request, ResponseAbstract $response, $data = null) { return $controller($this->app, $request, $response, $data); } diff --git a/Localization/ISO3166TwoEnum.php b/Localization/ISO3166TwoEnum.php index 6afd911e7..8d3100b74 100644 --- a/Localization/ISO3166TwoEnum.php +++ b/Localization/ISO3166TwoEnum.php @@ -15,7 +15,7 @@ */ namespace phpOMS\Localization; -use phpOMS\Datatypes\EnumArray; +use phpOMS\Datatypes\Enum; /** * Country codes ISO list. @@ -28,7 +28,7 @@ use phpOMS\Datatypes\EnumArray; * @link http://orange-management.com * @since 1.0.0 */ -class ISO3166TwoEnum extends EnumArray +class ISO3166TwoEnum extends Enum { const C_AFG = 'AF'; const C_ALA = 'AX'; diff --git a/Localization/Localization.php b/Localization/Localization.php index 69376c80e..1173f03ae 100644 --- a/Localization/Localization.php +++ b/Localization/Localization.php @@ -136,7 +136,7 @@ class Localization * @since 1.0.0 * @author Dennis Eichhorn */ - public function __construct(L11nManager $l11nManager) + public function __construct(L11nManager $l11nManager = null) { $this->l11nManager = $l11nManager; } diff --git a/Message/Http/Request.php b/Message/Http/Request.php index ad3af9bd0..65581a25a 100644 --- a/Message/Http/Request.php +++ b/Message/Http/Request.php @@ -15,7 +15,6 @@ */ namespace phpOMS\Message\Http; -use phpOMS\Localization\L11nManager; use phpOMS\Localization\Localization; use phpOMS\Message\RequestAbstract; use phpOMS\Router\RouteVerb; @@ -83,15 +82,15 @@ class Request extends RequestAbstract /** * Constructor. * - * @param L11nManager $l11nManager Localization manager - * @param UriInterface $uri Uri + * @param Localization $l11n Localization + * @param UriInterface $uri Uri * * @since 1.0.0 * @author Dennis Eichhorn */ - public function __construct(L11nManager $l11nManager, UriInterface $uri = null) + public function __construct(Localization $l11n, UriInterface $uri = null) { - $this->l11n = new Localization($l11nManager); + $this->l11n = $l11n; $this->uri = $uri; } diff --git a/Message/Http/Response.php b/Message/Http/Response.php index 83a08f2a3..37b9f10ed 100644 --- a/Message/Http/Response.php +++ b/Message/Http/Response.php @@ -16,7 +16,6 @@ namespace phpOMS\Message\Http; use phpOMS\Contract\RenderableInterface; -use phpOMS\Localization\L11nManager; use phpOMS\Localization\Localization; use phpOMS\Message\ResponseAbstract; use phpOMS\System\MimeType; @@ -38,15 +37,15 @@ class Response extends ResponseAbstract implements RenderableInterface /** * Constructor. * - * @param L11nManager $l11nManager Localization manager + * @param Localization $l11n Localization * * @since 1.0.0 * @author Dennis Eichhorn */ - public function __construct(L11nManager $l11nManager) + public function __construct(Localization $l11n) { $this->header = new Header(); - $this->l11n = new Localization($l11nManager); + $this->l11n = $l11n; } /** diff --git a/Module/ModuleFactory.php b/Module/ModuleFactory.php index 040b4913e..0cb310018 100644 --- a/Module/ModuleFactory.php +++ b/Module/ModuleFactory.php @@ -82,7 +82,7 @@ class ModuleFactory self::registerRequesting($obj); self::registerProvided($obj); } catch (\Exception $e) { - self::$loaded[$module] = new NullModule($app); + throw new \InvalidArgumentException(); } } diff --git a/Module/ModuleManager.php b/Module/ModuleManager.php index 0d5c5444f..be8569594 100644 --- a/Module/ModuleManager.php +++ b/Module/ModuleManager.php @@ -587,18 +587,22 @@ class ModuleManager * * @param string|array $module Module name * - * @throws \InvalidArgumentException + * @throws * * @since 1.0.0 * @author Dennis Eichhorn */ public function initModule($module) { - if (!is_array($module)) { - $module = [$module]; - } + try { + if (!is_array($module)) { + $module = [$module]; + } - $this->initModuleArray($module); + $this->initModuleArray($module); + } catch (\Exception $e) { + throw $e; + } } /** @@ -617,11 +621,7 @@ class ModuleManager try { $this->initModuleController($module); } catch (\InvalidArgumentException $e) { - $this->app->logger->warning(FileLogger::MSG_FULL, [ - 'message' => 'Trying to initialize ' . $module . ' without controller.', - 'line' => $e->getLine(), - 'file' => $e->getFile(), - ]); + throw $e; } } } @@ -635,13 +635,19 @@ class ModuleManager * * @return void * + * @throws + * * @since 1.0.0 * @author Dennis Eichhorn */ private function initModuleController(string $module) { - $this->running[$module] = ModuleFactory::getInstance($module, $this->app); - $this->app->dispatcher->set($this->running[$module], '\Modules\\' . $module . '\\Controller'); + try { + $this->running[$module] = ModuleFactory::getInstance($module, $this->app); + $this->app->dispatcher->set($this->running[$module], '\Modules\\' . $module . '\\Controller'); + } catch (\Exception $e) { + throw $e; + } } /** @@ -651,15 +657,21 @@ class ModuleManager * * @return \phpOMS\Module\ModuleAbstract * + * @throws + * * @since 1.0.0 * @author Dennis Eichhorn */ public function get(string $module) { - if (!isset($this->running[$module])) { - $this->initModule($module); - } + try { + if (!isset($this->running[$module])) { + $this->initModule($module); + } - return $this->running[$module]; + return $this->running[$module]; + } catch (\Exception $e) { + throw $e; + } } } diff --git a/Utils/ArrayUtils.php b/Utils/ArrayUtils.php index 4c42f8873..c1803cb58 100644 --- a/Utils/ArrayUtils.php +++ b/Utils/ArrayUtils.php @@ -61,7 +61,12 @@ class ArrayUtils foreach ($nodes as &$node) { $prevEl = &$el; - $el = &$el[$node]; + + if (!isset($el[$node])) { + break; + } + + $el = &$el[$node]; } if ($prevEl !== null) { diff --git a/Utils/Compression/LZW.php b/Utils/Compression/LZW.php index 2353bf017..f5672ded7 100644 --- a/Utils/Compression/LZW.php +++ b/Utils/Compression/LZW.php @@ -52,14 +52,14 @@ class LZW implements CompressionInterface if (array_key_exists($w . $c, $dictionary)) { $w = $w . $c; } else { - array_push($result, $dictionary[$w]); + $result[] = $dictionary[$w]; $dictionary[$wc] = $dictSize++; $w = (string) $c; } } if ($w !== '') { - array_push($result, $dictionary[$w]); + $result[] = $dictionary[$w]; } return implode(',', $result); @@ -90,7 +90,7 @@ class LZW implements CompressionInterface $entry = $dictionary[$k]; } else { if ($k !== $dictSize) { - return null; + throw new \Exception('Wrong dictionary size!'. $k . '.' . $dictSize); } $entry = $w . $w[0]; diff --git a/Utils/Permutation.php b/Utils/Permutation.php index eacab4375..b60c711df 100644 --- a/Utils/Permutation.php +++ b/Utils/Permutation.php @@ -51,7 +51,7 @@ class Permutation $newres = $result; $newres[] = $val; unset($newArr[$key]); - $permutations += self::permut($newArr, $newres); + $permutations = array_merge($permutations, self::permut($newArr, $newres)); } } @@ -107,7 +107,7 @@ class Permutation */ public static function permutate($toPermute, array $key) { - if (!is_array($toPermute) || !is_string($toPermute)) { + if (!is_array($toPermute) && !is_string($toPermute)) { throw new \InvalidArgumentException('Parameter has to be array or string'); } @@ -119,9 +119,9 @@ class Permutation $i = 0; foreach ($key as $pos) { - $temp = $toPermute[$i]; - $toPermute[$i] = $toPermute[$pos]; - $toPermute[$pos] = $temp; + $temp = $toPermute[$i]; + $toPermute[$i] = $toPermute[$pos - 1]; + $toPermute[$pos - 1] = $temp; $i++; } diff --git a/Utils/StringUtils.php b/Utils/StringUtils.php index cfdb6bb91..02e91e8c9 100644 --- a/Utils/StringUtils.php +++ b/Utils/StringUtils.php @@ -39,6 +39,28 @@ class StringUtils { } + /** + * Contains any string + * + * @param string $haystack Haystack + * @param array $needles Needles + * + * @return bool + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function contains(string $haystack, array $needles) : bool + { + foreach($needles as $needle) { + if(strpos($haystack, $needle) !== false) { + return true; + } + } + + return false; + } + /** * String ends with? * @@ -105,7 +127,7 @@ class StringUtils public static function mb_startsWith(string $haystack, $needles) : bool { if (is_string($needles)) { - self::mb_startsWith($haystack, [$needles]); + $needles = [$needles]; } foreach ($needles as $needle) { @@ -131,7 +153,7 @@ class StringUtils public static function mb_endsWith(string $haystack, $needles) : bool { if (is_string($needles)) { - self::mb_endsWith($haystack, [$needles]); + $needles = [$needles]; } foreach ($needles as $needle) { diff --git a/Validation/Base/Iban.php b/Validation/Base/Iban.php index daa30f336..ef931541b 100644 --- a/Validation/Base/Iban.php +++ b/Validation/Base/Iban.php @@ -37,7 +37,7 @@ abstract class Iban extends ValidatorAbstract * @since 1.0.0 * @author Dennis Eichhorn */ - public function __construct() + private function __construct() { } @@ -51,8 +51,8 @@ abstract class Iban extends ValidatorAbstract */ public static function isValid($value) : bool { - $value = \phpOMS\Datatypes\Iban::normalize($value); - $layout = str_replace(' ', '', IbanEnum::getByName($enumName = 'C_' . strtoupper(substr($value, 0, 2)))); + $value = str_replace(' ', '', strtolower($value)); + $enumName = 'C_' . strtoupper(substr($value, 0, 2)); if (!IbanEnum::isValidName($enumName)) { self::$error = IbanErrorType::INVALID_COUNTRY; @@ -60,6 +60,8 @@ abstract class Iban extends ValidatorAbstract return false; } + $layout = str_replace(' ', '', IbanEnum::getByName($enumName)); + if (strlen($value) !== strlen($layout)) { self::$error = IbanErrorType::INVALID_LENGTH; diff --git a/Validation/ValidatorAbstract.php b/Validation/ValidatorAbstract.php index 4fb7824d0..33bd7bb9f 100644 --- a/Validation/ValidatorAbstract.php +++ b/Validation/ValidatorAbstract.php @@ -52,4 +52,12 @@ abstract class ValidatorAbstract { return self::$msg; } + + /** + * {@inheritdoc} + */ + public static function getErrorCode() : int + { + return self::$error; + } }