From 2e3c475a8fe1c422a38e00adce219b3426abcc7a Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Sun, 24 Apr 2016 20:48:51 +0200 Subject: [PATCH] Unit test fixes --- DataStorage/Database/DataMapperAbstract.php | 16 +++++--------- Localization/ISO4217DecimalEnum.php | 1 - Localization/ISO4217NumEnum.php | 3 +-- Localization/Money.php | 24 ++++++++++----------- Log/FileLogger.php | 2 +- Stdlib/Map/MultiMap.php | 8 ++++--- 6 files changed, 24 insertions(+), 30 deletions(-) diff --git a/DataStorage/Database/DataMapperAbstract.php b/DataStorage/Database/DataMapperAbstract.php index 603f121ba..3cd267728 100644 --- a/DataStorage/Database/DataMapperAbstract.php +++ b/DataStorage/Database/DataMapperAbstract.php @@ -303,7 +303,6 @@ abstract class DataMapperAbstract implements DataMapperInterface */ public function create($obj, bool $relations = true) { - // todo: create should also have an option to create relations (default = true) $query = new Builder($this->db); $query->prefix($this->db->getPrefix()) ->into(static::$table); @@ -344,7 +343,7 @@ abstract class DataMapperAbstract implements DataMapperInterface $primaryKey = $mapper->create($property->getValue($obj)); } - $property->setValue($obj, $primaryKey); + //$property->setValue($obj, $primaryKey); } if ($column['internal'] === $pname) { @@ -362,6 +361,8 @@ abstract class DataMapperAbstract implements DataMapperInterface $value = isset($value) ? json_encode($value) : ''; } elseif ($column['type'] === 'Serializable') { $value = $value->serialize(); + } elseif(is_object($value)) { + $value = $value->getId(); } $query->insert($column['name']) @@ -453,7 +454,7 @@ abstract class DataMapperAbstract implements DataMapperInterface } } - $reflectionProperty = $reflectionClass->getProperty(static::$primaryField['internal']); + $reflectionProperty = $reflectionClass->getProperty(static::$columns[static::$primaryField]['internal']); // todo: can't i just set it accessible anyways and not set it to private afterwards? if (!($accessible = $reflectionProperty->isPublic())) { @@ -824,17 +825,13 @@ abstract class DataMapperAbstract implements DataMapperInterface */ public function getNewest(int $limit = 1, Builder $query = null, int $relations = RelationType::ALL) { - if (!isset(static::$createdAt) || !isset(static::$columns[static::$createdAt])) { - throw new \BadMethodCallException('Method "' . __METHOD__ . '" is not supported.'); - } - $query = $query ?? new Builder($this->db); $query->prefix($this->db->getPrefix()) ->select('*') ->from(static::$table) ->limit($limit); /* todo: limit is not working, setting this to 2 doesn't have any effect!!! */ - if (isset(static::$createdAt)) { + if (!empty(static::$createdAt)) { $query->orderBy(static::$table . '.' . static::$columns[static::$createdAt]['name'], 'DESC'); } else { $query->orderBy(static::$table . '.' . static::$columns[static::$primaryField]['name'], 'DESC'); @@ -844,9 +841,6 @@ abstract class DataMapperAbstract implements DataMapperInterface $sth->execute(); $results = $sth->fetchAll(\PDO::FETCH_ASSOC); - - /* todo: if limit get's used this has to call populateIterable */ - $obj = $this->populateIterable(is_bool($results) ? [] : $results); $this->fillRelations($obj, $relations); diff --git a/Localization/ISO4217DecimalEnum.php b/Localization/ISO4217DecimalEnum.php index 38190a0b8..14d752bc1 100644 --- a/Localization/ISO4217DecimalEnum.php +++ b/Localization/ISO4217DecimalEnum.php @@ -1,4 +1,3 @@ - value = self::toInt($value, $this->decimal); + $this->value = self::toInt($value, $this->decimal, $this->thousands); } /** @@ -134,27 +134,27 @@ class Money implements \Serializable * * @param string $value Money value * @param string $decimal Decimal character + * @param string $thousands Thousands character * * @return int * * @since 1.0.0 * @author Dennis Eichhorn */ - public static function toInt(string $value, string $decimal = ',') : int + public static function toInt(string $value, string $decimal = ',', string $thousands = '.') : int { $split = explode($value, $decimal); - $left = ''; $left = $split[0]; - $left = str_replace($this->thousands, '', $left); + $left = str_replace($thousands, '', $left); - $rigth = ''; + $right = ''; if(count($split) > 1) { $right = $split[1]; } $right = substr($right, 0, -self::MAX_DECIMALS); - $this->value = (int) $left * 100000 + (int) $right; + return (int) $left * 100000 + (int) $right; } /** @@ -169,16 +169,16 @@ class Money implements \Serializable */ public function getAmount(int $decimals = 2) : string { - if($decimals > ($dec = ISO4217DecimalEnum::${'C_' . strtoupper($this->currency)})) { + if($decimals > ($dec = constant('\phpOMS\Localization\ISO4217DecimalEnum::C_' . strtoupper($this->currency)))) { $decimals = $dec ; } - $value = (string) round($value, - self::MAX_DECIMALS + $this->decimals); + $value = (string) round($this->value, - self::MAX_DECIMALS + $decimals); $left = substr($value, 0, -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->thousands, $this->decimal) . $this->decimal . $right : (string) $left; } /** @@ -194,7 +194,7 @@ class Money implements \Serializable public function add($value) { if(is_string($value) || is_float($value)) { - $this->value += self::toInt((string) $value); + $this->value += self::toInt((string) $value, $this->decimal, $this->thousands); } elseif(is_int($value)) { $this->value += $value; } elseif($value instanceof Money) { @@ -215,7 +215,7 @@ class Money implements \Serializable public function sub($value) { if(is_string($value) || is_float($value)) { - $this->value -= self::toInt((string) $value); + $this->value -= self::toInt((string) $value, $this->decimal, $this->thousands); } elseif(is_int($value)) { $this->value -= $value; } elseif($value instanceof Money) { @@ -253,7 +253,7 @@ class Money implements \Serializable public function div($value) { if(is_float($value) || is_int($value)) { - $this->value = self::toInt((string) ($this->value / $value)); + $this->value = self::toInt((string) ($this->value / $value), $this->decimal, $this->thousands); } } diff --git a/Log/FileLogger.php b/Log/FileLogger.php index 344bddfeb..322a7e274 100644 --- a/Log/FileLogger.php +++ b/Log/FileLogger.php @@ -146,7 +146,7 @@ class FileLogger implements LoggerInterface */ public function __destruct() { - if ($this->fp !== false) { + if (is_resource($this->fp)) { fclose($this->fp); } } diff --git a/Stdlib/Map/MultiMap.php b/Stdlib/Map/MultiMap.php index 650c29ba5..9467c6150 100644 --- a/Stdlib/Map/MultiMap.php +++ b/Stdlib/Map/MultiMap.php @@ -94,7 +94,7 @@ class MultiMap implements \Countable $id = count($this->values); $inserted = false; - if ($this->keyType !== KeyType::MULTIPLE) { + if ($this->keyType !== KeyType::SINGLE) { $keys = [implode($keys, ':')]; } @@ -494,6 +494,8 @@ class MultiMap implements \Countable } } } + + return $siblings; } /** @@ -510,9 +512,9 @@ class MultiMap implements \Countable { if ($this->orderType === OrderType::LOOSE) { return Permutation::permut($key); - } else { - return []; } + + return []; } /**