teamtnt / php-stripe-webhook-tester

A PHP package for testing Stripe Webhooks localy
MIT License
97 stars 24 forks source link

Recommendation for setting environment when webhook called #3

Open wjgilmore opened 8 years ago

wjgilmore commented 8 years ago

Hi,

I'm finding this package useful, and plan on submitting a pull request soon which contains among other things an updated set of event templates for the 2015-10-12 API. I do have a question though: when a test is run, it makes a call to the webhook endpoint using setEndpoint, however when this call is made the webhook will be run using the local environment, which means the local (rather than testing) database is used. I can think of a few different ways to trick Laravel into using the test environment for such purposes, however wanted to ask what the Team TNT team is doing to resolve this issue.

Jason

stokic commented 8 years ago

Hi,

we were debating about the best way to enable this but it seems that everything we come up with is kind of an ugly hack, at least in our opinion. Any way of tricking Laravel is a bit "shady" IMO but I would love to hear your ideas, maybe you have a clean approach we didn't think of yet :)

rikh42 commented 8 years ago

Laravel 5+ test framework supports making post requests as part of the TestCase class. See https://laravel.com/api/5.2/Illuminate/Foundation/Testing/TestCase.html#method_post

nticaric commented 8 years ago

@rikh42 yap, you're right, in this case you could only use the loadEventData method to load the webhook data and simply make a post request

Sti3bas commented 6 years ago

I've also had this issue in Laravel, solved it by adding a method to TestCase class:

protected function triggerStripeWebhook($event)
{
    $tester = (new WebhookTester())->setVersion('2018-05-21');
    $data = $tester->loadEventData($event);

    return $this->call('POST', route('stripe.webhook'), [], [], [], [], $data);
}

Now I can trigger a webhook with $this->triggerStripeWebhook('charge.succeeded'); in my tests.