2011-10-27 23:45:12 +02:00
|
|
|
<?php
|
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
namespace SilverStripe\Dev\Tests;
|
|
|
|
|
2022-10-31 07:00:59 +01:00
|
|
|
use PHPUnit\Framework\Error\Deprecated;
|
|
|
|
use SilverStripe\Core\Config\Config;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Dev\Deprecation;
|
|
|
|
use SilverStripe\Dev\SapphireTest;
|
2022-10-31 07:00:59 +01:00
|
|
|
use SilverStripe\Dev\Tests\DeprecationTest\DeprecationTestObject;
|
2022-11-15 06:20:54 +01:00
|
|
|
use SilverStripe\Core\Injector\Injector;
|
2011-10-27 23:45:12 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
class DeprecationTest extends SapphireTest
|
|
|
|
{
|
2022-10-31 07:00:59 +01:00
|
|
|
protected static $extra_dataobjects = [
|
|
|
|
DeprecationTestObject::class,
|
|
|
|
];
|
|
|
|
|
|
|
|
private $oldHandler = null;
|
|
|
|
|
2022-11-15 06:20:54 +01:00
|
|
|
private bool $noticesWereEnabled = false;
|
|
|
|
|
2022-10-31 07:00:59 +01:00
|
|
|
protected function setup(): void
|
|
|
|
{
|
|
|
|
// Use custom error handler for two reasons:
|
|
|
|
// - Filter out errors for deprecated class properities unrelated to this unit test
|
|
|
|
// - Allow the use of expectDeprecation(), which doesn't work with E_USER_DEPRECATION by default
|
|
|
|
// https://github.com/laminas/laminas-di/pull/30#issuecomment-927585210
|
|
|
|
parent::setup();
|
2022-11-15 06:20:54 +01:00
|
|
|
$this->noticesWereEnabled = Deprecation::isEnabled();
|
2022-10-31 07:00:59 +01:00
|
|
|
$this->oldHandler = set_error_handler(function (int $errno, string $errstr, string $errfile, int $errline) {
|
2022-11-01 23:40:34 +01:00
|
|
|
if ($errno === E_USER_DEPRECATED) {
|
|
|
|
if (str_contains($errstr, 'SilverStripe\\Dev\\Tests\\DeprecationTest')) {
|
|
|
|
throw new Deprecated($errstr, $errno, '', 1);
|
|
|
|
} else {
|
|
|
|
// Surpress any E_USER_DEPRECATED unrelated to this unit-test
|
|
|
|
return true;
|
|
|
|
}
|
2022-10-31 07:00:59 +01:00
|
|
|
}
|
|
|
|
if (is_callable($this->oldHandler)) {
|
|
|
|
return call_user_func($this->oldHandler, $errno, $errstr, $errfile, $errline);
|
|
|
|
}
|
2022-11-01 23:40:34 +01:00
|
|
|
// Fallback to default PHP error handler
|
2022-10-31 07:00:59 +01:00
|
|
|
return false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-10-27 04:39:47 +02:00
|
|
|
protected function tearDown(): void
|
2016-12-16 05:34:21 +01:00
|
|
|
{
|
2022-11-15 06:20:54 +01:00
|
|
|
if (!$this->noticesWereEnabled) {
|
|
|
|
Deprecation::disable();
|
|
|
|
}
|
2022-11-03 04:19:17 +01:00
|
|
|
restore_error_handler();
|
2022-10-31 07:00:59 +01:00
|
|
|
$this->oldHandler = null;
|
2016-12-16 05:34:21 +01:00
|
|
|
parent::tearDown();
|
|
|
|
}
|
2011-10-27 23:45:12 +02:00
|
|
|
|
2022-11-15 06:20:54 +01:00
|
|
|
private function myDeprecatedMethod(): string
|
|
|
|
{
|
|
|
|
Deprecation::notice('1.2.3', 'My message');
|
|
|
|
return 'abc';
|
|
|
|
}
|
|
|
|
|
2022-10-20 02:14:58 +02:00
|
|
|
public function testNotice()
|
2016-12-16 05:34:21 +01:00
|
|
|
{
|
2022-10-20 02:14:58 +02:00
|
|
|
$message = implode(' ', [
|
2022-11-15 06:20:54 +01:00
|
|
|
'SilverStripe\Dev\Tests\DeprecationTest->myDeprecatedMethod is deprecated.',
|
2022-10-20 02:14:58 +02:00
|
|
|
'My message.',
|
2022-11-15 06:20:54 +01:00
|
|
|
'Called from SilverStripe\Dev\Tests\DeprecationTest->testNotice.'
|
2022-10-20 02:14:58 +02:00
|
|
|
]);
|
2022-10-31 07:00:59 +01:00
|
|
|
$this->expectDeprecation();
|
|
|
|
$this->expectDeprecationMessage($message);
|
2022-10-20 02:14:58 +02:00
|
|
|
Deprecation::enable();
|
2022-11-15 06:20:54 +01:00
|
|
|
$ret = $this->myDeprecatedMethod();
|
|
|
|
$this->assertSame('abc', $ret);
|
|
|
|
// call outputNotices() directly because the regular shutdown function that emits
|
|
|
|
// the notices within Deprecation won't be called until after this unit-test has finished
|
|
|
|
Deprecation::outputNotices();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCallUserFunc()
|
|
|
|
{
|
|
|
|
$message = implode(' ', [
|
|
|
|
'SilverStripe\Dev\Tests\DeprecationTest->myDeprecatedMethod is deprecated.',
|
|
|
|
'My message.',
|
|
|
|
'Called from SilverStripe\Dev\Tests\DeprecationTest->testCallUserFunc.'
|
|
|
|
]);
|
|
|
|
$this->expectDeprecation();
|
|
|
|
$this->expectDeprecationMessage($message);
|
|
|
|
Deprecation::enable();
|
|
|
|
$ret = call_user_func([$this, 'myDeprecatedMethod']);
|
|
|
|
$this->assertSame('abc', $ret);
|
|
|
|
Deprecation::outputNotices();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCallUserFuncArray()
|
|
|
|
{
|
|
|
|
$message = implode(' ', [
|
|
|
|
'SilverStripe\Dev\Tests\DeprecationTest->myDeprecatedMethod is deprecated.',
|
|
|
|
'My message.',
|
|
|
|
'Called from SilverStripe\Dev\Tests\DeprecationTest->testCallUserFuncArray.'
|
|
|
|
]);
|
|
|
|
$this->expectDeprecation();
|
|
|
|
$this->expectDeprecationMessage($message);
|
|
|
|
Deprecation::enable();
|
|
|
|
$ret = call_user_func_array([$this, 'myDeprecatedMethod'], []);
|
|
|
|
$this->assertSame('abc', $ret);
|
|
|
|
Deprecation::outputNotices();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testWithNoReplacementDefault()
|
|
|
|
{
|
|
|
|
Deprecation::enable();
|
|
|
|
$ret = Deprecation::withNoReplacement(function () {
|
|
|
|
return $this->myDeprecatedMethod();
|
|
|
|
});
|
|
|
|
$this->assertSame('abc', $ret);
|
|
|
|
Deprecation::outputNotices();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testWithNoReplacementTrue()
|
|
|
|
{
|
|
|
|
$message = implode(' ', [
|
|
|
|
'SilverStripe\Dev\Tests\DeprecationTest->myDeprecatedMethod is deprecated.',
|
|
|
|
'My message.',
|
|
|
|
'Called from SilverStripe\Dev\Tests\DeprecationTest->testWithNoReplacementTrue.'
|
|
|
|
]);
|
|
|
|
$this->expectDeprecation();
|
|
|
|
$this->expectDeprecationMessage($message);
|
|
|
|
Deprecation::enable(true);
|
|
|
|
$ret = Deprecation::withNoReplacement(function () {
|
|
|
|
return $this->myDeprecatedMethod();
|
|
|
|
});
|
|
|
|
$this->assertSame('abc', $ret);
|
|
|
|
Deprecation::outputNotices();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testWithNoReplacementTrueCallUserFunc()
|
|
|
|
{
|
|
|
|
$message = implode(' ', [
|
|
|
|
'SilverStripe\Dev\Tests\DeprecationTest->myDeprecatedMethod is deprecated.',
|
|
|
|
'My message.',
|
|
|
|
'Called from SilverStripe\Dev\Tests\DeprecationTest->testWithNoReplacementTrueCallUserFunc.'
|
|
|
|
]);
|
|
|
|
$this->expectDeprecation();
|
|
|
|
$this->expectDeprecationMessage($message);
|
|
|
|
Deprecation::enable(true);
|
|
|
|
$ret = Deprecation::withNoReplacement(function () {
|
|
|
|
return call_user_func([$this, 'myDeprecatedMethod']);
|
|
|
|
});
|
|
|
|
$this->assertSame('abc', $ret);
|
|
|
|
Deprecation::outputNotices();
|
|
|
|
}
|
|
|
|
|
2023-02-27 03:25:27 +01:00
|
|
|
public function testNoticeWithNoReplacementTrue()
|
|
|
|
{
|
|
|
|
$message = implode(' ', [
|
|
|
|
'SilverStripe\Dev\Tests\DeprecationTest->testNoticeWithNoReplacementTrue is deprecated.',
|
|
|
|
'My message.',
|
|
|
|
'Called from PHPUnit\Framework\TestCase->runTest.'
|
|
|
|
]);
|
|
|
|
$this->expectDeprecation();
|
|
|
|
$this->expectDeprecationMessage($message);
|
|
|
|
Deprecation::enable(true);
|
|
|
|
Deprecation::withNoReplacement(function () {
|
|
|
|
Deprecation::notice('123', 'My message.');
|
|
|
|
});
|
|
|
|
Deprecation::outputNotices();
|
|
|
|
}
|
|
|
|
|
2022-11-15 06:20:54 +01:00
|
|
|
public function testClassWithNoReplacement()
|
|
|
|
{
|
|
|
|
$message = implode(' ', [
|
|
|
|
'SilverStripe\Dev\Tests\DeprecationTest\DeprecationTestObject is deprecated.',
|
|
|
|
'Some class message.',
|
|
|
|
'Called from SilverStripe\Dev\Tests\DeprecationTest->testClassWithNoReplacement.'
|
|
|
|
]);
|
|
|
|
$this->expectDeprecation();
|
|
|
|
$this->expectDeprecationMessage($message);
|
|
|
|
Deprecation::enable(true);
|
|
|
|
// using this syntax because my IDE was complaining about DeprecationTestObject not existing
|
|
|
|
// when trying to use `new DeprecationTestObject();`
|
|
|
|
$class = DeprecationTestObject::class;
|
|
|
|
new $class();
|
|
|
|
Deprecation::outputNotices();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testClassWithInjectorWithNoReplacement()
|
|
|
|
{
|
|
|
|
$message = implode(' ', [
|
|
|
|
'SilverStripe\Dev\Tests\DeprecationTest\DeprecationTestObject is deprecated.',
|
|
|
|
'Some class message.',
|
|
|
|
'Called from SilverStripe\Dev\Tests\DeprecationTest->testClassWithInjectorWithNoReplacement.'
|
|
|
|
]);
|
|
|
|
$this->expectDeprecation();
|
|
|
|
$this->expectDeprecationMessage($message);
|
|
|
|
Deprecation::enable(true);
|
|
|
|
Injector::inst()->get(DeprecationTestObject::class);
|
|
|
|
Deprecation::outputNotices();
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
2022-10-20 02:14:58 +02:00
|
|
|
/**
|
|
|
|
* @doesNotPerformAssertions
|
|
|
|
*/
|
|
|
|
public function testDisabled()
|
2016-12-16 05:34:21 +01:00
|
|
|
{
|
2022-11-15 06:20:54 +01:00
|
|
|
if ($this->noticesWereEnabled) {
|
|
|
|
$this->markTestSkipped('Notices are enabled for this project outside of this unit test');
|
|
|
|
}
|
2022-10-20 02:14:58 +02:00
|
|
|
// test that no error error is raised because by default Deprecation is disabled
|
2022-11-15 06:20:54 +01:00
|
|
|
$this->myDeprecatedMethod();
|
|
|
|
Deprecation::outputNotices();
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
2022-10-31 07:00:59 +01:00
|
|
|
|
|
|
|
// The following tests would be better to put in the silverstripe/config module, however this is not
|
|
|
|
// possible to do in a clean way as the config for the DeprecationTestObject will not load if it
|
|
|
|
// is inside the silverstripe/config directory, as there is no _config.php file or _config folder.
|
|
|
|
// Adding a _config.php file will break existing unit-tests within silverstripe/config
|
|
|
|
// It is possible to put DeprecationTestObject in framework and the unit tests in config, however
|
|
|
|
// that's probably messier then just having everything within framework
|
|
|
|
public function testConfigGetFirst()
|
|
|
|
{
|
|
|
|
$message = implode(' ', [
|
|
|
|
'Config SilverStripe\Dev\Tests\DeprecationTest\DeprecationTestObject.first_config is deprecated.',
|
|
|
|
'My first config message.'
|
|
|
|
]);
|
|
|
|
$this->expectDeprecation();
|
|
|
|
$this->expectDeprecationMessage($message);
|
|
|
|
Deprecation::enable();
|
|
|
|
Config::inst()->get(DeprecationTestObject::class, 'first_config');
|
2022-11-15 06:20:54 +01:00
|
|
|
Deprecation::outputNotices();
|
2022-10-31 07:00:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testConfigGetSecond()
|
|
|
|
{
|
|
|
|
$message = implode(' ', [
|
|
|
|
'Config SilverStripe\Dev\Tests\DeprecationTest\DeprecationTestObject.second_config is deprecated.',
|
|
|
|
'My second config message.'
|
|
|
|
]);
|
|
|
|
$this->expectDeprecation();
|
|
|
|
$this->expectDeprecationMessage($message);
|
|
|
|
Deprecation::enable();
|
|
|
|
Config::inst()->get(DeprecationTestObject::class, 'second_config');
|
2022-11-15 06:20:54 +01:00
|
|
|
Deprecation::outputNotices();
|
2022-10-31 07:00:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testConfigGetThird()
|
|
|
|
{
|
|
|
|
$message = 'Config SilverStripe\Dev\Tests\DeprecationTest\DeprecationTestObject.third_config is deprecated.';
|
|
|
|
$this->expectDeprecation();
|
|
|
|
$this->expectDeprecationMessage($message);
|
|
|
|
Deprecation::enable();
|
|
|
|
Config::inst()->get(DeprecationTestObject::class, 'third_config');
|
2022-11-15 06:20:54 +01:00
|
|
|
Deprecation::outputNotices();
|
2022-10-31 07:00:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testConfigSet()
|
|
|
|
{
|
|
|
|
$message = implode(' ', [
|
|
|
|
'Config SilverStripe\Dev\Tests\DeprecationTest\DeprecationTestObject.first_config is deprecated.',
|
|
|
|
'My first config message.'
|
|
|
|
]);
|
|
|
|
$this->expectDeprecation();
|
|
|
|
$this->expectDeprecationMessage($message);
|
|
|
|
Deprecation::enable();
|
|
|
|
Config::modify()->set(DeprecationTestObject::class, 'first_config', 'abc');
|
2022-11-15 06:20:54 +01:00
|
|
|
Deprecation::outputNotices();
|
2022-10-31 07:00:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testConfigMerge()
|
|
|
|
{
|
|
|
|
$message = implode(' ', [
|
|
|
|
'Config SilverStripe\Dev\Tests\DeprecationTest\DeprecationTestObject.array_config is deprecated.',
|
|
|
|
'My array config message.'
|
|
|
|
]);
|
|
|
|
$this->expectDeprecation();
|
|
|
|
$this->expectDeprecationMessage($message);
|
|
|
|
Deprecation::enable();
|
|
|
|
Config::modify()->merge(DeprecationTestObject::class, 'array_config', ['abc']);
|
2022-11-15 06:20:54 +01:00
|
|
|
Deprecation::outputNotices();
|
2022-10-31 07:00:59 +01:00
|
|
|
}
|
2011-10-27 23:45:12 +02:00
|
|
|
}
|