Fixing test errors

This commit is contained in:
Dennis Eichhorn 2016-08-25 21:33:20 +02:00
parent 4391fd2056
commit 2b4d9ae7a5
6 changed files with 116 additions and 94 deletions

View File

@ -378,9 +378,12 @@ class DataMapperAbstract implements DataMapperInterface
} }
foreach (static::$columns as $key => $column) { foreach (static::$columns as $key => $column) {
if (isset(static::$ownsOne[$propertyName])) { if (isset(static::$ownsOne[$propertyName]) && $column['internal'] === $propertyName) {
self::createOwnsOne($propertyName, $property, $obj); $id = self::createOwnsOne($propertyName, $property, $obj);
} elseif ($column['internal'] === $propertyName) { $value = self::parseValue($column['type'], $id);
$query->insert($column['name'])->value($value, $column['type']);
} elseif ($column['internal'] === $propertyName && $column['type'] !== static::$primaryField) {
$value = self::parseValue($column['type'], $property->getValue($obj)); $value = self::parseValue($column['type'], $property->getValue($obj));
$query->insert($column['name'])->value($value, $column['type']); $query->insert($column['name'])->value($value, $column['type']);
@ -509,7 +512,7 @@ class DataMapperAbstract implements DataMapperInterface
} }
} }
private static function createHasOne() private static function createHasOne(\ReflectionClass $reflectionClass, $obj)
{ {
foreach (static::$hasOne as $propertyName => $rel) { foreach (static::$hasOne as $propertyName => $rel) {
@ -599,16 +602,26 @@ class DataMapperAbstract implements DataMapperInterface
*/ */
private static function parseValue(string $type, $value) private static function parseValue(string $type, $value)
{ {
if ($type === 'DateTime') { if (is_null($value)) {
return isset($value) ? $value->format('Y-m-d H:i:s') : null; return null;
} elseif ($type === 'DateTime') {
return $value->format('Y-m-d H:i:s');
} elseif ($type === 'json') { } elseif ($type === 'json') {
return isset($value) ? json_encode($value) : ''; return json_encode($value);
} elseif ($type === 'Serializable') { } elseif ($type === 'Serializable') {
return $value->serialize(); return $value->serialize();
} elseif ($type === 'jsonSerializable') { } elseif ($type === 'jsonSerializable') {
return $value->jsonSerializable(); return $value->jsonSerializable();
} elseif (is_object($value)) { } elseif (is_object($value)) {
return $value->getId(); return $value->getId();
} elseif ($type === 'int') {
return (int) $value;
} elseif ($type === 'string') {
return (string) $value;
} elseif ($type === 'float') {
return (float) $value;
} elseif ($type === 'bool') {
return (bool) $value;
} }
return $value; return $value;
@ -752,20 +765,8 @@ class DataMapperAbstract implements DataMapperInterface
$reflectionProperty->setAccessible(true); $reflectionProperty->setAccessible(true);
} }
// relation table vs relation defined in same table as object e.g. comments $objects = $mapper::get($values);
if ($values !== false) { $reflectionProperty->setValue($obj, $objects);
$objects = $mapper::get($values);
$reflectionProperty->setValue($obj, $objects);
} else {
// todo: replace getId() with lookup by primaryKey and the assoziated member variable and get value
$query = $mapper::find()->where(static::$hasMany[$member]['table'] . '.' . static::$hasMany[$member]['dst'], '=', $obj->getId());
$sth = self::$db->con->prepare($query->toSql());
$sth->execute();
$results = $sth->fetchAll(\PDO::FETCH_ASSOC);
$objects = $mapper::populateIterable($results);
$reflectionProperty->setValue($obj, $objects);
}
if (!$accessible) { if (!$accessible) {
$reflectionProperty->setAccessible(false); $reflectionProperty->setAccessible(false);
@ -781,10 +782,12 @@ class DataMapperAbstract implements DataMapperInterface
* *
* @return mixed * @return mixed
* *
* @todo accept reflection class as parameter
*
* @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 populateOneToOne(&$obj) public static function populateHasOne(&$obj)
{ {
$reflectionClass = new \ReflectionClass(get_class($obj)); $reflectionClass = new \ReflectionClass(get_class($obj));
@ -810,6 +813,44 @@ class DataMapperAbstract implements DataMapperInterface
} }
} }
/**
* Populate data.
*
* @param mixed $obj Object to add the relations to
*
* @return mixed
*
* @todo accept reflection class as parameter
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public static function populateOwnsOne(&$obj)
{
$reflectionClass = new \ReflectionClass(get_class($obj));
foreach (static::$ownsOne as $member => $one) {
// todo: is that if necessary? performance is suffering for sure!
if ($reflectionClass->hasProperty($member)) {
$reflectionProperty = $reflectionClass->getProperty($member);
if (!($accessible = $reflectionProperty->isPublic())) {
$reflectionProperty->setAccessible(true);
}
/** @var DataMapperAbstract $mapper */
$mapper = static::$ownsOne[$member]['mapper'];
$value = $mapper::get($reflectionProperty->getValue($obj));
$reflectionProperty->setValue($obj, $value);
if (!$accessible) {
$reflectionProperty->setAccessible(false);
}
}
}
}
/** /**
* Populate data. * Populate data.
* *
@ -1043,7 +1084,7 @@ class DataMapperAbstract implements DataMapperInterface
$hasOne = count(static::$hasOne) > 0; $hasOne = count(static::$hasOne) > 0;
$ownsOne = count(static::$ownsOne) > 0; $ownsOne = count(static::$ownsOne) > 0;
if ($relations !== RelationType::NONE && ($hasMany || $hasOne)) { if ($relations !== RelationType::NONE && ($hasMany || $hasOne || $ownsOne)) {
foreach ($obj as $key => $value) { foreach ($obj as $key => $value) {
/* loading relations from relations table and populating them and then adding them to the object */ /* loading relations from relations table and populating them and then adding them to the object */
if ($relations !== RelationType::NONE) { if ($relations !== RelationType::NONE) {
@ -1052,7 +1093,11 @@ class DataMapperAbstract implements DataMapperInterface
} }
if ($hasOne) { if ($hasOne) {
self::populateOneToOne($obj[$key]); self::populateHasOne($obj[$key]);
}
if ($ownsOne) {
self::populateOwnsOne($obj[$key]);
} }
} }
} }
@ -1117,41 +1162,39 @@ class DataMapperAbstract implements DataMapperInterface
$result = []; $result = [];
foreach (static::$hasMany as $member => $value) { foreach (static::$hasMany as $member => $value) {
if (!isset($value['relationmapper'])) { $query = new Builder(self::$db);
$query = new Builder(self::$db); $query->prefix(self::$db->getPrefix());
$query->prefix(self::$db->getPrefix());
if ($relations === RelationType::ALL) { if ($relations === RelationType::ALL) {
$query->select($value['table'] . '.' . $value['src']) $src = $value['src'] ?? $value['mapper']::$primaryField;
->from($value['table'])
->where($value['table'] . '.' . $value['dst'], '=', $primaryKey);
} elseif ($relations === RelationType::NEWEST) {
/* $query->select($value['table'] . '.' . $src)
SELECT c.*, p1.* ->from($value['table'])
FROM customer c ->where($value['table'] . '.' . $value['dst'], '=', $primaryKey);
JOIN purchase p1 ON (c.id = p1.customer_id) } elseif ($relations === RelationType::NEWEST) {
LEFT OUTER JOIN purchase p2 ON (c.id = p2.customer_id AND
(p1.date < p2.date OR p1.date = p2.date AND p1.id < p2.id))
WHERE p2.id IS NULL;
*/
/*
$query->select(static::$table . '.' . static::$primaryField, $value['table'] . '.' . $value['src'])
->from(static::$table)
->join($value['table'])
->on(static::$table . '.' . static::$primaryField, '=', $value['table'] . '.' . $value['dst'])
->leftOuterJoin($value['table'])
->on(new And('1', new And(new Or('d1', 'd2'), 'id')))
->where($value['table'] . '.' . $value['dst'], '=', 'NULL');
*/
}
$sth = self::$db->con->prepare($query->toSql()); /*
$sth->execute(); SELECT c.*, p1.*
$result[$member] = $sth->fetchAll(\PDO::FETCH_COLUMN); FROM customer c
} else { JOIN purchase p1 ON (c.id = p1.customer_id)
$result[$member] = false; LEFT OUTER JOIN purchase p2 ON (c.id = p2.customer_id AND
(p1.date < p2.date OR p1.date = p2.date AND p1.id < p2.id))
WHERE p2.id IS NULL;
*/
/*
$query->select(static::$table . '.' . static::$primaryField, $value['table'] . '.' . $value['src'])
->from(static::$table)
->join($value['table'])
->on(static::$table . '.' . static::$primaryField, '=', $value['table'] . '.' . $value['dst'])
->leftOuterJoin($value['table'])
->on(new And('1', new And(new Or('d1', 'd2'), 'id')))
->where($value['table'] . '.' . $value['dst'], '=', 'NULL');
*/
} }
$sth = self::$db->con->prepare($query->toSql());
$sth->execute();
$result[$member] = $sth->fetchAll(\PDO::FETCH_COLUMN);
} }
return $result; return $result;

View File

@ -105,7 +105,7 @@ class FileLogger implements LoggerInterface
if (is_dir($lpath) || strpos($lpath, '.') === false) { if (is_dir($lpath) || strpos($lpath, '.') === false) {
File::create($path = $path . '/' . date('Y-m-d') . '.log'); File::create($path = $path . '/' . date('Y-m-d') . '.log');
} else { } else {
File::create($lpath); File::create($path = $lpath);
} }
$this->path = $path; $this->path = $path;

View File

@ -52,7 +52,7 @@ class FinanceFormulas
/** /**
* Annual Percentage Yield * Annual Percentage Yield
* *
* @latex r = \left(APY + 1\right)^{\frac{1}{n}} \cdot n - 1 * @latex r = \left(\left(APY + 1\right)^{\frac{1}{n}} - 1\right) \cdot n
* *
* @param float $apy Annual percentage yield * @param float $apy Annual percentage yield
* @param int $n Number of times compounded * @param int $n Number of times compounded
@ -64,7 +64,7 @@ class FinanceFormulas
*/ */
public static function getStateAnnualInterestRateOfAPY(float $apy, int $n) : float public static function getStateAnnualInterestRateOfAPY(float $apy, int $n) : float
{ {
return pow($apy + 1, 1 / $n) * $n - 1; return (pow($apy + 1, 1 / $n) - 1) * $n;
} }
/** /**

View File

@ -145,7 +145,7 @@ class Prime
$number = next($primes); $number = next($primes);
} }
return $primes; return array_values($primes);
} }
/** /**

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) {
throw new \InvalidArgumentException('Module "' . $module . '" is not loaded.'); self::$loaded[$module] = new NullModule($app);
} }
} }

View File

@ -245,16 +245,15 @@ class ModuleManager
$c = count($files); $c = count($files);
for ($i = 0; $i < $c; $i++) { for ($i = 0; $i < $c; $i++) {
$path = realpath($oldPath = self::MODULE_PATH . '/' . $files[$i] . '/info.json'); $path = self::MODULE_PATH . '/' . $files[$i] . '/info.json';
if (file_exists($path)) { if (!file_exists($path)) {
if (strpos($path, self::MODULE_PATH) === false) { continue;
throw new PathException($oldPath); // throw new PathException($path);
}
$json = json_decode(file_get_contents($path), true);
self::$all[$json['name']['internal']] = $json;
} }
$json = json_decode(file_get_contents($path), true);
self::$all[$json['name']['internal']] = $json;
} }
} }
@ -388,8 +387,9 @@ class ModuleManager
return false; return false;
} }
if (!file_exists(self::MODULE_PATH . '/' . $module . '/Admin/Install.php')) { if (!file_exists(self::MODULE_PATH . '/' . $module . '/Admin/Installer.php')) {
// todo download; // todo download;
return false;
} }
try { try {
@ -526,7 +526,7 @@ class ModuleManager
{ {
$path = realpath($oldPath = self::MODULE_PATH . '/' . $module . '/' . 'info.json'); $path = realpath($oldPath = self::MODULE_PATH . '/' . $module . '/' . 'info.json');
if ($path === false || strpos($path, self::MODULE_PATH) === false) { if ($path === false) {
throw new PathException($oldPath); throw new PathException($oldPath);
} }
@ -584,38 +584,17 @@ class ModuleManager
/** /**
* Initialize module. * Initialize module.
* *
* @param string|array $module Module name * @param string|array $modules Module name
* *
* @throws * @throws
* *
* @since 1.0.0 * @since 1.0.0
* @author Dennis Eichhorn * @author Dennis Eichhorn
*/ */
public function initModule($module) public function initModule($modules)
{ {
try { $modules = (array) $modules;
if (!is_array($module)) {
$module = [$module];
}
$this->initModuleArray($module);
} catch (\Exception $e) {
throw $e;
}
}
/**
* Initialize array of modules.
*
* @param array $modules Modules
*
* @return void
*
* @since 1.0.0
* @author Dennis Eichhorn
*/
private function initModuleArray(array $modules)
{
foreach ($modules as $module) { foreach ($modules as $module) {
try { try {
$this->initModuleController($module); $this->initModuleController($module);