From ffdd45e32a3f71ab8c04fe231c575d16afc42886 Mon Sep 17 00:00:00 2001 From: Clemens Schwaighofer Date: Fri, 15 Apr 2022 15:19:30 +0900 Subject: [PATCH] Fix phpUnit tests with constant settings and missing checks BASE constant setting: moved all to same base folder in 4dev/tests check all other CONSTANT settings if they are already set and skip (used only in Language default set) Add missing phpunit check for array merge recursive --- .../CoreLibsCombinedArrayHandlerTest.php | 97 +++++++++++++++++-- 4dev/tests/CoreLibsCreateSessionTest.php | 4 +- 4dev/tests/CoreLibsDebugLoggingTest.php | 8 +- 4dev/tests/CoreLibsLanguageGetLocaleTest.php | 37 +++++-- 4dev/tests/CoreLibsLanguageL10nTest.php | 38 ++++++-- www/lib/CoreLibs/Combined/ArrayHandler.php | 3 +- 6 files changed, 153 insertions(+), 34 deletions(-) diff --git a/4dev/tests/CoreLibsCombinedArrayHandlerTest.php b/4dev/tests/CoreLibsCombinedArrayHandlerTest.php index 8ea87742..cd6a70ff 100644 --- a/4dev/tests/CoreLibsCombinedArrayHandlerTest.php +++ b/4dev/tests/CoreLibsCombinedArrayHandlerTest.php @@ -7,6 +7,7 @@ declare(strict_types=1); namespace tests; +use Exception; use PHPUnit\Framework\TestCase; /** @@ -274,7 +275,6 @@ final class CoreLibsCombinedArrayHandlerTest extends TestCase } /** - * TODO: create provider for n array merge * provides array listing for the merge test * * @return array @@ -282,6 +282,61 @@ final class CoreLibsCombinedArrayHandlerTest extends TestCase public function arrayMergeRecursiveProvider(): array { return [ + // 0: expected + // 1..n: to merge arrays + // n+1: trigger for handle keys as string + 'two arrays' => [ + ['a' => 1, 'b' => 2, 'c' => 3], + ['a' => 1, 'b' => 2], + ['b' => 2, 'c' => 3], + ], + 'two arrays, string flag' => [ + ['a' => 1, 'b' => 2, 'c' => 3], + ['a' => 1, 'b' => 2], + ['b' => 2, 'c' => 3], + true, + ], + // non hash arrays + 'non hash array merge, no string flag' => [ + [3, 4, 5], + [1, 2, 3], + [3, 4, 5], + ], + 'non hash array merge, string flag' => [ + [1, 2, 3, 3, 4, 5], + [1, 2, 3], + [3, 4, 5], + true + ], + ]; + } + + /** + * for warning checks + * + * @return array + */ + public function arrayMergeRecursiveProviderWarning(): array + { + return [ + // error <2 arguments + 'too view arguments' => [ + 'arrayMergeRecursive needs two or more array arguments', + [1] + ], + // error <2 arrays + 'only one array' => [ + 'arrayMergeRecursive needs two or more array arguments', + [1], + true, + ], + // error element is not array + 'non array between array' => [ + 'arrayMergeRecursive encountered a non array argument', + [1], + 'string', + [2] + ], ]; } @@ -626,21 +681,43 @@ final class CoreLibsCombinedArrayHandlerTest extends TestCase * Undocumented function * * @covers ::arrayMergeRecursive - * @#dataProvider arrayMergeRecursiveProvider - * @testdox arrayMergeRecursive ... will be $expected [$_dataName] + * @dataProvider arrayMergeRecursiveProvider + * @testdox arrayMergeRecursive ... [$_dataName] * - * @param array $input nested array set as each parameter - * @param bool $flag - * @param bool|array $expected * @return void - * array $input, bool $flag, $expected + * */ public function testArrayMergeRecursive(): void { - $this->assertTrue(true, 'Implement proper test run'); - $this->markTestIncomplete( - 'testArrayMergeRecursive has not been implemented yet.' + $arrays = func_get_args(); + // first is expected array, always + $expected = array_shift($arrays); + $output = \CoreLibs\Combined\ArrayHandler::arrayMergeRecursive( + ...$arrays ); + $this->assertEquals( + $expected, + $output + ); + } + + /** + * Undocumented function + * + * @covers ::arrayMergeRecursive + * @dataProvider arrayMergeRecursiveProviderWarning + * @testdox arrayMergeRecursive with E_USER_WARNING [$_dataName] + * + * @return void + */ + public function testArrayMergeRecursiveWarningA(): void + { + $arrays = func_get_args(); + // first is expected warning + $warning = array_shift($arrays); + $this->expectWarning(); + $this->expectWarningMessage($warning); + \CoreLibs\Combined\ArrayHandler::arrayMergeRecursive(...$arrays); } /** diff --git a/4dev/tests/CoreLibsCreateSessionTest.php b/4dev/tests/CoreLibsCreateSessionTest.php index ed08531d..5d9c2402 100644 --- a/4dev/tests/CoreLibsCreateSessionTest.php +++ b/4dev/tests/CoreLibsCreateSessionTest.php @@ -68,6 +68,7 @@ final class CoreLibsCreateSessionTest extends TestCase */ public function testStartSession(string $input, string $type, $expected_n, $expected_i): void { + // NEEDS MOCKING /* $session_id = ''; switch ($type) { case 'p': @@ -97,7 +98,8 @@ final class CoreLibsCreateSessionTest extends TestCase if ($type == 'g') { unset($GLOBALS['SET_SESSION_NAME']); } */ - $this->markTestSkipped('No implementation for Create\Session. Cannot run session_start in CLI'); + $this->markTestSkipped('[CoreLibsCreateSessionTest] No implementation ' + . 'for Create\Session. Cannot run session_start in CLI'); } } diff --git a/4dev/tests/CoreLibsDebugLoggingTest.php b/4dev/tests/CoreLibsDebugLoggingTest.php index f9f952cf..094c10ad 100644 --- a/4dev/tests/CoreLibsDebugLoggingTest.php +++ b/4dev/tests/CoreLibsDebugLoggingTest.php @@ -54,14 +54,16 @@ final class CoreLibsDebugLoggingTest extends TestCase 'no options set, constant set' => [ null, [ - 'log_folder' => '/tmp/', + 'log_folder' => str_replace('/configs', '', __DIR__) + . DIRECTORY_SEPARATOR . 'log/', 'debug_all' => false, 'print_all' => false, ], [ 'constant' => [ - 'BASE' => '/tmp', - 'LOG' => '/' + 'BASE' => str_replace('/configs', '', __DIR__) + . DIRECTORY_SEPARATOR, + 'LOG' => 'log/' ] ] ], diff --git a/4dev/tests/CoreLibsLanguageGetLocaleTest.php b/4dev/tests/CoreLibsLanguageGetLocaleTest.php index cd597e71..d43fe719 100644 --- a/4dev/tests/CoreLibsLanguageGetLocaleTest.php +++ b/4dev/tests/CoreLibsLanguageGetLocaleTest.php @@ -22,17 +22,36 @@ final class CoreLibsLanguageGetLocaleTest extends TestCase public static function setUpBeforeClass(): void { // default web page encoding setting - define('DEFAULT_ENCODING', 'UTF-8'); - // default lang + encoding - define('DEFAULT_LOCALE', 'en_US.UTF-8'); + if (!defined('DEFAULT_ENCODING')) { + define('DEFAULT_ENCODING', 'UTF-8'); + } + if (!defined('DEFAULT_LOCALE')) { + // default lang + encoding + define('DEFAULT_LOCALE', 'en_US.UTF-8'); + } // site - define('SITE_ENCODING', DEFAULT_ENCODING); - define('SITE_LOCALE', DEFAULT_LOCALE); + if (!defined('SITE_ENCODING')) { + define('SITE_ENCODING', DEFAULT_ENCODING); + } + if (!defined('SITE_LOCALE')) { + define('SITE_LOCALE', DEFAULT_LOCALE); + } // just set - define('BASE', str_replace('/configs', '', __DIR__) . DIRECTORY_SEPARATOR); - define('INCLUDES', 'includes' . DIRECTORY_SEPARATOR); - define('LOCALE', 'locale' . DIRECTORY_SEPARATOR); - define('CONTENT_PATH', 'frontend' . DIRECTORY_SEPARATOR); + if (!defined('BASE')) { + define('BASE', str_replace('/configs', '', __DIR__) . DIRECTORY_SEPARATOR); + } + if (!defined('INCLUDES')) { + define('INCLUDES', 'includes' . DIRECTORY_SEPARATOR); + } + if (!defined('LANG')) { + define('LANG', 'lang' . DIRECTORY_SEPARATOR); + } + if (!defined('LOCALE')) { + define('LOCALE', 'locale' . DIRECTORY_SEPARATOR); + } + if (!defined('CONTENT_PATH')) { + define('CONTENT_PATH', 'frontend' . DIRECTORY_SEPARATOR); + } // array session $_SESSION = []; global $_SESSION; diff --git a/4dev/tests/CoreLibsLanguageL10nTest.php b/4dev/tests/CoreLibsLanguageL10nTest.php index 65cca215..88d618e2 100644 --- a/4dev/tests/CoreLibsLanguageL10nTest.php +++ b/4dev/tests/CoreLibsLanguageL10nTest.php @@ -22,19 +22,37 @@ final class CoreLibsLanguageL10nTest extends TestCase */ public static function setUpBeforeClass(): void { - define('DEFAULT_LANG', 'en_US'); // default web page encoding setting - define('DEFAULT_ENCODING', 'UTF-8'); - // default lang + encoding - define('DEFAULT_LOCALE', 'en_US.UTF-8'); + if (!defined('DEFAULT_ENCODING')) { + define('DEFAULT_ENCODING', 'UTF-8'); + } + if (!defined('DEFAULT_LOCALE')) { + // default lang + encoding + define('DEFAULT_LOCALE', 'en_US.UTF-8'); + } // site - define('SITE_LANG', DEFAULT_LANG); + if (!defined('SITE_ENCODING')) { + define('SITE_ENCODING', DEFAULT_ENCODING); + } + if (!defined('SITE_LOCALE')) { + define('SITE_LOCALE', DEFAULT_LOCALE); + } // just set - define('BASE', str_replace('/configs', '', __DIR__) . DIRECTORY_SEPARATOR); - define('INCLUDES', 'includes' . DIRECTORY_SEPARATOR); - define('LANG', 'lang' . DIRECTORY_SEPARATOR); - define('LOCALE', 'locale' . DIRECTORY_SEPARATOR); - define('CONTENT_PATH', 'frontend' . DIRECTORY_SEPARATOR); + if (!defined('BASE')) { + define('BASE', str_replace('/configs', '', __DIR__) . DIRECTORY_SEPARATOR); + } + if (!defined('INCLUDES')) { + define('INCLUDES', 'includes' . DIRECTORY_SEPARATOR); + } + if (!defined('LANG')) { + define('LANG', 'lang' . DIRECTORY_SEPARATOR); + } + if (!defined('LOCALE')) { + define('LOCALE', 'locale' . DIRECTORY_SEPARATOR); + } + if (!defined('CONTENT_PATH')) { + define('CONTENT_PATH', 'frontend' . DIRECTORY_SEPARATOR); + } } /** diff --git a/www/lib/CoreLibs/Combined/ArrayHandler.php b/www/lib/CoreLibs/Combined/ArrayHandler.php index 18cb3666..219fbce2 100644 --- a/www/lib/CoreLibs/Combined/ArrayHandler.php +++ b/www/lib/CoreLibs/Combined/ArrayHandler.php @@ -218,7 +218,8 @@ class ArrayHandler continue; } foreach ($array as $key => $value) { - // if string or if key is assumed to be string do key match else add new entry + // if string or if key is assumed to be string do key match + // else add new entry if (is_string($key) || $key_is_string === false) { if (is_array($value) && array_key_exists($key, $merged) && is_array($merged[$key])) { // $merged[$key] = call_user_func(__METHOD__, $merged[$key], $value, $key_is_string);