Symfony CMF WebTestCase缺少phpcr:托管节点类型

时间:2018-02-20 10:11:34

标签: php symfony phpunit symfony-cmf doctrine-phpcr

我正在尝试为我们的Web应用程序中集成的简单博客编写一些WebTestCases。

其中一个测试用例如下所示:

public function testNewSubmitAction() 
{
    //load necessary data & login as admin
    $fixtureRepo = $this->loadFixtures([LoadUserData::class])->getReferenceRepository();

    $this->loadFixtures([LoadBlogData::class], null, 'doctrine_phpcr');
    $this->loginAs($customer = $fixtureRepo->getReference(LoadUserData::ADMIN_ACCOUNT), $this->getContainer()->getParameter('firewall.name'));

    $client = static::makeClient();

    $crawler = $client->request("GET", "/content/blog/new");

    $form = $crawler->selectButton('Erstellen')->form();

    $client->submit($form, [
        'blog[title]' => 'Testblog',
    ]);

    $this->assertTrue($client->getResponse()->isRedirect());
}

LoadBlogData类有一个带有此内容的加载方法:

public function load(ObjectManager $manager)
{
    parent::init($manager);
    NodeHelper::createPath($this->session, '/cms/routes/blog/de');
    NodeHelper::createPath($this->session, '/cms/routes/blog/fr');
    NodeHelper::createPath($this->session, '/cms/routes/blog/it');

    NodeHelper::createPath($this->session, '/cms/routes/categories/de');
    NodeHelper::createPath($this->session, '/cms/routes/categories/fr');
    NodeHelper::createPath($this->session, '/cms/routes/categories/it');

    NodeHelper::createPath($this->session, '/cms/pages/blog');

    NodeHelper::createPath($this->session, '/cms/content/blog');

    NodeHelper::createPath($this->session, '/cms/categories');

    $this->createBlog($manager);

    $manager->flush();
}

createBlog方法应该创建一个博客条目。

private function createBlog(ObjectManager $manager) 
{
    $blog = new Blog();
    $blog->setTitle('Test');
    $blog->setName('test');
    $blog->setCreatedAt(new \DateTime);
    $blog->setUpdatedAt(new \DateTime);
    $blog->setCreatedBy('Tester');
    $blog->setParentDocument($manager->find(null, '/cms/pages/blog'));

    $manager->persist($blog);
}

这就是失败的地方。运行测试时,我收到错误消息“首先注册phpcr:被管节点类型。”。

你知道如何解决这个问题吗?

我知道我必须首先初始化PHPCR。这可以在加载灯具时完成吗?

编辑:我也尝试使用自定义初始化程序加载此数据。但是我得到了同样的错误。

2 个答案:

答案 0 :(得分:0)

这听起来好像缺少对命令register-system-node-types的调用来设置您在测试中使用的存储库。该命令将注册该类型。

答案 1 :(得分:-1)

找到了可能的解决方案。我在我的初始化程序中注册了phpcr:managed节点类型。 这是我的完整初始化类。

<?php

namespace ContentBundle\Initializer;


use Doctrine\Bundle\PHPCRBundle\Initializer\InitializerInterface;
use Doctrine\Bundle\PHPCRBundle\ManagerRegistry;
use PHPCR\Util\NodeHelper;
use Doctrine\ODM\PHPCR\Translation\Translation;

class BlogInitializer implements InitializerInterface
{
    private $basePath;

    private $phpcrNamespace = 'phpcr';
    private $phpcrNamespaceUri = 'http://www.doctrine-project.org/projects/phpcr_odm';
    private $localeNamespace = Translation::LOCALE_NAMESPACE;
    private $localeNamespaceUri = Translation::LOCALE_NAMESPACE_URI;

    /**
     * BlogInitializer constructor.
     * @param string $basePath
     */
    public function __construct($basePath = '/cms/pages/blog')
    {
        $this->basePath = $basePath;
    }

    /**
     * @param ManagerRegistry $registry
     */
    public function init(ManagerRegistry $registry)
    {
        $dm = $registry->getManager();
        if ($dm->find(null, $this->basePath)) {
            return;
        }

        // register phpcr:managed node type
        $cnd = <<<CND
// register phpcr_locale namespace
<$this->localeNamespace='$this->localeNamespaceUri'>
// register phpcr namespace
<$this->phpcrNamespace='$this->phpcrNamespaceUri'>
[phpcr:managed]
mixin
- phpcr:class (STRING)
- phpcr:classparents (STRING) multiple
CND
        ;
        $session = $registry->getConnection();
        $ntm = $session->getWorkspace()->getNodeTypeManager();
        $ntm->registerNodeTypesCnd($cnd, true);

        // create basic paths in repository
        NodeHelper::createPath($session, '/cms/routes/blog/de');
        NodeHelper::createPath($session, '/cms/routes/blog/fr');
        NodeHelper::createPath($session, '/cms/routes/blog/it');

        NodeHelper::createPath($session, '/cms/routes/categories/de');
        NodeHelper::createPath($session, '/cms/routes/categories/fr');
        NodeHelper::createPath($session, '/cms/routes/categories/it');

        NodeHelper::createPath($session, '/cms/pages/blog');

        NodeHelper::createPath($session, '/cms/content/blog');

        NodeHelper::createPath($session, '/cms/categories');

        $session->save();
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'Blog Initializer';
    }
}