Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Services bugfix: convert nested null params to empty strings #933

Merged
merged 7 commits into from
May 15, 2020
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
15 changes: 6 additions & 9 deletions lib/Service/AbstractService.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,15 @@ public function getClient()
private static function formatParams($params)
{
if (null === $params) {
return $params;
return null;
}
$formatted = [];
foreach ($params as $k => $v) {
if (null === $v) {
$formatted[$k] = '';
} else {
$formatted[$k] = $v;
\array_walk_recursive($params, function (&$value, $key) {
if (null === $value) {
$value = '';
}
}
});

return $formatted;
return $params;
}

protected function request($method, $path, $params, $opts)
Expand Down
34 changes: 34 additions & 0 deletions tests/Stripe/Service/AbstractServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ final class AbstractServiceTest extends \PHPUnit\Framework\TestCase
/** @var CouponService */
private $service;

/** @var \ReflectionMethod */
private $formatParamsReflector;

/**
* @before
*/
Expand All @@ -26,6 +29,15 @@ public function setUpMockService()
$this->service = new \Stripe\Service\CouponService($this->client);
}

/**
* @before
*/
public function setUpReflectors()
{
$this->formatParamsReflector = new \ReflectionMethod(\Stripe\Service\AbstractService::class, 'formatParams');
$this->formatParamsReflector->setAccessible(true);
}

public function testNullGetsEmptyStringified()
{
$this->expectException(\Stripe\Exception\InvalidRequestException::class);
Expand Down Expand Up @@ -57,4 +69,26 @@ public function testRetrieveThrowsIfIdNullIsWhitespace()

$this->service->retrieve(' ');
}

public function testFormatParams()
{
$result = $this->formatParamsReflector->invoke(null, ['foo' => null]);
static::assertTrue('' === $result['foo']);
static::assertTrue(null !== $result['foo']);

$result = $this->formatParamsReflector->invoke(null, ['foo' => ['bar' => null, 'baz' => 1, 'nest' => ['triplynestednull' => null, 'triplynestednonnull' => 1]]]);
static::assertTrue('' === $result['foo']['bar']);
static::assertTrue(null !== $result['foo']['bar']);
static::assertTrue(1 === $result['foo']['baz']);
static::assertTrue('' === $result['foo']['nest']['triplynestednull']);
static::assertTrue(1 === $result['foo']['nest']['triplynestednonnull']);

$result = $this->formatParamsReflector->invoke(null, ['foo' => ['zero', null, null, 'three'], 'toplevelnull' => null, 'toplevelnonnull' => 4]);
static::assertTrue('zero' === $result['foo'][0]);
static::assertTrue('' === $result['foo'][1]);
static::assertTrue('' === $result['foo'][2]);
static::assertTrue('three' === $result['foo'][3]);
static::assertTrue('' === $result['toplevelnull']);
static::assertTrue(4 === $result['toplevelnonnull']);
}
}