[英]Database Connection default not configured in Typo3 v8 UnitTest when testing ViewHelper
我從Typo3 v7遷移到v8。 我也進行了一些測試,經過一些調整,這些測試仍然可以正常工作。 但是,一項測試仍然失敗。
如果在ViewHelper中正確處理了$this->templateVariableContainer->get('settings')
中提供的值,則我有一個UnitTest來測試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配置結構已更改
原來,我的遷移過程尚未完全與Typo3 v8兼容。 由於不贊成使用 getTemplateVariableContainer
方法, getTemplateVariableContainer
這可能是未初始化數據庫連接的根本原因。
我更新了我的測試文件,如下所示,使用此配置,所有測試均為綠色:
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.