Skip to content

Instantly share code, notes, and snippets.

@lukecurtis93
Created March 8, 2020 23:03
Show Gist options
  • Save lukecurtis93/2e113274aa0d7a2f47f4ad235d166cd7 to your computer and use it in GitHub Desktop.
Save lukecurtis93/2e113274aa0d7a2f47f4ad235d166cd7 to your computer and use it in GitHub Desktop.
Mock Stripe API with Mockery & Laravel
<?php
namespace Tests;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
use Stripe\Customer;
use Stripe\Stripe;
/**
* Class TestCase.
*/
abstract class TestCase extends BaseTestCase
{
use CreatesApplication;
// Other methods here for your base test class
// In your methods you can call $this->mockStripe() and amend method as expected for additional models if required
/**
* Mock the stripe models and get some expected results back
*
* @return null
*/
public function mockStripe()
{
$this->mock(Stripe::class, function ($mock) {
$mock->shouldReceive('setApiKey');
});
$this->mock(Customer::class, function ($mock) {
$mock->shouldReceive('create')->andReturn([
'id' => 'test_stripe_customer_id'
]);
});
}
}
@bryceandy
Copy link

Hi @lukecurtis93 , a few questions:

  1. Would this mock directly hit the test API?
  2. The customer create method returns an object Stripe\Customer, what's the reason behind returning that array?

@lukecurtis93
Copy link
Author

Hey there!

  1. No it wouldn't the idea is we don't hit the endpoints.
  2. Yes you're correct, it should actually return a new instance of that (this is an old snippet so may have changed since I last used their SDK)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment