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

bjones2015's avatar

Mockery Error

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();
    }
}
0 likes
1 reply
bjones2015's avatar

Ok it turns out I did something kind of dumb, but I still don't understand why it caused an out of memory error.

Anyway here is what I did. I updated the code to inject a billing interface and just bound that interface to my gateway class for injection. While I was doing that I noticed that when I called


   $stripeGateWay->shouldReceive('createCustomer')
            ->withArgs([$user->email, 'theToken'])
            ->once()
            ->andReturnValues([
                'id' => 'cus_123',
                'data' => [
                    'last4' => '4242'
                ]
            ]);

I actually had the withArgs backwards. In the interface I expect the token first and the email second. Once I changed the order of the params it worked.

Please or to participate in this conversation.