I'm writing some unit tests to test the API endpoints in my Laravel 5 application, and a lot of endpoints require user authentication. Instead of having the same user account creation code in every test, I wanted to define a RegistersUsers
trait to use on the test classes, which will have a registerUser()
method.
The directory structure of my tests
directory is like so:
/tests
/Traits
RegistersUsers.php
TestCase.php
UserTest.php
I've namespaced TestCase.php
and UserTest.php
by adding this namespace declaration:
namespace MyappTests;
and I've namespaced RegistersUsers.php
like so:
namespace MyappTests\Traits;
My UserTest
looks like this, with the namespace and the use
declaration so that I can leverage RegistersUsers
.
<?php
namespace MyappTests;
use MyappTests\Traits\RegistersUsers;
class UserTest extends TestCase
{
use RegistersUsers;
// ... rest of the class
However, when I run the test, PHPUnit dies with the fatal error:
Trait 'MyappTests\Traits\RegistersUsers' not found in /home/vagrant/demo-app-net/tests/UserTest.php on line 9
As far as I can tell, my namespacing is correct and my trait should be found. I've been going around in circles with this and can't seem to figure it out.