JSON-object Fixture with AliceBundle

Viewed 701

I'm working on a PHP application with Symfony 4 for which I am developing unit tests.

I want to create fixtures to avoid conflicts with the database.
I use the AliceBundle but I have some problems.

This is my entity :

/**
 * @ORM\Table(name="book")
 * @ORM\Entity
 */
class Book
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**

     * @var Type
     *
     * @ORM\ManyToOne(targetEntity="App\Entity\Type", inversedBy="books")
     * @ORM\JoinColumn(nullable=false, onDelete="CASCADE")
     */
    private $type;

    /**
     * @var object
     *
     * @ImportPropertiesConstraint(groups={"propertiesValidation"})
     *
     * @ORM\Column(name="properties", type="json_document")
     */
    private $properties;



    (Getters and Setters)
}

The field properties depends on the field type and corresponds to the class NovelProperties.

In the database, this field is a json_document corresponding to the NovelProperties entity.
I do it with the bundle Doctrine JSON ODM.
https://github.com/dunglas/doctrine-json-odm

The NovelProperties entity is simple and contains only string and int.

This is my fixtures files :

book.yaml

App\Entity\Book:
    book_{1..9}:
        type: '@type_novel'
        properties: "{#type:'App\\Entity\\NovelProperties',pages:100,language:'english'}"

bookType.yaml

App\Entity\Type:
    type_novel:
        name: 'novel'
    type_manga:
        name: 'manga'

For properties value, I tried several things that don’t work:
- pass the object as an array (ERROR : must be an object, array given)
- pass the object as a string (ERROR : must be an object, string given)
- pass the object as a JSON object (ERROR : must be an object, string given)

I have tested all the syntaxes for everything.
I don't know what to do.

0 Answers
Related