Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

shez1983's avatar

Stripe Testing (Subscriptions etc)

is there a guide on how to write tests for stripe (mocking it etc).. i have been googling but still a bit unsure?

I am using latest Laravel 6.0. creating a subscription when user registers. i am mocking the create() function of Stripe\Customer which is used to create customer. but regardless of mocking it is still being called?

public function test_i_can_register()
    {
        // arrange

        $this->mock(\Stripe\Customer::class, function ($mock) {
            $mock->shouldReceive('create')->andReturn(json_decode(example Request))
        });

this is what i am hoping to mock

www/vendor/stripe/stripe-php/lib/ApiOperations/Create.php

    public static function create($params = null, $options = null)
    {
        dump('come here');
        self::_validateParams($params);
        $url = static::classUrl();

        list($response, $opts) = static::_staticRequest('post', $url, $params, $options);
        $obj = \Stripe\Util\Util::convertToStripeObject($response->json, $opts);
        $obj->setLastResponse($response);
        return $obj;
    }

when i run my tests i can see the dump(come here).. so i know its not working :/

NOTE example request is just one i made and have pasted in there (eventually will be placed elsewhere)

array:26 [
  "id" => "cus_Fw9m3r1gzGagVj"
  "object" => "customer"
  "account_balance" => 0
  "address" => null
  "balance" => 0
  "created" => 1570298062
  "currency" => null
  "default_source" => null
  "delinquent" => false
  "description" => null
  "discount" => null
  "email" => "sdummyemail"
  "invoice_prefix" => "9FE4EC3E"
  "invoice_settings" => array:3 [
    "custom_fields" => null
    "default_payment_method" => null
    "footer" => null
  ]
  "livemode" => false
  "metadata" => []
  "name" => null
  "phone" => null
  "preferred_locales" => []
  "shipping" => null
  "sources" => array:5 [
    "object" => "list"
    "data" => []
    "has_more" => false
    "total_count" => 0
    "url" => "/v1/customers/cus_Fw9m3r1gzGagVj/sources"
  ]
  "subscriptions" => array:5 [
    "object" => "list"
    "data" => []
    "has_more" => false
    "total_count" => 0
    "url" => "/v1/customers/cus_Fw9m3r1gzGagVj/subscriptions"
  ]
  "tax_exempt" => "none"
  "tax_ids" => array:5 [
    "object" => "list"
    "data" => []
    "has_more" => false
    "total_count" => 0
    "url" => "/v1/customers/cus_Fw9m3r1gzGagVj/tax_ids"
  ]
  "tax_info" => null
  "tax_info_verification" => null
]
0 likes
4 replies
shez1983's avatar

@sti3bas thanks for that. - i get you shouldnt mock what you own.. but i think if u use mocks in your unit and then still test actual stripe connection in your integration tests which u only run once while uploading to your repo or your server etc i dont see what the problem is? at least thast what i have been led to believe. this way everything ADAM said in that link is applicable - i.e. stripe changes your integration tests will catch that..

Sti3bas's avatar

@shez1983 yeah, if you have integration tests which hits Stripe servers, then yes, it should be fine.

Please or to participate in this conversation.