diff --git a/lib/Util/Util.php b/lib/Util/Util.php index 9ebc413ca..cc7a8a48f 100644 --- a/lib/Util/Util.php +++ b/lib/Util/Util.php @@ -243,6 +243,10 @@ 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]; + } $params = $id; $id = $params['id']; unset($params['id']); diff --git a/tests/Stripe/StripeObjectTest.php b/tests/Stripe/StripeObjectTest.php index c8e0903dd..a50e581bf 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,33 @@ 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 */