I'm using laravel7 and codeception; and I have an Api test class called abcCest
located inside test/api
folder.
it looks like this:
<?php
use Tests\TestCase;
class abcCest extends TestCase
{}
the parent class TestCase
has the following content:
<?php
namespace Tests;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
abstract class TestCase extends BaseTestCase
{
use CreatesApplication;
}
I keep getting the error:
PHP Fatal error: Trait 'Tests\CreatesApplication' not found in /var/www/tests/TestCase.php on line 7
which is the exact place where i use the trait. The problem is that the trait CreatesApplication
exists inside the vendor folder under the path: vendor/laravel/laravel/tests/CreatesApplication.php
and this is its content:
<?php
namespace Tests;
use Illuminate\Contracts\Console\Kernel;
trait CreatesApplication
{
/**
* Creates the application.
*
* @return \Illuminate\Foundation\Application
*/
public function createApplication()
{
$app = require __DIR__.'/../bootstrap/app.php';
$app->make(Kernel::class)->bootstrap();
return $app;
}
}
in my composer.json
file i have the following already set:
"autoload": {
"psr-4": {
"App\\": "app/",
"Tests\\": "tests/"
},
"classmap": [
"database/",
"library/"
]
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
}
i ran composer dump-autoload many times but still keep on getting the same error...
CodePudding user response:
The problem you're seeing is that the namespace does not match where Composer's autoload thinks it is. According to PSR-4 specs you can't have the same namespace in multiple directories, so the file cannot be autoloaded.
You have two options: manually include the file (I honestly have no idea how this would be done for a trait) or just copy the method out of the trait into your base class.
The real problem is that you're including this trait at all. It's automatically part of the base Laravel TestCase
class that you extend. You can already use it by calling $this->createApplication()
within your test methods.
Although thinking further about this, it seems you might be misunderstanding how a Laravel app is set up. The laravel/laravel
package isn't meant to be included in your app; it is your app, or at least the skeleton of what will become your app. So what you're referring to as the vendor/laravel/laravel/tests
directory should be your app's tests
directory.
CodePudding user response:
There should be a namespace declaration in your test class.
Also you should have the CreatesApplication.php
file in your app's tests folder.