Skip to content

Commit

Permalink
DEP Use phpunit 11
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed Sep 4, 2024
1 parent 1a53bc1 commit 11d7ac7
Show file tree
Hide file tree
Showing 10 changed files with 150 additions and 37 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
},
"require-dev": {
"composer/semver": "^3.4",
"phpunit/phpunit": "^9.6",
"phpunit/phpunit": "^11.3",
"silverstripe/versioned": "^3",
"squizlabs/php_codesniffer": "^3.7",
"silverstripe/standards": "^1",
Expand Down
27 changes: 26 additions & 1 deletion tests/php/Control/HTTPRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,35 @@
use SilverStripe\Control\Middleware\TrustedProxyMiddleware;
use SilverStripe\Control\Session;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Control\Tests\HTTPRequestTest\HTTPRequestTestException;

class HTTPRequestTest extends SapphireTest
{
protected static $fixture_file = null;

/**
* @param callable|null $oldHandler
*/
private $oldHandler = null;

protected function setup(): void
{
// Use custom error handler to allow the use of expectException() to catch warnings
// which is required in testWildCardWithFurtherParams().
// PHPUnit does not support expecting warnings
parent::setup();
$this->oldHandler = set_error_handler(function (int $errno, string $errstr, string $errfile, int $errline) {
throw new HTTPRequestTestException($errstr);
});
}

protected function tearDown(): void
{
restore_error_handler();
$this->oldHandler = null;
parent::tearDown();
}

public function testMatch()
{
$request = new HTTPRequest("GET", "admin/crm/add");
Expand Down Expand Up @@ -55,7 +79,8 @@ public function testWildCardMatch()
*/
public function testWildCardWithFurtherParams()
{
$this->expectWarning();
$this->expectException(HTTPRequestTestException::class);
$this->expectExceptionMessage('All URL params after wildcard parameter $@ will be ignored');
$request = new HTTPRequest('GET', 'admin/crm/test');
// all parameters after the first wildcard parameter are ignored
$request->match('admin/$Action/$@/$Other/$*', true);
Expand Down
10 changes: 10 additions & 0 deletions tests/php/Control/HTTPRequestTest/HTTPRequestTestException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace SilverStripe\Control\Tests\HTTPRequestTest;

use SilverStripe\Dev\TestOnly;
use Exception;

class HTTPRequestTestException extends Exception implements TestOnly
{
}
32 changes: 28 additions & 4 deletions tests/php/Control/RequestHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use SilverStripe\Control\Controller;
use SilverStripe\Control\Director;
use SilverStripe\Control\RequestHandler;
use SilverStripe\Control\Tests\RequestHandlerTest\RequestHandlerTestException;
use SilverStripe\Dev\SapphireTest;

/**
Expand All @@ -14,6 +15,29 @@ class RequestHandlerTest extends SapphireTest
{
protected $usesDatabase = false;

/**
* @param callable|null $oldHandler
*/
private $oldHandler = null;

protected function setup(): void
{
// Use custom error handler to allow the use of expectException() to catch warnings
// which is required in testLink() and testAbsoluteLink() which expect warnings.
// PHPUnit does not support expecting warnings
parent::setup();
$this->oldHandler = set_error_handler(function (int $errno, string $errstr, string $errfile, int $errline) {
throw new RequestHandlerTestException($errstr);
});
}

protected function tearDown(): void
{
restore_error_handler();
$this->oldHandler = null;
parent::tearDown();
}

public function provideTestLink(): array
{
return [
Expand Down Expand Up @@ -48,8 +72,8 @@ public function provideTestLink(): array
public function testLink(?string $urlSegment, ?string $action, ?string $expected)
{
if ($urlSegment === null) {
$this->expectWarning();
$this->expectWarningMessage('Request handler SilverStripe\Control\RequestHandler does not have a url_segment defined. Relying on this link may be an application error');
$this->expectException(RequestHandlerTestException::class);
$this->expectExceptionMessage('Request handler SilverStripe\Control\RequestHandler does not have a url_segment defined. Relying on this link may be an application error');
}

$handler = new RequestHandler();
Expand All @@ -73,8 +97,8 @@ public function testLink(?string $urlSegment, ?string $action, ?string $expected
public function testAbsoluteLink(?string $urlSegment, ?string $action, ?string $expected)
{
if ($urlSegment === null) {
$this->expectWarning();
$this->expectWarningMessage('Request handler SilverStripe\Control\RequestHandler does not have a url_segment defined. Relying on this link may be an application error');
$this->expectException(RequestHandlerTestException::class);
$this->expectExceptionMessage('Request handler SilverStripe\Control\RequestHandler does not have a url_segment defined. Relying on this link may be an application error');
}

$handler = new RequestHandler();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace SilverStripe\Control\Tests\RequestHandlerTest;

use SilverStripe\Dev\TestOnly;
use Exception;

class RequestHandlerTestException extends Exception implements TestOnly
{
}
57 changes: 29 additions & 28 deletions tests/php/Dev/DeprecationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use SilverStripe\Core\Environment;
use SilverStripe\Dev\Deprecation;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Dev\Tests\DeprecationTest\DeprecationTestException;
use SilverStripe\Dev\Tests\DeprecationTest\DeprecationTestObject;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Core\Kernel;
Expand All @@ -27,14 +28,14 @@ protected function setup(): void
{
// Use custom error handler for two reasons:
// - Filter out errors for deprecated class properties unrelated to this unit test
// - Allow the use of expectDeprecation(), which doesn't work with E_USER_DEPRECATION by default
// - Allow the use of expectException(), 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::isEnabled();
$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')) {
throw new Deprecated($errstr, $errno, '', 1);
throw new DeprecationTestException($errstr);
} else {
// Suppress any E_USER_DEPRECATED unrelated to this unit-test
return true;
Expand Down Expand Up @@ -73,8 +74,8 @@ public function testNotice()
'My message.',
'Called from SilverStripe\Dev\Tests\DeprecationTest->testNotice.'
]);
$this->expectDeprecation();
$this->expectDeprecationMessage($message);
$this->expectException(DeprecationTestException::class);
$this->expectExceptionMessage($message);
Deprecation::enable();
$ret = $this->myDeprecatedMethod();
$this->assertSame('abc', $ret);
Expand All @@ -90,8 +91,8 @@ public function testCallUserFunc()
'My message.',
'Called from SilverStripe\Dev\Tests\DeprecationTest->testCallUserFunc.'
]);
$this->expectDeprecation();
$this->expectDeprecationMessage($message);
$this->expectException(DeprecationTestException::class);
$this->expectExceptionMessage($message);
Deprecation::enable();
$ret = call_user_func([$this, 'myDeprecatedMethod']);
$this->assertSame('abc', $ret);
Expand All @@ -105,8 +106,8 @@ public function testCallUserFuncArray()
'My message.',
'Called from SilverStripe\Dev\Tests\DeprecationTest->testCallUserFuncArray.'
]);
$this->expectDeprecation();
$this->expectDeprecationMessage($message);
$this->expectException(DeprecationTestException::class);
$this->expectExceptionMessage($message);
Deprecation::enable();
$ret = call_user_func_array([$this, 'myDeprecatedMethod'], []);
$this->assertSame('abc', $ret);
Expand All @@ -130,8 +131,8 @@ public function testWithNoReplacementTrue()
'My message.',
'Called from SilverStripe\Dev\Tests\DeprecationTest->testWithNoReplacementTrue.'
]);
$this->expectDeprecation();
$this->expectDeprecationMessage($message);
$this->expectException(DeprecationTestException::class);
$this->expectExceptionMessage($message);
Deprecation::enable(true);
$ret = Deprecation::withNoReplacement(function () {
return $this->myDeprecatedMethod();
Expand All @@ -147,8 +148,8 @@ public function testWithNoReplacementTrueCallUserFunc()
'My message.',
'Called from SilverStripe\Dev\Tests\DeprecationTest->testWithNoReplacementTrueCallUserFunc.'
]);
$this->expectDeprecation();
$this->expectDeprecationMessage($message);
$this->expectException(DeprecationTestException::class);
$this->expectExceptionMessage($message);
Deprecation::enable(true);
$ret = Deprecation::withNoReplacement(function () {
return call_user_func([$this, 'myDeprecatedMethod']);
Expand All @@ -164,8 +165,8 @@ public function testNoticeWithNoReplacementTrue()
'My message.',
'Called from PHPUnit\Framework\TestCase->runTest.'
]);
$this->expectDeprecation();
$this->expectDeprecationMessage($message);
$this->expectException(DeprecationTestException::class);
$this->expectExceptionMessage($message);
Deprecation::enable(true);
Deprecation::withNoReplacement(function () {
Deprecation::notice('123', 'My message.');
Expand All @@ -180,8 +181,8 @@ public function testClassWithNoReplacement()
'Some class message.',
'Called from SilverStripe\Dev\Tests\DeprecationTest->testClassWithNoReplacement.'
]);
$this->expectDeprecation();
$this->expectDeprecationMessage($message);
$this->expectException(DeprecationTestException::class);
$this->expectExceptionMessage($message);
Deprecation::enable(true);
// using this syntax because my IDE was complaining about DeprecationTestObject not existing
// when trying to use `new DeprecationTestObject();`
Expand All @@ -197,8 +198,8 @@ public function testClassWithInjectorWithNoReplacement()
'Some class message.',
'Called from SilverStripe\Dev\Tests\DeprecationTest->testClassWithInjectorWithNoReplacement.'
]);
$this->expectDeprecation();
$this->expectDeprecationMessage($message);
$this->expectException(DeprecationTestException::class);
$this->expectExceptionMessage($message);
Deprecation::enable(true);
Injector::inst()->get(DeprecationTestObject::class);
Deprecation::outputNotices();
Expand Down Expand Up @@ -229,8 +230,8 @@ public function testConfigGetFirst()
'Config SilverStripe\Dev\Tests\DeprecationTest\DeprecationTestObject.first_config is deprecated.',
'My first config message.'
]);
$this->expectDeprecation();
$this->expectDeprecationMessage($message);
$this->expectException(DeprecationTestException::class);
$this->expectExceptionMessage($message);
Deprecation::enable();
Config::inst()->get(DeprecationTestObject::class, 'first_config');
Deprecation::outputNotices();
Expand All @@ -242,8 +243,8 @@ public function testConfigGetSecond()
'Config SilverStripe\Dev\Tests\DeprecationTest\DeprecationTestObject.second_config is deprecated.',
'My second config message.'
]);
$this->expectDeprecation();
$this->expectDeprecationMessage($message);
$this->expectException(DeprecationTestException::class);
$this->expectExceptionMessage($message);
Deprecation::enable();
Config::inst()->get(DeprecationTestObject::class, 'second_config');
Deprecation::outputNotices();
Expand All @@ -252,8 +253,8 @@ public function testConfigGetSecond()
public function testConfigGetThird()
{
$message = 'Config SilverStripe\Dev\Tests\DeprecationTest\DeprecationTestObject.third_config is deprecated.';
$this->expectDeprecation();
$this->expectDeprecationMessage($message);
$this->expectException(DeprecationTestException::class);
$this->expectExceptionMessage($message);
Deprecation::enable();
Config::inst()->get(DeprecationTestObject::class, 'third_config');
Deprecation::outputNotices();
Expand All @@ -265,8 +266,8 @@ public function testConfigSet()
'Config SilverStripe\Dev\Tests\DeprecationTest\DeprecationTestObject.first_config is deprecated.',
'My first config message.'
]);
$this->expectDeprecation();
$this->expectDeprecationMessage($message);
$this->expectException(DeprecationTestException::class);
$this->expectExceptionMessage($message);
Deprecation::enable();
Config::modify()->set(DeprecationTestObject::class, 'first_config', 'abc');
Deprecation::outputNotices();
Expand All @@ -278,8 +279,8 @@ public function testConfigMerge()
'Config SilverStripe\Dev\Tests\DeprecationTest\DeprecationTestObject.array_config is deprecated.',
'My array config message.'
]);
$this->expectDeprecation();
$this->expectDeprecationMessage($message);
$this->expectException(DeprecationTestException::class);
$this->expectExceptionMessage($message);
Deprecation::enable();
Config::modify()->merge(DeprecationTestObject::class, 'array_config', ['abc']);
Deprecation::outputNotices();
Expand Down
10 changes: 10 additions & 0 deletions tests/php/Dev/DeprecationTest/DeprecationTestException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace SilverStripe\Dev\Tests\DeprecationTest;

use Exception;
use SilverStripe\Dev\TestOnly;

class DeprecationTestException extends Exception implements TestOnly
{
}
2 changes: 1 addition & 1 deletion tests/php/ORM/DataListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,7 @@ public function testSortWithCompositeSyntax()
public function testSortInvalidParameters()
{
$this->expectException(InvalidArgumentException::class);
$this->expectDeprecationMessage('Fans is not a linear relation on model SilverStripe\ORM\Tests\DataObjectTest\Player');
$this->expectExceptionMessage('Fans is not a linear relation on model SilverStripe\ORM\Tests\DataObjectTest\Player');
$list = Team::get();
$list->sort('Founder.Fans.Surname'); // Can't sort on has_many
}
Expand Down
27 changes: 25 additions & 2 deletions tests/php/ORM/DataQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use SilverStripe\ORM\Queries\SQLSelect;
use SilverStripe\ORM\Tests\DataQueryTest\ObjectE;
use SilverStripe\Security\Member;
use SilverStripe\ORM\Tests\DataQueryTest\DataQueryTestException;

class DataQueryTest extends SapphireTest
{
Expand All @@ -35,6 +36,28 @@ class DataQueryTest extends SapphireTest
SQLSelectTest\TestChild::class,
];

/**
* @param callable|null $oldHandler
*/
private $oldHandler = null;

protected function setup(): void
{
// Use custom error handler to allow the use of expectException() to catch errors
// which is required in testFieldCollision(). PHPUnit does not support expecting errors
parent::setup();
$this->oldHandler = set_error_handler(function (int $errno, string $errstr, string $errfile, int $errline) {
throw new DataQueryTestException($errstr);
});
}

protected function tearDown(): void
{
restore_error_handler();
$this->oldHandler = null;
parent::tearDown();
}

public function testSortByJoinedFieldRetainsSourceInformation()
{
$bar = new DataQueryTest\ObjectC();
Expand Down Expand Up @@ -235,8 +258,8 @@ public function testFieldCollision($allowCollisions)
if ($allowCollisions) {
$this->assertSQLContains('THEN "DataQueryTest_B"."Title" WHEN COALESCE(NULL, 1) AS "Title" IS NOT NULL THEN COALESCE(NULL, 1) AS "Title" ELSE NULL END AS "Title"', $dataQuery->sql());
} else {
$this->expectError();
$this->expectErrorMessageMatches('/^Bad collision item /');
$this->expectException(DataQueryTestException::class);
$this->expectExceptionMessageMatches('/^Bad collision item /');
}

$dataQuery->getFinalisedQuery();
Expand Down
10 changes: 10 additions & 0 deletions tests/php/ORM/DataQueryTest/DataQueryTestException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace SilverStripe\ORM\Tests\DataQueryTest;

use SilverStripe\Dev\TestOnly;
use Exception;

class DataQueryTestException extends Exception implements TestOnly
{
}

0 comments on commit 11d7ac7

Please sign in to comment.