Skip to content

Commit

Permalink
LunrBaseTestCase: Add methods to expect E_ERROR and E_WARNING
Browse files Browse the repository at this point in the history
PHPUnit 10 no longer does that itself.
  • Loading branch information
pprkut committed Jan 27, 2025
1 parent a94c31c commit 4f2cc49
Showing 1 changed file with 76 additions and 0 deletions.
76 changes: 76 additions & 0 deletions src/Lunr/Halo/LunrBaseTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,24 @@ abstract class LunrBaseTestCase extends TestCase
*/
private bool $isUserWarningHandlerSet = FALSE;

/**
* Whether we have an error handler set for E_WARNING.
* @var bool
*/
private bool $isWarningHandlerSet = FALSE;

/**
* Whether we have an error handler set for E_USER_ERROR.
* @var bool
*/
private bool $isUserErrorHandlerSet = FALSE;

/**
* Whether we have an error handler set for E_ERROR.
* @var bool
*/
private bool $isErrorHandlerSet = FALSE;

/**
* Whether we have an error handler set for E_USER_DEPRECATED.
* @var bool
Expand Down Expand Up @@ -110,12 +122,24 @@ public function tearDown(): void
$this->isUserWarningHandlerSet = FALSE;
}

if ($this->isWarningHandlerSet)
{
restore_error_handler();
$this->isWarningHandlerSet = FALSE;

Check warning on line 128 in src/Lunr/Halo/LunrBaseTestCase.php

View check run for this annotation

Codecov / codecov/patch

src/Lunr/Halo/LunrBaseTestCase.php#L127-L128

Added lines #L127 - L128 were not covered by tests
}

if ($this->isUserErrorHandlerSet)
{
restore_error_handler();
$this->isUserErrorHandlerSet = FALSE;
}

if ($this->isErrorHandlerSet)
{
restore_error_handler();
$this->isErrorHandlerSet = FALSE;

Check warning on line 140 in src/Lunr/Halo/LunrBaseTestCase.php

View check run for this annotation

Codecov / codecov/patch

src/Lunr/Halo/LunrBaseTestCase.php#L139-L140

Added lines #L139 - L140 were not covered by tests
}

if ($this->isUserDeprecatedHandlerSet)
{
restore_error_handler();
Expand Down Expand Up @@ -688,6 +712,32 @@ function (int $errno, string $errstr): bool {
$this->expectOutputString(implode("\n", $this->outputStrings));
}

/**
* Expect an E_WARNING error.
*
* @param string $message The error message to expect
*
* @return void
*/
public function expectWarning(string $message): void

Check warning on line 722 in src/Lunr/Halo/LunrBaseTestCase.php

View check run for this annotation

Codecov / codecov/patch

src/Lunr/Halo/LunrBaseTestCase.php#L722

Added line #L722 was not covered by tests
{
if (!$this->isUserWarningHandlerSet)

Check warning on line 724 in src/Lunr/Halo/LunrBaseTestCase.php

View check run for this annotation

Codecov / codecov/patch

src/Lunr/Halo/LunrBaseTestCase.php#L724

Added line #L724 was not covered by tests
{
set_error_handler(
function (int $errno, string $errstr): bool {
echo "WARNING: $errstr\n";
return TRUE;
},
E_WARNING,
);
$this->isUserWarningHandlerSet = TRUE;

Check warning on line 733 in src/Lunr/Halo/LunrBaseTestCase.php

View check run for this annotation

Codecov / codecov/patch

src/Lunr/Halo/LunrBaseTestCase.php#L726-L733

Added lines #L726 - L733 were not covered by tests
}

$this->outputStrings[] = "WARNING: $message\n";

Check warning on line 736 in src/Lunr/Halo/LunrBaseTestCase.php

View check run for this annotation

Codecov / codecov/patch

src/Lunr/Halo/LunrBaseTestCase.php#L736

Added line #L736 was not covered by tests

$this->expectOutputString(implode("\n", $this->outputStrings));

Check warning on line 738 in src/Lunr/Halo/LunrBaseTestCase.php

View check run for this annotation

Codecov / codecov/patch

src/Lunr/Halo/LunrBaseTestCase.php#L738

Added line #L738 was not covered by tests
}

/**
* Expect an E_USER_ERROR error.
*
Expand All @@ -714,6 +764,32 @@ function (int $errno, string $errstr): bool {
$this->expectOutputString(implode("\n", $this->outputStrings));
}

/**
* Expect an E_ERROR error.
*
* @param string $message The error message to expect
*
* @return void
*/
public function expectError(string $message): void

Check warning on line 774 in src/Lunr/Halo/LunrBaseTestCase.php

View check run for this annotation

Codecov / codecov/patch

src/Lunr/Halo/LunrBaseTestCase.php#L774

Added line #L774 was not covered by tests
{
if (!$this->isErrorHandlerSet)

Check warning on line 776 in src/Lunr/Halo/LunrBaseTestCase.php

View check run for this annotation

Codecov / codecov/patch

src/Lunr/Halo/LunrBaseTestCase.php#L776

Added line #L776 was not covered by tests
{
set_error_handler(
function (int $errno, string $errstr): bool {
echo "ERROR: $errstr\n";
return TRUE;
},
E_ERROR,
);
$this->isErrorHandlerSet = TRUE;

Check warning on line 785 in src/Lunr/Halo/LunrBaseTestCase.php

View check run for this annotation

Codecov / codecov/patch

src/Lunr/Halo/LunrBaseTestCase.php#L778-L785

Added lines #L778 - L785 were not covered by tests
}

$this->outputStrings[] = "ERROR: $message\n";

Check warning on line 788 in src/Lunr/Halo/LunrBaseTestCase.php

View check run for this annotation

Codecov / codecov/patch

src/Lunr/Halo/LunrBaseTestCase.php#L788

Added line #L788 was not covered by tests

$this->expectOutputString(implode("\n", $this->outputStrings));

Check warning on line 790 in src/Lunr/Halo/LunrBaseTestCase.php

View check run for this annotation

Codecov / codecov/patch

src/Lunr/Halo/LunrBaseTestCase.php#L790

Added line #L790 was not covered by tests
}

/**
* Expect an E_USER_DEPRECATED error.
*
Expand Down

0 comments on commit 4f2cc49

Please sign in to comment.