1

I have clean install of Laravel 5.0, and I have issues with phpunit tests. If I create a test for user model, I'm getting error - User class not found.

If I test controllers, works fine, controller classes are detected.

As a temporary workaround, just to test if it is working, I added class User inside UserTest.php.

I tried to add folder models in app folder, placing the class inside, similar as it was in Laravel 4.2, changed composer.json as well, ran composer dump-autoload, but it didn't work.

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

The simple classes looks like this:

// tests/models/UserTest.php

class UserTest extends TestCase
{

    protected $user;


    public function setUp()
    {
        parent::setUp();
    }

    public function testEmptyNameFailExpected()
    {
        $user = new User;
        $user->name = '';
        $result = $user->isValid();
        $this->assertFalse($result);

        return $user;
    }
}

And here is User.php class in app folder (in laravel 5.0 the architecture is different)

// app/User.php
namespace App;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
use Illuminate\Support\Facades\Validator;
class User extends Model implements AuthenticatableContract, CanResetPasswordContract
{
    use Authenticatable, CanResetPassword;

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'users';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['name', 'email', 'password'];
    public static $rules = [ 'name' => 'required|min:3'  ];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = ['password', 'remember_token'];


    /**
     * validate input
     *
     * @return bool
     */
    public function isValid()
    {
        $validation = Validator::make($this->attributes, static ::$rules);
        if ($validation->passes()) return true;
        $this->errors = $validation->messages();

        return false;
    }

}
3
  • 1
    Try using the FQN for the model. In your case it may be App\User Commented Jan 20, 2015 at 20:09
  • I tried, but still was not recognized. It searches for the class inside tests folder, so I supposed that something with autoloading is not good Commented Jan 20, 2015 at 20:31
  • @davidxd33 today this worked, not sure why yesterday didn't Commented Jan 21, 2015 at 10:15

1 Answer 1

3

I noticed two problems with your code:

You said your test folder is

app/tests/models/UserTest.php

That is incorrect. In a clean install of Laravel 5.0 - the test class is in the base folder - not the app folder - so it should be

tests/models/UserTest.php

Also - your User is namespaced in Laravel 5.0 - so your code will need to be

$user = new \App\User;
Sign up to request clarification or add additional context in comments.

1 Comment

for first problem - it is a typo, I set it up in tests folder, sorry. For second, thanks, today morning I make it work. Weird, as I tried the same thing yesterday and didn't work at all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.