简体   繁体   English

测试ViewHelper时,Typo3 v8 UnitTest中未配置数据库连接默认值

[英]Database Connection default not configured in Typo3 v8 UnitTest when testing ViewHelper

I migrated from Typo3 v7 to v8. 我从Typo3 v7迁移到v8。 I also have some tests which work fine after some adjustments. 我也进行了一些测试,经过一些调整,这些测试仍然可以正常工作。 However, one test still fails. 但是,一项测试仍然失败。

I have a UnitTest which tests a ViewHelper, if the provided values in $this->templateVariableContainer->get('settings') are properly processed in the ViewHelper. 如果在ViewHelper中正确处理了$this->templateVariableContainer->get('settings')中提供的值,则我有一个UnitTest来测试ViewHelper。

My testfile: 我的测试文件:

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();
    }

}

The ViewHelper which is tested: 经过测试的ViewHelper:

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;
    }
}

My test run call as follows: 我的测试运行调用如下:

/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

This test always fails with the following error: 该测试始终失败,并出现以下错误:

RuntimeException: The requested database connection named "Default" has not been configured.

Why even is a database connection needed here? 为什么这里甚至需要数据库连接? Because of the cache? 因为缓存? It worked in Typo3 v7. 它在Typo3 v7中起作用。

My environment: 我的环境:

  • PHP 7.1.15 PHP 7.1.15
  • Typo3: 8.7.18 错别字3:8.7.18
  • Nimut Testing Framework: 4.0 Nimut测试框架:4.0
  • PHPUnit: 6.5.11 PHPUnit:6.5.11

The DB config structure has changed in TYPO3 Version 8.1 TYPO3版本8.1中的数据库配置结构已更改

There is a changelog regarding this breaking change available at Breaking: #75454 - LocalConfiguration DB config structure has changed Breaking(中断)处有关于此重大更改的更改日志:#75454-LocalConfiguration DB配置结构已更改

It turned out that my migration process was not completly compatible to Typo3 v8 yet. 原来,我的迁移过程尚未完全与Typo3 v8兼容。 Since the method getTemplateVariableContainer is deprecated this could be the root of that that the database connection is not initialized. 由于不赞成使用 getTemplateVariableContainer方法, getTemplateVariableContainer这可能是未初始化数据库连接的根本原因。

I updated my testfile as follows, with this configuration all tests are green: 我更新了我的测试文件,如下所示,使用此配置,所有测试均为绿色:

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();
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM