Skip to content

Commit

Permalink
Merge pull request #14 from elnurvl/master
Browse files Browse the repository at this point in the history
Allow manipulating the fake response
  • Loading branch information
madmatt authored Sep 7, 2021
2 parents 9903b3b + 135510b commit 7441dd6
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 15 deletions.
12 changes: 6 additions & 6 deletions src/Gateway/Request/AuthenticateSPRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
class AuthenticateSPRequest
{
/**
* @var string
* @var ?string
*/
private string $CustomerReference;
private ?string $CustomerReference;

/**
* @var stdClass
Expand Down Expand Up @@ -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;

Expand Down
14 changes: 11 additions & 3 deletions src/Service/GlobalAuthenticationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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().
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
*/
Expand Down
41 changes: 37 additions & 4 deletions src/Stubs/Gateway/GlobalAuthenticationGatewayFake.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
));
}
}
49 changes: 47 additions & 2 deletions tests/Service/GlobalAuthenticationServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,20 @@

class GlobalAuthenticationServiceTest extends TestCase
{
/**
* @var GlobalAuthenticationGatewayFake
*/
private GlobalAuthenticationGatewayFake $fakeGateway;

/**
* @var GlobalAuthenticationService
*/
private GlobalAuthenticationService $service;

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);
}

/**
Expand Down Expand Up @@ -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();
Expand All @@ -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],
['', 0, '', GlobalAuthenticationGatewayFake::IDENTITY_BAND_REFER, 500],
];
}
}

0 comments on commit 7441dd6

Please sign in to comment.