Skip to content

Commit

Permalink
Refactor deprecation warning tests
Browse files Browse the repository at this point in the history
  • Loading branch information
distantnative committed Dec 27, 2023
1 parent 9ddc04d commit 2e83a0e
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 117 deletions.
20 changes: 6 additions & 14 deletions tests/Cms/Helpers/HelperFunctionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@
use Kirby\Image\QrCode;
use Kirby\Toolkit\Collection;
use Kirby\Toolkit\Obj;
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\Error\Deprecated;

class HelperFunctionsTest extends TestCase
class HelperFunctionsTest extends HelpersTestCase
{
protected $fixtures;
protected $kirby;
Expand Down Expand Up @@ -189,17 +187,11 @@ public function testCssWithArray()

public function testDeprecated()
{
// the deprecation warnings are always triggered in testing mode,
// so we cannot test it with disabled debug mode

try {
deprecated('The xyz method is deprecated.');
} catch (Deprecated $e) {
$this->assertSame('The xyz method is deprecated.', $e->getMessage());
return;
}

Assert::fail('Expected deprecation warning was not generated');
$this->assertError(
E_USER_DEPRECATED,
'The xyz method is deprecated.',
fn () => deprecated('The xyz method is deprecated.')
);
}

public function testDumpOnCli()
Expand Down
104 changes: 46 additions & 58 deletions tests/Cms/Helpers/HelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,13 @@

use Kirby\Exception\InvalidArgumentException;
use Kirby\Toolkit\Obj;
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\Error\Deprecated;
use PHPUnit\Framework\Error\Warning;

/**
* @coversDefaultClass Kirby\Cms\Helpers
*/
class HelpersTest extends TestCase
class HelpersTest extends HelpersTestCase
{
protected $deprecations = [];
protected $hasErrorHandler = false;

public function setUp(): void
{
Expand All @@ -28,69 +24,62 @@ public function tearDown(): void
parent::tearDown();

Helpers::$deprecations = $this->deprecations;

if ($this->hasErrorHandler === true) {
restore_error_handler();
$this->hasErrorHandler = false;
}
}

/**
* @covers ::deprecated
*/
public function testDeprecated()
{
// the deprecation warnings are always triggered in testing mode,
// so we cannot test it with disabled debug mode

try {
Helpers::deprecated('The xyz method is deprecated.');
} catch (Deprecated $e) {
$this->assertSame('The xyz method is deprecated.', $e->getMessage());
return;
}

Assert::fail('Expected deprecation warning was not generated');
$this->assertError(
E_USER_DEPRECATED,
'The xyz method is deprecated.',
fn () => Helpers::deprecated('The xyz method is deprecated.')
);
}

/**
* @covers ::deprecated
*/
public function testDeprecatedKeyUndefined()
{
try {
Helpers::deprecated('The xyz method is deprecated.', 'my-key');
} catch (Deprecated $e) {
$this->assertSame('The xyz method is deprecated.', $e->getMessage());
return;
}

Assert::fail('Expected deprecation warning was not generated');
$this->assertError(
E_USER_DEPRECATED,
'The xyz method is deprecated.',
fn () => Helpers::deprecated('The xyz method is deprecated.', 'my-key')
);
}

/**
* @covers ::deprecated
*/
public function testDeprecatedActivated()
{
try {
Helpers::$deprecations = ['my-key' => true];
Helpers::deprecated('The xyz method is deprecated.', 'my-key');
} catch (Deprecated $e) {
$this->assertSame('The xyz method is deprecated.', $e->getMessage());
return;
}

Assert::fail('Expected deprecation warning was not generated');
$this->assertError(
E_USER_DEPRECATED,
'The xyz method is deprecated.',
function () {
Helpers::$deprecations = ['my-key' => true];
Helpers::deprecated('The xyz method is deprecated.', 'my-key');
}
);
}

/**
* @covers ::deprecated
*/
public function testDeprecatedKeyDeactivated()
{
Helpers::$deprecations = ['my-key' => false];
$this->assertFalse(Helpers::deprecated('The xyz method is deprecated.', 'my-key'));
$result = $this->assertError(
E_USER_DEPRECATED,
'The xyz method is deprecated.',
function () {
Helpers::$deprecations = ['my-key' => false];
return Helpers::deprecated('The xyz method is deprecated.', 'my-key');
},
false
);
$this->assertFalse($result);
}

/**
Expand Down Expand Up @@ -211,24 +200,23 @@ public function testHandleErrorsWarningCaughtCallbackValue()
*/
public function testHandleErrorsWarningNotCaught()
{
try {
Helpers::handleErrors(
fn () => trigger_error('Some warning', E_USER_WARNING),
function (int $errno, string $errstr) {
$this->assertSame(E_USER_WARNING, $errno);
$this->assertSame('Some warning', $errstr);

// continue the handler chain
return false;
},
'handled'
);
} catch (Warning $e) {
$this->assertSame('Some warning', $e->getMessage());
return;
}

Assert::fail('Expected warning was not generated');
$this->assertError(
E_USER_WARNING,
'Some warning',
function () {
Helpers::handleErrors(
fn () => trigger_error('Some warning', E_USER_WARNING),
function (int $errno, string $errstr) {
$this->assertSame(E_USER_WARNING, $errno);
$this->assertSame('Some warning', $errstr);

// continue the handler chain
return false;
},
'handled'
);
}
);
}

/**
Expand Down
48 changes: 48 additions & 0 deletions tests/Cms/HelpersTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Kirby\Cms;

use Closure;

class HelpersTestCase extends TestCase
{
protected $hasErrorHandler = false;

public function tearDown(): void
{
parent::tearDown();

if ($this->hasErrorHandler === true) {
restore_error_handler();
$this->hasErrorHandler = false;
}
}

public function assertError(
int $expectedErrorType,
string $exptectedErrorMessage,
Closure $callback,
bool $expectedFailure = true
) {
$this->hasErrorHandler = true;

$called = false;

set_error_handler(
function (int $errno, string $errstr) use ($expectedErrorType, $exptectedErrorMessage, &$called) {
$called = true;
$this->assertSame($expectedErrorType, $errno);
$this->assertSame($exptectedErrorMessage, $errstr);
}
);

$result = $callback();

if ($expectedFailure === false) {
$this->assertFalse($called);
return $result;
}

$this->assertTrue($called);
}
}
Loading

0 comments on commit 2e83a0e

Please sign in to comment.