diff --git a/lib/Service/AbstractService.php b/lib/Service/AbstractService.php index abcc13ae2..2cc759c07 100644 --- a/lib/Service/AbstractService.php +++ b/lib/Service/AbstractService.php @@ -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) diff --git a/tests/Stripe/Service/AbstractServiceTest.php b/tests/Stripe/Service/AbstractServiceTest.php index deee3e386..cf79e063d 100644 --- a/tests/Stripe/Service/AbstractServiceTest.php +++ b/tests/Stripe/Service/AbstractServiceTest.php @@ -16,6 +16,9 @@ final class AbstractServiceTest extends \PHPUnit\Framework\TestCase /** @var CouponService */ private $service; + /** @var \ReflectionMethod */ + private $formatParamsReflector; + /** * @before */ @@ -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); @@ -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']); + } }