在Symfony2中测试用户的可责备行为

时间:2013-09-04 14:52:09

标签: unit-testing symfony doctrine-orm phpunit stofdoctrineextensions

我正在为我的实体Post编写一个测试,我在StofDoctrineExtensionsBundle使用了可行的行为。但无论我做什么,总会有一个错误:

  

PDOException:SQLSTATE [23000]:完整性约束违规:1048列'created_by'不能为空

我的Post课程的一部分:

namespace My\BlogBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * Post
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="My\BlogBundle\Entity\PostRepository")
 * @ORM\HasLifecycleCallbacks
 * @ORM\ChangeTrackingPolicy("DEFERRED_EXPLICIT")
 */
class Post
{
    /**
     * @Gedmo\Blameable(on="create")
     * @ORM\ManyToOne(targetEntity="My\UserBundle\Entity\User", inversedBy="posts")
     * @ORM\JoinColumn(name="created_by", referencedColumnName="id", nullable=false)
     */
    private $createdBy;

    /* ... */
}

我的PostTest课程的1个版本:

namespace My\BlogBundle\Tests\Entity;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;

class PostTest extends WebTestCase
{
    /**
     * @var \Doctrine\ORM\EntityManager
     */
    private $em;

    public function setUp()
    {
        static::$kernel = static::createKernel();
        static::$kernel->boot();
        $this->em = static::$kernel->getContainer()
            ->get('doctrine')
            ->getManager()
        ;
    }

    protected function loginAs($client, $username) {
        $container = $client->getContainer();
        $doctrine = $container->get('doctrine');

        $user = $this->loadUser($doctrine, $username);

        // First Parameter is the actual user object.
        // Change 'main' to whatever your firewall is called in security.yml
        $container->get('security.context')->setToken(
            new UsernamePasswordToken(
                $user, null, 'main', $user->getRoles()
            )
        );
    }

    private function loadUser($doctrine, $username) {
        // Assumes User entity implements UserInterface
        return $doctrine
                ->getRepository('MyUserBundle:User')
                ->findOneByUsername($username);
    }

    public function testAddEntity() {
        $this->loginAs(static::createClient(), 'Tester');

        $newEntity = new \My\BlogBundle\Entity\Post;
        $newEntity->setTitle('Test Add Entity');
        $this->em->persist($newEntity);

        $this->em->flush(); /* here is an error */
    }

    protected function tearDown()
    {
        parent::tearDown();
        $this->em->close();
    }
}

第二名:

namespace My\BlogBundle\Tests\Entity;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class PostTest extends WebTestCase
{
    /**
     * @var \Doctrine\ORM\EntityManager
     */
    private $em;

    public function setUp()
    {
        static::$kernel = static::createKernel();
        static::$kernel->boot();
        $this->em = static::$kernel->getContainer()
            ->get('doctrine')
            ->getManager()
        ;
    }

    public function testAddEntity() {
        $client = static::createClient(array(), array(
            'PHP_AUTH_USER' => 'Tester',
            'PHP_AUTH_PW'   => '1234',
        ));

        $newEntity = new \My\BlogBundle\Entity\Post;
        $newEntity->setTitle('Test Add Entity');
        $this->em->persist($newEntity);

        $this->em->flush(); /* here is an error */
    }

    protected function tearDown()
    {
        parent::tearDown();
        $this->em->close();
    }
}

当我登录时,代码在真实控制器中工作正常,我的测试数据库中有Tester用户(在另一个测试中我正在测试此帐户并且没关系),但我无法说服可以使用这个帐号在测试中。

第一个PostTest课程基于this答案。

0 个答案:

没有答案