代码之家  ›  专栏  ›  技术社区  ›  Wai Yan Hein

Laravel收银台:单元测试显式调度WebhookReceived事件不起作用

  •  0
  • Wai Yan Hein  · 技术社区  · 3 年前

    我正在构建一个Laravel应用程序。我正在使用Laravel收银台处理我的应用程序中的订阅。我正在为StripeWebhook编写单元测试,并为此创建一个自定义事件侦听器。

    我有一个事件监听器,如下所示:

    class StripeEventListener
    {
        /**
         * Create the event listener.
         *
         * @return void
         */
        public function __construct()
        {
            //
        }
    
        /**
         * Handle the event.
         *
         * @param  object  $event
         * @return void
         */
        public function handle(WebhookReceived $event)
        {
            if ($event->payload['type'] === 'invoice.payment_succeeded') {
                // Handle the incoming event...
            }
            // TODO: write unit test for this event.
            if ($event->payload['type'] === 'customer.subscription.updated') {
                // Handle the incoming event...
                // handleCustomerSubscriptionUpdated
                if ($user = $this->getUserByStripeId($event->payload['data']['object']['customer'])) {
                    $data = $event->payload['data']['object'];
                    if (
                        isset($data['status']) &&
                        ($data['status'] === StripeSubscription::STATUS_INCOMPLETE_EXPIRED ||
                        $data['status'] == StripeSubscription::STATUS_INCOMPLETE)
                    ) {
                        if (! $user->payment_method_needs_updating) {
                            $user->payment_method_needs_updating = true;
                            $user->save();
                        }
                    }
                }
            }
        }
    
        protected function getUserByStripeId($stripeId)
        {
            return Cashier::findBillable($stripeId);
        }
    }
    

    然后我将其绑定到EventServiceProvider中的WebhookReceived事件,如下所示:

    protected $listen = [
            WebhookReceived::class => [
                StripeEventListener::class
            ],
            Registered::class => [
                SendEmailVerificationNotification::class,
            ],
        ];
    

    我正在对侦听器进行单元测试,该侦听器调度WebhookReceived事件,如下所示:

    WebhookReceived::dispatch([
                .... mock data
            ]);
    

    但我得到了这个错误:

       Illuminate\Contracts\Container\BindingResolutionException 
    
      Target class [events] does not exist.
    
      at vendor/laravel/framework/src/Illuminate/Container/Container.php:879
        875▕ 
        876▕         try {
        877▕             $reflector = new ReflectionClass($concrete);
        878▕         } catch (ReflectionException $e) {
      ➜ 879▕             throw new BindingResolutionException("Target class [$concrete] does not exist.", 0, $e);
        880▕         }
    

    我该如何修复它,或者如何对它进行单元测试?

    0 回复  |  直到 3 年前