From 21a97039ca4b515165dc9dda43c404d4f64a36ae Mon Sep 17 00:00:00 2001 From: Craig Smith Date: Thu, 7 Jul 2022 10:56:40 +1200 Subject: [PATCH] fix: php 7,1 --- composer.json | 14 +++---- src/Datazoo.php | 26 +++++++++++-- src/Model/GlobalWatchlist.php | 22 ++++++++--- src/Model/ModelAbstract.php | 52 ++++++++++++++++++++----- tests/TestCase.php | 25 +++++++++++- tests/unit/DatazooTest.php | 33 ++++------------ tests/unit/PepSanctionScreeningTest.php | 19 +++++---- 7 files changed, 130 insertions(+), 61 deletions(-) diff --git a/composer.json b/composer.json index 1595667..d0278b4 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/src/Datazoo.php b/src/Datazoo.php index 39cf63a..9636d5a 100644 --- a/src/Datazoo.php +++ b/src/Datazoo.php @@ -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) { @@ -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; } } diff --git a/src/Model/GlobalWatchlist.php b/src/Model/GlobalWatchlist.php index 50165f5..34a66d0 100644 --- a/src/Model/GlobalWatchlist.php +++ b/src/Model/GlobalWatchlist.php @@ -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', @@ -26,5 +38,5 @@ class GlobalWatchlist /** * @param class-string $responseObject */ - public string $responseObject = '\CustomD\Datazoo\Response\GlobalWatchlistResponse'; + public $responseObject = '\CustomD\Datazoo\Response\GlobalWatchlistResponse'; } diff --git a/src/Model/ModelAbstract.php b/src/Model/ModelAbstract.php index 7c1424c..249a626 100644 --- a/src/Model/ModelAbstract.php +++ b/src/Model/ModelAbstract.php @@ -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; @@ -42,7 +45,10 @@ 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]); @@ -50,7 +56,10 @@ public function removeService(string $service): static return $this; } - public function validateData(): void + /** + * @return void + */ + public function validateData() { $required = [ 'countryCode' => ['required','string'], @@ -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); @@ -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))); } diff --git a/tests/TestCase.php b/tests/TestCase.php index 76fc6a9..2685f57 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -23,7 +23,7 @@ class TestCase extends BaseTestCase */ protected $api; - protected function setUp() + protected function setUp(): void { parent::setUp(); $file = __DIR__ . '/../config.test.php'; @@ -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'); @@ -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']); + } } diff --git a/tests/unit/DatazooTest.php b/tests/unit/DatazooTest.php index 07fd6b6..8859d36 100644 --- a/tests/unit/DatazooTest.php +++ b/tests/unit/DatazooTest.php @@ -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); - // } } diff --git a/tests/unit/PepSanctionScreeningTest.php b/tests/unit/PepSanctionScreeningTest.php index 5e822d8..8956b63 100644 --- a/tests/unit/PepSanctionScreeningTest.php +++ b/tests/unit/PepSanctionScreeningTest.php @@ -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); + } } }