I'm running into a strange issue and was curious if anyone else has ran into this. I'm trying to test my controller that is responsible for creating/updating Stripe customers.
Anyway I am getting an error when I mock my StripeGateway class.
The thing that confuses me is the test does not error out if I remove the
once()
call.
The error is:
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 79 bytes) in ...../vendor/fzaninotto/faker/src/Faker/Provider/Text.php on line 120
My Test Is:
/** @test */
public function it_should_create_a_customer_if_the_data_is_valid(){
$user = $this->createMerchant();
$stripeGateWay = Mockery::mock('App\Billing\StripeGateway');
$stripeGateWay->shouldReceive('createCustomer')
->withArgs([$user->email, 'theToken'])
->once()
->andReturnValues([
'id' => 'cus_123',
'data' => [
'last4' => '4242'
]
]);
App::instance('App\Billing\StripeGateway', $stripeGateWay);
$this->post($this->getUrl(), ['stripeToken' => 'theToken'])
->seeJsonContains([
'message' => 'Card Linked!',
'statusCode' => 200
])
->seeStatusCode(200);
$this->seeInDatabase('users_merchant', [
'id' => $user->userable->id,
'stripe_id' => 'cus_123',
'last_four' => '4242'
]);
}
The StripeGateWay class is:
class StripeGateway
{
/**
* StripeGateway constructor.
*/
public function __construct()
{
Stripe::setApiKey(
config('services.stripe.secret')
);
}
/**
* @param $stripeToken
* @param string $email
* @return Customer
*/
public function createCustomer($stripeToken, $email = '')
{
return Customer::create([
'email' => $email,
'source' => $stripeToken
]);
}
/**
* @param $customerId
* @param $stripeToken
* @return Customer
*/
public function updateCustomer($customerId, $stripeToken)
{
$customer = Customer::retrieve($customerId);
$customer->source = $stripeToken;
return $customer->save();
}
/**
* @param $customerId
* @param $charge
* @param $description
* @param string $currency
* @return Charge
*/
public function makeCharge($customerId, $charge, $description, $currency = 'usd')
{
return Charge::create([
'customer' => $customerId,
'amount' => $charge,
'description' => $description,
'currency' => $currency
]);
}
/**
* @param $customerId
* @param string $currency
* @return Charge
*/
public function makeLocationCharge($customerId, $currency = 'usd')
{
return Charge::create([
'customer' => $customerId,
'amount' => 10000,
'description' => 'Location Setup Fee',
'currency' => $currency
]);
}
/**
* @param $customerId
* @param $planId
* @return mixed
*/
public function createSubscription($customerId, $planId)
{
$billingAnchor = Carbon::now();
if ($billingAnchor->day != 1) {
$billingAnchor = Carbon::parse('first day of next month');
}
$customer = Customer::retrieve($customerId);
return $customer->subscriptions
->create([
'plan' => $planId,
'billing_cycle_anchor' => $billingAnchor->timestamp
]);
}
/**
* @param $customerId
* @param $subscriptionId
* @param $quantity
* @return mixed
*/
public function updateSubscriptionQuantity($customerId, $subscriptionId, $quantity)
{
$customer = Customer::retrieve($customerId);
$subscription = $customer->subscriptions->retrieve($subscriptionId);
$subscription->quantity = $quantity;
return $subscription->save();
}
/**
* @param $customerId
* @param $subscriptionId
* @return mixed
*/
public function cancelSubscription($customerId, $subscriptionId){
$customer = Customer::retrieve($customerId);
return $customer->subscriptions
->retrieve($subscriptionId)
->cancel();
}
}