From 305982a5f2e103906330c0f21826a06679d2e24c Mon Sep 17 00:00:00 2001 From: David Owusu <33735090+Ryouzanpaku@users.noreply.github.com> Date: Fri, 6 Nov 2026 15:24:17 +0100 Subject: [PATCH 1/2] [CC-4256] Parse Keypair booleans sent as strings validateBasket (and cof) arrive from the API as strings ("true"/"false"). The non-strict setters coerced any non-empty string to true, so "false" became true. Normalise both flags in handleResponse() via a new normalizeBoolean() helper (filter_var FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE), accepting native and string booleans; a missing field stays null. expose() keeps native booleans. Co-Authored-By: Claude Opus 4.8 (1M context) --- src/Resources/Keypair.php | 22 ++++++++++++ test/unit/Resources/KeypairTest.php | 53 +++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) diff --git a/src/Resources/Keypair.php b/src/Resources/Keypair.php index bf4c6bd0..77f2681e 100755 --- a/src/Resources/Keypair.php +++ b/src/Resources/Keypair.php @@ -290,6 +290,28 @@ public function handleResponse(stdClass $response, string $method = HttpAdapterI foreach ($paymentTypes as $paymentType) { $this->paymentTypes[] = $paymentType; } + + // Booleans may arrive as native values or as strings ("true"/"false"); normalise both. + if (isset($response->validateBasket)) { + $this->validateBasket = $this->normalizeBoolean($response->validateBasket); + } + if (isset($response->cof)) { + $this->cof = $this->normalizeBoolean($response->cof); + } + } + + /** + * Converts a value the API may send as a native boolean or as a boolean-like + * string ("true"/"false") into a real bool, returning null if it is neither. + * Prevents the non-strict setters from coercing the string "false" to true. + * + * @param mixed $value + * + * @return bool|null + */ + private function normalizeBoolean($value): ?bool + { + return filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE); } /** diff --git a/test/unit/Resources/KeypairTest.php b/test/unit/Resources/KeypairTest.php index 8801f995..dd00b934 100755 --- a/test/unit/Resources/KeypairTest.php +++ b/test/unit/Resources/KeypairTest.php @@ -205,4 +205,57 @@ public function exposeContainsAllRelevantKeyDataExceptPrivateKeyAndDetailed(): v $this->assertArrayNotHasKey('privateKey', $exposed); $this->assertArrayNotHasKey('detailed', $exposed); } + + /** + * Verify boolean flags are parsed correctly whether the API sends them as native + * booleans or as strings, and that expose() keeps them native booleans. + * + * @test + * + * @dataProvider wireBooleanDP + * + * @param mixed $wireValue + * @param bool $expected + */ + public function booleanFlagsAreParsedRegardlessOfWireType($wireValue, bool $expected): void + { + $keypair = new Keypair(); + $keypair->handleResponse((object)[ + 'publicKey' => 's-pub-stub', + 'validateBasket' => $wireValue, + 'cof' => $wireValue, + ]); + + $this->assertSame($expected, $keypair->isValidateBasket()); + $this->assertSame($expected, $keypair->isCof()); + $this->assertSame($expected, $keypair->expose()['validateBasket']); + $this->assertSame($expected, $keypair->expose()['cof']); + } + + /** + * @return array + */ + public function wireBooleanDP(): array + { + return [ + 'string "true"' => ['true', true], + 'string "false"' => ['false', false], + 'native true' => [true, true], + 'native false' => [false, false], + ]; + } + + /** + * Verify a missing boolean flag leaves the value null. + * + * @test + */ + public function missingBooleanFlagsStayNull(): void + { + $keypair = new Keypair(); + $keypair->handleResponse((object)['publicKey' => 's-pub-stub']); + + $this->assertNull($keypair->isValidateBasket()); + $this->assertNull($keypair->isCof()); + } } From 29526f4656d8d6e5defc3ed887b1818c1673a94c Mon Sep 17 00:00:00 2001 From: David Owusu <33735090+Ryouzanpaku@users.noreply.github.com> Date: Tue, 17 Nov 2026 17:28:43 +0100 Subject: [PATCH 2/2] [CC-4256] Address PR review: null-safe cof, empty-string handling, tests - setCof now accepts ?bool so a null/empty cof no longer throws a TypeError in parent::handleResponse (property docblock already declared bool|null). - normalizeBoolean treats null and empty string as null, so an empty validateBasket/cof stays null instead of being coerced to false, matching AbstractUnzerResource's empty-string-to-null behaviour. - Extend KeypairTest with "1"/"0" wire values and a parameterized test for empty string, invalid string, JSON null and missing flags (stay null, not exposed). - Add ResourceServiceTest coverage for fetchKeypair parsing string booleans, the raw-response reproduction path from the ticket. Co-Authored-By: Claude Opus 4.8 (1M context) --- src/Resources/Keypair.php | 10 ++++- test/unit/Resources/KeypairTest.php | 29 ++++++++++++-- test/unit/Services/ResourceServiceTest.php | 44 ++++++++++++++++++++++ 3 files changed, 78 insertions(+), 5 deletions(-) diff --git a/src/Resources/Keypair.php b/src/Resources/Keypair.php index 77f2681e..4f2956f6 100755 --- a/src/Resources/Keypair.php +++ b/src/Resources/Keypair.php @@ -221,11 +221,11 @@ public function isCof(): ?bool } /** - * @param bool $cof + * @param bool|null $cof * * @return Keypair */ - protected function setCof(bool $cof): Keypair + protected function setCof(?bool $cof): Keypair { $this->cof = $cof; return $this; @@ -304,6 +304,8 @@ public function handleResponse(stdClass $response, string $method = HttpAdapterI * Converts a value the API may send as a native boolean or as a boolean-like * string ("true"/"false") into a real bool, returning null if it is neither. * Prevents the non-strict setters from coercing the string "false" to true. + * An empty string is treated as null, matching the "missing value" behaviour + * of AbstractUnzerResource. * * @param mixed $value * @@ -311,6 +313,10 @@ public function handleResponse(stdClass $response, string $method = HttpAdapterI */ private function normalizeBoolean($value): ?bool { + if ($value === null || $value === '') { + return null; + } + return filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE); } diff --git a/test/unit/Resources/KeypairTest.php b/test/unit/Resources/KeypairTest.php index dd00b934..49fa711c 100755 --- a/test/unit/Resources/KeypairTest.php +++ b/test/unit/Resources/KeypairTest.php @@ -240,22 +240,45 @@ public function wireBooleanDP(): array return [ 'string "true"' => ['true', true], 'string "false"' => ['false', false], + 'string "1"' => ['1', true], + 'string "0"' => ['0', false], 'native true' => [true, true], 'native false' => [false, false], ]; } /** - * Verify a missing boolean flag leaves the value null. + * Verify boolean flags stay null and are not exposed when the API sends an empty + * string, an unrecognised string or JSON null, or omits the field entirely. Also + * guards against the setters throwing a TypeError for these values. * * @test + * + * @dataProvider nonBooleanWireValueDP + * + * @param array $flags */ - public function missingBooleanFlagsStayNull(): void + public function nonBooleanFlagsStayNull(array $flags): void { $keypair = new Keypair(); - $keypair->handleResponse((object)['publicKey' => 's-pub-stub']); + $keypair->handleResponse((object)array_merge(['publicKey' => 's-pub-stub'], $flags)); $this->assertNull($keypair->isValidateBasket()); $this->assertNull($keypair->isCof()); + $this->assertArrayNotHasKey('validateBasket', $keypair->expose()); + $this->assertArrayNotHasKey('cof', $keypair->expose()); + } + + /** + * @return array + */ + public function nonBooleanWireValueDP(): array + { + return [ + 'empty string' => [['validateBasket' => '', 'cof' => '']], + 'invalid string' => [['validateBasket' => 'abc', 'cof' => 'abc']], + 'json null' => [['validateBasket' => null, 'cof' => null]], + 'missing' => [[]], + ]; } } diff --git a/test/unit/Services/ResourceServiceTest.php b/test/unit/Services/ResourceServiceTest.php index fcc910b6..09525cf9 100755 --- a/test/unit/Services/ResourceServiceTest.php +++ b/test/unit/Services/ResourceServiceTest.php @@ -158,6 +158,50 @@ public function fetchKeypairShouldKeepRawResponseDP(): array ]; } + /** + * Verify fetchKeypair parses boolean flags sent as strings, which is the raw + * response path reported in CC-4256 (string "false" must not become true). + * + * @test + * + * @dataProvider fetchKeypairParsesStringBooleanDP + * + * @param string $rawResponse + * @param bool|null $expectedValidateBasket + * @param bool|null $expectedCof + */ + public function fetchKeypairParsesBooleanFlagsSentAsStrings( + string $rawResponse, + ?bool $expectedValidateBasket, + ?bool $expectedCof + ): void { + $unzer = new Unzer('s-priv-1234'); + $httpSrvMock = $this->getMockBuilder(HttpService::class)->setMethods(['send'])->getMock(); + $unzer->setHttpService($httpSrvMock); + $httpSrvMock->expects($this->once())->method('send') + ->with('/keypair', $this->isInstanceOf(Keypair::class), HttpAdapterInterface::REQUEST_GET) + ->willReturn($rawResponse); + + $keypair = $unzer->fetchKeypair(); + + $this->assertSame($expectedValidateBasket, $keypair->isValidateBasket()); + $this->assertSame($expectedCof, $keypair->isCof()); + $this->assertSame($rawResponse, $keypair->getRawResponse()); + } + + /** + * @return array + */ + public function fetchKeypairParsesStringBooleanDP(): array + { + return [ + 'string "false"' => ['{"publicKey":"s-pub-stub","validateBasket":"false","cof":"false"}', false, false], + 'string "true"' => ['{"publicKey":"s-pub-stub","validateBasket":"true","cof":"true"}', true, true], + 'empty string stays null' => ['{"publicKey":"s-pub-stub","validateBasket":"","cof":""}', null, null], + 'missing stays null' => ['{"publicKey":"s-pub-stub"}', null, null], + ]; + } + /** * Verify send will call send on httpService. *