From 55b598d9305ca58d7095bfad8e89676bb44b16d8 Mon Sep 17 00:00:00 2001 From: Steve Boyd Date: Mon, 1 Nov 2021 16:22:13 +1300 Subject: [PATCH] API phpunit9 support --- composer.json | 5 ++- phpunit.xml.dist | 8 +++-- tests/Auth/HandlerTest.php | 17 ++++------ tests/ConnectionTest.php | 4 +-- tests/ControllerTest.php | 30 ++++++++-------- tests/ManagerTest.php | 14 ++++---- tests/Middleware/CSRFMiddlewareTest.php | 4 +-- tests/Middleware/HTTPMethodMiddlewareTest.php | 4 +-- .../Middleware/MiddlewareProcessTestBase.php | 2 +- tests/PersistedQuery/HTTPProviderTest.php | 2 +- .../PersistedQuery/JSONStringProviderTest.php | 2 +- tests/QueryFilter/ReadTest.php | 2 +- .../Scaffolders/ArgumentScaffolderTest.php | 2 +- .../Scaffolders/CRUD/CreateTest.php | 6 ++-- .../Scaffolders/CRUD/DeleteTest.php | 4 +-- .../Scaffolders/CRUD/ReadOneTest.php | 2 +- .../Scaffolding/Scaffolders/CRUD/ReadTest.php | 2 +- .../Scaffolders/CRUD/UpdateTest.php | 4 +-- .../Scaffolders/DataObjectScaffolderTest.php | 34 +++++++++---------- .../Scaffolders/InheritanceScaffolderTest.php | 8 ++--- .../Scaffolders/ListQueryScaffolderTest.php | 4 +-- .../Scaffolders/OperationScaffolderTest.php | 16 ++++----- .../Scaffolders/SchemaScaffolderTest.php | 16 ++++----- .../Scaffolders/UnionScaffolderTest.php | 4 +-- tests/Scaffolding/StaticSchemaTest.php | 23 ++++++++----- .../Scaffolding/Util/ArrayTypeParserTest.php | 8 ++--- tests/Scaffolding/Util/OperationListTest.php | 4 +-- .../Scaffolding/Util/StringTypeParserTest.php | 8 ++--- tests/TypeCreatorTest.php | 4 +-- .../Util/CaseInsensitiveFieldAccessorTest.php | 12 ++----- 30 files changed, 127 insertions(+), 128 deletions(-) diff --git a/composer.json b/composer.json index 80b237a74..c3ce07c3c 100755 --- a/composer.json +++ b/composer.json @@ -4,7 +4,7 @@ "type": "silverstripe-vendormodule", "license": "BSD-3-Clause", "require": { - "silverstripe/framework": "^4.2", + "silverstripe/framework": "^4.10", "silverstripe/vendor-plugin": "^1.0", "webonyx/graphql-php": "~0.12.6" }, @@ -17,8 +17,7 @@ "dnadesign/silverstripe-elemental": "<4.6" }, "require-dev": { - "sminnee/phpunit": "^5.7", - "sminnee/phpunit-mock-objects": "^3.4.5", + "phpunit/phpunit": "^9.5", "squizlabs/php_codesniffer": "^3.0" }, "autoload": { diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 5c53cbdd9..0281bef32 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,8 +1,10 @@ - - tests - + + + tests + + diff --git a/tests/Auth/HandlerTest.php b/tests/Auth/HandlerTest.php index 7633db30f..f5c89faf3 100644 --- a/tests/Auth/HandlerTest.php +++ b/tests/Auth/HandlerTest.php @@ -22,7 +22,7 @@ class HandlerTest extends SapphireTest /** * {@inheritDoc} */ - public function setUp() + protected function setUp(): void { parent::setUp(); Handler::config()->remove('authenticators'); @@ -65,12 +65,11 @@ public function testGetAuthenticator() /** * Test that an exception is thrown if an authenticator is configured that doesn't implement the interface - * - * @expectedException \SilverStripe\ORM\ValidationException - * @expectedExceptionMessage stdClass must implement SilverStripe\GraphQL\Auth\AuthenticatorInterface! */ public function testExceptionThrownWhenAuthenticatorDoesNotImplementAuthenticatorInterface() { + $this->expectException(\SilverStripe\ORM\ValidationException::class); + $this->expectExceptionMessage('stdClass must implement SilverStripe\GraphQL\Auth\AuthenticatorInterface!'); Handler::config()->update('authenticators', [ ['class' => 'stdClass'] ]); @@ -131,12 +130,11 @@ public function prioritisedAuthenticatorProvider() /** * Ensure that an failed authentication attempt throws an exception - * - * @expectedException \SilverStripe\ORM\ValidationException - * @expectedExceptionMessage Never! */ public function testFailedAuthenticationThrowsException() { + $this->expectException(\SilverStripe\ORM\ValidationException::class); + $this->expectExceptionMessage('Never!'); Handler::config()->update('authenticators', [ ['class' => BrutalAuthenticatorFake::class] ]); @@ -148,12 +146,11 @@ public function testFailedAuthenticationThrowsException() * Ensure that when a falsy value is returned from an authenticator (when it should throw * an exception on failure) that a sensible default message is used in a ValidationException * instead. - * - * @expectedException \SilverStripe\ORM\ValidationException - * @expectedExceptionMessage Authentication failed. */ public function testFailedAuthenticationWithFalsyReturnValueThrowsDefaultException() { + $this->expectException(\SilverStripe\ORM\ValidationException::class); + $this->expectExceptionMessage('Authentication failed'); Handler::config()->update('authenticators', [ ['class' => FalsyAuthenticatorFake::class] ]); diff --git a/tests/ConnectionTest.php b/tests/ConnectionTest.php index cf7d253d5..1e6cb4e90 100644 --- a/tests/ConnectionTest.php +++ b/tests/ConnectionTest.php @@ -33,7 +33,7 @@ class ConnectionTest extends SapphireTest */ private $manager; - public function setUp() + protected function setUp(): void { parent::setUp(); @@ -158,7 +158,7 @@ public function testResolveListSortWithCustomMapping() public function testSortByInvalidColumnThrowsException() { - $this->expectException(InvalidArgumentException::class); + $this->expectException(\InvalidArgumentException::class); $list = DataObjectFake::get(); diff --git a/tests/ControllerTest.php b/tests/ControllerTest.php index 5538b4ead..881f5f813 100644 --- a/tests/ControllerTest.php +++ b/tests/ControllerTest.php @@ -4,7 +4,8 @@ use Exception; use GraphQL\Type\Definition\Type; -use PHPUnit_Framework_MockObject_MockBuilder; +use PHPUnit\Framework\Assert; +use PHPUnit\Framework\MockObject\MockBuilder; use SilverStripe\Assets\Dev\TestAssetStore; use SilverStripe\Control\HTTPRequest; use SilverStripe\Control\HTTPResponse; @@ -30,7 +31,7 @@ class ControllerTest extends SapphireTest { protected $usesDatabase = true; - public function setUp() + protected function setUp(): void { parent::setUp(); @@ -43,7 +44,7 @@ public function setUp() TestAssetStore::activate('GraphQLController'); } - public function tearDown() + protected function tearDown(): void { TestAssetStore::reset(); parent::tearDown(); @@ -83,7 +84,7 @@ public function testIndexWithException() $kernel = Injector::inst()->get(Kernel::class); $kernel->setEnvironment(Kernel::LIVE); - /** @var Manager|PHPUnit_Framework_MockObject_MockBuilder $managerMock */ + /** @var Manager|MockBuilder $managerMock */ $managerMock = $this->getMockBuilder(Manager::class) ->setMethods(['query']) ->getMock(); @@ -103,7 +104,7 @@ public function testIndexWithException() public function testIndexWithExceptionIncludesTraceInDevMode() { - /** @var Manager|PHPUnit_Framework_MockObject_MockBuilder $managerMock */ + /** @var Manager|MockBuilder $managerMock */ $managerMock = $this->getMockBuilder(Manager::class) ->setMethods(['query']) ->getMock(); @@ -148,9 +149,12 @@ public function testAuthenticationProtectionOnQueries($authenticator, $shouldFai $manager->addQuery($this->getQuery($manager), 'myquery'); $response = $controller->index(new HTTPRequest('GET', '')); - $assertion = ($shouldFail) ? 'assertContains' : 'assertNotContains'; // See Fake\BrutalAuthenticatorFake::authenticate for failure message - $this->{$assertion}('Never!', $response->getBody()); + if ($shouldFail) { + Assert::assertStringContainsString('Never!', $response->getBody()); + } else { + Assert::assertStringNotContainsString('Never!', $response->getBody()); + } } /** @@ -170,11 +174,9 @@ public function authenticatorProvider() ]; } - /** - * @expectedException \SilverStripe\Control\HTTPResponse_Exception - */ public function testAddCorsHeadersOriginDisallowed() { + $this->expectException(HTTPResponse_Exception::class); Config::modify()->set(Controller::class, 'cors', [ 'Enabled' => true, 'Allow-Origin' => null, @@ -394,7 +396,7 @@ public function testCorsOverride() public function testTypeCaching() { $expectedSchemaPath = TestAssetStore::base_path() . '/testSchema.types.graphql'; - $this->assertFileNotExists($expectedSchemaPath, 'Schema is not automatically cached'); + $this->assertFileDoesNotExist($expectedSchemaPath, 'Schema is not automatically cached'); Config::modify()->set(Controller::class, 'cache_types_in_filesystem', true); $controller = Controller::create(new Manager('testSchema')); @@ -410,7 +412,7 @@ public function testTypeCaching() Controller::create(new Manager('testSchema'))->processTypeCaching(); // Static cache should be removed when caching is disabled - $this->assertFileNotExists($expectedSchemaPath, 'Schema is not cached'); + $this->assertFileDoesNotExist($expectedSchemaPath, 'Schema is not cached'); } public function testIntrospectionProvider() @@ -562,7 +564,7 @@ protected function assertQueryError(Controller $controller, HTTPRequest $request $data = json_decode($controller->handleRequest($request)->getBody(), true); $this->assertArrayHasKey('errors', $data); $this->assertCount(1, $data['errors']); - $this->assertRegExp($regExp, $data['errors'][0]['message']); + $this->assertMatchesRegularExpression($regExp, $data['errors'][0]['message']); } protected function assertQuerySuccess(Controller $controller, HTTPRequest $request, $operation) @@ -657,7 +659,7 @@ protected function getQuery(Manager $manager) } /** - * @return \PHPUnit_Framework_MockObject_MockObject + * @return \MockObject */ protected function getStaticSchemaMock() { diff --git a/tests/ManagerTest.php b/tests/ManagerTest.php index 9a9a81712..df499b55e 100644 --- a/tests/ManagerTest.php +++ b/tests/ManagerTest.php @@ -21,7 +21,7 @@ class ManagerTest extends SapphireTest { - protected function setUp() + protected function setUp(): void { parent::setUp(); /** @var IdentityStore $store */ @@ -135,18 +135,18 @@ public function testSchemaKey() $manager->setSchemaKey('test'); $this->assertEquals('test', $manager->getSchemaKey()); - $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessageRegExp('/must be a string/'); + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessageMatches('/must be a string/'); $manager->setSchemaKey(['test']); $this->assertEquals('test', $manager->getSchemaKey()); - $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessageRegExp('/cannnot be empty/'); + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessageMatches('/cannnot be empty/'); $manager->setSchemaKey(''); $this->assertEquals('test', $manager->getSchemaKey()); - $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessageRegExp('/alphanumeric/'); + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessageMatches('/alphanumeric/'); $manager->setSchemaKey('completely % invalid #key'); $this->assertEquals('test', $manager->getSchemaKey()); } diff --git a/tests/Middleware/CSRFMiddlewareTest.php b/tests/Middleware/CSRFMiddlewareTest.php index 8e6acbdf5..4f758561a 100644 --- a/tests/Middleware/CSRFMiddlewareTest.php +++ b/tests/Middleware/CSRFMiddlewareTest.php @@ -23,7 +23,7 @@ public function testItDoesntDoAnythingIfNotAMutation() public function testItThrowsIfNoTokenIsProvided() { $this->expectException(Exception::class); - $this->expectExceptionMessageRegExp('/must provide a CSRF token/'); + $this->expectExceptionMessageMatches('/must provide a CSRF token/'); $result = $this->simulateMiddlewareProcess( new CSRFMiddleware(), ' mutation someMutation { tester }' @@ -69,7 +69,7 @@ public function testItThrowsIfNoTokenIsProvided() public function testItThrowsIfTokenIsInvalid() { $this->expectException(Exception::class); - $this->expectExceptionMessageRegExp('/Invalid CSRF token/'); + $this->expectExceptionMessageMatches('/Invalid CSRF token/'); $result = $this->simulateMiddlewareProcess( new CSRFMiddleware(), ' mutation someMutation { tester }', diff --git a/tests/Middleware/HTTPMethodMiddlewareTest.php b/tests/Middleware/HTTPMethodMiddlewareTest.php index 68ec6d10c..47c74eecc 100644 --- a/tests/Middleware/HTTPMethodMiddlewareTest.php +++ b/tests/Middleware/HTTPMethodMiddlewareTest.php @@ -24,7 +24,7 @@ public function testItDoesntDoAnythingIfNotAMutation() public function testItThrowsIfNotPOSTorGET() { $this->expectException(Exception::class); - $this->expectExceptionMessageRegExp('/must be POST or GET/'); + $this->expectExceptionMessageMatches('/must be POST or GET/'); $result = $this->simulateMiddlewareProcess( new HTTPMethodMiddleware(), ' query someQuery { tester }', @@ -36,7 +36,7 @@ public function testItThrowsIfNotPOSTorGET() public function testItThrowsIfMutationIsNotPOST() { $this->expectException(Exception::class); - $this->expectExceptionMessageRegExp('/Mutations must use the POST/'); + $this->expectExceptionMessageMatches('/Mutations must use the POST/'); $result = $this->simulateMiddlewareProcess( new HTTPMethodMiddleware(), ' mutation someMutation { tester }', diff --git a/tests/Middleware/MiddlewareProcessTestBase.php b/tests/Middleware/MiddlewareProcessTestBase.php index 32fdd5ba9..c5b9fc937 100644 --- a/tests/Middleware/MiddlewareProcessTestBase.php +++ b/tests/Middleware/MiddlewareProcessTestBase.php @@ -15,7 +15,7 @@ abstract class MiddlewareProcessTestBase extends SapphireTest */ protected $defaultCallback; - protected function setUp() + protected function setUp(): void { parent::setUp(); $this->defaultCallback = function () { diff --git a/tests/PersistedQuery/HTTPProviderTest.php b/tests/PersistedQuery/HTTPProviderTest.php index bdc93b7aa..c63223826 100644 --- a/tests/PersistedQuery/HTTPProviderTest.php +++ b/tests/PersistedQuery/HTTPProviderTest.php @@ -14,7 +14,7 @@ public function testURLValidation() { /* @var HTTPProvider $provider */ $provider = Injector::inst()->create(HTTPProvider::class); - $this->expectException(InvalidArgumentException::class); + $this->expectException(\InvalidArgumentException::class); $provider->setSchemaMapping(['default' => 'not a url']); } diff --git a/tests/PersistedQuery/JSONStringProviderTest.php b/tests/PersistedQuery/JSONStringProviderTest.php index 46373390a..325f87110 100644 --- a/tests/PersistedQuery/JSONStringProviderTest.php +++ b/tests/PersistedQuery/JSONStringProviderTest.php @@ -13,7 +13,7 @@ public function testJSONValidation() { /* @var PersistedQueryMappingProvider $provider */ $provider = new JSONStringProvider(); - $this->expectException(InvalidArgumentException::class); + $this->expectException(\InvalidArgumentException::class); $provider->setSchemaMapping(['default' => 'not a JSON string']); } diff --git a/tests/QueryFilter/ReadTest.php b/tests/QueryFilter/ReadTest.php index 156be3800..a9f9320f3 100644 --- a/tests/QueryFilter/ReadTest.php +++ b/tests/QueryFilter/ReadTest.php @@ -21,7 +21,7 @@ class ReadTest extends SapphireTest DataObjectFake::class, ]; - protected function setUp() + protected function setUp(): void { parent::setUp(); // Make sure we're only testing the native features diff --git a/tests/Scaffolding/Scaffolders/ArgumentScaffolderTest.php b/tests/Scaffolding/Scaffolders/ArgumentScaffolderTest.php index 37c336eaa..97f5b5750 100644 --- a/tests/Scaffolding/Scaffolders/ArgumentScaffolderTest.php +++ b/tests/Scaffolding/Scaffolders/ArgumentScaffolderTest.php @@ -63,7 +63,7 @@ public function testNonInternalType() public function testNonInternalTypeNoManager() { - $this->expectException(InvalidArgumentException::class); + $this->expectException(\InvalidArgumentException::class); $scaffolder = new ArgumentScaffolder('Test', 'MyType'); $scaffolder->toArray(); } diff --git a/tests/Scaffolding/Scaffolders/CRUD/CreateTest.php b/tests/Scaffolding/Scaffolders/CRUD/CreateTest.php index becd28f36..3284ee8d7 100644 --- a/tests/Scaffolding/Scaffolders/CRUD/CreateTest.php +++ b/tests/Scaffolding/Scaffolders/CRUD/CreateTest.php @@ -25,7 +25,7 @@ class CreateTest extends SapphireTest RestrictedDataObjectFake::class, ]; - protected function setUp() + protected function setUp(): void { parent::setUp(); StaticSchema::inst()->setTypeNames([]); @@ -35,7 +35,7 @@ protected function setUp() } } - protected function tearDown() + protected function tearDown(): void { StaticSchema::inst()->setTypeNames([]); parent::tearDown(); @@ -142,7 +142,7 @@ public function testCreateOperationPermissionCheck() $scaffold = $create->scaffold($manager); $this->expectException(Exception::class); - $this->expectExceptionMessageRegExp('/Cannot create/'); + $this->expectExceptionMessageMatches('/Cannot create/'); $scaffold['resolve']( null, diff --git a/tests/Scaffolding/Scaffolders/CRUD/DeleteTest.php b/tests/Scaffolding/Scaffolders/CRUD/DeleteTest.php index b7c9b7afc..6620e8744 100644 --- a/tests/Scaffolding/Scaffolders/CRUD/DeleteTest.php +++ b/tests/Scaffolding/Scaffolders/CRUD/DeleteTest.php @@ -24,7 +24,7 @@ class DeleteTest extends SapphireTest RestrictedDataObjectFake::class, ]; - protected function setUp() + protected function setUp(): void { parent::setUp(); // Make sure we're only testing the native features @@ -124,7 +124,7 @@ public function testDeleteOperationPermissionCheck() $scaffold = $delete->scaffold($manager); $this->expectException(Exception::class); - $this->expectExceptionMessageRegExp('/Cannot delete/'); + $this->expectExceptionMessageMatches('/Cannot delete/'); $scaffold['resolve']( $restrictedDataobject, diff --git a/tests/Scaffolding/Scaffolders/CRUD/ReadOneTest.php b/tests/Scaffolding/Scaffolders/CRUD/ReadOneTest.php index 00f0348e9..a430232b0 100644 --- a/tests/Scaffolding/Scaffolders/CRUD/ReadOneTest.php +++ b/tests/Scaffolding/Scaffolders/CRUD/ReadOneTest.php @@ -21,7 +21,7 @@ class ReadOneTest extends SapphireTest RestrictedDataObjectFake::class, ]; - protected function setUp() + protected function setUp(): void { parent::setUp(); // Make sure we're only testing the native features diff --git a/tests/Scaffolding/Scaffolders/CRUD/ReadTest.php b/tests/Scaffolding/Scaffolders/CRUD/ReadTest.php index 080a0eeaa..e3d85463a 100644 --- a/tests/Scaffolding/Scaffolders/CRUD/ReadTest.php +++ b/tests/Scaffolding/Scaffolders/CRUD/ReadTest.php @@ -20,7 +20,7 @@ class ReadTest extends SapphireTest RestrictedDataObjectFake::class, ]; - protected function setUp() + protected function setUp(): void { parent::setUp(); // Make sure we're only testing the native features diff --git a/tests/Scaffolding/Scaffolders/CRUD/UpdateTest.php b/tests/Scaffolding/Scaffolders/CRUD/UpdateTest.php index 80cb52e5d..74704c892 100644 --- a/tests/Scaffolding/Scaffolders/CRUD/UpdateTest.php +++ b/tests/Scaffolding/Scaffolders/CRUD/UpdateTest.php @@ -25,7 +25,7 @@ class UpdateTest extends SapphireTest RestrictedDataObjectFake::class, ]; - protected function setUp() + protected function setUp(): void { parent::setUp(); // Make sure we're only testing the native features @@ -138,7 +138,7 @@ public function testUpdateOperationPermissionCheck() $scaffold = $update->scaffold($manager); $this->expectException(Exception::class); - $this->expectExceptionMessageRegExp('/Cannot edit/'); + $this->expectExceptionMessageMatches('/Cannot edit/'); $scaffold['resolve']( $restrictedDataobject, diff --git a/tests/Scaffolding/Scaffolders/DataObjectScaffolderTest.php b/tests/Scaffolding/Scaffolders/DataObjectScaffolderTest.php index 329f314a5..0a96a7cd4 100644 --- a/tests/Scaffolding/Scaffolders/DataObjectScaffolderTest.php +++ b/tests/Scaffolding/Scaffolders/DataObjectScaffolderTest.php @@ -28,7 +28,7 @@ class DataObjectScaffolderTest extends SapphireTest { - protected function setUp() + protected function setUp(): void { parent::setUp(); foreach (Read::get_extensions() as $class) { @@ -42,8 +42,8 @@ public function testDataObjectScaffolderConstructor() $this->assertEquals(DataObjectFake::class, $scaffolder->getDataObjectClass()); $this->assertInstanceOf(DataObjectFake::class, $scaffolder->getDataObjectInstance()); - $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessageRegExp('/Non-existent classname/i'); + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessageMatches('/Non-existent classname/i'); new DataObjectScaffolder('fail'); } @@ -139,8 +139,8 @@ public function testDataObjectScaffolderOperations() $scaffolder->removeOperation(SchemaScaffolder::DELETE); $this->assertEquals(1, $scaffolder->getOperations()->count()); - $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessageRegExp('/Invalid operation/'); + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessageMatches('/Invalid operation/'); $scaffolder = $this->getFakeScaffolder(); $scaffolder->operation('fail'); } @@ -174,8 +174,8 @@ public function testDataObjectScaffolderNestedQueries() $scaffolder->addToManager($managerMock); // Can't add a nested query for a regular field - $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessageRegExp('/returns a DataList or ArrayList/'); + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessageMatches('/returns a DataList or ArrayList/'); $scaffolder = $this->getFakeScaffolder(); $scaffolder->nestedQuery('MyField'); } @@ -272,7 +272,7 @@ public function testDataObjectScaffolderApplyConfigNoFieldsException() // Must have "fields" defined $this->expectException(Exception::class); - $this->expectExceptionMessageRegExp('/^No fields or nestedQueries/'); + $this->expectExceptionMessageMatches('/^No fields or nestedQueries/'); $scaffolder->applyConfig([ 'operations' => ['create' => true], ]); @@ -284,7 +284,7 @@ public function testDataObjectScaffolderApplyConfigInvalidFieldsException() // Invalid fields $this->expectException(Exception::class); - $this->expectExceptionMessageRegExp('/Fields must be an array/'); + $this->expectExceptionMessageMatches('/Fields must be an array/'); $scaffolder->applyConfig([ 'fields' => 'fail', ]); @@ -295,8 +295,8 @@ public function testDataObjectScaffolderApplyConfigInvalidFieldsExceptException( $scaffolder = $this->getFakeScaffolder(); // Invalid fieldsExcept - $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessageRegExp('/"excludeFields" must be an enumerated list/'); + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessageMatches('/"excludeFields" must be an enumerated list/'); $scaffolder->applyConfig([ 'fields' => ['MyField'], 'excludeFields' => 'fail', @@ -309,7 +309,7 @@ public function testDataObjectScaffolderApplyConfigInvalidOperationsException() // Invalid operations $this->expectException(Exception::class); - $this->expectExceptionMessageRegExp('/Operations field must be a map/'); + $this->expectExceptionMessageMatches('/Operations field must be a map/'); $scaffolder->applyConfig([ 'fields' => ['MyField'], 'operations' => ['create'], @@ -322,7 +322,7 @@ public function testDataObjectScaffolderApplyConfigInvalidNestedQueriesException // Invalid nested queries $this->expectException(Exception::class); - $this->expectExceptionMessageRegExp('/"nestedQueries" must be a map of relation name/'); + $this->expectExceptionMessageMatches('/"nestedQueries" must be a map of relation name/'); $scaffolder->applyConfig([ 'fields' => ['MyField'], 'nestedQueries' => ['Files'], @@ -374,8 +374,8 @@ public function testDataObjectScaffolderScaffold() public function testDataObjectScaffolderScaffoldFieldException() { - $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessageRegExp('/Invalid field/'); + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessageMatches('/Invalid field/'); $scaffolder = $this->getFakeScaffolder() ->addFields(['not a field']) ->scaffold(new Manager()); @@ -384,8 +384,8 @@ public function testDataObjectScaffolderScaffoldFieldException() public function testDataObjectScaffolderScaffoldNestedQueryException() { - $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessageRegExp('/returns a list/'); + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessageMatches('/returns a list/'); $scaffolder = $this->getFakeScaffolder() ->addFields(['Files']) ->scaffold(new Manager()); diff --git a/tests/Scaffolding/Scaffolders/InheritanceScaffolderTest.php b/tests/Scaffolding/Scaffolders/InheritanceScaffolderTest.php index 8cdd43ad5..e84376839 100644 --- a/tests/Scaffolding/Scaffolders/InheritanceScaffolderTest.php +++ b/tests/Scaffolding/Scaffolders/InheritanceScaffolderTest.php @@ -18,16 +18,16 @@ class InheritanceScaffolderTest extends SapphireTest { public function testThrowsOnNonExistentClass() { - $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessageRegExp('/not exist/'); + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessageMatches('/not exist/'); $scaffolder = new InheritanceScaffolder('fail'); } public function testThrowsOnNonDataObjectClass() { - $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessageRegExp('/subclass of/'); + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessageMatches('/subclass of/'); $scaffolder = new InheritanceScaffolder(Controller::class); } diff --git a/tests/Scaffolding/Scaffolders/ListQueryScaffolderTest.php b/tests/Scaffolding/Scaffolders/ListQueryScaffolderTest.php index eeed43135..344617a91 100644 --- a/tests/Scaffolding/Scaffolders/ListQueryScaffolderTest.php +++ b/tests/Scaffolding/Scaffolders/ListQueryScaffolderTest.php @@ -162,8 +162,8 @@ public function testListQueryScaffolderApplyConfig() public function testListQueryScaffolderApplyConfigThrowsOnBadSortableFields() { - $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessageRegExp('/sortableFields must be an array/'); + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessageMatches('/sortableFields must be an array/'); $scaffolder = new ListQueryScaffolder('testQuery', 'testType'); $scaffolder->applyConfig([ 'sortableFields' => 'fail', diff --git a/tests/Scaffolding/Scaffolders/OperationScaffolderTest.php b/tests/Scaffolding/Scaffolders/OperationScaffolderTest.php index 655ebb71b..dc4201bf9 100644 --- a/tests/Scaffolding/Scaffolders/OperationScaffolderTest.php +++ b/tests/Scaffolding/Scaffolders/OperationScaffolderTest.php @@ -130,7 +130,7 @@ public function testOperationScaffolderArgs() } $this->assertInstanceOf(InvalidArgumentException::class, $ex); - $this->assertRegExp('/Tried to set description/', $ex->getMessage()); + $this->assertMatchesRegularExpression('/Tried to set description/', $ex->getMessage()); $ex = null; try { @@ -140,7 +140,7 @@ public function testOperationScaffolderArgs() } $this->assertInstanceOf(InvalidArgumentException::class, $ex); - $this->assertRegExp('/Tried to set default/', $ex->getMessage()); + $this->assertMatchesRegularExpression('/Tried to set default/', $ex->getMessage()); $ex = null; try { @@ -150,7 +150,7 @@ public function testOperationScaffolderArgs() } $this->assertInstanceOf(InvalidArgumentException::class, $ex); - $this->assertRegExp('/Tried to make arg [A-Za-z0-9]+ required/', $ex->getMessage()); + $this->assertMatchesRegularExpression('/Tried to make arg [A-Za-z0-9]+ required/', $ex->getMessage()); } public function testOperationScaffolderResolver() @@ -169,8 +169,8 @@ public function testOperationScaffolderResolver() $this->assertTrue($success); - $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessageRegExp('/closures, instances of/'); + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessageMatches('/closures, instances of/'); $scaffolder->setResolver('fail'); } @@ -213,7 +213,7 @@ public function testOperationScaffolderAppliesConfig() } $this->assertInstanceof(Exception::class, $ex); - $this->assertRegExp('/args must be an array/', $ex->getMessage()); + $this->assertMatchesRegularExpression('/args must be an array/', $ex->getMessage()); $ex = null; try { @@ -230,7 +230,7 @@ public function testOperationScaffolderAppliesConfig() } $this->assertInstanceof(Exception::class, $ex); - $this->assertRegExp('/must have a type/', $ex->getMessage()); + $this->assertMatchesRegularExpression('/must have a type/', $ex->getMessage()); $ex = null; try { @@ -244,6 +244,6 @@ public function testOperationScaffolderAppliesConfig() } $this->assertInstanceof(Exception::class, $ex); - $this->assertRegExp('/should be mapped to a string or an array/', $ex->getMessage()); + $this->assertMatchesRegularExpression('/should be mapped to a string or an array/', $ex->getMessage()); } } diff --git a/tests/Scaffolding/Scaffolders/SchemaScaffolderTest.php b/tests/Scaffolding/Scaffolders/SchemaScaffolderTest.php index 27bbd5941..a1c2b0f67 100644 --- a/tests/Scaffolding/Scaffolders/SchemaScaffolderTest.php +++ b/tests/Scaffolding/Scaffolders/SchemaScaffolderTest.php @@ -30,7 +30,7 @@ class SchemaScaffolderTest extends SapphireTest { - protected function setUp() + protected function setUp(): void { parent::setUp(); foreach (Read::get_extensions() as $class) { @@ -176,8 +176,8 @@ public function testSchemaScaffolderAddToManager() public function testSchemaScaffolderCreateFromConfigThrowsIfBadTypes() { - $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessageRegExp('/"types" must be a map of class name to settings/'); + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessageMatches('/"types" must be a map of class name to settings/'); SchemaScaffolder::createFromConfig([ 'types' => ['fail'], ]); @@ -185,8 +185,8 @@ public function testSchemaScaffolderCreateFromConfigThrowsIfBadTypes() public function testSchemaScaffolderCreateFromConfigThrowsIfBadQueries() { - $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessageRegExp('/must be a map of operation name to settings/'); + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessageMatches('/must be a map of operation name to settings/'); SchemaScaffolder::createFromConfig([ 'types' => [ DataObjectFake::class => [ @@ -257,7 +257,7 @@ public function testSchemaScaffolderFixedTypeMustBeViewableData() { Config::modify()->merge(SchemaScaffolder::class, 'fixed_types', ['stdclass']); $this->expectException(Exception::class); - $this->expectExceptionMessageRegExp('/Cannot auto register/'); + $this->expectExceptionMessageMatches('/Cannot auto register/'); (new SchemaScaffolder())->addToManager(new Manager()); } @@ -265,7 +265,7 @@ public function testSchemaScaffolderFixedTypeMustHaveTypeCreatorExtension() { Config::modify()->merge(SchemaScaffolder::class, 'fixed_types', [ArrayList::class]); $this->expectException(Exception::class); - $this->expectExceptionMessageRegExp('/Cannot auto register/'); + $this->expectExceptionMessageMatches('/Cannot auto register/'); (new SchemaScaffolder())->addToManager(new Manager()); } @@ -273,7 +273,7 @@ public function testSchemaScaffolderFixedTypeMustBeAnArray() { Config::modify()->merge(SchemaScaffolder::class, 'fixed_types', 'fail'); $this->expectException(Exception::class); - $this->expectExceptionMessageRegExp('/must be an array/'); + $this->expectExceptionMessageMatches('/must be an array/'); (new SchemaScaffolder())->addToManager(new Manager()); } diff --git a/tests/Scaffolding/Scaffolders/UnionScaffolderTest.php b/tests/Scaffolding/Scaffolders/UnionScaffolderTest.php index 42b15dc92..b0ef9771c 100644 --- a/tests/Scaffolding/Scaffolders/UnionScaffolderTest.php +++ b/tests/Scaffolding/Scaffolders/UnionScaffolderTest.php @@ -57,7 +57,7 @@ public function testUnionScaffolder() $ex = $e->getMessage(); } - $this->assertRegExp('/not a DataObject/', $ex); + $this->assertMatchesRegularExpression('/not a DataObject/', $ex); $ex = null; try { @@ -66,6 +66,6 @@ public function testUnionScaffolder() $ex = $e->getMessage(); } - $this->assertRegExp('/no type defined/', $ex); + $this->assertMatchesRegularExpression('/no type defined/', $ex); } } diff --git a/tests/Scaffolding/StaticSchemaTest.php b/tests/Scaffolding/StaticSchemaTest.php index 6a010eb36..89a39ef62 100644 --- a/tests/Scaffolding/StaticSchemaTest.php +++ b/tests/Scaffolding/StaticSchemaTest.php @@ -4,6 +4,7 @@ use GraphQL\Type\Definition\ObjectType; use InvalidArgumentException; +use PHPUnit\Framework\Error\Notice; use SilverStripe\Core\Config\Config; use SilverStripe\Dev\SapphireTest; use SilverStripe\GraphQL\Manager; @@ -18,7 +19,7 @@ class StaticSchemaTest extends SapphireTest { - protected function setUp() + protected function setUp(): void { parent::setUp(); StaticSchema::reset(); @@ -42,14 +43,14 @@ public function testTypeNameForDataObject() public function testEnsureDataObject() { - $this->expectException(InvalidArgumentException::class); + $this->expectException(\InvalidArgumentException::class); $schema = new StaticSchema(); $schema->setTypeNames(['fail' => 'fail']); } public function testEnsureUnique() { - $this->expectException(InvalidArgumentException::class); + $this->expectException(\InvalidArgumentException::class); $typeNames = [ DataObjectFake::class => 'test1', FakePage::class => 'test2', @@ -61,7 +62,7 @@ public function testEnsureUnique() public function testEnsureAssoc() { - $this->expectException(InvalidArgumentException::class); + $this->expectException(\InvalidArgumentException::class); $typeNames = [ 'test1', 'test2', @@ -192,13 +193,13 @@ public function testFetchFromManager() ->fetchFromManager(FakeSiteTree::class, $manager, StaticSchema::PREFER_SINGLE); $this->assertSame($result, $singleType); - $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessageRegExp('/illegal mode/'); + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessageMatches('/illegal mode/'); StaticSchema::inst() ->fetchFromManager(FakeSiteTree::class, $manager, 'fail'); - $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessageRegExp('/could not be resolved/'); + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessageMatches('/could not be resolved/'); StaticSchema::inst() ->fetchFromManager('fail', $manager); } @@ -268,7 +269,11 @@ public function testExtractKeys() $result = StaticSchema::inst()->extractKeys(['Foo', 'NotExists'], $arr); $this->assertEquals(['test1', null], $result); - $this->expectException(\PHPUnit_Framework_Error::class); + if (PHP_VERSION_ID < 80000) { + $this->expectNotice('StaticSchema throws notice when extracting on non-existent key on PHP 7'); + } else { + $this->expectWarning('StaticSchema throws warning when extracting on non-existent key on PHP 8'); + } StaticSchema::inst()->extractKeys(['Foo', 'NotExists'], $arr, false); } diff --git a/tests/Scaffolding/Util/ArrayTypeParserTest.php b/tests/Scaffolding/Util/ArrayTypeParserTest.php index 079a34a77..3a33b50a2 100644 --- a/tests/Scaffolding/Util/ArrayTypeParserTest.php +++ b/tests/Scaffolding/Util/ArrayTypeParserTest.php @@ -40,15 +40,15 @@ public function testGetType() public function testInvalidConstructorNotArray() { - $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessageRegExp('/second parameter must be an associative array/'); + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessageMatches('/second parameter must be an associative array/'); new ArrayTypeParser('test', 'String'); } public function testInvalidConstructorNotAssociative() { - $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessageRegExp('/second parameter must be an associative array/'); + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessageMatches('/second parameter must be an associative array/'); new ArrayTypeParser('test', ['oranges', 'apples']); } } diff --git a/tests/Scaffolding/Util/OperationListTest.php b/tests/Scaffolding/Util/OperationListTest.php index eeb846164..91fc83e69 100644 --- a/tests/Scaffolding/Util/OperationListTest.php +++ b/tests/Scaffolding/Util/OperationListTest.php @@ -28,8 +28,8 @@ public function testOperationList() $list->removeByName('nothing'); $this->assertEquals(1, $list->count()); - $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessageRegExp('/only accepts instances of/'); + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessageMatches('/only accepts instances of/'); $list->push(new OperationList()); } } diff --git a/tests/Scaffolding/Util/StringTypeParserTest.php b/tests/Scaffolding/Util/StringTypeParserTest.php index fdd7d8e12..e0dbbdc1f 100644 --- a/tests/Scaffolding/Util/StringTypeParserTest.php +++ b/tests/Scaffolding/Util/StringTypeParserTest.php @@ -54,15 +54,15 @@ public function testStringTypeParser() public function testTypeInvalid() { - $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessageRegExp('/Invalid argument/'); + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessageMatches('/Invalid argument/'); new StringTypeParser(' ... Nothing'); } public function testTypeNotAString() { - $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessageRegExp('/must be passed a string/'); + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessageMatches('/must be passed a string/'); new StringTypeParser(['fail']); } diff --git a/tests/TypeCreatorTest.php b/tests/TypeCreatorTest.php index d7d0314e0..ea295229b 100644 --- a/tests/TypeCreatorTest.php +++ b/tests/TypeCreatorTest.php @@ -4,7 +4,7 @@ use GraphQL\Type\Definition\InputObjectType; use GraphQL\Type\Definition\Type; -use PHPUnit_Framework_MockObject_MockObject; +use PHPUnit\Framework\MockObject\MockObject; use SilverStripe\Dev\SapphireTest; use SilverStripe\GraphQL\TypeCreator; @@ -126,7 +126,7 @@ public function testGetFieldsUsesAllFieldsResolverMethod() /** * @param array $extraMethods - * @return PHPUnit_Framework_MockObject_MockObject|TypeCreator + * @return MockObject|TypeCreator */ protected function getTypeCreatorMock($extraMethods = []) { diff --git a/tests/Util/CaseInsensitiveFieldAccessorTest.php b/tests/Util/CaseInsensitiveFieldAccessorTest.php index 2a8e18dd9..445199cb5 100644 --- a/tests/Util/CaseInsensitiveFieldAccessorTest.php +++ b/tests/Util/CaseInsensitiveFieldAccessorTest.php @@ -40,21 +40,17 @@ public function testGetValueWithMethod() $this->assertEquals('customMethodValue', $mapper->getValue($fake, 'customMethod')); } - /** - * @expectedException \InvalidArgumentException - */ public function testGetValueWithUnknownFieldThrowsException() { + $this->expectException(\InvalidArgumentException::class); $fake = new DataObjectFake([]); $mapper = new CaseInsensitiveFieldAccessor(); $mapper->getValue($fake, 'unknownField'); } - /** - * @expectedException \InvalidArgumentException - */ public function testGetValueWithCustomOpts() { + $this->expectException(\InvalidArgumentException::class); $fake = new DataObjectFake([ 'MyField' => 'myValue' ]); @@ -103,11 +99,9 @@ public function testSetValueWithMethod() $this->assertEquals('myNewValue', $fake->customSetterMethodResult); } - /** - * @expectedException \InvalidArgumentException - */ public function testSetValueWithUnknownFieldThrowsException() { + $this->expectException(\InvalidArgumentException::class); $fake = new DataObjectFake([]); $mapper = new CaseInsensitiveFieldAccessor(); $mapper->setValue($fake, 'unknownField', true);