I'm using the Symfony web test case to test my endpoints usually. In this case I just wanted to test a single repository call to persist a new entity so I wanted to avoid using the web test client.
I tested using the web test client first and POSTing to the endpoint will create the entity in the SQLite database using the repository. However if I get the repository class in my test and directly call the creation method it fails on persist()
with the exception:
Entity of type MyEntity is missing an assigned ID for field 'id'. The identifier generation strategy for this entity requires the ID field to be populated before EntityManager#persist() is called. If you want automatically generated identifiers instead you need to adjust the metadata mapping accordingly.
I've updated the schema using
app/console doctrine:schema:update --force --env=test
But app/console doctrine:schema:validate --env=test
still tells me the database is not up to date. This would suggest a problem with the mapping, but if there was a problem with the mapping why would it work in production (which uses MySql) or indeed why would it work if I call it using the web test client?
The doctrine mapping for the ID is:
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="IDENTITY")
*
* @var int
*/
private $id;