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:
-
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 extendIlluminate\Foundation\Testing\TestCaseand ensure theCreatesApplicationtrait is used correctly. -
Check
CreatesApplicationTrait: Verify that theCreatesApplicationtrait is properly defined and is returning the application instance. This is typically done by callingrequire_onceon thebootstrap/app.phpfile. -
Correct Pest Configuration: Ensure that your
Pest.phpfile is correctly extending the test case. It looks like you are doing this correctly, but double-check the paths and class names. -
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'); -
Testing Configuration: Now, in your test, you can safely use the
configfunction: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.