abkrim's avatar
Level 13

Pest 3 testing config values

I have a problems for testing config values in a pachage developing

Package

TestCase.php

<?php

namespace Tests;

use Illuminate\Foundation\Testing\TestCase as BaseTestCase;

abstract class TestCase extends BaseTestCase
{
    use CreatesApplication;

    protected function getPackageProviders($app)
    {
        return [
            \Spatie\LaravelRay\RayServiceProvider::class,
        ];
    }
}

Pest.php

pest()->extend(Tests\TestCase::class)->in('Feature','Unit');

Test Unit

<?php

it('throws an exception when CONTASIMPLE_API_KEY is not set', function () {
    ray(config('contasimple.api_key'));
});

Error

Illuminate\Contracts\Container\BindingResolutionException: Target class [config] does not exist.
at vendor/laravel/framework/src/Illuminate/Container/Container.php:944
at vendor/laravel/framework/src/Illuminate/Container/Container.php:819
at vendor/laravel/framework/src/Illuminate/Container/Container.php:755
at vendor/laravel/framework/src/Illuminate/Foundation/helpers.php:124
at vendor/laravel/framework/src/Illuminate/Foundation/helpers.php:279
at /Users/abkrim/Sites/packages/contasimple/tests/Unit/SageAuthenticatorTest.php:4
Caused by
ReflectionException: Class "config" does not exist

I can not test config() in Pest tests?

There're to many questions but older and not work on LR11 + Pest 3, or I have a mistake

0 likes
2 replies
LaryAI's avatar
Level 58

To resolve the issue of testing configuration values in a Laravel package using Pest, you need to ensure that the Laravel application is properly bootstrapped so that the config helper function is available. The error you're encountering suggests that the application container isn't set up correctly, which is why the config function can't resolve the configuration values.

Here's a step-by-step solution to address this:

  1. Ensure Application Bootstrapping: Make sure your test case is extending the correct base test case that bootstraps the Laravel application. In your TestCase.php, you should extend Illuminate\Foundation\Testing\TestCase and ensure the CreatesApplication trait is used correctly.

  2. Check CreatesApplication Trait: Verify that the CreatesApplication trait is properly defined and is returning the application instance. This is typically done by calling require_once on the bootstrap/app.php file.

  3. Correct Pest Configuration: Ensure that your Pest.php file is correctly extending the test case. It looks like you are doing this correctly, but double-check the paths and class names.

  4. Example Code: Here's a refined version of your setup to ensure everything is correctly configured:

    TestCase.php

    <?php
    
    namespace Tests;
    
    use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
    use Illuminate\Contracts\Console\Kernel;
    
    abstract class TestCase extends BaseTestCase
    {
        use CreatesApplication;
    
        public function createApplication()
        {
            $app = require __DIR__.'/../bootstrap/app.php';
    
            $app->make(Kernel::class)->bootstrap();
    
            return $app;
        }
    
        protected function getPackageProviders($app)
        {
            return [
                \Spatie\LaravelRay\RayServiceProvider::class,
            ];
        }
    }
    

    Pest.php

    <?php
    
    use Tests\TestCase;
    
    uses(TestCase::class)->in('Feature', 'Unit');
    
  5. Testing Configuration: Now, in your test, you can safely use the config function:

    Test Unit

    <?php
    
    it('throws an exception when CONTASIMPLE_API_KEY is not set', function () {
        $apiKey = config('contasimple.api_key');
        ray($apiKey);
    
        if (empty($apiKey)) {
            throw new Exception('CONTASIMPLE_API_KEY is not set');
        }
    });
    

By ensuring that the Laravel application is properly bootstrapped, the config helper should work as expected in your Pest tests. If you continue to face issues, double-check your environment setup and ensure that the bootstrap/app.php file is correctly included and configured.

abkrim's avatar
Level 13

@LaryAI is a package not and app. There are not a bootstrap.php

Please or to participate in this conversation.