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\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 <d.eichhorn@oms.com>
*/
public function setCreatedAt(\DateTime $created)
{
$this->createdAt = $created;
}
/**
* {@inheritdoc}
*/

View File

@ -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 <d.eichhorn@oms.com>
*/
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;
}
/**

View File

@ -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);
}
}

View File

@ -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');
}
/**

View File

@ -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 <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);
}

View File

@ -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';

View File

@ -136,7 +136,7 @@ class Localization
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function __construct(L11nManager $l11nManager)
public function __construct(L11nManager $l11nManager = null)
{
$this->l11nManager = $l11nManager;
}

View File

@ -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 <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;
}

View File

@ -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 <d.eichhorn@oms.com>
*/
public function __construct(L11nManager $l11nManager)
public function __construct(Localization $l11n)
{
$this->header = new Header();
$this->l11n = new Localization($l11nManager);
$this->l11n = $l11n;
}
/**

View File

@ -82,7 +82,7 @@ class ModuleFactory
self::registerRequesting($obj);
self::registerProvided($obj);
} 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
*
* @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;
}
}
}

View File

@ -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) {

View File

@ -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];

View File

@ -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++;
}

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?
*
@ -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) {

View File

@ -37,7 +37,7 @@ abstract class Iban extends ValidatorAbstract
* @since 1.0.0
* @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
{
$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;

View File

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