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

Tests\TestCase requests will show wasRecentlyCreated = true in models generated before the simulated request. #23270

Closed
stratease opened this issue Feb 23, 2018 · 7 comments

Comments

@stratease
Copy link

stratease commented Feb 23, 2018

  • Laravel Version: 5.5.27
  • PHP Version: 7.0.22-0ubuntu0.16.04.1
  • Database Driver & Version: MongoDB v3.3

Description:

In the feature test Tests\TestCase, Eloquent models that were created during the mocking of a database will retain the wasRecentlyCreated property when a request is issued to the same model using the TestCase::json() method. In effect, not simulating an "update" to an existing database entry, but showing as just created in that request.

Steps To Reproduce:

In a feature TestCase::setup() generate a new model, and utilize it in a test making a request to an endpoint that accesses that model. In the controller inspect the Illuminate\Database\Eloquent\Model::$wasRecentlyCreated property, it will be bool true.

I uncovered it when I saw updates returning a 201 from a Resource handler.

I'm curious if wasRecentlyCreated should be reset in the Model::refresh() method?

* Edit - this may only relate to the User model, I have not tested on others.*

@devcircus
Copy link
Contributor

Similar to #2110. Still seems like the framework should be able to handle this more appropriately, but looks like calling "fresh" before using the instance, should be enough.

@stratease
Copy link
Author

@devcircus you mean Model::refresh() is working, or that it should be updated to reset the wasRecentlyCreated property?

I think you tagged the wrong issue btw.

@devcircus
Copy link
Contributor

Ahhh I did. The other issue said you could call $model->fresh(). Seems like refresh would do the same but haven't looked at the code. I'll track down the correct issue.

@devcircus
Copy link
Contributor

#21107

@stratease
Copy link
Author

Yup, that's the same issue I'm seeing. Shall I continue in that thread?

@devcircus
Copy link
Contributor

I'd stay here since yours is still open.

@tillkruss
Copy link
Collaborator

It's not a bug, but please feel free to submit a PR for this to v5.7.

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