Framework: 5.4.19 seems to break my unit tests - eloquent model db connection inconsistency

Created on 18 Apr 2017  路  8Comments  路  Source: laravel/framework

  • Laravel Version: 5.4.19
  • PHP Version: 7.1.4
  • Database Driver & Version: mysql
  • Was introduced by pull request #18769 i guess

Description:

When I upgraded from laravel/framework 5.4.18 to 5.4.19 some of my unit tests broke. This is because somehow the models i get when making http requests (via the BrowserKitTest features) now have a connection provided, while the user i create via a factory and then use within the application via $this->actingAs have null as db connection. This leads to false returns when doing checks via $user->is($modelFromRouteModelBinding->user);

Steps To Reproduce:

Use $this->actingAs($userFromFactory) in BrowserKitTests and then call some of your routes via $this->json('GET', '/test') or so. When you use checks i.e. for security like $userFromRequest->is($modelFromRouteModelBinding->user), you will get false because the connections differ (null vs the (now) correct connection).

bug

Most helpful comment

All 8 comments

I am also finding that in my tests the models being generated from a factory and then used via actingAs has a null return value when calling getConnection, and thus in my Controller when I check the dependency injected request user against one pulled from the db...

$request->user()->is($dbUser);

It will always fail.

I tested again on 5.4.18 and there all models generated by factory have connection set to null. That would be fine, but now the one that is loaded through route model binding has a connection != null, and that is the problem.

That's weird I've upgraded my application to v5.4.19 and the result is green as expected

image

The suite has unit and feature tests. I also use the acting as and the factories

Do you use the is-Function provided by Eloquent Models? I use it to compare two models and to look if they reference the same piece of data. For that it compares the key, the database table and the connection. That is where it breaks.

Yea, I'm on 5.4.19 and experiencing this issue.

I agree there's inconsistency in this area, the connection should be always set on the model instance, it's currently set when you use Model::create() but not when you use save().

The factory builder uses save() and that's why it fails, #18769 is a fix not a breaking change and I believe I can fix this issue too but will have to make multiple changes to the current test suite. Will try to look into this today.

For those who have the issue, you can set

"laravel/framework": "5.4.* !=5.4.19",

in your composer.json, to be sure to avoid it.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

YannPl picture YannPl  路  3Comments

gabriellimo picture gabriellimo  路  3Comments

lzp819739483 picture lzp819739483  路  3Comments

felixsanz picture felixsanz  路  3Comments

progmars picture progmars  路  3Comments