简体   繁体   中英

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

I migrated from Typo3 v7 to 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.

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:

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.

My environment:

  • PHP 7.1.15
  • Typo3: 8.7.18
  • Nimut Testing Framework: 4.0
  • PHPUnit: 6.5.11

The DB config structure has changed in TYPO3 Version 8.1

There is a changelog regarding this breaking change available at Breaking: #75454 - LocalConfiguration DB config structure has changed

It turned out that my migration process was not completly compatible to Typo3 v8 yet. Since the method getTemplateVariableContainer is deprecated this could be the root of that that the database connection is not initialized.

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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