panthro's avatar

Cannot find trait inside my test?

I have a trait inside tests\Feature\Traits:

<?php

namespace Tests\Feature\Traits;

trait HasMediaTest
{
    public function test_something_in_the_trait()
    {
        $this->assertTrue(true);
    }
}

It is used like this:

<?php

namespace Tests\Feature;

use Tests\Feature\Traits\HasMediaTest;
use Tests\TestCase;

class UserTest extends TestCase
{
    use HasMediaTest;

    public function test_something()
    {
        $this->assertTrue(true);
    }
}

Both tests pass fine. But in cmd line I get a warning:

 WARN  Class HasMediaTest cannot be found in /Users/panthro/Documents/web/applawred/tests/Feature/Traits/HasMediaTest.php

I have done the usual composer dump-autoload.

Here is the composer file:

 "autoload": {
        "psr-4": {
            "App\": "app/",
            "Database\Factories\": "database/factories/",
            "Database\Seeders\": "database/seeders/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "Tests\": "tests/"
        }
    },
0 likes
4 replies
panthro's avatar

The warning goes away if I rename the class HasMediaTest to something else not ending with test e.g. HasMedia or HasMediaTestX.

Why?

2 likes
tisuchi's avatar
tisuchi
Best Answer
Level 70

@panthro Ahh....

Your class name is HasMediaTest. The keyword Test as a suffix is the problem. It is reserved for the test class only. Therefore file not found warning appears.

You simply rename it to HasMedia or HasMediaTrait of your class name.

You should not use test as a suffix.

4 likes

Please or to participate in this conversation.