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

Util::convertToStripeObject and Util::convertStripeObjectToArray don't seem to round-trip #702

Closed
CyberiaResurrection opened this issue Aug 22, 2019 · 3 comments · Fixed by #552

Comments

@CyberiaResurrection
Copy link

CyberiaResurrection commented Aug 22, 2019

Stripe-PHP version: v6.43.0
PHP version: 7.2.21
PHPUnit version: 8.3.4

It'd probably be easier to express what I've tripped over as executable test cases (if these help chasing it down, then please use them)

    public function testRoundTripFromObject()
    {
        $array = ['foo' => 'bar'];

        $stripeObject = \Stripe\Util\Util::convertToStripeObject($array, []);

        $final = \Stripe\Util\Util::convertStripeObjectToArray($stripeObject);

        $this->assertEquals($array, $final);
    }

    public function testRoundTripFromArray()
    {
        $array = ['foo' => 'bar'];

        $stripeObject = \Stripe\StripeObject::constructFrom($array, []);

        $final = \Stripe\Util\Util::convertStripeObjectToArray($stripeObject);

        $this->assertEquals($array, $final);
    }

On my environment (referenced above), both of those tests are failing, with convertStripeObjectToArray returning an empty array in both cases. The second test tries to narrow down the scope of what could go wrong when the StripeObject is being created.

Upon digging a bit further into convertStripeObjectToArray, the foreach loop on line 43 of \Stripe\Util\Util does not seem to be firing in either of these tests.

@ob-stripe
Copy link
Contributor

@CyberiaResurrection Thanks for the report. I think convertStripeObjectToArray is badly named and probably shouldn't even be public. The proper way to turn a Stripe object into an array is to call ->__toArray() on the object instance. (__toArray is also a terrible name, since it implies it's a magic method when it's not).

If you replace this line:

$final = \Stripe\Util\Util::convertStripeObjectToArray($stripeObject);

with this:

$final = $stripeObject->__toArray();

the tests should pass.

@CyberiaResurrection
Copy link
Author

@ob-stripe , thanks heaps. Converting the call got those tests passing, and cleared up the confusion.

Maybe mark convertStripeObjectToArray deprecated?

@ob-stripe
Copy link
Contributor

The \Stripe\Util\Util::convertStripeObjectToArray has been removed in stripe-php 7.0.0. You should now use the toArray() (previously __toArray()) method to convert Stripe objects to arrays.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants