代码之家  ›  专栏  ›  技术社区  ›  rook99

如何模拟包中的响应?

  •  0
  • rook99  · 技术社区  · 4 年前

    我试图在使用时模拟响应 php-oppwa

    我为这个包创建了这个包装:

    use Bryangruneberg\OPPWA\Factory;
    use Bryangruneberg\OPPWA\OPPWAResponse;
    use Illuminate\Support\Facades\Config;
    
    class OppwaService
    {
        public function client()
        {
            $client = Factory::createClient(
                Config::get('gateways.oopwa_user_id'),
                Config::get('gateways.oopwa_password'),
                Config::get('gateways.oopwa_entity_id'),
                Config::get('gateways.oopwa_url'),
            );
    
            $api = Factory::createAPI($client);
    
            return $api;
        }
    
        public function setupCheckout(int $amount, string $currency, string $paymentType, array $options = [])
        {
            return $this->client()->prepareCheckout($amount, $currency, $paymentType, $options);
        }
    
        public function checkoutSetupWasSuccessful(OPPWAResponse $response)
        {
            return $this->client()->isPrepareCheckoutSuccess($response);
        }
    }
    
    

    我的控制器里有这个:

        public function __construct(OppwaService $service)
        {
            $this->service = $service;
        }
    
        public function show()
        {
            $checkout = $this->service->setupCheckout(23, 'USD', OPPWA::PAYMENT_TYPE_DEBIT);
    
            if ($this->service->checkoutSetupWasSuccessful($checkout)) {
                $checkoutId = $checkout->getId(); // I am trying to mock this response
            }
    
            return view('index',['checkoutId' => $checkoutId ?? null);
        }
    

    我试图嘲笑 getId 在我的测试中,

        /** @test */
        public function checkout_id_exists()
        {
            $this->mock(OPPWAResponse::class, function ($mock) {
                $mock->shouldReceive('getId')->once()->andReturn('555555555-555555555');
            });
    
            $this->get(route('checkout.show'))->assertSee('555555555-555555555');
        }
    

    但是,这行不通。是因为 OPPWAResponse 是在 vendor 目录? 我该如何嘲笑对方的回应?

    0 回复  |  直到 4 年前