Skip to content

Commit

Permalink
API Stop using deprecated API
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed Nov 2, 2022
1 parent e454db6 commit ee3f54f
Show file tree
Hide file tree
Showing 11 changed files with 28 additions and 42 deletions.
8 changes: 6 additions & 2 deletions src/Control/Middleware/FlushMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

namespace SilverStripe\Control\Middleware;

use SilverStripe\Control\Director;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Core\BaseKernel;
use SilverStripe\Core\ClassInfo;
use SilverStripe\Core\Flushable;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Core\Kernel;

/**
* Triggers a call to flush() on all implementors of Flushable.
Expand All @@ -14,7 +16,9 @@ class FlushMiddleware implements HTTPMiddleware
{
public function process(HTTPRequest $request, callable $delegate)
{
if (Director::isManifestFlushed()) {
/** @var BaseKernel $kernel */
$kernel = Injector::inst()->get(Kernel::class);
if ((method_exists($kernel, 'isFlushed') && $kernel->isFlushed())) {
// Disable cache when flushing
HTTPCacheControlMiddleware::singleton()->disableCache(true);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace SilverStripe\Control\Middleware\URLSpecialsMiddleware;

use SilverStripe\Core\BaseKernel;
use SilverStripe\Core\Kernel;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Core\Startup\ScheduledFlushDiscoverer;
use SilverStripe\Control\Director;
use SilverStripe\Control\HTTPRequest;

/**
Expand All @@ -30,11 +30,12 @@ public function scheduleFlush(HTTPRequest $request)
{
$flush = array_key_exists('flush', $request->getVars() ?? []) || ($request->getURL() === 'dev/build');

if (!$flush || Director::isManifestFlushed()) {
/** @var BaseKernel $kernel */
$kernel = Injector::inst()->get(Kernel::class);
if (!$flush || (method_exists($kernel, 'isFlushed') && $kernel->isFlushed())) {
return false;
}

$kernel = Injector::inst()->get(Kernel::class);
ScheduledFlushDiscoverer::scheduleFlush($kernel);

return true;
Expand Down
3 changes: 0 additions & 3 deletions src/Core/BaseKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,12 +259,9 @@ protected function bootErrorHandling()
* Get the environment type
*
* @return string
*
* @deprecated 4.12.0 Use Director::get_environment_type() instead
*/
public function getEnvironment()
{
Deprecation::notice('4.12.0', 'Use Director::get_environment_type() instead');
// Check set
if ($this->enviroment) {
return $this->enviroment;
Expand Down
2 changes: 1 addition & 1 deletion src/Dev/Deprecation.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ public static function get_enabled()
// noop
}

private static function get_is_enabled(): bool
public static function get_is_enabled(): bool
{
if (!Director::isDev()) {
return false;
Expand Down
3 changes: 0 additions & 3 deletions src/ORM/Connect/DBSchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,9 @@ abstract class DBSchemaManager
/**
* @param string $table
* @param string $class
*
* @deprecated 4.0.1 Will be removed without equivalent functionality
*/
public static function showTableNameWarning($table, $class)
{
Deprecation::notice('4.0.1', 'Will be removed without equivalent functionality');
static::$table_name_warnings[$table] = $class;
}

Expand Down
4 changes: 4 additions & 0 deletions tests/php/Control/DirectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Core\Environment;
use SilverStripe\Core\Kernel;
use SilverStripe\Dev\Deprecation;
use SilverStripe\Dev\SapphireTest;

/**
Expand Down Expand Up @@ -856,6 +857,9 @@ public function testTestIgnoresHashes()

public function testRequestFilterInDirectorTest()
{
if (Deprecation::get_is_enabled()) {
$this->markTestSkipped('Test calls deprecated code');
}
$filter = new DirectorTest\TestRequestFilter;

$processor = new RequestProcessor([$filter]);
Expand Down
7 changes: 7 additions & 0 deletions tests/php/Control/HTTPRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\Middleware\TrustedProxyMiddleware;
use SilverStripe\Control\Session;
use SilverStripe\Dev\Deprecation;
use SilverStripe\Dev\SapphireTest;

class HTTPRequestTest extends SapphireTest
Expand Down Expand Up @@ -155,12 +156,18 @@ public function detectMethodDataProvider()
*/
public function testDetectMethod($realMethod, $post, $expected)
{
if (Deprecation::get_is_enabled()) {
$this->markTestSkipped('Test calls deprecated code');
}
$actual = HTTPRequest::detect_method($realMethod, $post);
$this->assertEquals($expected, $actual);
}

public function testBadDetectMethod()
{
if (Deprecation::get_is_enabled()) {
$this->markTestSkipped('Test calls deprecated code');
}
$this->expectException(\InvalidArgumentException::class);
HTTPRequest::detect_method('POST', ['_method' => 'Boom']);
}
Expand Down
7 changes: 6 additions & 1 deletion tests/php/Dev/DeprecationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@ class DeprecationTest extends SapphireTest

private $oldHandler = null;

private bool $noticesWereEnabled = false;

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();
$this->noticesWereEnabled = Deprecation::get_is_enabled();
$this->oldHandler = set_error_handler(function (int $errno, string $errstr, string $errfile, int $errline) {
if ($errno === E_USER_DEPRECATED) {
if (str_contains($errstr, 'SilverStripe\\Dev\\Tests\\DeprecationTest')) {
Expand All @@ -42,7 +45,9 @@ protected function setup(): void

protected function tearDown(): void
{
Deprecation::disable();
if (!$this->noticesWereEnabled) {
Deprecation::disable();
}
set_error_handler($this->oldHandler);
$this->oldHandler = null;
parent::tearDown();
Expand Down
12 changes: 0 additions & 12 deletions tests/php/ORM/SQLSelectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,6 @@ class SQLSelectTest extends SapphireTest

protected $oldDeprecation = null;

protected function setUp(): void
{
parent::setUp();
$this->oldDeprecation = Deprecation::dump_settings();
}

protected function tearDown(): void
{
Deprecation::restore_settings($this->oldDeprecation);
parent::tearDown();
}

public function testCount()
{

Expand Down
12 changes: 0 additions & 12 deletions tests/php/ORM/TransactionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,6 @@ class TransactionTest extends SapphireTest

private static $originalVersionInfo;

protected function setUp(): void
{
parent::setUp();
self::$originalVersionInfo = Deprecation::dump_settings();
}

protected function tearDown(): void
{
Deprecation::restore_settings(self::$originalVersionInfo);
parent::tearDown();
}

public static function setUpBeforeClass(): void
{
parent::setUpBeforeClass();
Expand Down
5 changes: 0 additions & 5 deletions tests/php/View/ArrayDataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,6 @@ public function testSetField()

public function testGetArray()
{
$originalDeprecation = Deprecation::dump_settings();
Deprecation::notification_version('2.4');

$array = [
'Foo' => 'Foo',
'Bar' => 'Bar',
Expand All @@ -79,8 +76,6 @@ public function testGetArray()
$arrayData = new ArrayData($array);

$this->assertEquals($arrayData->toMap(), $array);

Deprecation::restore_settings($originalDeprecation);
}

public function testArrayToObject()
Expand Down

0 comments on commit ee3f54f

Please sign in to comment.