Debugging database changes while testing
I love to use xdebug and I also love coding with unit tests. It saves me a lot of time clicking around in the backend. Both together are a powerful combination and you can watch your code doing stuff while running the test. Beside changes in the database - those are invisible while the test is running. After a pretty hard time I was told why and how to overcome it.
Currently I'm working on a task to upgrade fluent, SilverStripe's content translation package, from SS3 to SS4. Unfortunately the database structure changed a lot and it's not easy to think about all edge cases. So that's what a test is good for. Set up some fixtures with "old" fluent data, do somthing, test if everything went fine. I managed to add the old localised fields like "Title_en_US" to the database, without SS4 knowing about it. And while I was setting up fixtures, I was able to see the changes in the temporarly test db when I was taking a break on a breakpoint. Everything was fine and I was able to verify Rob Ingram's great migration code is working as expected.
Then I wanted to test published records. And I was not able to see my changes that should create and copy over the old translated values to the live or versions table. But the test told me, the page is already published. Didn't see it in the database. Am I blind?
A hot summer weekend can be long, when noone is on slack who can explain deep details. Until Scopey wrote:
The tests all run in transactions by default. This would prevent being able to gather detail with PHPMyAdmin because the data isn't actually changed for any other connection. You can choose to disable transactions while you debug though - just whack a protected $uses_transactions = false; in your test file.
So SilverStripe puts each test in a transaction and rolls back the database after that for speeding up tests. That's a great feature, tests cannot be fast enough and DB slows them down a lot. But sometimes, you know, when the breakpoint is slowing down the test and you want to watch the records change, one by one, sometimes it's good to know the switch to turn this optimisation off.