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

Deserialize into correct v2 EventData types #1775

Merged
merged 4 commits into from
Oct 18, 2024
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: 13 additions & 2 deletions lib/Events/V1BillingMeterErrorReportTriggeredEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,26 @@ class V1BillingMeterErrorReportTriggeredEvent extends \Stripe\V2\Event
*/
public function fetchRelatedObject()
{
$apiMode = \Stripe\Util\Util::getApiMode($this->related_object->url);
list($object, $options) = $this->_request(
'get',
$this->related_object->url,
[],
['stripe_account' => $this->context],
[],
'v2'
$apiMode
);

return \Stripe\Util\Util::convertToStripeObject($object, $options, 'v2');
return \Stripe\Util\Util::convertToStripeObject($object, $options, $apiMode);
}

public static function constructFrom($values, $opts = null, $apiMode = 'v2')
{
$evt = parent::constructFrom($values, $opts, $apiMode);
if (null !== $evt->data) {
$evt->data = \Stripe\EventData\V1BillingMeterErrorReportTriggeredEventData::constructFrom($evt->data, $opts, $apiMode);
}

return $evt;
}
}
10 changes: 10 additions & 0 deletions lib/Events/V1BillingMeterNoMeterFoundEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,14 @@
class V1BillingMeterNoMeterFoundEvent extends \Stripe\V2\Event
{
const LOOKUP_TYPE = 'v1.billing.meter.no_meter_found';

public static function constructFrom($values, $opts = null, $apiMode = 'v2')
{
$evt = parent::constructFrom($values, $opts, $apiMode);
if (null !== $evt->data) {
$evt->data = \Stripe\EventData\V1BillingMeterNoMeterFoundEventData::constructFrom($evt->data, $opts, $apiMode);
}

return $evt;
}
}
4 changes: 0 additions & 4 deletions lib/Util/ObjectTypes.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,6 @@ class ObjectTypes
* @var array Mapping from v2 object types to resource classes
*/
const v2Mapping = [
// V1 Class needed for fetching the right related object
// TODO: https://go/j/DEVSDK-2204 Make a more standardized fix in codegen for all languages
\Stripe\Billing\Meter::OBJECT_NAME => \Stripe\Billing\Meter::class,

// v2 object classes: The beginning of the section generated from our OpenAPI spec
\Stripe\V2\Billing\MeterEvent::OBJECT_NAME => \Stripe\V2\Billing\MeterEvent::class,
\Stripe\V2\Billing\MeterEventAdjustment::OBJECT_NAME => \Stripe\V2\Billing\MeterEventAdjustment::class,
Expand Down
72 changes: 72 additions & 0 deletions tests/Stripe/EventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
/**
* @internal
* @covers \Stripe\Event
* @covers \Stripe\V2\Event
*/
final class EventTest extends \Stripe\TestCase
{
Expand Down Expand Up @@ -32,4 +33,75 @@ public function testIsRetrievable()
$resource = Event::retrieve(self::TEST_RESOURCE_ID);
static::assertInstanceOf(\Stripe\Event::class, $resource);
}

public function testV2EventDataDeserializesIntoType()
{
$jsonEvent = [
'id' => 'evt_123',
'object' => 'v2.core.event',
'type' => 'v1.billing.meter.error_report_triggered',
'created' => '2022-02-15T00:27:45.330Z',
'related_object' => [
'id' => 'mtr_123',
'type' => 'billing.meter',
'url' => '/v1/billing/meters/mtr_123',
],
'data' => [
'reason' => [
'error_count' => 1,
],
],
];

$this->stubRequest(
'GET',
'/v2/core/events/evt_123',
[],
null,
false,
$jsonEvent,
200,
BaseStripeClient::DEFAULT_API_BASE
);
$client = new StripeClient(['api_key' => 'sk_test_client']);

$event = $client->v2->core->events->retrieve('evt_123');

static::assertInstanceOf(\Stripe\Events\V1BillingMeterErrorReportTriggeredEvent::class, $event);
static::assertInstanceOf(\Stripe\EventData\V1BillingMeterErrorReportTriggeredEventData::class, $event->data);
}

public function testV2EventFetchRelatedObject()
{
$jsonEvent = [
'id' => 'evt_123',
'object' => 'v2.core.event',
'type' => 'v1.billing.meter.error_report_triggered',
'created' => '2022-02-15T00:27:45.330Z',
'context' => 'acct_123',
'related_object' => [
'id' => 'mtr_123',
'type' => 'billing.meter',
'url' => '/v1/billing/meters/mtr_123',
],
'data' => [],
];

// right now PHP only supports one request per unit test
$fullEvent = \Stripe\Util\Util::convertToStripeObject($jsonEvent, [], 'v2');
static::assertInstanceOf(\Stripe\Events\V1BillingMeterErrorReportTriggeredEvent::class, $fullEvent);
$this->stubRequest(
'GET',
'/v1/billing/meters/mtr_123',
[],
null,
false,
['object' => 'billing.meter', 'id' => 'mtr_123'],
200,
MOCK_URL
);
$meter = $fullEvent->fetchRelatedObject();
static::assertInstanceOf(\Stripe\Billing\Meter::class, $meter);
static::assertSame('mtr_123', $meter->id);
}
}
Loading