Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

$model->validate() reports correctly, but $model->save() does not #4

Open
felixkiss opened this issue Jun 4, 2013 · 5 comments
Open

Comments

@felixkiss
Copy link

Basic Model validation, 2 tests defined:

public function testShouldBeInvalidWithoutUsername()
{
    $user = Factory::user();
    $user->username = null;

    Should::beFalse($user->validate(), 'User should be invalid without username');
}

public function testShouldPreventFromSavingWithoutUsername()
{
    $user = Factory::user();
    $user->username = null;

    Should::beFalse($user->save(), 'User should not be saved without username');
}

User model:

class User extends Model
{
    protected static $rules = array(
        'username' => 'required',
    );
}

First test passes, second does not.
Seems odd to me, because save() does directly react to the return value of validate().

What am I doing wrong?

@JeffreyWay
Copy link
Owner

Are you hooking into the save() method somehow? By default, Laravel doesn't auto-validate.

@felixkiss
Copy link
Author

I'm confused, this is what this package is supposed to do, isn't it?

simply save the model as you normally would, and let the package worry about the validation. If it fails, then the model's save method will return false.

Doesn't Way\Database\Model hook into save()?

static::saving(function($model)

@JeffreyWay
Copy link
Owner

I'm sorry. I thought you were commenting in a different project entirely. I'll take a look. :)

@bmitch
Copy link

bmitch commented Aug 13, 2013

Is this still an issue? I believe I'm running into the same thing.

@felixkiss
Copy link
Author

For me, this tests are successful, if I extend my Test class from PHPUnit_Framework_TestCase, but go wrong if I extend from TestCase (included into the framework).

I couldn't figure out why, but opted to extend PHPUnit_Framework_TestCase in model tests.

Issue is still active, because what if I want to use the nice helper methods for controller testing, etc. defined in Illuminate\Foundation\Testing\TestCase?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants