带有FOSUserBundle的Symfony2数据夹具

时间:2013-03-24 12:38:01

标签: symfony doctrine fosuserbundle

我想为我的项目创建一个som fixtures进行测试。我现在在命令行脚本中执行此操作,但是我发现了Doctrine中的DataFixtures包并想要测试它。 我有以下代码:

<?php
// src/Pan100/MoodLogBundle/DataFixtures/ORM/LoadUserData.php

namespace Acme\HelloBundle\DataFixtures\ORM;

use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\DependencyInjection\ContainerAware;

use Pan100\MoodLogBundle\Entity\Day;
use Pan100\MoodLogBundle\Entity\Medication;
use Pan100\MoodLogBundle\Entity\Trigger;

use FOS\UserBundle\Doctrine\UserManager;

class MockDataMaker extends ContainerAware
{
    /**
     * {@inheritDoc}
     */
    public function load(ObjectManager $manager)
    {
        $userManager = $container->get('fos_user.user_manager');
        //make some users
        $user = $userManager->createUser();
        $user->setUsername('John');
        $user->setEmail('john.doe@example.com');
        $user->setPlainPassword('passord');
        $userManager->updateUser($user);
    }

}

我明白了:

  [InvalidArgumentException]                                                   
  Could not find any fixtures to load in:                                      

  - /var/www/path/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/D  
  ataFixtures/ORM                                                              
  - /var/www/path/vendor/symfony/symfony/src/Symfony/Bundle/SecurityBundle/Da  
  taFixtures/ORM                                                               
  - /var/www/path/vendor/symfony/symfony/src/Symfony/Bundle/TwigBundle/DataFi  
  xtures/ORM                                                                   
  - /var/www/path/vendor/symfony/monolog-bundle/Symfony/Bundle/MonologBundle/  
  DataFixtures/ORM                                                             
  - /var/www/path/vendor/symfony/swiftmailer-bundle/Symfony/Bundle/Swiftmaile  
  rBundle/DataFixtures/ORM                                                     
  - /var/www/path/vendor/symfony/assetic-bundle/Symfony/Bundle/AsseticBundle/  
  DataFixtures/ORM                                                             
  - /var/www/path/vendor/doctrine/doctrine-bundle/Doctrine/Bundle/DoctrineBun  
  dle/DataFixtures/ORM                                                         
  - /var/www/path/vendor/sensio/framework-extra-bundle/Sensio/Bundle/Framewor  
  kExtraBundle/DataFixtures/ORM                                                
  - /var/www/path/vendor/jms/aop-bundle/JMS/AopBundle/DataFixtures/ORM         
  - /var/www/path/vendor/jms/di-extra-bundle/JMS/DiExtraBundle/DataFixtures/O  
  RM                                                                           
  - /var/www/path/vendor/jms/security-extra-bundle/JMS/SecurityExtraBundle/Da  
  taFixtures/ORM                                                               
  - /var/www/path/vendor/friendsofsymfony/user-bundle/FOS/UserBundle/DataFixt  
  ures/ORM                                                                     
  - /var/www/path/src/Pan100/MoodLogBundle/DataFixtures/ORM                    
  - /var/www/path/vendor/sonata-project/intl-bundle/Sonata/IntlBundle/DataFix  
  tures/ORM                                                                    
  - /var/www/path/vendor/ob/highcharts-bundle/Ob/HighchartsBundle/DataFixture  
  s/ORM                                                                        
  - /var/www/path/vendor/doctrine/doctrine-fixtures-bundle/Doctrine/Bundle/Fi  
  xturesBundle/DataFixtures/ORM                                                
  - /var/www/path/src/Acme/DemoBundle/DataFixtures/ORM                         
  - /var/www/path/vendor/symfony/symfony/src/Symfony/Bundle/WebProfilerBundle  
  /DataFixtures/ORM                                                            
  - /var/www/path/vendor/sensio/distribution-bundle/Sensio/Bundle/Distributio  
  nBundle/DataFixtures/ORM                                                     
  - /var/www/path/vendor/sensio/generator-bundle/Sensio/Bundle/GeneratorBundl  
  e/DataFixtures/ORM

我的问题是:如何使用FOSUserBundle在灯具中创建用户?

2 个答案:

答案 0 :(得分:6)

您的类必须扩展“AbstractFixture”并实现“ContainerAwareInterface”以访问usermanager服务! 如果你的班级没有扩展“AbstractFixture”,不是夹具;-) 在fosub和symfony中寻找不同的灯具接口和不同的容器接口以获得更多方法。 验证名称空间是否为复制和粘贴 萨登为我真的不可怕的英语! 例如:订购灯具

use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Doctrine\Common\Persistence\ObjectManager;
class LoadArticleFixture extends AbstractFixture implements OrderedFixtureInterface, ContainerAwareInterface
{
/**
 * @var ContainerInterface
 */
private $container;

/**
 * {@inheritDoc}
 */
public function setContainer(ContainerInterface $container = null)
{
    $this->container = $container;
}
public function load(ObjectManager $manager)
{
    $auteurs = $this->container->get('fos_user.user_manager')->findUsers();
    etc...
}

答案 1 :(得分:1)

您的源文件位置与您的命名空间不匹配:

src/Pan100/MoodLogBundle/DataFixtures/ORM/LoadUserData.php vs namespace Acme\HelloBundle\DataFixtures\ORM;

尝试将名称空间设置为Pan100\MoodLogBundle\DataFixtures\ORM

正如MolecularMan指出的那样,你也应该实施FixtureInterface