Skip to content

Commit

Permalink
Merge pull request #933 from stripe/richardm-recursive-formatparams
Browse files Browse the repository at this point in the history
Services bugfix: handle null properly on arrays
  • Loading branch information
richardm-stripe authored May 15, 2020
2 parents a75a888 + 29ef98b commit 7f582fd
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
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']);
}
}

0 comments on commit 7f582fd

Please sign in to comment.