Reputation: 2742
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;
}
}
Upvotes: 1
Views: 927
Reputation: 60068
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;
Upvotes: 3