How to create tests w/Doctrine entities without persisting them (how to set id)

前端 未结 3 484
孤街浪徒
孤街浪徒 2021-02-04 08:41

I\'m working on tests for a Symfony2 project, and right now I\'m looking for a way to create tests involving entity objects without persisting them. The problem is: id

3条回答
  •  日久生厌
    2021-02-04 09:30

    You can configure doctrine to use an in-memory database in your app/config/config_test.yml.

    # app/config/config_test
    doctrine:
        dbal:
            driver: pdo_sqlite
            path: :memory:
            memory: true
    

    This speeds up the process and you can quickly persist some fixtures in the setUp() method that will have (auto-generated) id's after flushing... all without messing with your "real" database.

    You can find some inspiration in this answer and this blog post.

提交回复
热议问题