-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[DoctrineBridge] Add a DoctrineTestCase class that rollbacks after each test #19401
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
Conversation
This is rather a new feature related to doctrine, isn't it? |
I don't think so, Doctrine has no integration with PHPUnit so why should we not integrate this in DoctrineBridge? |
As far as I know those bridges were built in order to "connect" symfony with other libraries (e.g. Swiftmailer, ProxyManager or Doctrine) and not to connect those libraries with other stuff.
can you elaborate that? |
I understand what you mean. I'm not sure if this belongs here then. What I meant is that I don't think Doctrine provides a base test class that allows for this kind of behavior. |
@Cydonia7 I understand your point now, but I still don't think that an integration layer for symphony is the right place for that. Furthermore I don't think whether I'd use this: in case of changes I'd simply use the ORMPurger which will be executed before every single fixture appliance. Your implementation has the problem that nothing will be written to the database level which means that everything is in the UnitOfWork, but DQL queries will produce invalid resultsets as nothing is really in the database. |
The proposed feature is nice ... but I feel like it's missing something: how do you create the database before starting to create and rollback transactions. In some projects I use a strategy similar to LiipFunctionalTest: I run some console commands to create theSQLite test database and populate it with fixtures ... and then I make a backup copy of that database (which is just a file) and overwrite the database before running each test (see details). The question is: should this PR provide a feature to create the test database before running the first test too ... or would that be a bad idea? Thanks. |
How do you test code with multiple entity managers? I'm starting to see a nasty extending pattern here, I don't know if that's the right way to do it. To be honest, I don't think this should be in the core of symfony on its own, it belongs more to a test package. To me this just looks like a hack tbh. |
@iltar I don't know if this should go in the core or not ... but this feature has been asked for several times and even the oldest pending issue in symfony-docs repository is related to this: symfony/symfony-docs#480 Personally this is something I'd use in all my projects (at the moment I use LiipFunctionalTest bundle sometimes and a custom solution other times). |
@javiereguiluz Hence I said "not on its own". I don't think in its current state it brings a lot of value or usefulness, especially not if you use multiple entity managers. I think this would be a perfect candidate of a doctrine-phpunit-bridge of some sort if you want to do it somewhat like this. Hostnet started developing this package (for our specific case): https://github.com/hostnet/database-test-lib. Internally we use this to hook up to repository tests to generate a database per test and throw it away afterwards. I do have to note:
|
I'm 👎 for adding this into the core. Rolling back transactions in tests is just one of many approaches you could take. I wouldn't like Symfony to suggest this one's good for most projects by adding a test case like this to the core. Therefore I think class like this belongs to 3rd party extension (like the mentioned LiipFunctionalTest). |
@jakzal I can't imagine a real project without tests or fixtures. That's why, in my opinion, tests with fixtures is a must for any project. What could Symfony do to help us here? |
@javiereguiluz I defintly agree with the point that "tests with fixtures is a must for any project". |
Usually, with Symfony, people use fixtures that are loaded at the beginning of the test suite. This can sometimes lead to problems after a lot of modifications have been made to the database during the test suite.
Laravel uses a trait that rollbacks after each test to tackle this problem. This is an attempt to add this to Symfony.
In order to use this, people can create a Kernel in their setUp method and set the created entity manager on the class. I'm pretty sure there should be a better way to do that but I'm still a beginner in contributing to Symfony.
What do you think?