-1

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...

miken32
  • 42,008
  • 16
  • 111
  • 154
Alladin
  • 1,010
  • 3
  • 27
  • 45
  • you have to use `use` in your TestCase file to tell php where is CreatesApplication file – Giacomo M May 31 '22 at 14:34
  • @GiacomoM if u notice, you will see that the abstract parent `testCase` has the same namespace as the trait itself. I also double checked and found that they r both in the same folder, that's why it's using it without the need to add the `use` statement... but i still tried to use it in my `testCase` class exactly how i used the parent abstract `TestCase` by adding: `use Illuminate\Foundation\Testing\CreatesApplication;` but i still get the same error, in fact even my IDE detects that the class doesnt exist, which is weird as both r literally inside the same folder and share the same namespace – Alladin May 31 '22 at 14:42

2 Answers2

1

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.

miken32
  • 42,008
  • 16
  • 111
  • 154
  • thank you so much i just took a look at the laravel erpo and found that this trait should be part of my test folder instead of trying to use it from the vendor laravel package.. i spent hours trying to figure this out and i wasn't even open to the fact that this file should exist in my local files.. problem solved now thanks to your answer – Alladin May 31 '22 at 15:18
0

There should be a namespace declaration in your test class. Also you should have the CreatesApplication.php file in your app's tests folder.

miken32
  • 42,008
  • 16
  • 111
  • 154
Hakim Benmazouz
  • 321
  • 1
  • 4
  • my `abcCest` is already able to access the testCase, which can also extend from the abstract `BaseTestCase`.. the problem is that the parent abstract `testCase` isn't able to access the `CreatesApplication` trait which exists in the same folder and under the same namespace.. that's why I'm so confused. – Alladin May 31 '22 at 14:51