Upcoming meetup at PHP Quebec where i will be presenting ways to go from STUPID to SOLID. Hope to see you there!
Today i hit a relatively simple problem where my tests would issue the same query twice and thus return the same result although the result should have been different. The problem is related to the use of the STOF/Doctrine-Bundle-Extension which adds a listener to add a condition to the query i build using the softdeleteable behavior. The problem with this is that if you query the first part of the query, it means that next time you try to get the same query, the query is not updated with the latest timeaware parameter and thus:
SELECT pi FROM PersonalizableItem pi WHERE token = :token
gets changed to
SELECT pi FROM PersonalizableItem pi WHERE token = :token AND (deletedAt is null or deletedAt <= 'some date')
This second query gets cached so the time in “some date” doesn’t change and thus, even if you deleted the item and try to query it again (for testing purposes in my case) then the result is incorrect and your test fails.
To fix, this, substitute the query cache currently used in your application with a VoidCache and this will prevent the query caching from occuring. Obviously, you don’t want and usually don’t need this in the production context, but in a test context, it is very valid and often necessary for integration like tests.
self::$entityManager = static::getSharedKernel()->getContainer()->get('doctrine.orm.default_entity_manager'); self::$entityManager->getConfiguration()->setQueryCacheImpl(new VoidCache());
Symfony is pretty new to me and i’m learning new stuff everyday. Building this api where i work made me learn that sometimes you may want to have the null serialized and sometimes not. So by default, let’s say you activate it, when you want to deactivate it on the fly, the best way is just to turn on/off the null serialization strategy.
IMO, it’s best to simply always serialize nulls as not all end users will be able to deserialize missing properties or not. If a property is not set, it should still be serialized by default but with a null. It’s a matter of taste i guess, but tecnically speaking, i’d always do that!