From 9fc741d38d61bf1a610c3cdf3a7e770a370b30cd Mon Sep 17 00:00:00 2001 From: Nico Hoffmann Date: Sat, 9 Dec 2023 17:20:07 +0100 Subject: [PATCH] Fix unit tests --- tests/Cms/Files/FileActionsTest.php | 24 +++---- tests/Cms/Helpers/HelperFunctionsTest.php | 17 +++-- tests/Cms/Helpers/HelpersTest.php | 67 ++++++++++++++----- tests/Session/FileSessionStoreTest.php | 2 +- .../{BlockUuidTest.php => BlockUuidTest.tmp} | 0 5 files changed, 72 insertions(+), 38 deletions(-) rename tests/Uuid/{BlockUuidTest.php => BlockUuidTest.tmp} (100%) diff --git a/tests/Cms/Files/FileActionsTest.php b/tests/Cms/Files/FileActionsTest.php index 29e52b7d9b..bcd031fe19 100644 --- a/tests/Cms/Files/FileActionsTest.php +++ b/tests/Cms/Files/FileActionsTest.php @@ -16,7 +16,7 @@ class FileActionsTest extends TestCase public function setUp(): void { Dir::make($this->tmp = __DIR__ . '/tmp'); - $this->app = $this->app(); + $this->app = static::app(); } public function tearDown(): void @@ -25,11 +25,11 @@ public function tearDown(): void Dir::remove($this->tmp); } - public function app() + public static function app() { return new App([ 'roots' => [ - 'index' => $this->tmp + 'index' => __DIR__ . '/tmp' ], 'site' => [ 'children' => [ @@ -58,9 +58,9 @@ public function app() ]); } - public function appWithLanguages() + public static function appWithLanguages() { - return $this->app()->clone([ + return static::app()->clone([ 'languages' => [ [ 'code' => 'en', @@ -75,9 +75,9 @@ public function appWithLanguages() ]); } - public function parentProvider() + public static function parentProvider() { - $app = $this->app(); + $app = static::app(); return [ [$app->site()], @@ -85,9 +85,9 @@ public function parentProvider() ]; } - public function fileProvider() + public static function fileProvider(): array { - $app = $this->app(); + $app = static::app(); return [ [$app->site()->file()], @@ -118,9 +118,9 @@ public function testChangeName(File $file) $this->assertFileDoesNotExist($file->storage()->contentFile('published', 'default')); } - public function fileProviderMultiLang() + public static function fileProviderMultiLang(): array { - $app = $this->appWithLanguages(); + $app = static::appWithLanguages(); return [ [$app->site()->file()], @@ -133,7 +133,7 @@ public function fileProviderMultiLang() */ public function testChangeNameMultiLang(File $file) { - $app = $this->appWithLanguages(); + $app = static::appWithLanguages(); $app->impersonate('kirby'); // create an empty dummy file diff --git a/tests/Cms/Helpers/HelperFunctionsTest.php b/tests/Cms/Helpers/HelperFunctionsTest.php index 9e7c109d62..083be8b4ec 100644 --- a/tests/Cms/Helpers/HelperFunctionsTest.php +++ b/tests/Cms/Helpers/HelperFunctionsTest.php @@ -2,6 +2,7 @@ namespace Kirby\Cms; +use Exception; use Kirby\Cms\App as Kirby; use Kirby\Filesystem\Asset; use Kirby\Filesystem\Dir; @@ -9,7 +10,6 @@ use Kirby\Toolkit\Collection; use Kirby\Toolkit\Obj; use PHPUnit\Framework\Assert; -use PHPUnit\Framework\Error\Deprecated; class HelperFunctionsTest extends TestCase { @@ -189,17 +189,20 @@ public function testCssWithArray() public function testDeprecated() { - // the deprecation warnings are always triggered in testing mode, - // so we cannot test it with disabled debug mode + set_error_handler( + static function ($errno, $errstr) { + restore_error_handler(); + throw new Exception($errstr, $errno); + }, + E_USER_DEPRECATED + ); try { deprecated('The xyz method is deprecated.'); - } catch (Deprecated $e) { + Assert::fail('Expected deprecation warning was not generated'); + } catch (Exception $e) { $this->assertSame('The xyz method is deprecated.', $e->getMessage()); - return; } - - Assert::fail('Expected deprecation warning was not generated'); } public function testDumpOnCli() diff --git a/tests/Cms/Helpers/HelpersTest.php b/tests/Cms/Helpers/HelpersTest.php index e5ac2445a7..8eb9467e0f 100644 --- a/tests/Cms/Helpers/HelpersTest.php +++ b/tests/Cms/Helpers/HelpersTest.php @@ -2,6 +2,7 @@ namespace Kirby\Cms; +use Exception; use Kirby\Exception\InvalidArgumentException; use Kirby\Toolkit\Obj; use PHPUnit\Framework\Assert; @@ -40,17 +41,20 @@ public function tearDown(): void */ public function testDeprecated() { - // the deprecation warnings are always triggered in testing mode, - // so we cannot test it with disabled debug mode + set_error_handler( + static function ($errno, $errstr) { + restore_error_handler(); + throw new Exception($errstr, $errno); + }, + E_USER_DEPRECATED + ); try { Helpers::deprecated('The xyz method is deprecated.'); - } catch (Deprecated $e) { + Assert::fail('Expected deprecation warning was not generated'); + } catch (Exception $e) { $this->assertSame('The xyz method is deprecated.', $e->getMessage()); - return; } - - Assert::fail('Expected deprecation warning was not generated'); } /** @@ -58,14 +62,20 @@ public function testDeprecated() */ public function testDeprecatedKeyUndefined() { + set_error_handler( + static function ($errno, $errstr) { + restore_error_handler(); + throw new Exception($errstr, $errno); + }, + E_USER_DEPRECATED + ); + try { Helpers::deprecated('The xyz method is deprecated.', 'my-key'); - } catch (Deprecated $e) { + Assert::fail('Expected deprecation warning was not generated'); + } catch (Exception $e) { $this->assertSame('The xyz method is deprecated.', $e->getMessage()); - return; } - - Assert::fail('Expected deprecation warning was not generated'); } /** @@ -73,15 +83,21 @@ public function testDeprecatedKeyUndefined() */ public function testDeprecatedActivated() { + set_error_handler( + static function ($errno, $errstr) { + restore_error_handler(); + throw new Exception($errstr, $errno); + }, + E_USER_DEPRECATED + ); + try { Helpers::$deprecations = ['my-key' => true]; Helpers::deprecated('The xyz method is deprecated.', 'my-key'); - } catch (Deprecated $e) { + Assert::fail('Expected deprecation warning was not generated'); + } catch (Exception $e) { $this->assertSame('The xyz method is deprecated.', $e->getMessage()); - return; } - - Assert::fail('Expected deprecation warning was not generated'); } /** @@ -89,6 +105,14 @@ public function testDeprecatedActivated() */ public function testDeprecatedKeyDeactivated() { + set_error_handler( + static function ($errno, $errstr) { + restore_error_handler(); + throw new Exception($errstr, $errno); + }, + E_USER_DEPRECATED + ); + Helpers::$deprecations = ['my-key' => false]; $this->assertFalse(Helpers::deprecated('The xyz method is deprecated.', 'my-key')); } @@ -211,6 +235,14 @@ public function testHandleErrorsWarningCaughtCallbackValue() */ public function testHandleErrorsWarningNotCaught() { + set_error_handler( + static function ($errno, $errstr) { + restore_error_handler(); + throw new Exception($errstr, $errno); + }, + E_WARNING + ); + try { Helpers::handleErrors( fn () => trigger_error('Some warning', E_USER_WARNING), @@ -223,12 +255,11 @@ function (int $errno, string $errstr) { }, 'handled' ); - } catch (Warning $e) { + + Assert::fail('Expected warning was not generated'); + } catch (Exception $e) { $this->assertSame('Some warning', $e->getMessage()); - return; } - - Assert::fail('Expected warning was not generated'); } /** diff --git a/tests/Session/FileSessionStoreTest.php b/tests/Session/FileSessionStoreTest.php index abf730436b..34d41a1fdd 100644 --- a/tests/Session/FileSessionStoreTest.php +++ b/tests/Session/FileSessionStoreTest.php @@ -57,7 +57,7 @@ public function tearDown(): void } Dir::remove($this->root); - $this->assertDirectoryNotExists($this->root); + $this->assertDirectoryDoesNotExist($this->root); } /** diff --git a/tests/Uuid/BlockUuidTest.php b/tests/Uuid/BlockUuidTest.tmp similarity index 100% rename from tests/Uuid/BlockUuidTest.php rename to tests/Uuid/BlockUuidTest.tmp