My DoctrineFixturesBundle is installed and I can load fixture trough the command-line but , how can I load fixtures from my functional test ?
相关问题
- Symfony2 Set Controller in the kernelControllerEve
- Webpack Encore: cannot import local dependencies
- Render custom attribute KNP Menu
- Problems with cap deploy a symfony2 project, can
- Allow CORS on symfony 4
相关文章
- Symfony : Doctrine data fixture : how to handle la
- Symfony is linked to the wrong PHP version
- Symfony2: check whether session exists or not
- Is there a way to modify the entity mapping config
- symfony2 form choice and mongodb
- Symfony 3.1 and OneUpUploaderBundle + Blueimp = Up
- Symfony does not remove entity from collection
- Symfony: Inject object (not service) to service co
I just wanted to offer a slightly neater approach if you want to first purge your table of previous test data, e.g. if you are running your tests in phpunit.
This allows fixtures to be loaded, (you can push more into the add fixture method), and purge the tables before they are loaded. Also note MongoDB has the same option using MongoDBPurger, and MongoDBExecutor. Hope it helps someone
You can load the fixtures in your test's
setUp()
method as you can see in this question.You can use the code in the question but need to append
--append
to thedoctrine:fixtures:load
command in order to avoid the confirmation by the fixtures-bundle.The better solution is to have a look at the LiipFunctionalTestBundle which makes using data-fixtures easier.
As it was already mentioned it's recommended to use the LiipFunctionalTestBundle. Then you want extend your WebTestCase from the Liip\FunctionalTestBundle\Test\WebTestCase. This will allow to call
$this->loadFixtures()
which takes an array of fixtures as an argument.For more details I wrote a short blogpost: http://marcjuch.li/blog/2014/04/06/symfony2-rest-functional-testing-with-fixtures/
If you use symfony's
WebTestCase
, there's actually a very easy way to load your fixtures. Your fixture has to implement theFixtureInterface
; thus, you can call it'sload()
method directly in your test'ssetUp()
method. You just have to pass anEntityManager
to theload()
method, which can be aquired from the symfony container: