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

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

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

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

我的测试文件:

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:

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

我的测试运行调用如下:

/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

此测试总是失败并出现以下错误:

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

为什么这里还需要数据库连接?因为缓存?它在 Typo3 v7 中有效。

我的环境:

TYPO3 8.1 版中的数据库配置结构已更改

Breaking: #75454 - LocalConfiguration DB config structure has changed

提供了有关此重大更改的更改日志

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

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

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

}