From e9783e3a2fc4ab8b0d82d26ad86cb63f32313350 Mon Sep 17 00:00:00 2001 From: Elnur Hajiyev Date: Mon, 6 Sep 2021 03:10:39 +0400 Subject: [PATCH 1/4] Allow manipulating the fake response --- src/Service/GlobalAuthenticationService.php | 8 +++ .../GlobalAuthenticationGatewayFake.php | 41 ++++++++++++++-- .../GlobalAuthenticationServiceTest.php | 49 ++++++++++++++++++- 3 files changed, 92 insertions(+), 6 deletions(-) diff --git a/src/Service/GlobalAuthenticationService.php b/src/Service/GlobalAuthenticationService.php index ff11fed..7026ff2 100644 --- a/src/Service/GlobalAuthenticationService.php +++ b/src/Service/GlobalAuthenticationService.php @@ -213,6 +213,14 @@ public function getGateway(): GlobalAuthenticationGateway return $this->gateway; } + /** + * @param GlobalAuthenticationGateway $gateway + */ + public function setGateway(GlobalAuthenticationGateway $gateway): void + { + $this->gateway = $gateway; + } + /** * @return array */ diff --git a/src/Stubs/Gateway/GlobalAuthenticationGatewayFake.php b/src/Stubs/Gateway/GlobalAuthenticationGatewayFake.php index 411afcd..c707932 100644 --- a/src/Stubs/Gateway/GlobalAuthenticationGatewayFake.php +++ b/src/Stubs/Gateway/GlobalAuthenticationGatewayFake.php @@ -25,18 +25,51 @@ class GlobalAuthenticationGatewayFake extends GlobalAuthenticationGateway */ const IDENTITY_BAND_ALERT = 'ALERT'; + private string $bandText = self::IDENTITY_BAND_PASS; + + private int $score = 3000; + public function __construct($username, $password, $soapClientOptions = [], $usePilotSite = false) { parent::__construct($username, $password, $soapClientOptions, $usePilotSite); } - public function AuthenticateSP($profileID, $profileVersion, $customerReference, Identity $identity) + /** + * @param string $bandText + * + * @return GlobalAuthenticationGatewayFake + */ + public function setBandText(string $bandText): GlobalAuthenticationGatewayFake { - $bandText = self::IDENTITY_BAND_PASS; + $this->bandText = $bandText; + return $this; + } + + /** + * @param int $score + * + * @return GlobalAuthenticationGatewayFake + */ + public function setScore(int $score): GlobalAuthenticationGatewayFake + { + $this->score = $score; + + return $this; + } + + public function AuthenticateSP(string $profileID, int $profileVersion, ?string $customerReference, Identity $identity) + { return unserialize(sprintf( - 'O:8:"stdClass":1:{s:20:"AuthenticateSPResult";O:8:"stdClass":12:{s:16:"AuthenticationID";s:36:"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";s:9:"Timestamp";s:33:"2016-01-01T00:00:00.0000000+01:00";s:11:"CustomerRef";s:1:"x";s:9:"ProfileID";s:36:"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";s:11:"ProfileName";s:15:"Default Profile";s:14:"ProfileVersion";i:1;s:15:"ProfileRevision";i:1;s:12:"ProfileState";s:9:"Effective";s:11:"ResultCodes";O:8:"stdClass":1:{s:26:"GlobalItemCheckResultCodes";a:0:{}}s:5:"Score";i:3000;s:8:"BandText";s:4:"%s";s:7:"Country";s:11:"New Zealand";}}', - $bandText + 'O:8:"stdClass":1:{s:20:"AuthenticateSPResult";O:8:"stdClass":12:{s:16:"AuthenticationID";s:36:"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";s:9:"Timestamp";s:33:"2016-01-01T00:00:00.0000000+01:00";s:11:"CustomerRef";s:%d:"%s";s:9:"ProfileID";s:%d:"%s";s:11:"ProfileName";s:15:"Default Profile";s:14:"ProfileVersion";i:%d;s:15:"ProfileRevision";i:1;s:12:"ProfileState";s:9:"Effective";s:11:"ResultCodes";O:8:"stdClass":1:{s:26:"GlobalItemCheckResultCodes";a:0:{}}s:5:"Score";i:%d;s:8:"BandText";s:%d:"%s";s:7:"Country";s:11:"New Zealand";}}', + strlen($customerReference), + $customerReference, + strlen($profileID), + $profileID, + $profileVersion, + $this->score, + strlen($this->bandText), + $this->bandText )); } } diff --git a/tests/Service/GlobalAuthenticationServiceTest.php b/tests/Service/GlobalAuthenticationServiceTest.php index 73daf5c..b7f721a 100644 --- a/tests/Service/GlobalAuthenticationServiceTest.php +++ b/tests/Service/GlobalAuthenticationServiceTest.php @@ -13,6 +13,11 @@ class GlobalAuthenticationServiceTest extends TestCase { + /** + * @var GlobalAuthenticationGatewayFake + */ + private GlobalAuthenticationGatewayFake $fakeGateway; + /** * @var GlobalAuthenticationService */ @@ -20,8 +25,8 @@ class GlobalAuthenticationServiceTest extends TestCase public function setUp(): void { - $fakeGateway = new GlobalAuthenticationGatewayFake('username', 'password'); - $this->service = new GlobalAuthenticationService($fakeGateway); + $this->fakeGateway = new GlobalAuthenticationGatewayFake('username', 'password'); + $this->service = new GlobalAuthenticationService($this->fakeGateway); } /** @@ -62,6 +67,38 @@ public function testSuccessfulResponse(?string $customerReference, ?DateTime $bi $this->assertSame('Default Profile', $response->AuthenticateSPResult->ProfileName); } + /** + * @dataProvider fakeResponses + * + * @param string $profileId + * @param int $profileVersion + * @param string $customerReference + * @param string $bandText + * @param int $score + * + * @throws IdentityVerificationFailureException + */ + public function testFakeResponses(string $profileId, int $profileVersion, string $customerReference, string $bandText, int $score) + { + // Arrange + $this->fakeGateway->setBandText($bandText)->setScore($score); + + $identity = new Identity(); + + // Act + $result = $this->service + ->setProfileId($profileId) + ->setProfileVersion($profileVersion) + ->verifyIdentity($identity, $customerReference); + + // Assert + $this->assertSame($bandText, $result); + $this->assertSame($score, $this->service->getLastVerifyIdentityResponse()->AuthenticateSPResult->Score); + $this->assertSame($profileId, $this->service->getLastVerifyIdentityResponse()->AuthenticateSPResult->ProfileID); + $this->assertSame($profileVersion, $this->service->getLastVerifyIdentityResponse()->AuthenticateSPResult->ProfileVersion); + $this->assertSame($customerReference, $this->service->getLastVerifyIdentityResponse()->AuthenticateSPResult->CustomerRef); + } + public function testNotSettingProfileIdThrows() { $identity = new Identity(); @@ -78,4 +115,12 @@ public function authenticateSp(): array ['customer-reference', null], ]; } + + public function fakeResponses(): array + { + return [ + ['profile-id', 1, 'customer-1', GlobalAuthenticationGatewayFake::IDENTITY_BAND_ALERT, 20000], + ['profile-id-2', 0, 'customer-2', GlobalAuthenticationGatewayFake::IDENTITY_BAND_REFER, 500], + ]; + } } From 9dd1fc85c6b0078647d6c738a4a773db75cf0f8a Mon Sep 17 00:00:00 2001 From: Elnur Hajiyev Date: Mon, 6 Sep 2021 03:14:55 +0400 Subject: [PATCH 2/4] Apply fixes from StyleCI (#6) Co-authored-by: Elnur Hajiyev --- tests/Service/GlobalAuthenticationServiceTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Service/GlobalAuthenticationServiceTest.php b/tests/Service/GlobalAuthenticationServiceTest.php index b7f721a..3fc6ee9 100644 --- a/tests/Service/GlobalAuthenticationServiceTest.php +++ b/tests/Service/GlobalAuthenticationServiceTest.php @@ -71,10 +71,10 @@ public function testSuccessfulResponse(?string $customerReference, ?DateTime $bi * @dataProvider fakeResponses * * @param string $profileId - * @param int $profileVersion + * @param int $profileVersion * @param string $customerReference * @param string $bandText - * @param int $score + * @param int $score * * @throws IdentityVerificationFailureException */ From d61a0520fd992f3bcf7a01a3921cc556997fa258 Mon Sep 17 00:00:00 2001 From: Elnur Hajiyev Date: Mon, 6 Sep 2021 14:45:36 +0400 Subject: [PATCH 3/4] Make `$CustomerReference` nullable in `AuthenticateSPRequest` --- src/Gateway/Request/AuthenticateSPRequest.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Gateway/Request/AuthenticateSPRequest.php b/src/Gateway/Request/AuthenticateSPRequest.php index ddea84b..be66ff3 100644 --- a/src/Gateway/Request/AuthenticateSPRequest.php +++ b/src/Gateway/Request/AuthenticateSPRequest.php @@ -14,9 +14,9 @@ class AuthenticateSPRequest { /** - * @var string + * @var ?string */ - private string $CustomerReference; + private ?string $CustomerReference; /** * @var stdClass @@ -111,19 +111,19 @@ private function addContactDetails(Identity $identity) } /** - * @return string + * @return ?string */ - public function getCustomerReference(): string + public function getCustomerReference(): ?string { return $this->CustomerReference; } /** - * @param string $CustomerReference + * @param ?string $CustomerReference * * @return AuthenticateSPRequest */ - public function setCustomerReference(string $CustomerReference): AuthenticateSPRequest + public function setCustomerReference(?string $CustomerReference): AuthenticateSPRequest { $this->CustomerReference = $CustomerReference; From 135510b46523d64e3c60776cca27820e7106c8a3 Mon Sep 17 00:00:00 2001 From: Elnur Hajiyev Date: Mon, 6 Sep 2021 15:39:09 +0400 Subject: [PATCH 4/4] Require `$ProfileID` to be not null rather than not empty So it is possible to send an empty string when using the fake gateway --- src/Service/GlobalAuthenticationService.php | 6 +++--- tests/Service/GlobalAuthenticationServiceTest.php | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Service/GlobalAuthenticationService.php b/src/Service/GlobalAuthenticationService.php index 7026ff2..83e26a7 100644 --- a/src/Service/GlobalAuthenticationService.php +++ b/src/Service/GlobalAuthenticationService.php @@ -18,11 +18,11 @@ class GlobalAuthenticationService extends ID3BaseService private GlobalAuthenticationGateway $gateway; /** - * @var string The Profile ID to be used when verifying identities via->verifyIdentity(). + * @var ?string The Profile ID to be used when verifying identities via->verifyIdentity(). * * @see self::setProfileId() */ - private string $profileId = ''; + private ?string $profileId = null; /** * @var int The version of the Profile ID to be used when verifying identities via->verifyIdentity(). @@ -99,7 +99,7 @@ public function verifyIdentity(Identity $identity, ?string $customerReference = $gateway = $this->getGateway(); - if (!$this->profileId) { + if ($this->profileId === null) { $error = 'An ID3global Profile ID must be set by calling setProfileId() before calling verifyIdentity().'; throw new LogicException($error); diff --git a/tests/Service/GlobalAuthenticationServiceTest.php b/tests/Service/GlobalAuthenticationServiceTest.php index 3fc6ee9..ef1b5ee 100644 --- a/tests/Service/GlobalAuthenticationServiceTest.php +++ b/tests/Service/GlobalAuthenticationServiceTest.php @@ -120,7 +120,7 @@ public function fakeResponses(): array { return [ ['profile-id', 1, 'customer-1', GlobalAuthenticationGatewayFake::IDENTITY_BAND_ALERT, 20000], - ['profile-id-2', 0, 'customer-2', GlobalAuthenticationGatewayFake::IDENTITY_BAND_REFER, 500], + ['', 0, '', GlobalAuthenticationGatewayFake::IDENTITY_BAND_REFER, 500], ]; } }