From f63be3e1161d4569dfc7a216e9087825b319e2e8 Mon Sep 17 00:00:00 2001 From: Steve Boyd Date: Thu, 21 Oct 2021 16:43:24 +1300 Subject: [PATCH] API phpunit 9 support --- composer.json | 4 +- phpunit.xml.dist | 8 +-- tests/Behat/Context/LoginContext.php | 3 +- .../ChangePasswordHandlerTest.php | 51 +++++++++++-------- tests/php/Authenticator/LoginHandlerTest.php | 38 +++++++------- .../php/Authenticator/RegisterHandlerTest.php | 25 +++++---- tests/php/BackupCode/RegisterHandlerTest.php | 18 +++---- tests/php/BackupCode/VerifyHandlerTest.php | 6 +-- .../php/BasicMath/MethodVerifyHandlerTest.php | 8 +-- .../AdminRegistrationControllerTest.php | 45 ++++++++-------- .../SecurityAdminExtensionTest.php | 8 +-- .../AccountReset/SecurityExtensionTest.php | 14 ++--- tests/php/Extension/MemberExtensionTest.php | 6 +-- .../RegisteredMFAMethodListFieldTest.php | 10 ++-- .../Report/EnabledMembersFunctionalTest.php | 24 ++++----- tests/php/Report/EnabledMembersTest.php | 2 +- tests/php/Service/BackupCodeGeneratorTest.php | 2 +- tests/php/Service/EnforcementManagerTest.php | 2 +- tests/php/Service/MethodRegistryTest.php | 29 +++++------ .../php/Service/PHPEncryptionAdapterTest.php | 4 +- tests/php/Service/SchemaGeneratorTest.php | 2 +- .../php/State/AvailableMethodDetailsTest.php | 8 +-- .../php/State/RegisteredMethodDetailsTest.php | 11 ++-- tests/php/Store/SessionStoreTest.php | 12 ++--- 24 files changed, 172 insertions(+), 168 deletions(-) diff --git a/composer.json b/composer.json index 1266c053..98be03be 100644 --- a/composer.json +++ b/composer.json @@ -26,7 +26,7 @@ ], "require": { "php": ">=7.1.0", - "silverstripe/framework": "^4.1", + "silverstripe/framework": "^4.10", "silverstripe/admin": "^1.1", "silverstripe/siteconfig": "^4.1", "defuse/php-encryption": "^2.2", @@ -34,7 +34,7 @@ "silverstripe/security-extensions": "^4.0" }, "require-dev": { - "sminnee/phpunit": "^5.7", + "phpunit/phpunit": "^9", "squizlabs/php_codesniffer": "^3.0" }, "conflict": { diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 3e41eee8..270ac3c6 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,7 +1,9 @@ - - tests - + + + tests + + diff --git a/tests/Behat/Context/LoginContext.php b/tests/Behat/Context/LoginContext.php index 4c8133e6..f337780f 100644 --- a/tests/Behat/Context/LoginContext.php +++ b/tests/Behat/Context/LoginContext.php @@ -4,6 +4,7 @@ namespace SilverStripe\MFA\Tests\Behat\Context; +use PHPUnit\Framework\Assert; use SilverStripe\CMS\Tests\Behaviour\LoginContext as CMSLoginContext; use SilverStripe\BehatExtension\Context\LoginContext as BehatLoginContext; use SilverStripe\MFA\Extension\SiteConfigExtension; @@ -32,7 +33,7 @@ public function multiFactorAuthenticationIsOptional() { /** @var SiteConfig&SiteConfigExtension $siteConfig */ $siteConfig = SiteConfig::current_site_config(); - assertNotNull($siteConfig, 'Current SiteConfig record could not be found!'); + Assert::assertNotNull($siteConfig, 'Current SiteConfig record could not be found!'); $siteConfig->MFARequired = false; $siteConfig->write(); diff --git a/tests/php/Authenticator/ChangePasswordHandlerTest.php b/tests/php/Authenticator/ChangePasswordHandlerTest.php index 47f7af82..02d5c325 100644 --- a/tests/php/Authenticator/ChangePasswordHandlerTest.php +++ b/tests/php/Authenticator/ChangePasswordHandlerTest.php @@ -2,7 +2,8 @@ namespace SilverStripe\MFA\Tests\Authenticator; -use PHPUnit_Framework_MockObject_MockObject; +use LogicException; +use PHPUnit\Framework\MockObject\MockObject; use Psr\Log\LoggerInterface; use SilverStripe\Control\HTTPRequest; use SilverStripe\Control\HTTPResponse; @@ -26,7 +27,7 @@ class ChangePasswordHandlerTest extends FunctionalTest { protected static $fixture_file = 'ChangePasswordHandlerTest.yml'; - protected function setUp() + protected function setUp(): void { parent::setUp(); @@ -60,7 +61,7 @@ public function testMFADoesNotLoadWhenAUserIsLoggedIn() { $this->logInAs('simon'); $response = $this->get('Security/changepassword'); - $this->assertContains('OldPassword', $response->getBody()); + $this->assertStringContainsString('OldPassword', $response->getBody()); } public function testMFADoesNotLoadWhenAUserDoesNotHaveRegisteredMethods() @@ -71,8 +72,12 @@ public function testMFADoesNotLoadWhenAUserDoesNotHaveRegisteredMethods() $token = $member->generateAutologinTokenAndStoreHash(); $response = $this->get("Security/changepassword?m={$memberId}&t={$token}"); - $this->assertContains('NewPassword1', $response->getBody(), 'There should be a new password field'); - $this->assertContains('NewPassword2', $response->getBody(), 'There should be a confirm new password field'); + $this->assertStringContainsString('NewPassword1', $response->getBody(), 'There should be a new password field'); + $this->assertStringContainsString( + 'NewPassword2', + $response->getBody(), + 'There should be a confirm new password field' + ); } public function testMFALoadsWhenAUserHasConfiguredMethods() @@ -83,8 +88,12 @@ public function testMFALoadsWhenAUserHasConfiguredMethods() $token = $member->generateAutologinTokenAndStoreHash(); $response = $this->get("Security/changepassword?m={$memberId}&t={$token}"); - $this->assertNotContains('type="password"', $response->getBody(), 'Password form should be circumvented'); - $this->assertContains('id="mfa-app"', $response->getBody(), 'MFA screen should be displayed'); + $this->assertStringNotContainsString( + 'type="password"', + $response->getBody(), + 'Password form should be circumvented' + ); + $this->assertStringContainsString('id="mfa-app"', $response->getBody(), 'MFA screen should be displayed'); } public function testGetSchema() @@ -114,13 +123,11 @@ public function testMfaRedirectsBackWithoutMember() $this->assertEquals(302, $response->getStatusCode()); } - /** - * @expectedException \LogicException - * @expectedExceptionMessage Store not found, please create one first. - */ public function testStartMfaCheckThrowsExceptionWithoutStore() { - /** @var ChangePasswordHandler&PHPUnit_Framework_MockObject_MockObject $handler */ + $this->expectException(\LogicException::class); + $this->expectExceptionMessage('Store not found, please create one first.'); + /** @var ChangePasswordHandler&MockObject $handler */ $handler = $this->getMockBuilder(ChangePasswordHandler::class) ->disableOriginalConstructor() ->setMethods(['getStore']) @@ -133,7 +140,7 @@ public function testStartMfaCheckThrowsExceptionWithoutStore() public function testStartMfaReturnsForbiddenWithoutMember() { - /** @var ChangePasswordHandler&PHPUnit_Framework_MockObject_MockObject $handler */ + /** @var ChangePasswordHandler&MockObject $handler */ $handler = $this->getMockBuilder(ChangePasswordHandler::class) ->disableOriginalConstructor() ->setMethods(['getStore']) @@ -150,7 +157,7 @@ public function testStartMfaReturnsForbiddenWithoutMember() public function testMfaStartsVerificationResponse() { - /** @var ChangePasswordHandler&PHPUnit_Framework_MockObject_MockObject $handler */ + /** @var ChangePasswordHandler&MockObject $handler */ $handler = $this->getMockBuilder(ChangePasswordHandler::class) ->disableOriginalConstructor() ->setMethods(['getStore', 'createStartVerificationResponse']) @@ -175,13 +182,13 @@ public function testMfaStartsVerificationResponse() public function testVerifyMfaCheckReturnsForbiddenOnVerificationFailure() { - /** @var ChangePasswordHandler&PHPUnit_Framework_MockObject_MockObject $handler */ + /** @var ChangePasswordHandler&MockObject $handler */ $handler = $this->getMockBuilder(ChangePasswordHandler::class) ->disableOriginalConstructor() ->setMethods(['getStore', 'completeVerificationRequest']) ->getMock(); - /** @var LoggerInterface&PHPUnit_Framework_MockObject_MockObject $logger */ + /** @var LoggerInterface&MockObject $logger */ $logger = $this->createMock(LoggerInterface::class); $logger->expects($this->once())->method('debug'); $handler->setLogger($logger); @@ -199,7 +206,7 @@ public function testVerifyMfaCheckReturnsForbiddenOnVerificationFailure() public function testVerifyMfaCheckReturnsUnsuccessfulValidationResult() { - /** @var ChangePasswordHandler&PHPUnit_Framework_MockObject_MockObject $handler */ + /** @var ChangePasswordHandler&MockObject $handler */ $handler = $this->getMockBuilder(ChangePasswordHandler::class) ->disableOriginalConstructor() ->setMethods(['getStore', 'completeVerificationRequest']) @@ -214,12 +221,12 @@ public function testVerifyMfaCheckReturnsUnsuccessfulValidationResult() $response = $handler->verifyMFACheck(new HTTPRequest('GET', '')); $this->assertEquals(401, $response->getStatusCode()); - $this->assertContains('It is a test', $response->getBody()); + $this->assertStringContainsString('It is a test', $response->getBody()); } public function testVerifyMfaReturnsWhenVerificationIsNotComplete() { - /** @var ChangePasswordHandler&PHPUnit_Framework_MockObject_MockObject $handler */ + /** @var ChangePasswordHandler&MockObject $handler */ $handler = $this->getMockBuilder(ChangePasswordHandler::class) ->disableOriginalConstructor() ->setMethods(['getStore', 'completeVerificationRequest', 'isVerificationComplete']) @@ -233,12 +240,12 @@ public function testVerifyMfaReturnsWhenVerificationIsNotComplete() $response = $handler->verifyMFACheck(new HTTPRequest('GET', '')); $this->assertEquals(202, $response->getStatusCode()); - $this->assertContains('Additional authentication required', $response->getBody()); + $this->assertStringContainsString('Additional authentication required', $response->getBody()); } public function testVerifyMfaResultSuccessful() { - /** @var ChangePasswordHandler&PHPUnit_Framework_MockObject_MockObject $handler */ + /** @var ChangePasswordHandler&MockObject $handler */ $handler = $this->getMockBuilder(ChangePasswordHandler::class) ->disableOriginalConstructor() ->setMethods(['getStore', 'completeVerificationRequest', 'isVerificationComplete']) @@ -255,6 +262,6 @@ public function testVerifyMfaResultSuccessful() $response = $handler->verifyMFACheck($request); $this->assertEquals(200, $response->getStatusCode()); - $this->assertContains('Multi-factor authenticated', $response->getBody()); + $this->assertStringContainsString('Multi-factor authenticated', $response->getBody()); } } diff --git a/tests/php/Authenticator/LoginHandlerTest.php b/tests/php/Authenticator/LoginHandlerTest.php index 754b28c2..211a5c7b 100644 --- a/tests/php/Authenticator/LoginHandlerTest.php +++ b/tests/php/Authenticator/LoginHandlerTest.php @@ -2,7 +2,7 @@ namespace SilverStripe\MFA\Tests\Authenticator; -use PHPUnit_Framework_MockObject_MockObject; +use PHPUnit\Framework\MockObject\MockObject; use SilverStripe\Control\Controller; use SilverStripe\Control\HTTPRequest; use SilverStripe\Control\HTTPResponse; @@ -14,6 +14,7 @@ use SilverStripe\MFA\Authenticator\LoginHandler; use SilverStripe\MFA\Authenticator\MemberAuthenticator; use SilverStripe\MFA\Extension\MemberExtension; +use SilverStripe\MFA\Exception\MemberNotFoundException; use SilverStripe\MFA\Method\Handler\VerifyHandlerInterface; use SilverStripe\MFA\Method\MethodInterface; use SilverStripe\MFA\Model\RegisteredMethod; @@ -35,7 +36,7 @@ class LoginHandlerTest extends FunctionalTest { protected static $fixture_file = 'LoginHandlerTest.yml'; - protected function setUp() + protected function setUp(): void { parent::setUp(); Config::modify()->set(MethodRegistry::class, 'methods', [Method::class]); @@ -52,7 +53,7 @@ protected function setUp() ] ]); - /** @var SudoModeServiceInterface&PHPUnit_Framework_MockObject_MockObject $sudoModeService */ + /** @var SudoModeServiceInterface&MockObject $sudoModeService */ $sudoModeService = $this->createMock(SudoModeServiceInterface::class); $sudoModeService->expects($this->any())->method('check')->willReturn(true); Injector::inst()->registerService($sudoModeService, SudoModeServiceInterface::class); @@ -76,7 +77,7 @@ public function testMFAStepIsAdded() public function testMFARedirectsBackWhenSudoModeIsInactive() { - /** @var SudoModeServiceInterface&PHPUnit_Framework_MockObject_MockObject $sudoModeService */ + /** @var SudoModeServiceInterface&MockObject $sudoModeService */ $sudoModeService = $this->createMock(SudoModeServiceInterface::class); $sudoModeService->expects($this->once())->method('check')->willReturn(false); Injector::inst()->registerService($sudoModeService, SudoModeServiceInterface::class); @@ -150,7 +151,7 @@ public function testMFASchemaEndpointReturnsMethodDetails() $this->assertSame($method->getName(), $firstMethod['name']); $this->assertSame($registerHandler->getDescription(), $firstMethod['description']); $this->assertSame($registerHandler->getSupportLink(), $firstMethod['supportLink']); - $this->assertContains('client/dist/images', $firstMethod['thumbnail']); + $this->assertStringContainsString('client/dist/images', $firstMethod['thumbnail']); $this->assertSame('BasicMathRegister', $firstMethod['component']); } @@ -181,7 +182,7 @@ public function testMFASchemaEndpointShowsRegisteredMethodsIfSetUp() $this->assertSame($method->getName(), $result['name']); $this->assertSame('BasicMathLogin', $result['component']); $this->assertSame('https://google.com', $result['supportLink']); - $this->assertContains('totp.svg', $result['thumbnail']); + $this->assertStringContainsString('totp.svg', $result['thumbnail']); } public function testMFASchemaEndpointProvidesDefaultMethodIfSet() @@ -221,7 +222,7 @@ public function testCannotSkipMFA($mfaRequired, $member = 'robbie') } $response = $this->get(Controller::join_links(Security::login_url(), 'default/mfa/skip')); - $this->assertContains('You cannot skip MFA registration', $response->getBody()); + $this->assertStringContainsString('You cannot skip MFA registration', $response->getBody()); } /** @@ -308,11 +309,9 @@ public function testBackURLIsPreservedWhenSkipping($memberFixture) $this->assertStringEndsWith('admin/pages', $response->getHeader('location')); } - /** - * @expectedException \SilverStripe\MFA\Exception\MemberNotFoundException - */ public function testGetMemberThrowsExceptionWithoutMember() { + $this->expectException(MemberNotFoundException::class); $this->logOut(); $handler = new LoginHandler('foo', $this->createMock(MemberAuthenticator::class)); $handler->setRequest(new HTTPRequest('GET', '/')); @@ -393,7 +392,7 @@ public function testVerifyAssertsValidCSRFToken() $response = $handler->finishVerification($request); $this->assertSame(403, $response->getStatusCode()); - $this->assertContains('Your request timed out', $response->getBody()); + $this->assertStringContainsString('Your request timed out', $response->getBody()); $request = new HTTPRequest('GET', '/', [ SecurityToken::inst()->getName() => SecurityToken::inst()->getValue() @@ -437,7 +436,7 @@ public function testStartVerificationReturnsForbiddenWithoutSudoMode() $member = $this->objFromFixture(Member::class, 'robbie'); $this->scaffoldPartialLogin($member); - /** @var SudoModeServiceInterface&PHPUnit_Framework_MockObject_MockObject $sudoModeService */ + /** @var SudoModeServiceInterface&MockObject $sudoModeService */ $sudoModeService = $this->createMock(SudoModeServiceInterface::class); $sudoModeService->method('check')->willReturn(false); Injector::inst()->registerService($sudoModeService, SudoModeServiceInterface::class); @@ -472,7 +471,7 @@ public function testFinishVerificationHandlesMembersLockedOut() $response = $handler->finishVerification($request); $this->assertEquals(403, $response->getStatusCode()); - $this->assertContains('Your account is temporarily locked', (string) $response->getBody()); + $this->assertStringContainsString('Your account is temporarily locked', (string) $response->getBody()); } public function testFinishVerificationChecksSudoModeIsActive() @@ -488,14 +487,17 @@ public function testFinishVerificationChecksSudoModeIsActive() $store->setMethod('basic-math'); $handler->setStore($store); - /** @var SudoModeServiceInterface&PHPUnit_Framework_MockObject_MockObject $sudoModeService */ + /** @var SudoModeServiceInterface&MockObject $sudoModeService */ $sudoModeService = $this->createMock(SudoModeServiceInterface::class); $sudoModeService->method('check')->willReturn(false); Injector::inst()->registerService($sudoModeService, SudoModeServiceInterface::class); $response = $handler->finishVerification($request); $this->assertEquals(403, $response->getStatusCode()); - $this->assertContains('You need to re-verify your account before continuing', (string) $response->getBody()); + $this->assertStringContainsString( + 'You need to re-verify your account before continuing', + (string) $response->getBody() + ); } public function testFinishVerificationPassesExceptionMessagesThroughFromMethodsWithValidationFailures() @@ -505,7 +507,7 @@ public function testFinishVerificationPassesExceptionMessagesThroughFromMethodsW $member->config()->set('lock_out_after_incorrect_logins', 5); $failedLogins = $member->FailedLoginCount; - /** @var LoginHandler|PHPUnit_Framework_MockObject_MockObject $handler */ + /** @var LoginHandler|MockObject $handler */ $handler = $this->getMockBuilder(LoginHandler::class) ->setMethods(['completeVerificationRequest']) ->disableOriginalConstructor() @@ -524,7 +526,7 @@ public function testFinishVerificationPassesExceptionMessagesThroughFromMethodsW $response = $handler->finishVerification($request); $this->assertEquals(401, $response->getStatusCode()); - $this->assertContains('It failed because it\'s mocked', (string) $response->getBody()); + $this->assertStringContainsString('It failed because it\'s mocked', (string) $response->getBody()); $this->assertSame($failedLogins + 1, $member->FailedLoginCount, 'Failed login is registered'); } @@ -540,7 +542,7 @@ public function testFinishVerificationWillRedirectToTheBackURLSetAsLoginIsStarte $this->doLogin($member, 'Password123', 'admin/pages'); - /** @var LoginHandler|PHPUnit_Framework_MockObject_MockObject $handler */ + /** @var LoginHandler|MockObject $handler */ $handler = $this->getMockBuilder(LoginHandler::class) ->setMethods(['completeVerificationRequest']) ->disableOriginalConstructor() diff --git a/tests/php/Authenticator/RegisterHandlerTest.php b/tests/php/Authenticator/RegisterHandlerTest.php index 7091c101..4db9f7a6 100644 --- a/tests/php/Authenticator/RegisterHandlerTest.php +++ b/tests/php/Authenticator/RegisterHandlerTest.php @@ -2,7 +2,7 @@ namespace SilverStripe\MFA\Tests\Authenticator; -use PHPUnit_Framework_MockObject_MockObject; +use PHPUnit\Framework\MockObject\MockObject; use SilverStripe\Core\Config\Config; use SilverStripe\Core\Injector\Injector; use SilverStripe\Dev\FunctionalTest; @@ -30,7 +30,7 @@ class RegisterHandlerTest extends FunctionalTest protected static $fixture_file = 'RegisterHandlerTest.yml'; - protected function setUp() + protected function setUp(): void { parent::setUp(); Config::modify()->set(MethodRegistry::class, 'methods', [Method::class]); @@ -45,7 +45,7 @@ protected function setUp() ] ]); - /** @var SudoModeServiceInterface&PHPUnit_Framework_MockObject_MockObject $sudoModeService */ + /** @var SudoModeServiceInterface&MockObject $sudoModeService */ $sudoModeService = $this->createMock(SudoModeServiceInterface::class); $sudoModeService->expects($this->any())->method('check')->willReturn(true); Injector::inst()->registerService($sudoModeService, SudoModeServiceInterface::class); @@ -82,7 +82,7 @@ public function testStartRegistrationFailsWhenInvalidMethodIsPassed() $response = $this->get('Security/login/default/mfa/register/inert/'); $this->assertEquals(400, $response->getStatusCode()); - $this->assertContains('No such method is available', $response->getBody()); + $this->assertStringContainsString('No such method is available', $response->getBody()); } /** @@ -97,7 +97,7 @@ public function testStartRegistrationFailsWhenRegisteredMethodExists() $response = $this->get(self::URL); $this->assertEquals(400, $response->getStatusCode()); - $this->assertContains('This member already has an MFA method', $response->getBody()); + $this->assertStringContainsString('This member already has an MFA method', $response->getBody()); } /** @@ -109,7 +109,10 @@ public function testStartRegistrationFailsWhenMethodIsAlreadyRegistered() $response = $this->get(self::URL); $this->assertEquals(400, $response->getStatusCode()); - $this->assertContains('That method has already been registered against this Member', $response->getBody()); + $this->assertStringContainsString( + 'That method has already been registered against this Member', + $response->getBody() + ); } /** @@ -152,7 +155,7 @@ public function testFinishRegistrationFailsWhenCalledDirectly() $response = $this->post(self::URL, ['dummy' => 'data'], null, $this->session(), json_encode(['number' => 7])); $this->assertEquals(400, $response->getStatusCode()); - $this->assertContains('No registration in progress', $response->getBody()); + $this->assertStringContainsString('No registration in progress', $response->getBody()); } /** @@ -167,7 +170,7 @@ public function testFinishRegistrationFailsWhenMethodIsMismatched() $response = $this->post(self::URL, ['dummy' => 'data'], null, $this->session(), json_encode(['number' => 7])); $this->assertEquals(400, $response->getStatusCode()); - $this->assertContains('Method does not match registration in progress', $response->getBody()); + $this->assertStringContainsString('Method does not match registration in progress', $response->getBody()); } public function testFinishRegistrationFailsWhenMethodCannotBeRegistered() @@ -203,7 +206,7 @@ public function testFinishRegistrationFailsWhenMethodCannotBeRegistered() $response = $this->post(self::URL, ['dummy' => 'data'], null, $this->session(), json_encode(['number' => 7])); $this->assertEquals(400, $response->getStatusCode()); - $this->assertContains('No. Bad user', $response->getBody()); + $this->assertStringContainsString('No. Bad user', $response->getBody()); } /** @@ -236,7 +239,7 @@ public function testFinishRegistrationValidatesCSRF() $response = $this->post(self::URL, ['dummy' => 'data'], null, $this->session(), json_encode(['number' => 7])); $this->assertEquals(403, $response->getStatusCode()); - $this->assertContains('Your request timed out', $response->getBody()); + $this->assertStringContainsString('Your request timed out', $response->getBody()); $this->scaffoldPartialLogin($freshMember, 'basic-math'); @@ -262,7 +265,7 @@ public function testEnforcesSudoMode() $response = $this->post(self::URL, ['dummy' => 'data'], null, $this->session(), json_encode(['number' => 7])); $this->assertSame(403, $response->getStatusCode()); - $this->assertContains('You must be logged in or logging in', (string) $response->getBody()); + $this->assertStringContainsString('You must be logged in or logging in', (string) $response->getBody()); } /** diff --git a/tests/php/BackupCode/RegisterHandlerTest.php b/tests/php/BackupCode/RegisterHandlerTest.php index a3d81eb2..2ddb84a7 100644 --- a/tests/php/BackupCode/RegisterHandlerTest.php +++ b/tests/php/BackupCode/RegisterHandlerTest.php @@ -2,7 +2,7 @@ namespace SilverStripe\MFA\Tests\BackupCode; -use PHPUnit_Framework_MockObject_MockObject; +use PHPUnit\Framework\MockObject\MockObject; use SilverStripe\Control\HTTPRequest; use SilverStripe\Dev\SapphireTest; use SilverStripe\MFA\BackupCode\Method; @@ -18,13 +18,11 @@ class RegisterHandlerTest extends SapphireTest { protected static $fixture_file = 'RegisterHandlerTest.yml'; - /** - * @expectedException \SilverStripe\MFA\Exception\RegistrationFailedException - * @expectedExceptionMessage Attempted to register backup codes with no registered methods - */ public function testStartThrowsExceptionForMemberWithoutRegisteredMethods() { - /** @var TestStore&PHPUnit_Framework_MockObject_MockObject $store */ + $this->expectException(\SilverStripe\MFA\Exception\RegistrationFailedException::class); + $this->expectExceptionMessage('Attempted to register backup codes with no registered methods'); + /** @var TestStore&MockObject $store */ $store = $this->createMock(TestStore::class); /** @var Member&MemberExtension $member */ @@ -43,7 +41,7 @@ public function testStartReturnsPlainTextCodes() $method = new BasicMathMethod(); RegisteredMethodManager::singleton()->registerForMember($member, $method, ['need' => 'one']); - /** @var TestStore&PHPUnit_Framework_MockObject_MockObject $store */ + /** @var TestStore&MockObject $store */ $store = $this->createMock(TestStore::class); $store->expects($this->once())->method('getMember')->willReturn($member); @@ -57,7 +55,7 @@ public function testStartReturnsPlainTextCodes() public function testStartStoresHashesOfBackupCodesOnMember() { - /** @var TestStore&PHPUnit_Framework_MockObject_MockObject $store */ + /** @var TestStore&MockObject $store */ $store = $this->createMock(TestStore::class); /** @var Member&MemberExtension $member */ @@ -94,10 +92,10 @@ public function testStartStoresHashesOfBackupCodesOnMember() public function testRegisterReturnsNoContext() { - /** @var TestStore&PHPUnit_Framework_MockObject_MockObject $store */ + /** @var TestStore&MockObject $store */ $store = $this->createMock(TestStore::class); - /** @var HTTPRequest|PHPUnit_Framework_MockObject_MockObject $request */ + /** @var HTTPRequest|MockObject $request */ $request = $this->createMock(HTTPRequest::class); $handler = new RegisterHandler(); diff --git a/tests/php/BackupCode/VerifyHandlerTest.php b/tests/php/BackupCode/VerifyHandlerTest.php index e36b8438..59d411c3 100644 --- a/tests/php/BackupCode/VerifyHandlerTest.php +++ b/tests/php/BackupCode/VerifyHandlerTest.php @@ -2,7 +2,7 @@ namespace SilverStripe\MFA\Tests\BackupCode; -use PHPUnit_Framework_MockObject_MockObject; +use PHPUnit\Framework\MockObject\MockObject; use SilverStripe\Control\HTTPRequest; use SilverStripe\Dev\SapphireTest; use SilverStripe\MFA\BackupCode\VerifyHandler; @@ -73,11 +73,11 @@ protected function scaffoldVerifyParams($userInput) /** @var Member|MemberExtension $member */ $member = $this->objFromFixture(Member::class, 'guy'); - /** @var HTTPRequest|PHPUnit_Framework_MockObject_MockObject $request */ + /** @var HTTPRequest|MockObject $request */ $request = $this->createMock(HTTPRequest::class); $request->expects($this->once())->method('getBody')->willReturn("{\"code\":\"{$userInput}\"}"); - /** @var TestStore|PHPUnit_Framework_MockObject_MockObject $store */ + /** @var TestStore|MockObject $store */ $store = $this->createMock(TestStore::class); $store->expects($this->any())->method('getMember')->willReturn($member); diff --git a/tests/php/BasicMath/MethodVerifyHandlerTest.php b/tests/php/BasicMath/MethodVerifyHandlerTest.php index 38499b0e..735470e7 100644 --- a/tests/php/BasicMath/MethodVerifyHandlerTest.php +++ b/tests/php/BasicMath/MethodVerifyHandlerTest.php @@ -2,7 +2,7 @@ namespace SilverStripe\MFA\Tests\BasicMath; -use PHPUnit_Framework_MockObject_MockObject; +use PHPUnit\Framework\MockObject\MockObject; use SilverStripe\Control\HTTPRequest; use SilverStripe\Dev\SapphireTest; use SilverStripe\MFA\Model\RegisteredMethod; @@ -15,7 +15,7 @@ public function testStart() { $handler = new MethodVerifyHandler(); - /** @var TestStore|PHPUnit_Framework_MockObject_MockObject $store */ + /** @var TestStore|MockObject $store */ $store = $this->createMock(TestStore::class); // Need to specify willReturn() otherwise mock will attempt to an interface which causes a fatal error in PHP8 @@ -30,11 +30,11 @@ public function testVerify() { $handler = new MethodVerifyHandler(); - /** @var HTTPRequest|PHPUnit_Framework_MockObject_MockObject $request */ + /** @var HTTPRequest|MockObject $request */ $request = $this->createMock(HTTPRequest::class); $request->expects($this->once())->method('getBody')->willReturn('{"answer":"10"}'); - /** @var TestStore|PHPUnit_Framework_MockObject_MockObject $store */ + /** @var TestStore|MockObject $store */ $store = $this->createMock(TestStore::class); $store->expects($this->once())->method('getState')->willReturn([ 'answer' => 10, diff --git a/tests/php/Controller/AdminRegistrationControllerTest.php b/tests/php/Controller/AdminRegistrationControllerTest.php index 9622f357..8053944e 100644 --- a/tests/php/Controller/AdminRegistrationControllerTest.php +++ b/tests/php/Controller/AdminRegistrationControllerTest.php @@ -2,7 +2,7 @@ namespace SilverStripe\MFA\Tests\Controller; -use PHPUnit_Framework_MockObject_MockObject; +use PHPUnit\Framework\MockObject\MockObject; use Psr\Log\LoggerInterface; use SilverStripe\Admin\AdminRootController; use SilverStripe\Control\Controller; @@ -29,7 +29,7 @@ class AdminRegistrationControllerTest extends FunctionalTest { protected static $fixture_file = 'AdminRegistrationControllerTest.yml'; - protected function setUp() + protected function setUp(): void { parent::setUp(); @@ -37,7 +37,7 @@ protected function setUp() BasicMathMethod::class, ]); - /** @var SudoModeServiceInterface&PHPUnit_Framework_MockObject_MockObject $sudoModeService */ + /** @var SudoModeServiceInterface&MockObject $sudoModeService */ $sudoModeService = $this->createMock(SudoModeServiceInterface::class); $sudoModeService->expects($this->any())->method('check')->willReturn(true); Injector::inst()->registerService($sudoModeService, SudoModeServiceInterface::class); @@ -50,14 +50,14 @@ public function testStartRegistrationAssertsValidMethod() $result = $this->get(Controller::join_links(AdminRootController::admin_url(), 'mfa', 'register/foo')); $this->assertSame(400, $result->getStatusCode()); - $this->assertContains('No such method is available', $result->getBody()); + $this->assertStringContainsString('No such method is available', $result->getBody()); } public function testStartRegistrationEnforcesSudoMode() { $this->logInAs($this->objFromFixture(Member::class, 'sally_smith')); - /** @var SudoModeServiceInterface&PHPUnit_Framework_MockObject_MockObject $sudoModeService */ + /** @var SudoModeServiceInterface&MockObject $sudoModeService */ $sudoModeService = $this->createMock(SudoModeServiceInterface::class); $sudoModeService->expects($this->any())->method('check')->willReturn(false); Injector::inst()->registerService($sudoModeService, SudoModeServiceInterface::class); @@ -65,7 +65,10 @@ public function testStartRegistrationEnforcesSudoMode() $result = $this->get(Controller::join_links(AdminRootController::admin_url(), 'mfa', 'register/foo')); $this->assertSame(400, $result->getStatusCode()); - $this->assertContains('Invalid session. Please refresh and try again.', (string) $result->getBody()); + $this->assertStringContainsString( + 'Invalid session. Please refresh and try again.', + (string) $result->getBody() + ); } public function testStartRegistrationReturns200Response() @@ -114,7 +117,7 @@ public function testFinishRegistrationGracefullyHandlesInvalidSessions() ); $this->assertSame(400, $result->getStatusCode()); - $this->assertContains('Invalid session', $result->getBody()); + $this->assertStringContainsString('Invalid session', $result->getBody()); } public function testFinishRegistrationAssertsValidMethod() @@ -139,7 +142,7 @@ public function testFinishRegistrationAssertsValidMethod() ); $this->assertSame(400, $result->getStatusCode()); - $this->assertContains('No such method is available', $result->getBody()); + $this->assertStringContainsString('No such method is available', $result->getBody()); } public function testFinishRegistrationCompletesWhenValid() @@ -178,14 +181,14 @@ public function testRemoveRegistrationChecksCSRF() $response = $controller->removeRegisteredMethod($request); $this->assertSame(400, $response->getStatusCode()); - $this->assertContains('Request timed out', $response->getBody()); + $this->assertStringContainsString('Request timed out', $response->getBody()); $token = SecurityToken::inst(); $request = new HTTPRequest('DELETE', '', [$token->getName() => $token->getValue()]); $response = $controller->removeRegisteredMethod($request); - $this->assertNotContains('Request timed out', $response->getBody()); + $this->assertStringNotContainsString('Request timed out', $response->getBody()); } public function testRemoveRegistrationRequiresMethod() @@ -202,7 +205,7 @@ public function testRemoveRegistrationRequiresMethod() $response = $controller->removeRegisteredMethod($request); $this->assertSame(400, $response->getStatusCode()); - $this->assertContains('No such method is available', $response->getBody()); + $this->assertStringContainsString('No such method is available', $response->getBody()); // Method provided but non-existing $request = new HTTPRequest('DELETE', ''); @@ -210,7 +213,7 @@ public function testRemoveRegistrationRequiresMethod() $response = $controller->removeRegisteredMethod($request); $this->assertSame(400, $response->getStatusCode()); - $this->assertContains('No such method is available', $response->getBody()); + $this->assertStringContainsString('No such method is available', $response->getBody()); // Existing method $request = new HTTPRequest('DELETE', ''); @@ -246,7 +249,7 @@ public function testRemoveRegistrationSuccessIsReflectedInResponse() $response = $controller->removeRegisteredMethod($request); $this->assertSame(400, $response->getStatusCode()); - $this->assertContains('Could not delete the specified method from the user', $response->getBody()); + $this->assertStringContainsString('Could not delete the specified method from the user', $response->getBody()); } public function testRemoveRegistrationSuccessResponseIncludesTheNowAvailableMethod() @@ -344,7 +347,7 @@ public function testEnforcesSudoMode() ); $this->assertSame(400, $result->getStatusCode()); - $this->assertContains('Invalid session', $result->getBody()); + $this->assertStringContainsString('Invalid session', $result->getBody()); } /** @@ -369,14 +372,14 @@ public function testSetDefaultRegisteredMethodChecksCSRF() $response = $controller->setDefaultRegisteredMethod($request); $this->assertSame(400, $response->getStatusCode()); - $this->assertContains('Request timed out', $response->getBody()); + $this->assertStringContainsString('Request timed out', $response->getBody()); $token = SecurityToken::inst(); $request = new HTTPRequest('POST', '', [$token->getName() => $token->getValue()]); $request->setSession($this->session()); $response = $controller->setDefaultRegisteredMethod($request); - $this->assertNotContains('Request timed out', $response->getBody()); + $this->assertStringNotContainsString('Request timed out', $response->getBody()); } public function testSetDefaultRegisteredMethodFailsWhenMethodWasNotFound() @@ -389,7 +392,7 @@ public function testSetDefaultRegisteredMethodFailsWhenMethodWasNotFound() $response = $controller->setDefaultRegisteredMethod($request); $this->assertSame(400, $response->getStatusCode()); - $this->assertContains('No such method is available', $response->getBody()); + $this->assertStringContainsString('No such method is available', $response->getBody()); } public function testSetDefaultRegisteredMethodFailsWhenRegisteredMethodWasNotFoundForUser() @@ -406,7 +409,7 @@ public function testSetDefaultRegisteredMethodFailsWhenRegisteredMethodWasNotFou $response = $controller->setDefaultRegisteredMethod($request); $this->assertSame(400, $response->getStatusCode()); - $this->assertContains('No such registered method is available', $response->getBody()); + $this->assertStringContainsString('No such registered method is available', $response->getBody()); } public function testSetDefaultRegisteredMethodHandlesExceptionsOnWrite() @@ -414,7 +417,7 @@ public function testSetDefaultRegisteredMethodHandlesExceptionsOnWrite() $registeredMethodManager = $this->scaffoldRegisteredMethodManagerMock(); $controller = new AdminRegistrationController(); - /** @var LoggerInterface&PHPUnit_Framework_MockObject_MockObject $loggerMock */ + /** @var LoggerInterface&MockObject $loggerMock */ $loggerMock = $this->createMock(LoggerInterface::class); $controller->setLogger($loggerMock); @@ -428,7 +431,7 @@ public function testSetDefaultRegisteredMethodHandlesExceptionsOnWrite() ->method('getFromMember') ->willReturn(new RegisteredMethod()); - /** @var Member&PHPUnit_Framework_MockObject_MockObject $memberMock */ + /** @var Member&MockObject $memberMock */ $memberMock = $this->createMock(Member::class); $memberMock->method('write')->willThrowException(new ValidationException()); Security::setCurrentUser($memberMock); @@ -436,7 +439,7 @@ public function testSetDefaultRegisteredMethodHandlesExceptionsOnWrite() $loggerMock->expects($this->once())->method('debug'); $response = $controller->setDefaultRegisteredMethod($request); $this->assertSame(400, $response->getStatusCode()); - $this->assertContains('Could not set the default method for the user', $response->getBody()); + $this->assertStringContainsString('Could not set the default method for the user', $response->getBody()); } public function testSetDefaultRegisteredMethod() diff --git a/tests/php/Extension/AccountReset/SecurityAdminExtensionTest.php b/tests/php/Extension/AccountReset/SecurityAdminExtensionTest.php index d2277204..8e4d3df1 100644 --- a/tests/php/Extension/AccountReset/SecurityAdminExtensionTest.php +++ b/tests/php/Extension/AccountReset/SecurityAdminExtensionTest.php @@ -17,14 +17,14 @@ class SecurityAdminExtensionTest extends FunctionalTest { protected static $fixture_file = 'SecurityAdminExtensionTest.yml'; - protected function setUp() + protected function setUp(): void { parent::setUp(); SecurityToken::enable(); } - protected function tearDown() + protected function tearDown(): void { parent::tearDown(); @@ -41,7 +41,7 @@ public function testEndpointRequiresCSRF() $response = $this->post(SecurityAdmin::singleton()->Link("reset/{$member->ID}"), [true]); $this->assertEquals(400, $response->getStatusCode(), $response->getBody()); - $this->assertContains('Invalid or missing CSRF', $response->getBody()); + $this->assertStringContainsString('Invalid or missing CSRF', $response->getBody()); } public function testResetCanBeInitiatedByAdmin() @@ -79,6 +79,6 @@ public function testResetCannotBeInitiatedByStandardUser() ); $this->assertEquals(403, $response->getStatusCode(), $response->getBody()); - $this->assertContains('Insufficient permissions', $response->getBody()); + $this->assertStringContainsString('Insufficient permissions', $response->getBody()); } } diff --git a/tests/php/Extension/AccountReset/SecurityExtensionTest.php b/tests/php/Extension/AccountReset/SecurityExtensionTest.php index be285627..b69667d4 100644 --- a/tests/php/Extension/AccountReset/SecurityExtensionTest.php +++ b/tests/php/Extension/AccountReset/SecurityExtensionTest.php @@ -18,7 +18,7 @@ class SecurityExtensionTest extends FunctionalTest { protected static $fixture_file = 'SecurityExtensionTest.yml'; - protected function setUp() + protected function setUp(): void { parent::setUp(); @@ -43,7 +43,7 @@ public function testResetAccountFailsWhenAlreadyAuthenticated() $response = $this->get($url); $this->assertEquals(400, $response->getStatusCode()); - $this->assertContains('Already authenticated', $response->getBody()); + $this->assertStringContainsString('Already authenticated', $response->getBody()); } public function testResetAccountFailsWithInvalidToken() @@ -56,7 +56,7 @@ public function testResetAccountFailsWithInvalidToken() $response = $this->get($url); $this->assertEquals(400, $response->getStatusCode()); - $this->assertContains('Invalid member or token', $response->getBody()); + $this->assertStringContainsString('Invalid member or token', $response->getBody()); } public function testResetAccountFailsWithExpiredToken() @@ -73,7 +73,7 @@ public function testResetAccountFailsWithExpiredToken() $response = $this->get($url); $this->assertEquals(400, $response->getStatusCode()); - $this->assertContains('Invalid member or token', $response->getBody()); + $this->assertStringContainsString('Invalid member or token', $response->getBody()); } public function testResetAccountSubmissionFailsWithExpiredSession() @@ -96,7 +96,7 @@ public function testResetAccountSubmissionFailsWithExpiredSession() ['NewPassword1' => 'testtest', 'NewPassword2' => 'testtest'] ); - $this->assertContains('The account reset process timed out', $response->getBody()); + $this->assertStringContainsString('The account reset process timed out', $response->getBody()); } public function testResetAccountSubmissionPasses() @@ -117,7 +117,7 @@ public function testResetAccountSubmissionPasses() ); // User should have been redirected to Login form with session message - $this->assertContains('Login', $response->getBody()); - $this->assertContains('Reset complete', $response->getBody()); + $this->assertStringContainsString('Login', $response->getBody()); + $this->assertStringContainsString('Reset complete', $response->getBody()); } } diff --git a/tests/php/Extension/MemberExtensionTest.php b/tests/php/Extension/MemberExtensionTest.php index 89e2eace..cbc6980a 100644 --- a/tests/php/Extension/MemberExtensionTest.php +++ b/tests/php/Extension/MemberExtensionTest.php @@ -56,12 +56,10 @@ public function testStandardUserCanViewAndEditTheirOwnMFAConfig() $this->assertTrue($targetMember->currentUserCanEditMFAConfig(), 'Can Edit'); } - /** - * @expectedException \SilverStripe\MFA\Exception\InvalidMethodException - * @expectedExceptionMessage The provided method does not belong to this member - */ public function testSetDefaultRegisteredMethodThrowsExceptionWhenSettingSomeoneElsesMethodAsDefault() { + $this->expectException(\SilverStripe\MFA\Exception\InvalidMethodException::class); + $this->expectExceptionMessage('The provided method does not belong to this member'); /** @var Member&MemberExtension $targetMember */ $targetMember = $this->objFromFixture(Member::class, 'admin'); $method = new Method(); diff --git a/tests/php/FormField/RegisteredMFAMethodListFieldTest.php b/tests/php/FormField/RegisteredMFAMethodListFieldTest.php index dcaf7d27..8c651f26 100644 --- a/tests/php/FormField/RegisteredMFAMethodListFieldTest.php +++ b/tests/php/FormField/RegisteredMFAMethodListFieldTest.php @@ -17,16 +17,14 @@ public function testSchemaContainsEndpoints() $field = new RegisteredMFAMethodListField('test', null, $memberID); $schema = $field->getSchemaDataDefaults(); - $this->assertContains('register/', $schema['schema']['endpoints']['register']); - $this->assertContains('method/{urlSegment}', $schema['schema']['endpoints']['remove']); - $this->assertContains('method/{urlSegment}/default', $schema['schema']['endpoints']['setDefault']); + $this->assertStringContainsString('register/', $schema['schema']['endpoints']['register']); + $this->assertStringContainsString('method/{urlSegment}', $schema['schema']['endpoints']['remove']); + $this->assertStringContainsString('method/{urlSegment}/default', $schema['schema']['endpoints']['setDefault']); } - /** - * @expectedException TypeError - */ public function testConstructorRequiresMemberValue() { + $this->expectException(TypeError::class); new RegisteredMFAMethodListField('test', null, null); } } diff --git a/tests/php/Report/EnabledMembersFunctionalTest.php b/tests/php/Report/EnabledMembersFunctionalTest.php index b94b115a..bd76f406 100644 --- a/tests/php/Report/EnabledMembersFunctionalTest.php +++ b/tests/php/Report/EnabledMembersFunctionalTest.php @@ -9,7 +9,7 @@ class EnabledMembersFunctionalTest extends FunctionalTest { protected static $fixture_file = 'EnabledMembersTest.yml'; - protected function setUp() + protected function setUp(): void { parent::setUp(); @@ -20,12 +20,12 @@ public function testReportHasMemberNames() { $result = (string) $this->get(EnabledMembers::create()->getLink())->getBody(); - $this->assertContains('Eleanor', $result); - $this->assertContains('Ditor', $result); - $this->assertContains('Michelle', $result); - $this->assertContains('Fa', $result); - $this->assertContains('Ursula', $result); - $this->assertContains('Ser', $result); + $this->assertStringContainsString('Eleanor', $result); + $this->assertStringContainsString('Ditor', $result); + $this->assertStringContainsString('Michelle', $result); + $this->assertStringContainsString('Fa', $result); + $this->assertStringContainsString('Ursula', $result); + $this->assertStringContainsString('Ser', $result); } public function testReportHasRegisteredMethods() @@ -34,7 +34,7 @@ public function testReportHasRegisteredMethods() $result = (string) $this->get(EnabledMembers::create()->getLink())->getBody(); - $this->assertContains('Math problem, Null', $result); + $this->assertStringContainsString('Math problem, Null', $result); } public function testFilterReportByMemberName() @@ -45,8 +45,8 @@ public function testFilterReportByMemberName() ]); $result = (string) $response->getBody(); - $this->assertContains('mfa@example.com', $result); - $this->assertNotContains('admin@example.com', $result); + $this->assertStringContainsString('mfa@example.com', $result); + $this->assertStringNotContainsString('admin@example.com', $result); } public function testFilterReportBySkippedRegistration() @@ -57,7 +57,7 @@ public function testFilterReportBySkippedRegistration() ]); $result = (string) $response->getBody(); - $this->assertContains('user@example.com', $result); - $this->assertNotContains('admin@example.com', $result); + $this->assertStringContainsString('user@example.com', $result); + $this->assertStringNotContainsString('admin@example.com', $result); } } diff --git a/tests/php/Report/EnabledMembersTest.php b/tests/php/Report/EnabledMembersTest.php index 4197420a..5f6e2ac4 100644 --- a/tests/php/Report/EnabledMembersTest.php +++ b/tests/php/Report/EnabledMembersTest.php @@ -15,7 +15,7 @@ class EnabledMembersTest extends SapphireTest { protected static $fixture_file = 'EnabledMembersTest.yml'; - protected function setUp() + protected function setUp(): void { parent::setUp(); Config::modify() diff --git a/tests/php/Service/BackupCodeGeneratorTest.php b/tests/php/Service/BackupCodeGeneratorTest.php index 0a89c07d..1cbc8d92 100644 --- a/tests/php/Service/BackupCodeGeneratorTest.php +++ b/tests/php/Service/BackupCodeGeneratorTest.php @@ -9,7 +9,7 @@ class BackupCodeGeneratorTest extends SapphireTest { - protected function setUp() + protected function setUp(): void { parent::setUp(); diff --git a/tests/php/Service/EnforcementManagerTest.php b/tests/php/Service/EnforcementManagerTest.php index 7759a911..56c7f1d0 100644 --- a/tests/php/Service/EnforcementManagerTest.php +++ b/tests/php/Service/EnforcementManagerTest.php @@ -16,7 +16,7 @@ class EnforcementManagerTest extends SapphireTest { protected static $fixture_file = 'EnforcementManagerTest.yml'; - protected function setUp() + protected function setUp(): void { parent::setUp(); diff --git a/tests/php/Service/MethodRegistryTest.php b/tests/php/Service/MethodRegistryTest.php index cd83899d..32d352b9 100644 --- a/tests/php/Service/MethodRegistryTest.php +++ b/tests/php/Service/MethodRegistryTest.php @@ -22,27 +22,24 @@ public function testGetAllMethodsReturnsRegisteredMethods() $this->assertInstanceOf(Method::class, reset($methods)); } - /** - * @expectedException UnexpectedValueException - * phpcs:disable - * @expectedExceptionMessage Given method "SilverStripe\Security\Member" does not implement SilverStripe\MFA\Method\MethodInterface - * phpcs:enable - */ public function testInvalidMethodsThrowExceptions() { + $this->expectException(UnexpectedValueException::class); + $this->expectExceptionMessage( + 'Given method "SilverStripe\Security\Member" does not implement SilverStripe\MFA\Method\MethodInterface' + ); Config::modify()->set(MethodRegistry::class, 'methods', [Member::class]); $registry = MethodRegistry::singleton(); $registry->getMethods(); } - /** - * @expectedException UnexpectedValueException - * phpcs:disable - * @expectedExceptionMessage Cannot register MFA methods more than once. Check your config: SilverStripe\MFA\Tests\Stub\BasicMath\Method - * phpcs:enable - */ public function testRegisteringMethodMultipleTimesThrowsException() { + $this->expectException(UnexpectedValueException::class); + $this->expectExceptionMessage( + 'Cannot register MFA methods more than once. ' . + 'Check your config: SilverStripe\MFA\Tests\Stub\BasicMath\Method' + ); Config::modify()->set(MethodRegistry::class, 'methods', [ Method::class, Method::class, @@ -52,12 +49,12 @@ public function testRegisteringMethodMultipleTimesThrowsException() MethodRegistry::singleton()->getMethods(); } - /** - * @expectedException UnexpectedValueException - * @expectedExceptionMessage Cannot register multiple MFA methods with the same URL segment: basic-math - */ public function testRegisteringMethodsWithSameURLSegmentThrowsException() { + $this->expectException(UnexpectedValueException::class); + $this->expectExceptionMessage( + 'Cannot register multiple MFA methods with the same URL segment: basic-math' + ); Config::modify()->set(MethodRegistry::class, 'methods', [ Method::class, DuplicatedBasicMath\Method::class, diff --git a/tests/php/Service/PHPEncryptionAdapterTest.php b/tests/php/Service/PHPEncryptionAdapterTest.php index f132205c..f90dfa39 100644 --- a/tests/php/Service/PHPEncryptionAdapterTest.php +++ b/tests/php/Service/PHPEncryptionAdapterTest.php @@ -18,11 +18,9 @@ public function testEncryptAndDecrypt() $this->assertSame('Hello World', $plaintext); } - /** - * @expectedException \SilverStripe\MFA\Exception\EncryptionAdapterException - */ public function testExceptionThrownWhenDecryptionFails() { + $this->expectException(\SilverStripe\MFA\Exception\EncryptionAdapterException::class); (new DefusePHPEncryptionAdapter())->decrypt('892g359gohsdf', ''); } } diff --git a/tests/php/Service/SchemaGeneratorTest.php b/tests/php/Service/SchemaGeneratorTest.php index b36f011d..865e324d 100644 --- a/tests/php/Service/SchemaGeneratorTest.php +++ b/tests/php/Service/SchemaGeneratorTest.php @@ -18,7 +18,7 @@ class SchemaGeneratorTest extends SapphireTest */ protected $generator; - protected function setUp() + protected function setUp(): void { parent::setUp(); diff --git a/tests/php/State/AvailableMethodDetailsTest.php b/tests/php/State/AvailableMethodDetailsTest.php index b6973568..61cfcf2b 100644 --- a/tests/php/State/AvailableMethodDetailsTest.php +++ b/tests/php/State/AvailableMethodDetailsTest.php @@ -2,7 +2,7 @@ namespace SilverStripe\MFA\Tests\State; -use PHPUnit_Framework_MockObject_MockObject; +use PHPUnit\Framework\MockObject\MockObject; use SilverStripe\Dev\SapphireTest; use SilverStripe\MFA\Method\Handler\RegisterHandlerInterface; use SilverStripe\MFA\Method\MethodInterface; @@ -11,7 +11,7 @@ class AvailableMethodDetailsTest extends SapphireTest { /** - * @var MethodInterface|PHPUnit_Framework_MockObject_MockObject + * @var MethodInterface|MockObject */ protected $method; @@ -20,7 +20,7 @@ class AvailableMethodDetailsTest extends SapphireTest */ protected $details; - protected function setUp() + protected function setUp(): void { parent::setUp(); @@ -32,6 +32,6 @@ public function testJsonSerialize() { $this->method->expects($this->once())->method('getName')->willReturn('Backup Codes'); $result = json_encode($this->details); - $this->assertContains('Backup Codes', $result); + $this->assertStringContainsString('Backup Codes', $result); } } diff --git a/tests/php/State/RegisteredMethodDetailsTest.php b/tests/php/State/RegisteredMethodDetailsTest.php index cb860737..bc97adb8 100644 --- a/tests/php/State/RegisteredMethodDetailsTest.php +++ b/tests/php/State/RegisteredMethodDetailsTest.php @@ -2,15 +2,16 @@ namespace SilverStripe\MFA\Tests\State; -use PHPUnit_Framework_MockObject_MockObject; +use PHPUnit\Framework\MockObject\MockObject; +use PHPUnit\Framework\TestCase; use SilverStripe\MFA\Method\Handler\VerifyHandlerInterface; use SilverStripe\MFA\Method\MethodInterface; use SilverStripe\MFA\State\RegisteredMethodDetails; -class RegisteredMethodDetailsTest extends \PHPUnit_Framework_TestCase +class RegisteredMethodDetailsTest extends TestCase { /** - * @var MethodInterface|PHPUnit_Framework_MockObject_MockObject + * @var MethodInterface|MockObject */ protected $method; @@ -19,7 +20,7 @@ class RegisteredMethodDetailsTest extends \PHPUnit_Framework_TestCase */ protected $details; - protected function setUp() + protected function setUp(): void { parent::setUp(); @@ -35,6 +36,6 @@ public function testJsonSerialize() { $this->method->expects($this->once())->method('getURLSegment')->willReturn('foo-bar'); $result = json_encode($this->details); - $this->assertContains('foo-bar', $result); + $this->assertStringContainsString('foo-bar', $result); } } diff --git a/tests/php/Store/SessionStoreTest.php b/tests/php/Store/SessionStoreTest.php index bc75eaab..27e27106 100644 --- a/tests/php/Store/SessionStoreTest.php +++ b/tests/php/Store/SessionStoreTest.php @@ -10,12 +10,10 @@ class SessionStoreTest extends SapphireTest { - /** - * @expectedException \RuntimeException - * @expectedExceptionMessageRegExp /possibly incorrectly encoded/ - */ public function testSerializeThrowsExceptionOnFailure() { + $this->expectException(\RuntimeException::class); + $this->expectExceptionMessageMatches('/possibly incorrectly encoded/'); $store = new SessionStore($this->createMock(Member::class)); $store->setState(['some binary' => random_bytes(32)]); $store->serialize(); @@ -36,12 +34,10 @@ public function testAddState() $this->assertSame(['foo' => 'baz', 'bar' => 'baz'], $store->getState()); } - /** - * @expectedException \SilverStripe\MFA\Exception\InvalidMethodException - * @expectedExceptionMessage You cannot verify with a method you have already verified - */ public function testSetMethodWithVerifiedMethod() { + $this->expectException(\SilverStripe\MFA\Exception\InvalidMethodException::class); + $this->expectExceptionMessage('You cannot verify with a method you have already verified'); $store = new SessionStore($this->createMock(Member::class)); $store->addVerifiedMethod('foobar'); $store->setMethod('foobar');