Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ahmed_ab's avatar

Getting "PHP Fatal Error : Trait" when running phpunit?

Hi all,

I am getting this error whenever I run phpunit


Fatal error: Trait 'Tests\Traits\MakeCustomerTrait' not found in C:\Users\ahmed\dev\gamla\tests\CustomerApiTest.php on line 13

PHP Fatal error:  Trait 'Tests\Traits\MakeCustomerTrait' not found in C:\Users\ahmed\dev\gamla\tests\CustomerApiTest.php on line 13


Screenshot

Composer.json

"autoload": {
        "classmap": [
            "database"
        ],
        "psr-4": {
            "App\\": "app/",
            "Tests\\": "tests"
        }
    },

0 likes
7 replies
tykus's avatar

~~You are already in the Test\Traits namespace, so you can just use MakeCustomerTrait in your class~~ assuming you have a file named tests/traits/MakeCustomerTrait.php with a trait keyword defining the MakeCustomerTrait trait.

Edit: your CustomerAPITest is in the wrong namespace compared the directory structure.

bobbybouwmann's avatar

CustomerAPITest should have the namespace set to Tests instead of Tests\Traits

ahmed_ab's avatar

@bobbybouwmann I have defined them within autoload-dev

"autoload-dev": {
        "classmap": [
            "tests/TestCase.php",
            "tests/traits/MakeCustomerTrait.php",
            "tests/traits/MakeFileTrait.php",
            "tests/traits/MakeUserTrait.php",
            "tests/ApiTestTrait.php"
        ]

It worked! but this time when I do on testUserCanLogin I am getting this error :(

Caused by
ErrorException: Trying to get property of non-object in C:\Users\ahmed\dev\myproject\app\Http\Controllers\CustomerController.php:325
Stack trace:
#0 C:\Users\ahmed\dev\myproject\app\Http\Controllers\CustomerController.php(325): Illuminate\Foundation\Bootstrap\HandleExceptions->handleError(8, 'Trying to get p...', 'C:\\Users\\ahmed\\...', 325, Array)
#1 [internal function]: App\Http\Controllers\CustomerController->getCustomerPanel()

LoginTest Code

<?php

use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;

class LoginTest extends TestCase
{
    use DatabaseTransactions;
    /**
     * A basic test example.
     *
     * @return void
     */

    public function testLoginUser()
    {
        $user =  factory(App\Models\User::class)->create([ 
            'password' => bcrypt('testpass1234'),
        ]);

        $this->assertFalse(Auth::check());

        $this->visit('/login')
            ->type($user->email, 'email')
            ->type('testpass1234', 'password')
            ->press('Login')
            ->seePageIs('/')

        $this->assertTrue(Auth::check());
    }
}

and getCustomerPanel code inside CustomerController


  //Customer Panel

  public function getCustomerPanel(){

    $user = User::find(Auth::user()->id);
    $news = News::orderBy('created_at', 'desc')->get();

    if ($user->isAdmin()){
      $devices= Device::all();
    }
    else {
      $devices = $user->customer->devices;
    }

    return view('customers.customerpanel.index')
        ->with('devices', $devices)
        ->with('news', $news);
  }

ahmed_ab's avatar

@bobbybouwmann it's the Else part in getCustomerPanel


public function getCustomerPanel(){

    $user = User::find(Auth::user()->id);
    $news = News::orderBy('created_at', 'desc')->get();

    if ($user->isAdmin()){
      $devices= Device::all();
    }
    else {
    // Here is where I get the error 
      $devices= $user->customer->devices;
    }

    return view('customers.customerpanel.index')
        ->with('devices', $devices)
        ->with('news', $news);
  }

bobbybouwmann's avatar

So customer is not a property of the $user object. Did you define that relation?

Please or to participate in this conversation.