我正在测试Stripe用来与我的Laravel应用程序通信的webhook . 我正在使用PHP Stripe Webhooks Tester,我正在关注他们的official tutorial .

这是我的测试用例:

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);
    }
}

当我运行 phpunit 时,我收到以下错误:

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://curl.haxx.se/libcurl/c/libcurl-errors.html)

/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.

我查了一下错误代码,发现这个(source)

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

这是我对此webhook的路线:

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

这是我的实际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));
  }

}

UPDATE

好吧,看起来我忘了将第二个 / 放在 http://localhost 中 . 谢谢@jszobody的提醒 .

现在我得到一个不同的错误:

2)WebhooksControllerTest :: testWhenCustomerSubscriptionDeleted GuzzleHttp \ Exception \ ClientException:客户端错误:POST http:// localhost / stripe / webhook导致404 Not Found响应:404 Not Found Not Not Found(截断...)/ home / levi / SIGadmin / vendor / guzzlehttp / guzzle / src / Exception / RequestException.php:111 /home/levi/SIGadmin/vendor/guzzlehttp/guzzle/src/Middleware.php:65 / home / levi / SIGadmin / vendor / guzzlehttp / promises / src / Promise.php:203 /home/levi/SIGadmin/vendor/guzzlehttp/promises/src/Promise.php:156/home/levi/SIGadmin/vendor/guzzlehttp/promises/src/TaskQueue.php:47 / home / levi / SIGadmin / vendor / guzzlehttp / promises / src / Promise.php:246/home/levi/SIGadmin/vendor/guzzlehttp/promises/src/Promise.php:223 / home / levi / SIGadmin / vendor / guzzlehttp / promises / src / Promise.php:267 /home/levi/SIGadmin/vendor/guzzlehttp/promises/src/Promise.php:225/home/levi/SIGadmin/vendor/guzzlehttp/promises/src/Promise.php:62 / home / levi / SIGadmin / vendor / guzzlehttp / guzzle / src / Client.php:129 / home / levi / SIGadmi n / 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