Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JMS Serializer not reading config when running PHPUnit tests in Symfony2

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

like image 472
benrcole Avatar asked Mar 24 '23 08:03

benrcole


1 Answers

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
like image 102
K. Norbert Avatar answered Apr 06 '23 12:04

K. Norbert