簡體   English   中英

Symfony CMF WebTestCase缺少phpcr:受管節點類型

[英]Symfony CMF WebTestCase missing phpcr:managed node type

我正在嘗試為集成在我們的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。 加載燈具時可以完成嗎?

編輯:我也嘗試使用自定義初始化程序加載此數據。 但是我得到了同樣的錯誤。

聽起來好像您缺少對命令register-system-node-types的調用來建立您在測試中使用的存儲庫。 該命令將注冊類型。

找到了可能的解決方案。 我自己在初始化程序中注冊了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';
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM