-
Notifications
You must be signed in to change notification settings - Fork 13
Doctrine2: grabEntityFromRepository()
causes invalid entity to be persisted
#26
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
Comments
(Note: What I'm trying to say is the following: this is a complex issue spread across several systems (ORM, Symfony, Codeception), and in my opinion only ORM does decent job here. Both Symfony and Codeception use somewhat dirty tricks. Your code (not blaming you, as you seem to follow what Symfony docs say) also implicitly 'hopes' for request to die and to discard invalid entity in that way. Which might not always be true, e.g. in case of this functional test, and also maybe if you use something like I agree that removing implicit Another (much more complex, but probably more correct) solution would be decoupling entity from form data object. Entity must be valid at all times, and |
@DavertMik, it seems you're the one who added |
Another solution would be detaching entity if validation fails -- |
This issue just caused me some trouble again ;-) I even suggested a (bogus) improvement for Symfony, before realizing that it's in fact Codeception that is magically saving my entities to the database; see symfony/symfony#7828 (comment) So what can we do? I see those options:
Before discussing other options, let me show you how I just fixed it in my project: "Inject" the repository like this in $this->myRepository = $I->grabService('App\Repository\MyRepository'); ...and then go with Doctrine's built-in functions (like e.g. This workaround is so easy and adds so little overhead that I'm asking a fundamental question: What's the real benefit of If the answer is to keep
Bottom line: This is a serious issue, since it leads to false test results. I think it should be fixed now. @alexkunin, @Naktibalda - what do you say? |
I now finally added what I recommended above to this module's docs: https://codeception.com/docs/modules/Doctrine2#Grabbing-Entities-with-Symfony |
In short: When editing an existing entity with a Symfony form and making it invalid, calling
$I->grabEntityFromRepository()
in a functional tests causes the (invalid!) entity to get persisted to the database.Sample repository to reproduce: https://github.com/ThomasLandauer/codeception_issue_5439
Instructions:
/data.sqlite
. Doublecheck that user ID 1's name is NULL./public
withphp -S localhost:8000
vendor/bin/codecept run functional UserCest
. The test does the same as you just did in the browser. Even the error message is present, see https://github.com/ThomasLandauer/codeception_issue_5439/blob/master/tests/functional/UserCest.php#L24The bug only appears if the form is editing an existing user (not creating a new one).
This might be related to https://github.com/Codeception/Codeception/issues/4804 , but it's worse, since it doesn't just flush what is "ready" to get flushed, but performs a full
persist
on its own.The text was updated successfully, but these errors were encountered: