Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API phpunit 9 support #95

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
],
"license": "BSD-3-Clause",
"require": {
"php": "^7.3 || ^8.0",
"silverstripe/mfa": "^4.0",
"web-auth/webauthn-lib": "^1.0",
"guzzlehttp/psr7": "^1.6"
},
"require-dev": {
"sminnee/phpunit": "^5.7",
"phpunit/phpunit": "^9.5",
"squizlabs/php_codesniffer": "^3"
},
"autoload": {
Expand Down
8 changes: 5 additions & 3 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<phpunit bootstrap="vendor/silverstripe/cms/tests/bootstrap.php" colors="true">
<testsuite name="webauthn">
<directory>tests/</directory>
</testsuite>
<testsuites>
<testsuite name="webauthn">
<directory>tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist addUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src/</directory>
Expand Down
6 changes: 2 additions & 4 deletions tests/CredentialRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@ public function testHas()
$this->assertFalse($repo->has('barbaz'));
}

/**
* @expectedException InvalidArgumentException
* @expectedExceptionMessage Given credential ID does not match any stored credentials
*/
public function testGetThrowsExceptionOnInvalidCredentialId()
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Given credential ID does not match any stored credentials');
$repo = new CredentialRepository('1');
$repo->get('non-existent');
}
Expand Down
4 changes: 2 additions & 2 deletions tests/MethodTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function testGetRegisterHandler()
public function testGetThumbnail()
{
$method = new Method();
$this->assertContains('images/securityKey.svg', $method->getThumbnail());
$this->assertStringContainsString('images/securityKey.svg', $method->getThumbnail());
}

public function testApplyRequirements()
Expand Down Expand Up @@ -63,6 +63,6 @@ public function testIsNotAvailableUnderHttp()
public function testGetUnavailableMessage()
{
$method = new Method();
$this->assertContains('can only be used over HTTPS', $method->getUnavailableMessage());
$this->assertStringContainsString('can only be used over HTTPS', $method->getUnavailableMessage());
}
}
32 changes: 21 additions & 11 deletions tests/RegisterHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace SilverStripe\WebAuthn\Tests;

use Exception;
use PHPUnit_Framework_MockObject_MockObject;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Core\Injector\Injector;
Expand Down Expand Up @@ -55,7 +55,7 @@ class RegisterHandlerTest extends SapphireTest
*/
protected $originalServer;

protected function setUp()
protected function setUp(): void
{
parent::setUp();

Expand All @@ -77,7 +77,7 @@ protected function setUp()
);
}

protected function tearDown()
protected function tearDown(): void
{
$_SERVER = $this->originalServer;

Expand Down Expand Up @@ -157,7 +157,7 @@ public function testRegisterReturnsErrorWhenRequiredInformationIsMissing()
$result = $this->handler->register($this->request, $this->store);

$this->assertFalse($result->isSuccessful());
$this->assertContains('Incomplete data', $result->getMessage());
$this->assertStringContainsString('Incomplete data', $result->getMessage());
}

/**
Expand All @@ -175,7 +175,7 @@ public function testRegister(
callable $responseValidatorMockCallback = null,
callable $storeModifier = null
) {
/** @var RegisterHandler&PHPUnit_Framework_MockObject_MockObject $handlerMock */
/** @var RegisterHandler&MockObject $handlerMock */
$handlerMock = $this->getMockBuilder(RegisterHandler::class)
->setMethods(['getPublicKeyCredentialLoader', 'getAuthenticatorAttestationResponseValidator'])
->getMock();
Expand Down Expand Up @@ -213,7 +213,7 @@ public function testRegister(

$this->assertSame($expectedResult->isSuccessful(), $result->isSuccessful());
if ($expectedResult->getMessage()) {
$this->assertContains($expectedResult->getMessage(), $result->getMessage());
$this->assertStringContainsString($expectedResult->getMessage(), $result->getMessage());
}

$this->assertCount(
Expand Down Expand Up @@ -279,18 +279,28 @@ public function registerProvider()
$responseMock,
new Result(true),
1,
function (PHPUnit_Framework_MockObject_MockObject $responseValidatorMock) {
function (MockObject $responseValidatorMock) {
// Specifically setting expectations for the result of the response validator's "check" call
$responseValidatorMock->expects($this->once())->method('check')->willReturn(true);
$responseValidatorMock
->expects($this->once())
->method('check')
->willReturnCallback(function (): bool {
return true;
});
},
],
'valid response with existing credential' => [
$responseMock,
new Result(true),
1,
function (PHPUnit_Framework_MockObject_MockObject $responseValidatorMock) {
function (MockObject $responseValidatorMock) {
// Specifically setting expectations for the result of the response validator's "check" call
$responseValidatorMock->expects($this->once())->method('check')->willReturn(true);
$responseValidatorMock
->expects($this->once())
->method('check')
->willReturnCallback(function (): bool {
return true;
});
},
function (SessionStore $store) use ($testSource) {
$repo = new CredentialRepository((string) $store->getMember()->ID);
Expand All @@ -316,7 +326,7 @@ function (SessionStore $store) use ($testSource) {
$responseMock,
new Result(false, 'I am a test'),
0,
function (PHPUnit_Framework_MockObject_MockObject $responseValidatorMock) {
function (MockObject $responseValidatorMock) {
// Specifically setting expectations for the result of the response validator's "check" call
$responseValidatorMock->expects($this->once())->method('check')
->willThrowException(new Exception('I am a test'));
Expand Down
25 changes: 14 additions & 11 deletions tests/VerifyHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace SilverStripe\WebAuthn\Tests;

use Exception;
use PHPUnit_Framework_MockObject_MockObject;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Core\Injector\Injector;
Expand Down Expand Up @@ -54,7 +54,7 @@ class VerifyHandlerTest extends SapphireTest
*/
protected $mockData = [];

protected function setUp()
protected function setUp(): void
{
parent::setUp();

Expand Down Expand Up @@ -94,11 +94,9 @@ protected function setUp()
// phpcs:enable
}

/**
* @expectedException \SilverStripe\MFA\Exception\AuthenticationFailedException
*/
public function testStartThrowsExceptionWithMissingData()
{
$this->expectException(\SilverStripe\MFA\Exception\AuthenticationFailedException::class);
$this->registeredMethod->Data = '';
$this->handler->start($this->store, $this->registeredMethod);
}
Expand All @@ -115,7 +113,7 @@ public function testVerifyReturnsErrorWhenRequiredInformationIsMissing()
$result = $this->handler->verify($this->request, $this->store, $this->registeredMethod);

$this->assertFalse($result->isSuccessful());
$this->assertContains('Incomplete data', $result->getMessage());
$this->assertStringContainsString('Incomplete data', $result->getMessage());
}

/**
Expand All @@ -129,7 +127,7 @@ public function testVerify(
$expectedResult,
callable $responseValidatorMockCallback = null
) {
/** @var VerifyHandler&PHPUnit_Framework_MockObject_MockObject $handlerMock */
/** @var VerifyHandler&MockObject $handlerMock */
$handlerMock = $this->getMockBuilder(VerifyHandler::class)
->setMethods(['getPublicKeyCredentialLoader', 'getAuthenticatorAssertionResponseValidator'])
->getMock();
Expand Down Expand Up @@ -162,7 +160,7 @@ public function testVerify(

$this->assertSame($expectedResult->isSuccessful(), $result->isSuccessful());
if ($expectedResult->getMessage()) {
$this->assertContains($expectedResult->getMessage(), $result->getMessage());
$this->assertStringContainsString($expectedResult->getMessage(), $result->getMessage());
}
}

Expand All @@ -183,15 +181,20 @@ public function verifyProvider()
'valid response' => [
$this->createMock(AuthenticatorAssertionResponse::class),
new Result(true),
function (PHPUnit_Framework_MockObject_MockObject $responseValidatorMock) {
function (MockObject $responseValidatorMock) {
// Specifically setting expectations for the result of the response validator's "check" call
$responseValidatorMock->expects($this->once())->method('check')->willReturn(true);
$responseValidatorMock
->expects($this->once())
->method('check')
->willReturnCallback(function (): bool {
return true;
});
},
],
'invalid response' => [
$this->createMock(AuthenticatorAssertionResponse::class),
new Result(false, 'I am a test'),
function (PHPUnit_Framework_MockObject_MockObject $responseValidatorMock) {
function (MockObject $responseValidatorMock) {
// Specifically setting expectations for the result of the response validator's "check" call
$responseValidatorMock->expects($this->once())->method('check')
->willThrowException(new Exception('I am a test'));
Expand Down