首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >测试Typo3 v8 UnitTest测试ViewHelper时未配置数据库连接默认设置

测试Typo3 v8 UnitTest测试ViewHelper时未配置数据库连接默认设置
EN

Stack Overflow用户
提问于 2018-08-15 13:18:54
回答 2查看 865关注 0票数 1

我从Typo3 v7迁移到v8。我也有一些测试,工作良好,经过一些调整。然而,有一个测试仍然失败。

我有一个UnitTest,它测试ViewHelper,如果$this->templateVariableContainer->get('settings')中提供的值在ViewHelper中被正确处理。

我的遗嘱档案:

代码语言:javascript
复制
namespace SomeVendor\Extension\Tests\Unit\ViewHelper;

use Nimut\TestingFramework\TestCase\ViewHelperBaseTestcase;
use SomeVendor\Extension\ViewHelpers\ContactFormViewHelper;
use TYPO3\CMS\Fluid\Core\ViewHelper\TemplateVariableContainer;


class ContactFormTest extends ViewHelperBaseTestcase {

    /**
     * @var \PHPUnit_Framework_MockObject_MockObject
     */
    protected $viewHelper;

    protected function setUp() {

        parent::setUp();

        $mock = $this->getMockBuilder(ContactFormViewHelper::class);
        $mock->setMethods(['renderChildren']);

        $this->viewHelper = $mock->getMock();
        $this->injectDependenciesIntoViewHelper($this->viewHelper);
        $this->viewHelper->initializeArguments();
    }

    /**
     * @test
     */
    public function testExcludes() {

        $renderingMock = $this->getMockBuilder(\TYPO3\CMS\Fluid\Core\Rendering\RenderingContext::class);

        $templateVariableContainerMock = $this->getMockBuilder(TemplateVariableContainer::class);
        $templateVariableContainerMock
            ->getMock()
            ->method('get')
            ->withAnyParameters()
            ->willReturn([
                'exclude' => ['foo', 'bar']
                ]
            ]);

        $renderingMock
            ->getMock()
            ->method('getTemplateVariableContainer')
            ->willReturn($templateVariableContainerMock);

        $this->viewHelper->setRenderingContext($renderingMock);

        // foo, bar should be excluded in ViewHelper
        // and the array should only contain ['foz', 'baz']
        $resultsCleaned = [
            'foz', 'baz'
        ];

        $this->assertEquals($resultsCleaned, $this->viewHelper->render();
    }

}

经过测试的ViewHelper:

代码语言:javascript
复制
namespace SomeVendor\Extension\ViewHelpers;


class ContactFormViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper {

    const VALID_FIELDS = [
        'foo',
        'bar',
        'foz',
        'baz'
    ];


    /**
     *
     * @return array
     */
    public function render() {

        $retval = [];

        // get settings defined in TS Setup
        // comma separated, eg: foo,bar
        $settings = $this->templateVariableContainer->get('settings');

        if (isset($settings['excludes']) ) {
            $settings = preg_split('/,/', $settings['excludes']);

            if (is_array($settings) === false) {
                $settings = [];
            }

        } else {
            $settings = [];
        }


        // include exclude magic here
        // resulting array $retval contains only values which are NOT excluded

        return $retval;
    }
}

我的测试运行调用如下:

代码语言:javascript
复制
/var/www/html/vendor/bin/phpunit -c /var/www/html/vendor/nimut/testing-framework/res/Configuration/UnitTests.xml /var/www/html/typo3_app/typo3conf/ext/extension/Tests/Unit/ViewHelper/ContactFormTest.php

如果出现以下错误,此测试总是失败:

代码语言:javascript
复制
RuntimeException: The requested database connection named "Default" has not been configured.

为什么这里甚至需要数据库连接?因为缓存?它在Typo3 v7起了作用。

我的环境:

  • PHP 7.1.15
  • Typo3: 8.7.18
  • Nimut测试框架: 4.0
  • PHPUnit: 6.5.11
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-08-16 10:35:11

事实证明,我的迁移过程还不完全兼容Typo3 v8。由于方法getTemplateVariableContainer已弃用,这可能是数据库连接未初始化的根。

我按照以下方式更新了我的testfile,使用此配置,所有测试都是绿色的:

代码语言:javascript
复制
namespace SomeVendor\Extension\Tests\Unit\ViewHelper;

use Nimut\TestingFramework\TestCase\ViewHelperBaseTestcase;
use SomeVendor\Extension\ViewHelpers\ContactFormViewHelper;
// use TYPO3\CMS\Fluid\Core\Variables\CmsVariableProvider instead of TYPO3\CMS\Fluid\Core\ViewHelper\TemplateVariableContainer;
use TYPO3\CMS\Fluid\Core\Variables\CmsVariableProvider;


class ContactFormTest extends ViewHelperBaseTestcase {

    /**
     * @var \PHPUnit_Framework_MockObject_MockObject
     */
    protected $viewHelper;

    protected function setUp() {

        parent::setUp();

        $mock = $this->getMockBuilder(ContactFormViewHelper::class);
        $mock->setMethods(['renderChildren']);

        $this->viewHelper = $mock->getMock();
        $this->injectDependenciesIntoViewHelper($this->viewHelper);
        $this->viewHelper->initializeArguments();
    }

    /**
     * @test
     */
    public function testExcludes() {
        // completly remove the setting of the templateVariableContainer through the renderingContext and set it directly through the setVariableProvider, don't forget to call injectDependenciesIntoViewHelper($this->viewHelper) afterwards

        $CMSvariableContainer = $this->getMockBuilder(CmsVariableProvider::class);
        $CMSvariableContainerMock = $CMSvariableContainer->getMock();
        $CMSvariableContainerMock
            ->method('get')
            ->withAnyParameters()
            ->willReturn([
                'exclude' => ['foo', 'bar']
            ]);

        $this->renderingContext->setVariableProvider($CMSvariableContainerMock);
        $this->injectDependenciesIntoViewHelper($this->viewHelper);

        // foo, bar should be excluded in ViewHelper
        // and the array should only contain ['foz', 'baz']
        $resultsCleaned = [
            'foz', 'baz'
        ];

        $this->assertEquals($resultsCleaned, $this->viewHelper->render();
    }

}
票数 0
EN

Stack Overflow用户

发布于 2018-08-15 14:15:21

在TYPO3版本8.1中,DB配置结构已经更改

对于在突破:#75454 - LocalConfiguration DB配置结构已更改上可用的这一突然变化,有一种变化

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51859505

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档