Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unit testing 在Symfony2中测试用户的可指责行为_Unit Testing_Symfony_Doctrine Orm_Phpunit_Stofdoctrineextensions - Fatal编程技术网

Unit testing 在Symfony2中测试用户的可指责行为

Unit testing 在Symfony2中测试用户的可指责行为,unit-testing,symfony,doctrine-orm,phpunit,stofdoctrineextensions,Unit Testing,Symfony,Doctrine Orm,Phpunit,Stofdoctrineextensions,我正在为我的实体帖子写一个测试,在那里我使用了来自的可指责行为。但无论我做什么,总有一个错误: PDOException:SQLSTATE[23000]:完整性约束冲突:1048列“创建人”不能为null 我的博士后课程的一部分: namespace My\BlogBundle\Entity; use Doctrine\ORM\Mapping as ORM; use Gedmo\Mapping\Annotation as Gedmo; use Symfony\Component\Validat

我正在为我的实体帖子写一个测试,在那里我使用了来自的可指责行为。但无论我做什么,总有一个错误:

PDOException:SQLSTATE[23000]:完整性约束冲突:1048列“创建人”不能为null

我的博士后课程的一部分:

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;

    /* ... */
}
我的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();
    }
}
当我登录时,代码在real controller中运行良好,在我的测试数据库中有一个Tester用户(在另一个测试中,我正在测试这个帐户,这是可以的),但我无法说服可责备的人在测试中使用这个帐户

第一堂课是以答案为基础的