2

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

PDOException:SQLSTATE [23000]:违反完整性约束:1048 列“created_by”不能为空

我的帖子课程的一部分:

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 用户(在另一个测试中,我正在测试这个帐户并且没关系),但我无法说服使用这个帐户在测试。

First PostTest 课程基于答案。

4

0 回答 0