I will build model validation in Laravel 4 with creating Model Event:
class User extends Eloquent { public function isValid() { return Validator::make($this->toArray(), array('name' => 'required'))->passes(); } public static function boot() { parent::boot(); static::creating(function($user) { echo "Hello"; if (!$user->isValid()) return false; }); } }
It works well, but I have problems with PHPUnit. The following two tests are exactly the same, but only the first pass:
class UserTest extends TestCase { public function testSaveUserWithoutName() { $count = User::all()->count(); $user = new User; $saving = $user->save(); assertFalse($saving);
If I try to create a user twice in the same test, it will work, but this is similar if the binding event is present only in the first test of my test class. echo "Hello"; printed only once during the first test run.
I am simplifying the matter for my question, but you can see the problem: I cannot verify several validation rules in different unit tests. I have been trying almost everything since then, but I have almost jumped out of the windows! Any idea?
phpunit laravel laravel-4
Alexandre Butynski
source share