Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor deprecation warning tests #6093

Merged
merged 4 commits into from
Dec 31, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
lukasbestle marked this conversation as resolved.
Show resolved Hide resolved
) {
$this->hasErrorHandler = true;

$called = false;

set_error_handler(
lukasbestle marked this conversation as resolved.
Show resolved Hide resolved
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