From c82f98997e3353c680e2fe4abd94988aa3c0a25e Mon Sep 17 00:00:00 2001 From: Richard Marmorstein Date: Mon, 6 Nov 2023 14:46:39 -0800 Subject: [PATCH 1/3] Handle non-string ids better --- lib/Util/Util.php | 3 +++ tests/Stripe/StripeObjectTest.php | 33 +++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/lib/Util/Util.php b/lib/Util/Util.php index 9ebc413ca..85ff8188a 100644 --- a/lib/Util/Util.php +++ b/lib/Util/Util.php @@ -243,6 +243,9 @@ public static function urlEncode($key) public static function normalizeId($id) { if (\is_array($id)) { + if (!isset($id['id'])) { + return [null, $id]; + } $params = $id; $id = $params['id']; unset($params['id']); diff --git a/tests/Stripe/StripeObjectTest.php b/tests/Stripe/StripeObjectTest.php index c8e0903dd..128f121c9 100644 --- a/tests/Stripe/StripeObjectTest.php +++ b/tests/Stripe/StripeObjectTest.php @@ -16,6 +16,9 @@ final class StripeObjectTest extends \Stripe\TestCase /** @var \ReflectionProperty */ private $optsReflector; + /** @var \ReflectionProperty */ + private $retrieveOptionsReflector; + /** * @before */ @@ -31,6 +34,10 @@ public function setUpReflectors() // This is used to access the `_opts` protected variable $this->optsReflector = new \ReflectionProperty(\Stripe\StripeObject::class, '_opts'); $this->optsReflector->setAccessible(true); + + // This is used to access the `_retrieveOptions` protected variable + $this->retrieveOptionsReflector = new \ReflectionProperty(\Stripe\StripeObject::class, '_retrieveOptions'); + $this->retrieveOptionsReflector->setAccessible(true); } public function testArrayAccessorsSemantics() @@ -540,6 +547,32 @@ public function testIsDeleted() static::assertTrue($obj->isDeleted()); } + public function testConstructorIdPassing() + { + $obj = new StripeObject(['id' => 'id_foo', 'other' => 'bar']); + static::assertSame('id_foo', $obj->id); + static::assertSame(['other' => 'bar'], $this->retrieveOptionsReflector->getValue($obj)); + + $obj = new StripeObject('id_foo'); + static::assertSame('id_foo', $obj->id); + static::assertSame([], $this->retrieveOptionsReflector->getValue($obj)); + + $obj = new StripeObject(['id' => 'id_foo']); + static::assertSame('id_foo', $obj->id); + static::assertSame([], $this->retrieveOptionsReflector->getValue($obj)); + + $obj = new StripeObject(['id' => ['foo' => 'bar']]); + static::assertSame(['foo' => 'bar'], $obj->id); + static::assertSame([], $this->retrieveOptionsReflector->getValue($obj)); + } + + public function testConstructFromIdPassing() + { + $obj = StripeObject::constructFrom(['inner' => ['id' => ['foo' => 'bar']]]); + static::assertSame(['foo' => 'bar'], $obj->inner->id->toArray()); + static::assertSame([], $this->retrieveOptionsReflector->getValue($obj)); + } + public function testDeserializeEmptyMetadata() { /** @var mixed $obj */ From 063fef7a362afc6b39bc2dc3730c3f02c62cc379 Mon Sep 17 00:00:00 2001 From: Richard Marmorstein Date: Mon, 6 Nov 2023 15:04:02 -0800 Subject: [PATCH 2/3] Fix test --- tests/Stripe/StripeObjectTest.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/Stripe/StripeObjectTest.php b/tests/Stripe/StripeObjectTest.php index 128f121c9..a50e581bf 100644 --- a/tests/Stripe/StripeObjectTest.php +++ b/tests/Stripe/StripeObjectTest.php @@ -569,7 +569,8 @@ public function testConstructorIdPassing() public function testConstructFromIdPassing() { $obj = StripeObject::constructFrom(['inner' => ['id' => ['foo' => 'bar']]]); - static::assertSame(['foo' => 'bar'], $obj->inner->id->toArray()); + + static::assertSame(['foo' => 'bar'], $obj['inner']->id->toArray()); static::assertSame([], $this->retrieveOptionsReflector->getValue($obj)); } From 97d8649406ad70784cddda67ae0bd9210800c3ce Mon Sep 17 00:00:00 2001 From: Richard Marmorstein <52928443+richardm-stripe@users.noreply.github.com> Date: Mon, 6 Nov 2023 15:15:22 -0800 Subject: [PATCH 3/3] Update lib/Util/Util.php Co-authored-by: pakrym-stripe <99349468+pakrym-stripe@users.noreply.github.com> --- lib/Util/Util.php | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/Util/Util.php b/lib/Util/Util.php index 85ff8188a..cc7a8a48f 100644 --- a/lib/Util/Util.php +++ b/lib/Util/Util.php @@ -243,6 +243,7 @@ public static function urlEncode($key) public static function normalizeId($id) { if (\is_array($id)) { + // see https://github.com/stripe/stripe-php/pull/1602 if (!isset($id['id'])) { return [null, $id]; }