Test fixes

This commit is contained in:
Dennis Eichhorn 2016-07-23 18:17:25 +02:00
parent 500bf7fb18
commit 38350e9c77
17 changed files with 107 additions and 73 deletions

View File

@ -17,6 +17,7 @@ namespace phpOMS\Account;
use phpOMS\Contract\ArrayableInterface; use phpOMS\Contract\ArrayableInterface;
use phpOMS\Localization\Localization; use phpOMS\Localization\Localization;
use phpOMS\Localization\NullLocalization;
use phpOMS\Validation\Base\Email; use phpOMS\Validation\Base\Email;
/** /**
@ -159,6 +160,7 @@ class Account implements ArrayableInterface, \JsonSerializable
{ {
$this->createdAt = new \DateTime('now'); $this->createdAt = new \DateTime('now');
$this->id = $id; $this->id = $id;
$this->l11n = new NullLocalization();
} }
/** /**
@ -425,21 +427,6 @@ class Account implements ArrayableInterface, \JsonSerializable
$this->lastActive = new \DateTime('NOW'); $this->lastActive = new \DateTime('NOW');
} }
/**
* Set created at.
*
* @param \DateTime $created Created at
*
* @return void
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function setCreatedAt(\DateTime $created)
{
$this->createdAt = $created;
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */

View File

@ -130,24 +130,24 @@ class AccountManager implements \Countable
} }
/** /**
* Set account. * Add account.
* *
* @param Account $account Account * @param Account $account Account
* *
* @return int Account id * @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 function set(Account $account) public function add(Account $account) : bool
{ {
if (!isset($this->accounts[$account->getId()])) { if (!isset($this->accounts[$account->getId()])) {
$this->accounts[$account->getId()] = $account; $this->accounts[$account->getId()] = $account;
return $account->getId(); return true;
} }
return null; return false;
} }
/** /**

View File

@ -95,10 +95,10 @@ abstract class Enum
public static function getByName(string $name) public static function getByName(string $name)
{ {
if (!self::isValidName($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 public static function isValidName(string $name) : bool
{ {
return defined($name); return defined('static::' . $name);
} }
} }

View File

@ -65,11 +65,11 @@ class Iban implements \Serializable
*/ */
private function parse(string $iban) 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'); throw new \InvalidArgumentException('Invalid IBAN');
} }
$this->iban = self::normalize($iban);
} }
/** /**
@ -135,7 +135,7 @@ class Iban implements \Serializable
return ''; 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 public function getAccount() : string
{ {
return $this->getSequence('n'); return $this->getSequence('c');
} }
/** /**

View File

@ -165,12 +165,12 @@ class Dispatcher
* @param ResponseAbstract $response Response * @param ResponseAbstract $response Response
* @param mixed $data Data * @param mixed $data Data
* *
* @return array * @return mixed
* *
* @since 1.0.0 * @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com> * @author Dennis Eichhorn <d.eichhorn@oms.com>
*/ */
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); return $controller($this->app, $request, $response, $data);
} }

View File

@ -15,7 +15,7 @@
*/ */
namespace phpOMS\Localization; namespace phpOMS\Localization;
use phpOMS\Datatypes\EnumArray; use phpOMS\Datatypes\Enum;
/** /**
* Country codes ISO list. * Country codes ISO list.
@ -28,7 +28,7 @@ use phpOMS\Datatypes\EnumArray;
* @link http://orange-management.com * @link http://orange-management.com
* @since 1.0.0 * @since 1.0.0
*/ */
class ISO3166TwoEnum extends EnumArray class ISO3166TwoEnum extends Enum
{ {
const C_AFG = 'AF'; const C_AFG = 'AF';
const C_ALA = 'AX'; const C_ALA = 'AX';

View File

@ -136,7 +136,7 @@ class Localization
* @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(L11nManager $l11nManager) public function __construct(L11nManager $l11nManager = null)
{ {
$this->l11nManager = $l11nManager; $this->l11nManager = $l11nManager;
} }

View File

@ -15,7 +15,6 @@
*/ */
namespace phpOMS\Message\Http; namespace phpOMS\Message\Http;
use phpOMS\Localization\L11nManager;
use phpOMS\Localization\Localization; use phpOMS\Localization\Localization;
use phpOMS\Message\RequestAbstract; use phpOMS\Message\RequestAbstract;
use phpOMS\Router\RouteVerb; use phpOMS\Router\RouteVerb;
@ -83,15 +82,15 @@ class Request extends RequestAbstract
/** /**
* Constructor. * Constructor.
* *
* @param L11nManager $l11nManager Localization manager * @param Localization $l11n Localization
* @param UriInterface $uri Uri * @param UriInterface $uri Uri
* *
* @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(L11nManager $l11nManager, UriInterface $uri = null) public function __construct(Localization $l11n, UriInterface $uri = null)
{ {
$this->l11n = new Localization($l11nManager); $this->l11n = $l11n;
$this->uri = $uri; $this->uri = $uri;
} }

View File

@ -16,7 +16,6 @@
namespace phpOMS\Message\Http; namespace phpOMS\Message\Http;
use phpOMS\Contract\RenderableInterface; use phpOMS\Contract\RenderableInterface;
use phpOMS\Localization\L11nManager;
use phpOMS\Localization\Localization; use phpOMS\Localization\Localization;
use phpOMS\Message\ResponseAbstract; use phpOMS\Message\ResponseAbstract;
use phpOMS\System\MimeType; use phpOMS\System\MimeType;
@ -38,15 +37,15 @@ class Response extends ResponseAbstract implements RenderableInterface
/** /**
* Constructor. * Constructor.
* *
* @param L11nManager $l11nManager Localization manager * @param Localization $l11n Localization
* *
* @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(L11nManager $l11nManager) public function __construct(Localization $l11n)
{ {
$this->header = new Header(); $this->header = new Header();
$this->l11n = new Localization($l11nManager); $this->l11n = $l11n;
} }
/** /**

View File

@ -82,7 +82,7 @@ class ModuleFactory
self::registerRequesting($obj); self::registerRequesting($obj);
self::registerProvided($obj); self::registerProvided($obj);
} catch (\Exception $e) { } catch (\Exception $e) {
self::$loaded[$module] = new NullModule($app); throw new \InvalidArgumentException();
} }
} }

View File

@ -587,18 +587,22 @@ class ModuleManager
* *
* @param string|array $module Module name * @param string|array $module Module name
* *
* @throws \InvalidArgumentException * @throws
* *
* @since 1.0.0 * @since 1.0.0
* @author Dennis Eichhorn * @author Dennis Eichhorn
*/ */
public function initModule($module) public function initModule($module)
{ {
if (!is_array($module)) { try {
$module = [$module]; if (!is_array($module)) {
} $module = [$module];
}
$this->initModuleArray($module); $this->initModuleArray($module);
} catch (\Exception $e) {
throw $e;
}
} }
/** /**
@ -617,11 +621,7 @@ class ModuleManager
try { try {
$this->initModuleController($module); $this->initModuleController($module);
} catch (\InvalidArgumentException $e) { } catch (\InvalidArgumentException $e) {
$this->app->logger->warning(FileLogger::MSG_FULL, [ throw $e;
'message' => 'Trying to initialize ' . $module . ' without controller.',
'line' => $e->getLine(),
'file' => $e->getFile(),
]);
} }
} }
} }
@ -635,13 +635,19 @@ class ModuleManager
* *
* @return void * @return void
* *
* @throws
*
* @since 1.0.0 * @since 1.0.0
* @author Dennis Eichhorn * @author Dennis Eichhorn
*/ */
private function initModuleController(string $module) private function initModuleController(string $module)
{ {
$this->running[$module] = ModuleFactory::getInstance($module, $this->app); try {
$this->app->dispatcher->set($this->running[$module], '\Modules\\' . $module . '\\Controller'); $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 * @return \phpOMS\Module\ModuleAbstract
* *
* @throws
*
* @since 1.0.0 * @since 1.0.0
* @author Dennis Eichhorn * @author Dennis Eichhorn
*/ */
public function get(string $module) public function get(string $module)
{ {
if (!isset($this->running[$module])) { try {
$this->initModule($module); if (!isset($this->running[$module])) {
} $this->initModule($module);
}
return $this->running[$module]; return $this->running[$module];
} catch (\Exception $e) {
throw $e;
}
} }
} }

View File

@ -61,7 +61,12 @@ class ArrayUtils
foreach ($nodes as &$node) { foreach ($nodes as &$node) {
$prevEl = &$el; $prevEl = &$el;
$el = &$el[$node];
if (!isset($el[$node])) {
break;
}
$el = &$el[$node];
} }
if ($prevEl !== null) { if ($prevEl !== null) {

View File

@ -52,14 +52,14 @@ class LZW implements CompressionInterface
if (array_key_exists($w . $c, $dictionary)) { if (array_key_exists($w . $c, $dictionary)) {
$w = $w . $c; $w = $w . $c;
} else { } else {
array_push($result, $dictionary[$w]); $result[] = $dictionary[$w];
$dictionary[$wc] = $dictSize++; $dictionary[$wc] = $dictSize++;
$w = (string) $c; $w = (string) $c;
} }
} }
if ($w !== '') { if ($w !== '') {
array_push($result, $dictionary[$w]); $result[] = $dictionary[$w];
} }
return implode(',', $result); return implode(',', $result);
@ -90,7 +90,7 @@ class LZW implements CompressionInterface
$entry = $dictionary[$k]; $entry = $dictionary[$k];
} else { } else {
if ($k !== $dictSize) { if ($k !== $dictSize) {
return null; throw new \Exception('Wrong dictionary size!'. $k . '.' . $dictSize);
} }
$entry = $w . $w[0]; $entry = $w . $w[0];

View File

@ -51,7 +51,7 @@ class Permutation
$newres = $result; $newres = $result;
$newres[] = $val; $newres[] = $val;
unset($newArr[$key]); 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) 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'); throw new \InvalidArgumentException('Parameter has to be array or string');
} }
@ -119,9 +119,9 @@ class Permutation
$i = 0; $i = 0;
foreach ($key as $pos) { foreach ($key as $pos) {
$temp = $toPermute[$i]; $temp = $toPermute[$i];
$toPermute[$i] = $toPermute[$pos]; $toPermute[$i] = $toPermute[$pos - 1];
$toPermute[$pos] = $temp; $toPermute[$pos - 1] = $temp;
$i++; $i++;
} }

View File

@ -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? * String ends with?
* *
@ -105,7 +127,7 @@ class StringUtils
public static function mb_startsWith(string $haystack, $needles) : bool public static function mb_startsWith(string $haystack, $needles) : bool
{ {
if (is_string($needles)) { if (is_string($needles)) {
self::mb_startsWith($haystack, [$needles]); $needles = [$needles];
} }
foreach ($needles as $needle) { foreach ($needles as $needle) {
@ -131,7 +153,7 @@ class StringUtils
public static function mb_endsWith(string $haystack, $needles) : bool public static function mb_endsWith(string $haystack, $needles) : bool
{ {
if (is_string($needles)) { if (is_string($needles)) {
self::mb_endsWith($haystack, [$needles]); $needles = [$needles];
} }
foreach ($needles as $needle) { foreach ($needles as $needle) {

View File

@ -37,7 +37,7 @@ abstract class Iban extends ValidatorAbstract
* @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() private function __construct()
{ {
} }
@ -51,8 +51,8 @@ abstract class Iban extends ValidatorAbstract
*/ */
public static function isValid($value) : bool public static function isValid($value) : bool
{ {
$value = \phpOMS\Datatypes\Iban::normalize($value); $value = str_replace(' ', '', strtolower($value));
$layout = str_replace(' ', '', IbanEnum::getByName($enumName = 'C_' . strtoupper(substr($value, 0, 2)))); $enumName = 'C_' . strtoupper(substr($value, 0, 2));
if (!IbanEnum::isValidName($enumName)) { if (!IbanEnum::isValidName($enumName)) {
self::$error = IbanErrorType::INVALID_COUNTRY; self::$error = IbanErrorType::INVALID_COUNTRY;
@ -60,6 +60,8 @@ abstract class Iban extends ValidatorAbstract
return false; return false;
} }
$layout = str_replace(' ', '', IbanEnum::getByName($enumName));
if (strlen($value) !== strlen($layout)) { if (strlen($value) !== strlen($layout)) {
self::$error = IbanErrorType::INVALID_LENGTH; self::$error = IbanErrorType::INVALID_LENGTH;

View File

@ -52,4 +52,12 @@ abstract class ValidatorAbstract
{ {
return self::$msg; return self::$msg;
} }
/**
* {@inheritdoc}
*/
public static function getErrorCode() : int
{
return self::$error;
}
} }