From b3f6071797dc7cf1ad24dadb70453075ecf1c282 Mon Sep 17 00:00:00 2001 From: Emmanuel Bernaszuk Date: Mon, 7 Oct 2024 09:17:28 +0200 Subject: [PATCH] Fix expect error handling --- tests/Unit/DBTest.php | 14 ++++++++------ tests/Unit/Mock/ObjectMock/ObjectMockBuilder.php | 2 +- tests/Unit/Mock/PhpErrorMock.php | 7 +++++++ 3 files changed, 16 insertions(+), 7 deletions(-) create mode 100644 tests/Unit/Mock/PhpErrorMock.php diff --git a/tests/Unit/DBTest.php b/tests/Unit/DBTest.php index d3fd053559..902b3eed61 100644 --- a/tests/Unit/DBTest.php +++ b/tests/Unit/DBTest.php @@ -5,6 +5,7 @@ use PHPUnit\Framework\TestCase; use Tests\Unit\Mock\DBTestable; use Tests\Unit\Mock\ObjectMock\ObjectMockBuilder; +use Tests\Unit\Mock\PhpErrorMock; final class DBTest extends TestCase { @@ -180,7 +181,7 @@ public function test_can_not_clone_object(): void { $db = new \DB(); - $this->expectPhpError('DB : Cloner cet objet n\'est pas permis'); + $this->expectPhpErrorMessage('DB : Cloner cet objet n\'est pas permis'); $clone = clone $db; } @@ -363,7 +364,6 @@ public function test_save_unknown_object_identified_with_replace_flag_insert_new { $object = $this->thereIsAnObject()->identifiedBy($this->randomPositiveInt())->object(); - $this->expectPhpError(''); \DB::save($object, false, true); $this->assertCount(1, $this->contentOfTable($object->getTableName())); @@ -525,13 +525,15 @@ private function thereIsABadDatabaseConfiguration(): void $reflection->setStaticPropertyValue('connection', null); } - private function expectPhpError(string $message, int $errorType = E_USER_ERROR): void + private function expectPhpErrorMessage(string $message): void { $this->rollback(); - set_error_handler(function (int $errno, string $errstr) use ($message, $errorType): void { - $this->assertSame($errorType, $errno, $errstr); - $this->assertEquals($message, $errstr); + + set_error_handler(function (int $errno, string $errstr): void { + throw new PhpErrorMock($errstr, $errno); }); + + $this->expectExceptionMessage($message); } private function randomPositiveInt(): int diff --git a/tests/Unit/Mock/ObjectMock/ObjectMockBuilder.php b/tests/Unit/Mock/ObjectMock/ObjectMockBuilder.php index 4e56327254..73d098152b 100644 --- a/tests/Unit/Mock/ObjectMock/ObjectMockBuilder.php +++ b/tests/Unit/Mock/ObjectMock/ObjectMockBuilder.php @@ -29,7 +29,7 @@ class ObjectMockBuilder */ private $classMethods; - private string $tableStructure; + private $tableStructure; public function __construct() { diff --git a/tests/Unit/Mock/PhpErrorMock.php b/tests/Unit/Mock/PhpErrorMock.php new file mode 100644 index 0000000000..9498a9b5bb --- /dev/null +++ b/tests/Unit/Mock/PhpErrorMock.php @@ -0,0 +1,7 @@ +