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

MikeHopley's avatar

Invoking other classes from Acceptance tests in Codeception

I have an acceptance test that requires some setup before it runs. Specifically, I need to start a new subscription in Stripe. I have a class that does this, but when I run the test, Codeception complains that core Laravel classes such as Config cannot be found.

Here's some code, reduced for clarity. First the test:

<?php
// app/tests/browser/stopAndResumeSubscriptionCept.php

use BBapp\providers\payment\stripe\StripeTestBiller;
use BBapp\providers\payment\SubscriptionPlan as Plan;
use BBapp\models\User;

$I = new WebGuy($scenario);
$I->wantTo('stop and then resume my subscription');

// First we need to make a subscription
$biller = new StripeTestBiller;
$user   = User::whereEmail('foo@example.com')->first();
$result = $biller->startSubscription( Plan::monthly(), $user, null );

// Now do acceptance testing stuff
// ...

And here's the StripeTestBiller:

<?php namespace BBapp\providers\payment\stripe;

use BBapp\providers\payment\SubscriptionPlan as Plan;
use BBapp\models\User;

use App;
use Carbon\Carbon;

class StripeTestBiller extends StripeApi {

    public function __construct()
    {
        parent::__construct();
    }

    public function startSubscription( Plan $plan, User $user, $token = null )
    {
        $subscription = Stripe_Customer::create([
            'card' => [
                'number'    => '4242424242424242',
                'exp_month' => '01',
                'exp_year'  => Carbon::now()->addYears(1)->year,
                'cvc'       => '123',
            ],
            'plan'     => $plan->id,
            'email'    => $user->email,
            'metadata' => [
                'user_id'     => $user->id,
                'environment' => App::environment(),
            ],
        ]);

        return $subscription;
    }
    
    // Other methods here
}

Finally, the StripeApi class that StripeTestBiller extends:

<?php namespace BBapp\providers\payment\stripe;

use Config;
use Stripe;

class StripeApi implements StripeInterface {

    public function __construct()
    {
        Stripe::setApiKey( Config::get('stripe.secretKey') );
    }

    // A load of API wrapper methods here
}

The error from the console says:

FATAL ERROR. TESTS NOT FINISHED.
Class 'Config' not found
in *[Path to laravel]*\laravel\app\BBapp\providers\payment\stripe\StripeApi.php:24

...where line 24 is the line in the constructor above.

Is this an autoloading issue? How can I get these classes loaded during acceptance tests?

0 likes
2 replies
MikeHopley's avatar
MikeHopley
OP
Best Answer
Level 17

I found a solution:

Handle all the setup on a webpage, and start the acceptance test by visiting the webpage.

This page is purely a testing artefact. Since I have Codeception running in its own "acceptance" environment, I can check for this environment and App::abort(404) otherwise. I don't like random test pages hanging around that can change my database if someone visits them!

Doing it this way sidesteps all the thorny issues about trying to access Laravel "directly" from an acceptance test.

I did try using the Laravel4 module, which solves the immediate class-loading problem. But this module causes very strange behaviour with acceptance tests, and it's clearly not intended to be used with them (for example, see the module's sample tests, which have no acceptance suite).

I also tried "manually" booting up the Laravel framework by requiring index.php. But my code then changes the "real" database, whereas Codeception operates on a testing database (and this feels pretty hacky anyway).

majacirkova's avatar

Hello , i have simillar problem , i can't invoke models into tests files ? If I put this in my test class :

use App\Contacts\Models\Category as Category;
$var = Category::all();

i gets this error :

Class 'App\Contacts\Models\Category' not found 

Thank you :)))

Please or to participate in this conversation.