JeffreyWay / Laravel-Test-Helpers

Easier testing in Laravel.
https://packagist.org/packages/way/laravel-test-helpers
217 stars 40 forks source link

exists in database rule not flagging an error within test...? #40

Closed RyanHavoc closed 11 years ago

RyanHavoc commented 11 years ago

Hi,

I have a model here:

<?php

class PlanOption extends BaseModel {
    protected $guarded = array();

    public static $rules = [
        'plan_id'       => 'required|exists:plans,id',
        'plan_price_id' => 'required|exists:plan_prices,id',
        'item_limit'    => 'required|integer'
    ];
}

Which when I use Factory::PlanOption to create a dummy object, run it through my validate method and split out to the browser like so (in routes.php):

use Way\Tests\Factory;
Route::get('/', function(){
    $planOption = Factory::PlanOption(['plan_id' => 44]);
    $planOption->validate();
    var_dump($planOption); 
});

Give me what I'd expect, namely a couple of errors telling me that the plan_id and plan_option_id don't exist in the database:

/*...extract...*/
["errors"]=>
object(Illuminate\Support\MessageBag)#238 (2) {
    ["messages":protected]=>
    array(1) {
    ["plan_id"]=>
        array(1) {
            [0]=>
                string(32) "The selected plan id is invalid."
        }
    ["format":protected]=>
    string(8) ":message"
}

However when I spit out the Factory::PlanOption within my test to ensure that validation is false if plan_id is invalid:

public function testIsInvalidWithoutAPlanId()
{
    $planOption = Factory::PlanOption(['plan_id' => 44]);

    dd($planOption);

    $this->assertNotValid($planOption);
}

I get:

 ["errors"]=>null

Same code, but different results? Does anyone have any idea why?

RyanHavoc commented 11 years ago

This was me being a tool. Move along, nothing to see here.