JMS Serializer does not read configuration when running PHPUnit tests in Symfony2

I am using the JMSSerializer to create JSON responses for the Symfony2 project I'm working on and trying to build unit tests for each answer, but I find variations below:

JMS\Serializer\Exception\RuntimeException: You must define a type for FooBundle\Entity\Bar::$name. 

I use the YML configuration for serializer and works great 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 was wondering if I need to pre-configure the configuration and find this link: http://jmsyst.com/libs/serializer/master/configuration , which says to configure the metadata path and also include the file suffix:

 $serializer = JMS\Serializer\SerializerBuilder::create() ->addMetadataDir($someDir) ->build(); 

I tried to set the config directory when creating the serializer in unit test:

 $serializer = SerializerBuilder::create()->addMetadataDir('path-to-dir')->build(); 

But this did not help to solve the problem, I checked the documentation page again and it will inform you to indicate the full path to the file

"So, if the class is called Vendor \ Package \ Foo, the metadata file will need to be found in $ someDir / Vendor.Package.Foo. (Xml | yml)."

but the get attempt generates:

 JMS\Serializer\Exception\InvalidArgumentException: The directory "path-to-file" does not exist. 

Am I missing something obvious?

thanks

Ben

+4
source share
1 answer

JMSSerializer caches configuration files the first time you read them, you need to clear the test environment cache with:

 php app/console cache:clear -e test 
+7
source

All Articles