Symfony / Doctrine UnitTests with SQLite memory DB

喜夏-厌秋 提交于 2019-12-03 12:58:47
melvin

I never used in memory sqlite database but for testing i do use a sqlite database that is saved.

For that you should add

# app/config/config_test.yml
doctrine:
    dbal:
        default_connection: default
        connections:
            default:
                driver:   pdo_sqlite
                path:     %kernel.cache_dir%/test.db

To your test config (for me config_test.yml)

You should be able to change this to in memory according to the documentation

http://docs.doctrine-project.org/projects/doctrine- dbal/en/latest/reference/configuration.html#pdo-sqlite

memory (boolean): True if the SQLite database should be in-memory (non-persistent). Mutually exclusive with path. path takes precedence.

So the config should then be

# app/config/config_test.yml
doctrine:
    dbal:
        default_connection: default
        connections:
            default:
                driver:   pdo_sqlite
                memory:   true

You can easily create entitymanager instance your self. Check this helper class You probably don't have it in symfony 2 but you can use the idea to build your own base testCaseClass and use it when needed. This even does not require to boot kernel.

The accepted answer didn't work for me, because I was using the URL to specify the connection parameters, and althogh I was setting the driver to pdo_sqliteand memoryto true, Doctrine was still using the url parameter from the root configuration, so I had to overwrite it.

#api/config/packages/test/doctrine.yaml
doctrine:
  dbal:
    url: "sqlite:///:memory:"
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!