From 1ec33bc2c342c87a21d90cfaa716ee5427eae5ef Mon Sep 17 00:00:00 2001 From: "daan.rijpkema" Date: Sat, 30 Dec 2023 14:51:14 +0100 Subject: [PATCH] improved syntax Improved tests Improved tests --- composer.json | 2 +- src/Contexts/IdentityContext.php | 4 -- src/Extensions/IPAPI.php | 7 +-- src/Helpers/BIC.php | 2 +- src/Helpers/BluemConfiguration.php | 9 +-- src/Helpers/BluemCurrency.php | 1 - src/Helpers/BluemIdentityCategoryList.php | 1 - src/Helpers/BluemMaxAmount.php | 6 +- src/Helpers/Now.php | 31 +++++----- src/Webhook.php | 26 ++++----- tests/Unit/BluemTest.php | 71 ++++++++++++++++++----- 11 files changed, 85 insertions(+), 75 deletions(-) diff --git a/composer.json b/composer.json index 1d3cdf3..84c9747 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,7 @@ "issues": "https://github.com/bluem-development/bluem-php" }, "require": { - "php": ">=8.0", + "php": "^8.0|^8.1|^8.2|^8.3", "ext-dom": "*", "ext-libxml": "*", "ext-simplexml": "*", diff --git a/src/Contexts/IdentityContext.php b/src/Contexts/IdentityContext.php index 06209e1..74dfa06 100644 --- a/src/Contexts/IdentityContext.php +++ b/src/Contexts/IdentityContext.php @@ -65,10 +65,6 @@ public function addPaymentMethodDetails(array $details = []): void private function validateDetails(array $details = []): array { - if ($this->isIDIN()) { - // no validation yet - } - return []; } diff --git a/src/Extensions/IPAPI.php b/src/Extensions/IPAPI.php index c5eb7af..c08ae76 100644 --- a/src/Extensions/IPAPI.php +++ b/src/Extensions/IPAPI.php @@ -21,11 +21,6 @@ */ class IPAPI { - /** - * @var bool - */ - private bool $debug = false; - /** * @var string */ @@ -69,7 +64,7 @@ public function QueryIP(string $ip = ""): mixed $ip = $this->GetCurrentIP(); } - $call_url = "{$this->baseURL}{$ip}?access_key=$this->accessKey"; + $call_url = sprintf("%s%s?access_key=%s", $this->baseURL, $ip, $this->accessKey); // Initialize CURL: $ch = curl_init( diff --git a/src/Helpers/BIC.php b/src/Helpers/BIC.php index c5ea997..21d9d8d 100644 --- a/src/Helpers/BIC.php +++ b/src/Helpers/BIC.php @@ -8,7 +8,7 @@ namespace Bluem\BluemPHP\Helpers; -class BIC +final class BIC { public function __construct( public string $issuerID, diff --git a/src/Helpers/BluemConfiguration.php b/src/Helpers/BluemConfiguration.php index 3b85e13..6aca9d8 100644 --- a/src/Helpers/BluemConfiguration.php +++ b/src/Helpers/BluemConfiguration.php @@ -37,12 +37,7 @@ class BluemConfiguration * this is given by the bank and never changed (default 0) */ public string $merchantSubID; - private string $PaymentsBrandID; - // @todo: consider deprecating this? - private string $EmandateBrandID; - // @todo: consider deprecating this? - // additional helper flags /** * Allows for testing webhook on local environments with no HTTPS check and verbose output. */ @@ -80,9 +75,7 @@ public function __construct(object|array $raw) $this->test_accessToken = $validated->test_accessToken ?? null; $this->IDINBrandID = $this->_assumeBrandID("Identity", $this->brandID); - $this->PaymentsBrandID = $this->_assumeBrandID("Payment", $this->brandID); - $this->EmandateBrandID = $this->_assumeBrandID("Mandate", $this->brandID); - + $this->sequenceType = $validated->sequenceType ?? null; $this->merchantID = $validated->merchantID ?? null; diff --git a/src/Helpers/BluemCurrency.php b/src/Helpers/BluemCurrency.php index 0c1228b..5aa8d6c 100644 --- a/src/Helpers/BluemCurrency.php +++ b/src/Helpers/BluemCurrency.php @@ -14,7 +14,6 @@ class BluemCurrency implements Stringable { - private const EURO_CURRENCY = 'EUR'; private const US_DOLLAR_CURRENCY = 'USD'; diff --git a/src/Helpers/BluemIdentityCategoryList.php b/src/Helpers/BluemIdentityCategoryList.php index 5de1dc1..0664ad1 100644 --- a/src/Helpers/BluemIdentityCategoryList.php +++ b/src/Helpers/BluemIdentityCategoryList.php @@ -11,7 +11,6 @@ class BluemIdentityCategoryList { - /** * @var string[] $categories */ diff --git a/src/Helpers/BluemMaxAmount.php b/src/Helpers/BluemMaxAmount.php index b334551..3b9de32 100644 --- a/src/Helpers/BluemMaxAmount.php +++ b/src/Helpers/BluemMaxAmount.php @@ -11,19 +11,15 @@ class BluemMaxAmount implements \Stringable { - public BluemCurrency $currency; - public float $amount; public function __construct( - float $amount, + private float $amount, string $currencyCode ) { // @todo: validate the amount to be non-negative and have a maximum, see xsd - $this->amount = $amount; try { - $this->currency = new BluemCurrency($currencyCode); } catch (\Exception $e) { $this->currency = new BluemCurrency(); diff --git a/src/Helpers/Now.php b/src/Helpers/Now.php index 684bf07..5f95c01 100644 --- a/src/Helpers/Now.php +++ b/src/Helpers/Now.php @@ -18,13 +18,13 @@ public function __construct($timezoneString = self::DEFAULT_TIMEZONE) { try { $timezone = new DateTimeZone($timezoneString); - } catch (\Exception $e) { + } catch (Exception) { $timezone = new DateTimeZone(self::DEFAULT_TIMEZONE); } try { $this->dateTime = new DateTimeImmutable(datetime: "now", timezone: $timezone); - } catch (Exception $e) { + } catch (Exception) { $this->dateTime = new DateTimeImmutable("now", self::DEFAULT_TIMEZONE); } } @@ -55,28 +55,25 @@ public function addDay(int $days): static return $this; } + /** + * @throws Exception + */ public function fromDate(string $dateTimeString): static { - try { - $this->dateTime = new DateTimeImmutable( - datetime: $dateTimeString, - timezone: new DateTimeZone(self::DEFAULT_TIMEZONE) - ); - } catch (Exception $e) { - throw $e; - } + $this->dateTime = new DateTimeImmutable( + datetime: $dateTimeString, + timezone: new DateTimeZone(self::DEFAULT_TIMEZONE) + ); + return $this; } public function fromTimestamp(string $timestamp): static { - try { - $this->dateTime = (new DateTimeImmutable()) - ->setTimestamp($timestamp) - ->setTimezone(new DateTimeZone(self::DEFAULT_TIMEZONE)); - } catch (Exception $e) { - throw $e; - } + $this->dateTime = (new DateTimeImmutable()) + ->setTimestamp($timestamp) + ->setTimezone(new DateTimeZone(self::DEFAULT_TIMEZONE)); + return $this; } } diff --git a/src/Webhook.php b/src/Webhook.php index 94a4728..a22aac1 100644 --- a/src/Webhook.php +++ b/src/Webhook.php @@ -44,18 +44,15 @@ private function parse($xmlData = ''): void { if (!$this->isHttpsRequest()) { $this->exitWithError('Not HTTPS'); - return; } if ($_SERVER['REQUEST_METHOD'] !== 'POST') { $this->exitWithError('Not POST'); - return; } // Check: content type: XML with utf-8 encoding if ($_SERVER["CONTENT_TYPE"] !== self::XML_UTF8_CONTENT_TYPE) { $this->exitWithError('Wrong Content-Type given: should be XML with UTF-8 encoding'); - return; } // Check: An empty POST to the URL (normal HTTP request) always has to respond with HTTP 200 OK. @@ -63,27 +60,23 @@ private function parse($xmlData = ''): void if (empty($xmlData)) { $this->exitWithError('No data body given'); - return; } } $xmlObject = $this->parseRawXML($xmlData); - if (! $xmlObject instanceof \SimpleXMLElement) { + if (! $xmlObject instanceof SimpleXMLElement) { $this->exitWithError('Could not parse XML'); - return; } $xmlValidation = (new WebhookXmlValidation($this->senderID))->validate($xmlObject); if (! $xmlValidation::$isValid) { $this->exitWithError($xmlValidation->errorMessage()); - return; } $signatureValidation = (new WebhookSignatureValidation($this->environment))->validate($xmlData); if (! $signatureValidation::$isValid) { $this->exitWithError($signatureValidation->errorMessage()); - return; } $this->xmlObject = $xmlObject; @@ -98,10 +91,11 @@ private function isHttpsRequest(): bool || $_SERVER['SERVER_PORT'] === 443 ); } - - private function exitWithError(string $string): void + + private function exitWithError(string $errorMessage): void { http_response_code(self::STATUSCODE_BAD_REQUEST); + exit($errorMessage); } private function parseRawXML($postData): string|SimpleXMLElement @@ -144,7 +138,7 @@ private function getPayloadValue(string $key): SimpleXMLElement|string|null if ((is_countable($payload->children()) ? count($payload->children()) : 0) > 0) { return $payload; } - return $payload->$key ?? ''; + return $payload->$key ? ($payload->$key . '') : ''; } private function getPayload(): SimpleXMLElement @@ -261,7 +255,7 @@ public function getIDealDetails(): ?SimpleXMLElement { $paymentDetails = $this->getPaymentMethodDetails(); - if (!$paymentDetails instanceof \SimpleXMLElement) { + if (!$paymentDetails instanceof SimpleXMLElement) { return null; } return $paymentDetails->IDealDetails; @@ -269,7 +263,7 @@ public function getIDealDetails(): ?SimpleXMLElement public function getDebtorAccountName(): ?string { $details = $this->getIDealDetails(); - if (!$details instanceof \SimpleXMLElement) { + if (!$details instanceof SimpleXMLElement) { return ""; } return $details->DebtorAccountName."" ?? ""; @@ -277,7 +271,7 @@ public function getDebtorAccountName(): ?string public function getDebtorIBAN(): ?string { $details = $this->getIDealDetails(); - if (!$details instanceof \SimpleXMLElement) { + if (!$details instanceof SimpleXMLElement) { return ""; } return $details->DebtorIBAN."" ?? ""; @@ -285,7 +279,7 @@ public function getDebtorIBAN(): ?string public function getDebtorBankID(): ?string { $details = $this->getIDealDetails(); - if (!$details instanceof \SimpleXMLElement) { + if (!$details instanceof SimpleXMLElement) { return ""; } return $details->DebtorBankID."" ?? ""; @@ -358,7 +352,7 @@ public function getIdentityReportArray(): array { $report = $this->getPayload()->IdentityReport ?? null; - if (!$report instanceof \SimpleXMLElement) { + if (!$report instanceof SimpleXMLElement) { return []; } diff --git a/tests/Unit/BluemTest.php b/tests/Unit/BluemTest.php index 8fd67c0..88192a8 100644 --- a/tests/Unit/BluemTest.php +++ b/tests/Unit/BluemTest.php @@ -9,41 +9,82 @@ namespace Unit; use Bluem\BluemPHP\Bluem; +use Bluem\BluemPHP\Contexts\IdentityContext; use Bluem\BluemPHP\Exceptions\InvalidBluemConfigurationException; -use Exception; +use Bluem\BluemPHP\Interfaces\BluemResponseInterface; +use Bluem\BluemPHP\Requests\BluemRequest; +use Bluem\BluemPHP\Responses\ErrorBluemResponse; use PHPUnit\Framework\TestCase; +use RuntimeException; use stdClass; class BluemTest extends TestCase { private Bluem $bluem; + /** + * @throws InvalidBluemConfigurationException + */ protected function setUp(): void { - parent::setUp(); + // Mock the configuration as needed + $mockedConfig = $this->getConfig(); + $this->bluem = new Bluem($mockedConfig); + } + - $bluem_config = $this->getConfig(); - try { - $this->bluem = new Bluem( - $bluem_config - ); - } catch (Exception $e) { - $this->fail($e->getMessage()); - } + public function testConstructorWithValidConfig(): void + { + $this->assertInstanceOf(Bluem::class, $this->bluem); + } + + public function testConstructorWithInvalidConfig(): void + { + $this->expectException(InvalidBluemConfigurationException::class); + new Bluem(null); } - protected function tearDown(): void + + public function testMandateWithValidParameters(): void { - //$this->bluem = Bluem; + // Mock the expected response + $mockedResponse = $this->createMock(BluemResponseInterface::class); + + // Test the Mandate method with valid parameters + $response = $this->bluem->Mandate('customer_id', 'order_id', 'mandate_id'); + + // Assertions + $this->assertInstanceOf(BluemResponseInterface::class, $response); } - public function testMandateRequest() + public function testMandateWithException(): void + { + $this->expectException(RuntimeException::class); + $this->bluem->Mandate('', '', ''); + } + public function testCreateMandateID(): void + { + $mandateID = $this->bluem->CreateMandateID('order_id', 'customer_id'); + $this->assertIsString($mandateID); + } + public function testPerformRequestWithInvalidXml(): void { - $result = true; - $this->assertEquals(true, $result); + // Mock a request that would generate invalid XML + $mockBluemRequest = $this->createMock(BluemRequest::class); + + $mockBluemRequest->method('XmlString') + ->willReturn('Some invalid aaXML String'); + + $mockBluemRequest->method('HttpRequestURL') + ->willReturn('https://example.com/api/request'); + $mockBluemRequest->method('RequestContext')->willReturn(new IdentityContext()); + + $result = $this->bluem->PerformRequest($mockBluemRequest); + $this->assertInstanceOf(ErrorBluemResponse::class, $result); } + // helper classes private function getConfig(): stdClass { $bluem_config = new stdClass;