Skip to content

[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

Closed
wants to merge 1 commit into from
Closed

[DoctrineBridge] Add a DoctrineTestCase class that rollbacks after each test #19401

wants to merge 1 commit into from

Conversation

Cydonia7
Copy link
Contributor

Q A
Branch? master
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets no
License MIT
Doc PR not for now

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?

@Ma27
Copy link

Ma27 commented Jul 21, 2016

This is rather a new feature related to doctrine, isn't it?

@Cydonia7 Cydonia7 changed the title Add a DoctrineTestCase class that rollbacks after each test [DoctrineBundle] Add a DoctrineTestCase class that rollbacks after each test Jul 21, 2016
@Cydonia7 Cydonia7 changed the title [DoctrineBundle] Add a DoctrineTestCase class that rollbacks after each test [DoctrineBridge] Add a DoctrineTestCase class that rollbacks after each test Jul 21, 2016
@Cydonia7
Copy link
Contributor Author

I don't think so, Doctrine has no integration with PHPUnit so why should we not integrate this in DoctrineBridge?

@Ma27
Copy link

Ma27 commented Jul 21, 2016

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.

Doctrine has no integration with PHPUnit

can you elaborate that?

@Cydonia7
Copy link
Contributor Author

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.

@Ma27
Copy link

Ma27 commented Jul 21, 2016

@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.
In order to work with the purger on test level you might want to use sth. like the LiipFunctionalTestBundle which implemented its own fixture appliance in a generic test base class: https://github.com/liip/LiipFunctionalTestBundle/blob/master/Test/WebTestCase.php#L359

@javiereguiluz
Copy link
Member

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.

@linaori
Copy link
Contributor

linaori commented Jul 21, 2016

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.

@javiereguiluz
Copy link
Member

@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).

@linaori
Copy link
Contributor

linaori commented Jul 21, 2016

@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:

This behaviour is accomplished using bash. This means it will only work on systems supporting bash and having a mysql daemon or drop-in replacement installed.

@jakzal
Copy link
Contributor

jakzal commented Jul 25, 2016

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).

@javiereguiluz
Copy link
Member

@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?

@Ma27
Copy link

Ma27 commented Jul 25, 2016

@javiereguiluz I defintly agree with the point that "tests with fixtures is a must for any project".
But symfony suggests Doctrine for interaction with an RDBMS or suggests Swiftmailer for sending emails. So why not suggesting a proven testing tool for that such as LiipFunctionalTest which is tested and works?

@Cydonia7 Cydonia7 closed this Jul 26, 2016
@Cydonia7 Cydonia7 deleted the doctrine-test-transactions branch July 26, 2016 07:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants