[TikiWiki-commits] [Git][tikiwiki/tiki][master] [FIX] Add custom assertion to handle triggered errors

"luci \(@luciash\) via TikiWiki-cvs" <[email protected]>
Newsgroups gmane.comp.cms.tiki.cvs
Message-ID <68cae9f0c2d4e_2cdeaf81533e@gitlab-sidekiq-low-urgency-cpu-bound-v2-757fcbb555-pbptg.mail>

luci pushed to branch master at Tiki Wiki CMS Groupware / Tiki


Commits:
d7976d61 by ushindi bienvenu at 2025-09-17T16:55:47+00:00
[FIX] Add custom assertion to handle triggered errors
---
* [FIX] Add custom assertion to handle triggered errors

See merge request tikiwiki/tiki!8591

- - - - -


2 changed files:

- lib/test/TikiLib/LibTest.php
- lib/test/TikiTestCase.php


Changes:

=====================================
lib/test/TikiLib/LibTest.php
=====================================
@@ -16,8 +16,17 @@ class TikiLib_LibTest extends TikiTestCase
         $this->assertInstanceOf(CalendarLib::class, TikiLib::lib('calendar'));
     }
 
-    public function testLibShouldReturnNullForInvalidClass(): void
+    /**
+     * @test
+     * #[WithoutErrorHandler]
+     * @throws ErrorException
+     */
+    public function testLibShouldTriggerErrorForInvalidClass(): void
     {
-        $this->assertThrowableMessage(tr("%0 library not found. This may be due to a typo or caused by a recent update.", 'invalidClass'), TikiLib::class . '::lib', 'invalidClass');
+        $this->assertTriggeredError(
+            tr("%0 library not found. This may be due to a typo or caused by a recent update.", 'invalidClass'),
+            'TikiLib::lib',
+            'invalidClass'
+        );
     }
 }


=====================================
lib/test/TikiTestCase.php
=====================================
@@ -71,4 +71,49 @@ abstract class TikiTestCase extends TestCase
     {
         $this->warnings[] = $warning;
     }
+
+    /**
+     * Asserts that a specific user-triggered error with a given message is thrown.
+     *
+     * @param string   $message  The expected error message.
+     * @param callable $callback The callable that is expected to trigger the error.
+     * @param mixed    ...$args  Arguments to pass to the callable.
+     *
+     * @throws ErrorException
+     */
+    protected function assertTriggeredError(string $message, callable $callback, ...$args): void
+    {
+        // This error handler is to convert E_USER_NOTICE into an exception.
+        $errorHandler = function ($severity, $errMessage, $file, $line) use ($message) {
+            if ($severity & (E_USER_NOTICE | E_USER_WARNING | E_USER_ERROR | E_USER_DEPRECATED)) {
+                if ($errMessage !== $message) {
+                    throw new \PHPUnit\Framework\ExpectationFailedException(
+                        sprintf(
+                            'Failed asserting that error message "%s" matches expected "%s".',
+                            $errMessage,
+                            $message
+                        )
+                    );
+                }
+                throw new \ErrorException($errMessage, 0, $severity, $file, $line);
+            }
+        };
+
+        // Temporarily set the custom error handler.
+        set_error_handler($errorHandler, E_USER_NOTICE);
+
+        try {
+            $callback(...$args);
+
+            // Assertion fails if code completes without throwing an exception,
+            throw new \PHPUnit\Framework\ExpectationFailedException(
+                'Expected an error to be triggered, but none was.'
+            );
+        } catch (\ErrorException $e) {
+            // Assertion passes if we catch the expected ErrorException.
+            $this->assertEquals($message, $e->getMessage());
+        } finally {
+            restore_error_handler();
+        }
+    }
 }



View it on GitLab: https://gitlab.com/tikiwiki/tiki/-/commit/d7976d61e952e38a05d8ffbe2fcb771d2d9f21ec

-- 
View it on GitLab: https://gitlab.com/tikiwiki/tiki/-/commit/d7976d61e952e38a05d8ffbe2fcb771d2d9f21ec
You're receiving this email because of your account on gitlab.com.

_______________________________________________
TikiWiki-cvs mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/tikiwiki-cvs
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.