vendredi 30 décembre 2016

Error while running tests in Laravel Webhook for Stripe - Could not resolve host

I am testing a webhook that Stripe uses to communicate with my Laravel application. I am using PHP Stripe Webhooks Tester for this and I'm following their official tutorial.

Here's my Test Case:

use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use TeamTNT\Stripe;

class WebhooksControllerTest extends TestCase
{
    /**
     * A basic test example.
     *
     * @return void
     */
    public function testWhenCustomerSubscriptionDeleted()
    {
      $tester = new TeamTNT\Stripe\WebhookTester('http:/localhost/stripe/webhook');
      $response = $tester->triggerEvent('customer.subscription.deleted');
      $this->assertEquals(200,$response->getStatusCode());

        //$this->assertTrue(true);
    }
}

When I run phpunit I get the following error:

PHPUnit 5.7.5 by Sebastian Bergmann and contributors.

E                                                                   1 / 1 (100%)

Time: 4.57 seconds, Memory: 10.00MB

There was 1 error:

1) WebhooksControllerTest::testWhenCustomerSubscriptionDeleted
GuzzleHttp\Exception\ConnectException: cURL error 6: Could not resolve host: http (see http://ift.tt/1mgwZgQ)

/home/levi/SIGadmin/vendor/guzzlehttp/guzzle/src/Handler/CurlFactory.php:186
/home/levi/SIGadmin/vendor/guzzlehttp/guzzle/src/Handler/CurlFactory.php:150
/home/levi/SIGadmin/vendor/guzzlehttp/guzzle/src/Handler/CurlFactory.php:103
/home/levi/SIGadmin/vendor/guzzlehttp/guzzle/src/Handler/CurlHandler.php:43
/home/levi/SIGadmin/vendor/guzzlehttp/guzzle/src/Handler/Proxy.php:28
/home/levi/SIGadmin/vendor/guzzlehttp/guzzle/src/Handler/Proxy.php:51
/home/levi/SIGadmin/vendor/guzzlehttp/guzzle/src/PrepareBodyMiddleware.php:72
/home/levi/SIGadmin/vendor/guzzlehttp/guzzle/src/Middleware.php:30
/home/levi/SIGadmin/vendor/guzzlehttp/guzzle/src/RedirectMiddleware.php:68
/home/levi/SIGadmin/vendor/guzzlehttp/guzzle/src/Middleware.php:59
/home/levi/SIGadmin/vendor/guzzlehttp/guzzle/src/HandlerStack.php:67
/home/levi/SIGadmin/vendor/guzzlehttp/guzzle/src/Client.php:275
/home/levi/SIGadmin/vendor/guzzlehttp/guzzle/src/Client.php:123
/home/levi/SIGadmin/vendor/guzzlehttp/guzzle/src/Client.php:129
/home/levi/SIGadmin/vendor/guzzlehttp/guzzle/src/Client.php:87
/home/levi/SIGadmin/vendor/teamtnt/php-stripe-webhook-tester/src/Stripe/WebhookTester.php:85
/home/levi/SIGadmin/tests/WebhooksControllerTest.php:18
/usr/share/php/PHPUnit/TextUI/Command.php:155
/usr/share/php/PHPUnit/TextUI/Command.php:106

FAILURES!
Tests: 1, Assertions: 0, Errors: 1.

I looked up the error code, I found this (source):

Couldn't resolve host. The given remote host was not resolved.

Here's my route for this webhook:

Route::post('stripe/webhook', 'WebhooksController@handle');

And here's my actual webhook:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\User;
class WebhooksController extends Controller
{
  public function handle()
  {
      $payload = request()->all();

      $method = $this->eventToMethod($payload['type']);

      if(method_exists($this, $method))
      {
        $this->$method($payload);
      }

      return response('Webhook Received');
      //return redirect('myaccount')->with('success', 'Account deactivated');
  }

  public function whenCustomerSubscriptionDeleted($payload)
  {
    User::byStripeId(
      $payload['data']['object']['customer']
    )->deactivate();
  }

  public function whenCustomerSubscriptionCreated($payload)
  {
    User::byStripeId(
      $payload['data']['object']['customer']
    )->addSubscriptionId($payload['data']['object']['id']);
  }

  public function eventToMethod($event)
  {
    return 'when' . studly_case(str_replace('.', '_', $event));
  }

}

Aucun commentaire:

Enregistrer un commentaire