Skip to content

Commit

Permalink
fix: php 7,1
Browse files Browse the repository at this point in the history
  • Loading branch information
craigAtCD committed Jul 6, 2022
1 parent 9d49be0 commit 21a9703
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 61 deletions.
14 changes: 7 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
}
},
"require": {
"php": "^8.0",
"illuminate/support": "^8.0|^9.0",
"illuminate/validation": "^8.0|^9.0",
"illuminate/translation": "^8.0|^9.0"
"php": "^7.1|^8.0",
"illuminate/support": ">=5.8",
"illuminate/validation": ">=5.8",
"illuminate/translation": ">=5.8"
},
"require-dev": {
"phpunit/phpunit": "^9.0",
"nunomaduro/larastan": "^1.0|^2.0",
"guzzlehttp/guzzle": "^7.0"
"phpunit/phpunit": ">=7",
"nunomaduro/larastan": "*",
"guzzlehttp/guzzle": "^6.0|^7.0"
},
"scripts": {
"test": "phpunit",
Expand Down
26 changes: 22 additions & 4 deletions src/Datazoo.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,30 @@
use GuzzleHttp\Client;
use GuzzleHttp\ClientInterface;
use CustomD\Datazoo\Model\ModelAbstract;
use GuzzleHttp\Exception\ClientException;
use Illuminate\Validation\ValidationException;

class Datazoo
{
protected string $apiUrl = 'https://idu.datazoo.com/api/v2';
/**
* @var string
*/
protected $apiUrl = 'https://idu.datazoo.com/api/v2';

protected array $config;
/**
* @var array
*/
protected $config;

protected ?string $sessionToken = null;
/**
* @var string|null
*/
protected $sessionToken = null;

/**
* @var \GuzzleHttp\ClientInterface&\GuzzleHttp\ClientTrait
*/
protected ClientInterface $client;
protected $client;

public function __construct(array $config, ?ClientInterface $client = null)
{
Expand All @@ -45,10 +55,18 @@ public function auth(): bool
"password" => $this->config['password']
]
]);

$response = json_decode($res->getBody(), true);

$this->sessionToken = $response['sessionToken'] ?? null;

return $this->sessionToken !== null;
} catch (ClientException $e) {
throw new ValidationException([
'message' => $e->getMessage()
]);
} catch (\Throwable $e) {
dd($e);
return false;
}
}
Expand Down
22 changes: 17 additions & 5 deletions src/Model/GlobalWatchlist.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,28 @@

class GlobalWatchlist
{
public string $service = 'Watchlist AML';
/**
* @var string $service
*/
public $service = 'Watchlist AML';

public array $concent = [];
/**
* @var array $concent
*/
public $concent = [];

public array $rules = [
/**
* @var array $rules
*/
public $rules = [
'dateOfBirth' => ['required','date'],
'gender' => ['required'],
];

public array $fields = [
/**
* @var array $fields
*/
public $fields = [
'firstName',
'middleName',
'lastName',
Expand All @@ -26,5 +38,5 @@ class GlobalWatchlist
/**
* @param class-string $responseObject
*/
public string $responseObject = '\CustomD\Datazoo\Response\GlobalWatchlistResponse';
public $responseObject = '\CustomD\Datazoo\Response\GlobalWatchlistResponse';
}
52 changes: 42 additions & 10 deletions src/Model/ModelAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ public function __construct(array $data = [], array $services = [])
}
}

public function addService(string $service): static
/**
* @return static
*/
public function addService(string $service)
{
$class = new $this->serviceMap[$service]();
$this->services[$service] = $class;
Expand All @@ -42,15 +45,21 @@ public function addService(string $service): static
}


public function removeService(string $service): static
/**
* @return static
*/
public function removeService(string $service)
{
if (isset($this->services[$service])) {
unset($this->services[$service]);
}
return $this;
}

public function validateData(): void
/**
* @return void
*/
public function validateData()
{
$required = [
'countryCode' => ['required','string'],
Expand All @@ -73,31 +82,48 @@ public function validateData(): void
$validator->validate();
}

public function setClientReferenceValue(string $value): static
/**
* @return static
*/
public function setClientReferenceValue(string $value)
{
$this->fields['clientReference'] = $value;
return $this;
}

public function setFirstNameValue(string $value): static
/**
* @return static
*/
public function setFirstNameValue(string $value)
{
$this->fields['firstName'] = $value;
return $this;
}

public function setMiddleNameValue(string $value): static
/**
* @return static
*/
public function setMiddleNameValue(string $value)
{
$this->fields['middleName'] = $value;
return $this;
}

public function setLastNameValue(string $value): static
/**
* @return static
*/
public function setLastNameValue(string $value)
{
$this->fields['lastName'] = $value;
return $this;
}

public function setDateOfBirthValue(string|DateTime $dob): static
/**
* @param string|DateTime $dob
*
* @return static
*/
public function setDateOfBirthValue($dob)
{
if (! $dob instanceof DateTime) {
$dob = new DateTime($dob);
Expand All @@ -106,13 +132,19 @@ public function setDateOfBirthValue(string|DateTime $dob): static
return $this;
}

public function toRequest(): array
/**
* @return array
*/
public function toRequest()
{
$this->validateData();
return $this->fields;
}

public function setResponse(string $body): AbstractResponse
/**
* @return AbstractResponse
*/
public function setResponse(string $body)
{
return (new AbstractResponse($body, array_keys($this->services)));
}
Expand Down
25 changes: 23 additions & 2 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class TestCase extends BaseTestCase
*/
protected $api;

protected function setUp()
protected function setUp(): void
{
parent::setUp();
$file = __DIR__ . '/../config.test.php';
Expand All @@ -36,13 +36,14 @@ protected function setUp()
'password' => getenv("DATAZOO_PASSWORD")
];
}

$this->datazooConfig = $config;

$this->api = new Datazoo($config);
}


protected function fakeCallFor(string $call, array $headers = [], $code = 200): Datazoo
protected function fakeCallFor(string $call, array $headers = [], $code = 200)
{

$authResponse = file_get_contents(__DIR__ . '/Responses/auth.json');
Expand All @@ -58,4 +59,24 @@ protected function fakeCallFor(string $call, array $headers = [], $code = 200):

return new Datazoo($this->datazooConfig, $client);
}

protected function fakeCallForAuth($code = 200)
{

$authResponse = file_get_contents(__DIR__ . '/Responses/auth.json');

$mock = new MockHandler([
new Response(200, [], $authResponse),
]);

$handlerStack = HandlerStack::create($mock);
$client = new Client(['handler' => $handlerStack]);

return new Datazoo($this->datazooConfig, $client);
}

protected function hasCredentials()
{
return isset($this->datazooConfig['username']) && ! blank($this->datazooConfig['username']);
}
}
33 changes: 7 additions & 26 deletions tests/unit/DatazooTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,13 @@ class DatazooTest extends TestCase
{
public function testAuth()
{
$this->assertTrue(! empty($this->datazooConfig['username']));
$this->assertTrue(! empty($this->datazooConfig['password']));

$this->assertTrue($this->api->auth());
if ($this->hasCredentials()) {
$this->assertTrue(! empty($this->datazooConfig['username']));
$this->assertTrue(! empty($this->datazooConfig['password']));
$this->assertTrue($this->api->auth());
} else {
$this->assertTrue($this->fakeCallForAuth()->auth());
}
}

// public function testCallDiaBirthNZValidationException()
// {
// $call = new NewZealand([
// 'firstName' => 'John',
// 'lastName' => 'doe',
// 'dateOfBirth' => '1978-07-07'
// ], ['DiaBirth']);

// $this->api->performRequest($call);
// }

// public function testCallDiaBirthNZValidationPasses()
// {
// $call = new NewZealand([
// 'clientReference' => '123456798',
// 'firstName' => 'John',
// 'lastName' => 'doe',
// 'dateOfBirth' => '1978-07-07'
// ], ['DiaBirth']);

// $res = $this->api->performRequest($call);
// }
}
19 changes: 12 additions & 7 deletions tests/unit/PepSanctionScreeningTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,23 @@ public function test_call_service()
{
$call = new PepSanctionScreening([
'clientReference' => '123456798',
'firstName' => 'Craig',
'middleName' => 'Glenham',
'firstName' => 'John',
'middleName' => 'Graham',
'lastName' => 'Smith',
'dateOfBirth' => '1978-07-12',
'dateOfBirth' => '1977-07-12',
'gender' => 'Male',
], ['GlobalWatchlist']);

//$res = $this->api->performRequest($call)->getGlobalWatchlistResponse();
$res = $this->fakeCallFor('PepSanctionScreening')->performRequest($call)->getGlobalWatchlistResponse();
if ($this->hasCredentials()) {
$res = $this->api->performRequest($call)->getGlobalWatchlistResponse();

$json = json_encode($res);
$this->assertEquals("Successful", $res->sourceStatus);
} else {
$res = $this->fakeCallFor('PepSanctionScreening')->performRequest($call)->getGlobalWatchlistResponse();

$this->assertJsonStringEqualsJsonString(file_get_contents(__DIR__ . '/../Responses/PepSanctionScreeningParsed.json'), $json);
$json = json_encode($res);

$this->assertJsonStringEqualsJsonString(file_get_contents(__DIR__ . '/../Responses/PepSanctionScreeningParsed.json'), $json);
}
}
}

0 comments on commit 21a9703

Please sign in to comment.