mirror of
https://github.com/Karaka-Management/phpOMS.git
synced 2026-02-12 14:58:42 +00:00
Unit test fixes
This commit is contained in:
parent
c2905ecf92
commit
a112735b92
|
|
@ -45,9 +45,9 @@ abstract class Enum
|
||||||
*/
|
*/
|
||||||
public static function isValidValue($value) : bool
|
public static function isValidValue($value) : bool
|
||||||
{
|
{
|
||||||
$values = array_values(self::getConstants());
|
$constants = self::getConstants();
|
||||||
|
|
||||||
return in_array($value, $values, true);
|
return in_array($value, $constants, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -76,8 +76,9 @@ abstract class Enum
|
||||||
public static function getRandom()
|
public static function getRandom()
|
||||||
{
|
{
|
||||||
$constants = self::getConstants();
|
$constants = self::getConstants();
|
||||||
|
$keys = array_keys($constants);
|
||||||
|
|
||||||
return $constants[mt_rand(0, count($constants))];
|
return $constants[$keys[mt_rand(0, count($constants) - 1)]];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -71,10 +71,17 @@ class EventManager implements Mediator
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
public function detach(string $group)
|
public function detach(string $group) : bool
|
||||||
{
|
{
|
||||||
unset($this->callbacks[$group]);
|
if(isset($this->callbacks[$group])) {
|
||||||
unset($this->groups[$group]);
|
unset($this->callbacks[$group]);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(isset($this->groups[$group])) {
|
||||||
|
unset($this->groups[$group]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -15,8 +15,6 @@
|
||||||
*/
|
*/
|
||||||
namespace phpOMS\Localization;
|
namespace phpOMS\Localization;
|
||||||
|
|
||||||
use phpOMS\Math\Number\OperationInterface;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Money class.
|
* Money class.
|
||||||
*
|
*
|
||||||
|
|
@ -28,7 +26,7 @@ use phpOMS\Math\Number\OperationInterface;
|
||||||
* @link http://orange-management.com
|
* @link http://orange-management.com
|
||||||
* @since 1.0.0
|
* @since 1.0.0
|
||||||
*/
|
*/
|
||||||
class Money implements \Serializable, OperationInterface
|
class Money implements \Serializable
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -37,15 +35,7 @@ class Money implements \Serializable, OperationInterface
|
||||||
* @var int
|
* @var int
|
||||||
* @since 1.0.0
|
* @since 1.0.0
|
||||||
*/
|
*/
|
||||||
const MAX_DECIMALS = 5;
|
const MAX_DECIMALS = 4;
|
||||||
|
|
||||||
/**
|
|
||||||
* Currency symbol.
|
|
||||||
*
|
|
||||||
* @var string
|
|
||||||
* @since 1.0.0
|
|
||||||
*/
|
|
||||||
private $currency = ISO4217CharEnum::C_USD;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Thousands separator.
|
* Thousands separator.
|
||||||
|
|
@ -74,16 +64,16 @@ class Money implements \Serializable, OperationInterface
|
||||||
/**
|
/**
|
||||||
* Constructor.
|
* Constructor.
|
||||||
*
|
*
|
||||||
* @param string $currency Currency symbol
|
* @param string|int|float $value Value
|
||||||
* @param string $thousands Thousands separator
|
* @param string $thousands Thousands separator
|
||||||
* @param string $decimal Decimal separator
|
* @param string $decimal Decimal separator
|
||||||
*
|
*
|
||||||
* @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(string $currency = ISO4217CharEnum::C_USD, string $thousands = ',', string $decimal = '.')
|
public function __construct($value = 0, string $thousands = ',', string $decimal = '.')
|
||||||
{
|
{
|
||||||
$this->currency = $currency;
|
$this->value = is_int($value) ? $value : self::toInt((string) $value);
|
||||||
$this->thousands = $thousands;
|
$this->thousands = $thousands;
|
||||||
$this->decimal = $decimal;
|
$this->decimal = $decimal;
|
||||||
}
|
}
|
||||||
|
|
@ -93,43 +83,44 @@ class Money implements \Serializable, OperationInterface
|
||||||
*
|
*
|
||||||
* @param string $value Money value
|
* @param string $value Money value
|
||||||
*
|
*
|
||||||
* @return void
|
* @return Money
|
||||||
*
|
*
|
||||||
* @since 1.0.0
|
* @since 1.0.0
|
||||||
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||||
*/
|
*/
|
||||||
public function setString(string $value)
|
public function setString(string $value) : Money
|
||||||
{
|
{
|
||||||
$this->value = self::toInt($value, $this->decimal, $this->thousands);
|
$this->value = self::toInt($value, $this->thousands, $this->decimal);
|
||||||
|
|
||||||
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Money to int.
|
* Money to int.
|
||||||
*
|
*
|
||||||
* @param string $value Money value
|
* @param string $value Money value
|
||||||
* @param string $decimal Decimal character
|
|
||||||
* @param string $thousands Thousands character
|
* @param string $thousands Thousands character
|
||||||
|
* @param string $decimal Decimal character
|
||||||
*
|
*
|
||||||
* @return int
|
* @return int
|
||||||
*
|
*
|
||||||
* @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 toInt(string $value, string $decimal = ',', string $thousands = '.') : int
|
public static function toInt(string $value, string $thousands = ',', string $decimal = '.') : int
|
||||||
{
|
{
|
||||||
$split = explode($value, $decimal);
|
$split = explode($decimal, $value);
|
||||||
|
$left = $split[0];
|
||||||
$left = $split[0];
|
$left = str_replace($thousands, '', $left);
|
||||||
$left = str_replace($thousands, '', $left);
|
|
||||||
|
|
||||||
$right = '';
|
$right = '';
|
||||||
|
|
||||||
if (count($split) > 1) {
|
if (count($split) > 1) {
|
||||||
$right = $split[1];
|
$right = $split[1];
|
||||||
}
|
}
|
||||||
|
|
||||||
$right = substr($right, 0, -self::MAX_DECIMALS);
|
$right = substr($right, 0, self::MAX_DECIMALS);
|
||||||
|
|
||||||
return (int) $left * 10 * self::MAX_DECIMALS + (int) $right;
|
return (int) (((int) $left) * 10 ** self::MAX_DECIMALS + (int) str_pad($right, self::MAX_DECIMALS, '0'));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -144,16 +135,12 @@ class Money implements \Serializable, OperationInterface
|
||||||
*/
|
*/
|
||||||
public function getAmount(int $decimals = 2) : string
|
public function getAmount(int $decimals = 2) : string
|
||||||
{
|
{
|
||||||
if ($decimals > ($dec = constant('\phpOMS\Localization\ISO4217DecimalEnum::C_' . strtoupper($this->currency)))) {
|
|
||||||
$decimals = $dec;
|
|
||||||
}
|
|
||||||
|
|
||||||
$value = (string) round($this->value, -self::MAX_DECIMALS + $decimals);
|
$value = (string) round($this->value, -self::MAX_DECIMALS + $decimals);
|
||||||
|
|
||||||
$left = substr($value, 0, -self::MAX_DECIMALS);
|
$left = substr($value, 0, -self::MAX_DECIMALS);
|
||||||
$right = substr($value, -self::MAX_DECIMALS);
|
$right = substr($value, -self::MAX_DECIMALS);
|
||||||
|
|
||||||
return ($decimals > 0) ? number_format($left, 0, $this->thousands, $this->decimal) . $this->decimal . $right : (string) $left;
|
return ($decimals > 0) ? number_format($left, 0, $this->decimal, $this->thousands) . $this->decimal . substr($right, 0, $decimals) : (string) $left;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -169,7 +156,7 @@ class Money implements \Serializable, OperationInterface
|
||||||
public function add($value) : Money
|
public function add($value) : Money
|
||||||
{
|
{
|
||||||
if (is_string($value) || is_float($value)) {
|
if (is_string($value) || is_float($value)) {
|
||||||
$this->value += self::toInt((string) $value, $this->decimal, $this->thousands);
|
$this->value += self::toInt((string) $value, $this->thousands, $this->decimal);
|
||||||
} elseif (is_int($value)) {
|
} elseif (is_int($value)) {
|
||||||
$this->value += $value;
|
$this->value += $value;
|
||||||
} elseif ($value instanceof Money) {
|
} elseif ($value instanceof Money) {
|
||||||
|
|
@ -205,7 +192,7 @@ class Money implements \Serializable, OperationInterface
|
||||||
public function sub($value) : Money
|
public function sub($value) : Money
|
||||||
{
|
{
|
||||||
if (is_string($value) || is_float($value)) {
|
if (is_string($value) || is_float($value)) {
|
||||||
$this->value -= self::toInt((string) $value, $this->decimal, $this->thousands);
|
$this->value -= self::toInt((string) $value, $this->thousands, $this->decimal);
|
||||||
} elseif (is_int($value)) {
|
} elseif (is_int($value)) {
|
||||||
$this->value -= $value;
|
$this->value -= $value;
|
||||||
} elseif ($value instanceof Money) {
|
} elseif ($value instanceof Money) {
|
||||||
|
|
@ -228,7 +215,7 @@ class Money implements \Serializable, OperationInterface
|
||||||
public function mult($value) : Money
|
public function mult($value) : Money
|
||||||
{
|
{
|
||||||
if (is_float($value) || is_int($value)) {
|
if (is_float($value) || is_int($value)) {
|
||||||
$this->value *= $value;
|
$this->value = (int) ($this->value * $value);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
|
|
@ -247,7 +234,7 @@ class Money implements \Serializable, OperationInterface
|
||||||
public function div($value) : Money
|
public function div($value) : Money
|
||||||
{
|
{
|
||||||
if (is_float($value) || is_int($value)) {
|
if (is_float($value) || is_int($value)) {
|
||||||
$this->value = self::toInt((string) ($this->value / $value), $this->decimal, $this->thousands);
|
$this->value = (int) ($this->value / $value);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
|
|
@ -320,13 +307,15 @@ class Money implements \Serializable, OperationInterface
|
||||||
*
|
*
|
||||||
* @param int $value Value
|
* @param int $value Value
|
||||||
*
|
*
|
||||||
* @return void
|
* @return Money
|
||||||
*
|
*
|
||||||
* @since 1.0.0
|
* @since 1.0.0
|
||||||
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||||
*/
|
*/
|
||||||
public function setInt(int $value)
|
public function setInt(int $value) : Money
|
||||||
{
|
{
|
||||||
$this->value = $value;
|
$this->value = $value;
|
||||||
|
|
||||||
|
return $this;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -57,18 +57,6 @@ interface Mediator extends \Countable
|
||||||
*/
|
*/
|
||||||
public function detach(string $group) : bool;
|
public function detach(string $group) : bool;
|
||||||
|
|
||||||
/**
|
|
||||||
* Has outstanding.
|
|
||||||
*
|
|
||||||
* @param string $group Group
|
|
||||||
*
|
|
||||||
* @return bool
|
|
||||||
*
|
|
||||||
* @since 1.0.0
|
|
||||||
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
|
||||||
*/
|
|
||||||
private function hasOutstanding(string $group) : bool;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add group.
|
* Add group.
|
||||||
*
|
*
|
||||||
|
|
|
||||||
|
|
@ -53,7 +53,6 @@ class BitcoinValidator extends ValidatorAbstract
|
||||||
return true;
|
return true;
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
self::$msg = $e->getMessage();
|
self::$msg = $e->getMessage();
|
||||||
} finally {
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -113,7 +113,7 @@ final class Validator extends ValidatorAbstract
|
||||||
*/
|
*/
|
||||||
public static function contains(string $var, $substr)
|
public static function contains(string $var, $substr)
|
||||||
{
|
{
|
||||||
return StringUtils::contains($var, $substr);
|
return is_string($substr) ? strpos($var, $substr) : StringUtils::contains($var, $substr);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -365,11 +365,15 @@ class View implements \Serializable
|
||||||
{
|
{
|
||||||
$viewArray = [];
|
$viewArray = [];
|
||||||
|
|
||||||
$viewArray[] = $this->render();
|
if($this->template === '') {
|
||||||
|
$viewArray[] = $this->render();
|
||||||
|
}
|
||||||
|
|
||||||
foreach ($this->views as $key => $view) {
|
foreach ($this->views as $key => $view) {
|
||||||
$viewArray[$key] = $view->toArray();
|
$viewArray[$key] = $view->toArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return $viewArray;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user