Skip to content

Commit

Permalink
Fix expect error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
kwizer15 committed Oct 7, 2024
1 parent fc86b05 commit b3f6071
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
14 changes: 8 additions & 6 deletions tests/Unit/DBTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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()));
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Mock/ObjectMock/ObjectMockBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class ObjectMockBuilder
*/
private $classMethods;

private string $tableStructure;
private $tableStructure;

public function __construct()
{
Expand Down
7 changes: 7 additions & 0 deletions tests/Unit/Mock/PhpErrorMock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Tests\Unit\Mock;

class PhpErrorMock extends \Exception
{
}

0 comments on commit b3f6071

Please sign in to comment.