Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions src/Resources/Keypair.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -290,6 +290,34 @@ 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.
* An empty string is treated as null, matching the "missing value" behaviour
* of AbstractUnzerResource.
*
* @param mixed $value
*
* @return bool|null
*/
private function normalizeBoolean($value): ?bool
{
if ($value === null || $value === '') {
return null;
}

return filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
}

/**
Expand Down
76 changes: 76 additions & 0 deletions test/unit/Resources/KeypairTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,4 +205,80 @@ 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],
'string "1"' => ['1', true],
'string "0"' => ['0', false],
'native true' => [true, true],
'native false' => [false, false],
];
}

/**
* 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 nonBooleanFlagsStayNull(array $flags): void
{
$keypair = new Keypair();
$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' => [[]],
];
}
}
44 changes: 44 additions & 0 deletions test/unit/Services/ResourceServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
Loading