From 6443ab6c14ad1e86944751d04749f1098cbc83e5 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Sat, 28 Nov 2020 19:26:40 +0100 Subject: [PATCH] fix tests --- DataStorage/Database/DataMapperAbstract.php | 38 ++++++++++++-------- Localization/Localization.php | 2 +- Module/ModuleManager.php | 2 +- System/File/Ftp/Directory.php | 4 +-- System/File/Local/Directory.php | 4 +-- tests/Application/ApplicationManagerTest.php | 2 +- tests/Localization/LocalizationTest.php | 12 ------- tests/Module/ModuleManagerTest.php | 4 +-- tests/Socket/Client/ClientTest.php | 2 +- tests/Socket/Client/ClientTestHelper.php | 2 +- tests/Socket/Server/ServerTest.php | 2 +- 11 files changed, 35 insertions(+), 39 deletions(-) diff --git a/DataStorage/Database/DataMapperAbstract.php b/DataStorage/Database/DataMapperAbstract.php index e222e966d..da0f2c387 100644 --- a/DataStorage/Database/DataMapperAbstract.php +++ b/DataStorage/Database/DataMapperAbstract.php @@ -761,12 +761,13 @@ class DataMapperAbstract implements DataMapperInterface $values = $property->getValue($obj); /** @var self $mapper */ - $mapper = static::$hasMany[$propertyName]['mapper']; + $mapper = static::$hasMany[$propertyName]['mapper']; + $internalName = isset($mapper::$columns[static::$hasMany[$propertyName]['self']]) ? $mapper::$columns[static::$hasMany[$propertyName]['self']]['internal'] : 'ERROR'; if (\is_object($values)) { // conditionals $relReflectionClass = new \ReflectionClass($values); - $relProperty = $relReflectionClass->getProperty($mapper::$columns[static::$hasMany[$propertyName]['self']]['internal']); + $relProperty = $relReflectionClass->getProperty($internalName); if (!($isPublicRel = $relProperty->isPublic())) { $relProperty->setAccessible(true); @@ -779,10 +780,17 @@ class DataMapperAbstract implements DataMapperInterface } $mapper::create($values); - continue; - } - if (!\is_array($values)) { + if (!$isPublic) { + $property->setAccessible(false); + } + + continue; + } elseif (!\is_array($values)) { + if (!$isPublic) { + $property->setAccessible(false); + } + // conditionals continue; } @@ -814,13 +822,20 @@ class DataMapperAbstract implements DataMapperInterface // Setting relation value (id) for relation (since the relation is not stored in an extra relation table) if (!isset(static::$hasMany[$propertyName]['external'])) { - $relProperty = $relReflectionClass->getProperty($mapper::$columns[static::$hasMany[$propertyName]['self']]['internal']); + $relProperty = $relReflectionClass->getProperty($internalName); if (!$isPublic) { $relProperty->setAccessible(true); } - $relProperty->setValue($value, $objId); + // todo maybe consider to just set the column type to object, and then check for that (might be faster) + if (isset($mapper::$belongsTo[$internalName]) + || isset($mapper::$ownsOne[$internalName]) + ) { + $relProperty->setValue($value, self::createNullModel($objId)); + } else { + $relProperty->setValue($value, $objId); + } if (!$isPublic) { $relProperty->setAccessible(false); @@ -3464,14 +3479,7 @@ class DataMapperAbstract implements DataMapperInterface $parts[$c] = 'Null' . $name; $class = \implode('\\', $parts); - $obj = new $class(); - - if ($id !== null) { - $refClass = new \ReflectionClass($obj); - self::setObjectId($refClass, $obj, $id); - } - - return $obj; + return $id !== null ? new $class($id) : new $class(); } /** diff --git a/Localization/Localization.php b/Localization/Localization.php index f0437e469..77b71bd68 100644 --- a/Localization/Localization.php +++ b/Localization/Localization.php @@ -273,7 +273,7 @@ class Localization implements \JsonSerializable return; } - $fileContent = \file_get_contents(DEFINITIONS_PATH . 'en_US.json'); + $fileContent = \file_get_contents(self::DEFINITIONS_PATH . 'en_US.json'); if ($fileContent === false) { return; // @codeCoverageIgnore } diff --git a/Module/ModuleManager.php b/Module/ModuleManager.php index 9ac08755f..0f1c6525b 100644 --- a/Module/ModuleManager.php +++ b/Module/ModuleManager.php @@ -113,7 +113,7 @@ final class ModuleManager * Constructor. * * @param ApplicationAbstract $app Application - * @param string $modulePath Path to modules + * @param string $modulePath Path to modules (must end with '/') * * @since 1.0.0 */ diff --git a/System/File/Ftp/Directory.php b/System/File/Ftp/Directory.php index 85e353bb4..9da0d5a37 100644 --- a/System/File/Ftp/Directory.php +++ b/System/File/Ftp/Directory.php @@ -673,7 +673,7 @@ class Directory extends FileAbstract implements DirectoryInterface /** * {@inheritdoc} */ - public function current() + public function current() : FileAbstract { $current = \current($this->nodes); @@ -695,7 +695,7 @@ class Directory extends FileAbstract implements DirectoryInterface /** * {@inheritdoc} */ - public function next() : FileAbstract + public function next() { $next = \next($this->nodes); diff --git a/System/File/Local/Directory.php b/System/File/Local/Directory.php index e902191bc..95173e874 100644 --- a/System/File/Local/Directory.php +++ b/System/File/Local/Directory.php @@ -496,7 +496,7 @@ final class Directory extends FileAbstract implements DirectoryInterface /** * {@inheritdoc} */ - public function current() : self + public function current() : FileAbstract { $current = \current($this->nodes); @@ -518,7 +518,7 @@ final class Directory extends FileAbstract implements DirectoryInterface /** * {@inheritdoc} */ - public function next() : FileAbstract + public function next() { $next = \next($this->nodes); diff --git a/tests/Application/ApplicationManagerTest.php b/tests/Application/ApplicationManagerTest.php index 6418d4d06..0f47a96b5 100644 --- a/tests/Application/ApplicationManagerTest.php +++ b/tests/Application/ApplicationManagerTest.php @@ -43,7 +43,7 @@ class ApplicationManagerTest extends \PHPUnit\Framework\TestCase $app->router = new WebRouter(); $app->dispatcher = new Dispatcher($app); $app->appSettings = new CoreSettings($app->dbPool->get('admin')); - $app->moduleManager = new ModuleManager($app, __DIR__ . '/../../../Modules'); + $app->moduleManager = new ModuleManager($app, __DIR__ . '/../../../Modules/'); $this->appManager = new ApplicationManager($app->moduleManager); } diff --git a/tests/Localization/LocalizationTest.php b/tests/Localization/LocalizationTest.php index f7e66e1e1..0b50e2280 100644 --- a/tests/Localization/LocalizationTest.php +++ b/tests/Localization/LocalizationTest.php @@ -375,16 +375,4 @@ class LocalizationTest extends \PHPUnit\Framework\TestCase $this->localization->loadFromLanguage(ISO639x1Enum::_AA); self::assertEquals(ISO4217CharEnum::_USD, $this->localization->getCurrency()); } - - /** - * @testdox Loading localization data from a file with invalid language throws InvalidEnumValue - * @covers phpOMS\Localization\Localization - * @group framework - */ - public function testInvalidLocalizationLoading() : void - { - $this->expectException(\phpOMS\Stdlib\Base\Exception\InvalidEnumValue::class); - - $this->localization->loadFromLanguage('INVALID'); - } } diff --git a/tests/Module/ModuleManagerTest.php b/tests/Module/ModuleManagerTest.php index 884d981a8..5101691fc 100644 --- a/tests/Module/ModuleManagerTest.php +++ b/tests/Module/ModuleManagerTest.php @@ -46,7 +46,7 @@ class ModuleManagerTest extends \PHPUnit\Framework\TestCase $this->app->router = new WebRouter(); $this->app->dispatcher = new Dispatcher($this->app); $this->app->appSettings = new CoreSettings($this->app->dbPool->get('admin')); - $this->moduleManager = new ModuleManager($this->app, __DIR__ . '/../../../Modules'); + $this->moduleManager = new ModuleManager($this->app, __DIR__ . '/../../../Modules/'); } /** @@ -251,7 +251,7 @@ class ModuleManagerTest extends \PHPUnit\Framework\TestCase */ public function testInvalidModulePath() : void { - $moduleManager = new ModuleManager($this->app, __DIR__ . '/Testmodule'); + $moduleManager = new ModuleManager($this->app, __DIR__ . '/Testmodule/'); self::assertEquals([], $moduleManager->getAllModules()); self::assertEquals([], $moduleManager->getInstalledModules()); diff --git a/tests/Socket/Client/ClientTest.php b/tests/Socket/Client/ClientTest.php index 5c8b88bfb..363a09a43 100644 --- a/tests/Socket/Client/ClientTest.php +++ b/tests/Socket/Client/ClientTest.php @@ -62,7 +62,7 @@ class ClientTest extends \PHPUnit\Framework\TestCase $this->app->cachePool = new CachePool($this->app->dbPool); $this->app->accountManager = new AccountManager($GLOBALS['session']); $this->app->appSettings = new CoreSettings($this->app->dbPool->get()); - $this->app->moduleManager = new ModuleManager($this->app, __DIR__ . '/../../../../Modules'); + $this->app->moduleManager = new ModuleManager($this->app, __DIR__ . '/../../../../Modules/'); $this->app->dispatcher = new Dispatcher($this->app); $this->app->eventManager = new EventManager($this->app->dispatcher); $this->app->eventManager->importFromFile(__DIR__ . '/../../../Socket/Hooks.php'); diff --git a/tests/Socket/Client/ClientTestHelper.php b/tests/Socket/Client/ClientTestHelper.php index 4c97b953d..e49975f92 100644 --- a/tests/Socket/Client/ClientTestHelper.php +++ b/tests/Socket/Client/ClientTestHelper.php @@ -55,7 +55,7 @@ $app->orgId = 1; $app->cachePool = new CachePool($app->dbPool); $app->accountManager = new AccountManager($GLOBALS['session']); $app->appSettings = new CoreSettings($app->dbPool->get()); -$app->moduleManager = new ModuleManager($app, __DIR__ . '/../../../../Modules'); +$app->moduleManager = new ModuleManager($app, __DIR__ . '/../../../../Modules/'); $app->dispatcher = new Dispatcher($app); $app->eventManager = new EventManager($app->dispatcher); $app->eventManager->importFromFile(__DIR__ . '/../../../Socket/Hooks.php'); diff --git a/tests/Socket/Server/ServerTest.php b/tests/Socket/Server/ServerTest.php index 8539a26b2..589644d9d 100644 --- a/tests/Socket/Server/ServerTest.php +++ b/tests/Socket/Server/ServerTest.php @@ -62,7 +62,7 @@ class ServerTest extends \PHPUnit\Framework\TestCase $this->app->cachePool = new CachePool($this->app->dbPool); $this->app->accountManager = new AccountManager($GLOBALS['session']); $this->app->appSettings = new CoreSettings($this->app->dbPool->get()); - $this->app->moduleManager = new ModuleManager($this->app, __DIR__ . '/../../../../Modules'); + $this->app->moduleManager = new ModuleManager($this->app, __DIR__ . '/../../../../Modules/'); $this->app->dispatcher = new Dispatcher($this->app); $this->app->eventManager = new EventManager($this->app->dispatcher); $this->app->eventManager->importFromFile(__DIR__ . '/../../../Socket/Hooks.php');