JMS Serializer not reading config when running PHPUnit tests in Symfony2
jmsserializerbundle, phpunit, serialization, symfony
Solution
JMSSerializer caches the configuration files the first time it reads them, you have to clear the test environment's cache with:
php app/console cache:clear -e test
Problem
I'm using JMSSerializer to create JSON responses for a Symfony2 project I'm working on and a trying to build unit tests for each response but I'm hitting variations of the below: ``` JMS\Serializer\Exception\RuntimeException: You must define a type for FooBundle\Entity\Bar::$name. ``` I'm using YML config for the serializer and it works perfectly when generating responses. ``` #src/FooBundle/Resources/config/serializer/Entity.Bar.yml FooBundle\Entity\Bar: exclusion_policy: none properties: id: exclude: true type: integer name: type: string ``` I wondered if I need to preload the config somehow and found this link: http://jmsyst.com/libs/serializer/master/configuration that says to configure a metadata path but also to include a file suffix: ``` $serializer = JMS\Serializer\SerializerBuilder::create() ->addMetadataDir($someDir) ->build(); ``` I've tried setting the config directory when generating the serializer in the unit test: ``` $serializer = SerializerBuilder::create()->addMetadataDir('path-to-dir')->build(); ``` But this didn't resolve the problem, I checked the documentation page again and it tells you to list a full path to a file "So, if you class would be named Vendor\Package\Foo, the metadata file would need to be located at $someDir/Vendor.Package.Foo.(xml|yml)." but trying get generates: ``` JMS\Serializer\Exception\InvalidArgumentException: The directory "path-to-file" does not exist. ``` Am I missing something obvious? Thanks Ben