Testing 如何在测试上下文中使用LocalizationUtility
我正在尝试测试返回本地化错误消息的控制器操作。我正在使用静态Testing 如何在测试上下文中使用LocalizationUtility,testing,phpunit,typo3,functional-testing,typo3-7.6.x,Testing,Phpunit,Typo3,Functional Testing,Typo3 7.6.x,我正在尝试测试返回本地化错误消息的控制器操作。我正在使用静态TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate方法处理本地化错误消息。当我通过浏览器调用该操作时,它会起作用。但是当在测试上下文下调用控制器操作时,它总是返回NULL。似乎未正确加载localang.xml 为了使用本地化实用程序,我是否需要做一些准备工作 我的电话: /var/www/html/vendor/bin/phpunit -c /var/www/html/ve
TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate
方法处理本地化错误消息。当我通过浏览器调用该操作时,它会起作用。但是当在测试上下文下调用控制器操作时,它总是返回NULL
。似乎未正确加载localang.xml
为了使用本地化实用程序
,我是否需要做一些准备工作
我的电话:
/var/www/html/vendor/bin/phpunit -c /var/www/html/vendor/typo3/cms/typo3/sysext/core/Build/FunctionalTests.xml /var/www/html/typo3_app/typo3conf/ext/some_ext/Tests/Functional/Controller/SomeControllerTest.php
作为参考,我的测试类:
<?php
namespace SomeExt\Tests\Functional\Controller;
use SomeExt\Controller\SomeController;
use Nimut\TestingFramework\TestCase\FunctionalTestCase;
use TYPO3\CMS\Core\Utility\GeneralUtility;
/**
* Test class
*
*/
class SomeControllerTest extends FunctionalTestCase
{
/**
* @var SomeController
*/
protected $controller;
/**
* @var \TYPO3\CMS\Extbase\Mvc\Request
*/
protected $request;
/**
* @var \TYPO3\CMS\Fluid\View\TemplateView
*/
protected $view;
/**
* @var array
*/
protected $settings;
public function setUp() {
parent::setUp();
$OM = GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\ObjectManager::class);
$this->controller = $OM->get(SomeController::class);
$this->prepareController();
}
public function prepareController() {
$this->view = $this->getMock(\TYPO3\CMS\Fluid\View\TemplateView::class);
$this->controller->setView($this->view);
}
/**
* @test
*/
public function submitFormActionFailure() {
$submitData = [
['some_field1', null],
['some_field2', null],
['some_field3', null],
['some_field4', null]
];
$failRetval = [
'success' => false,
'errors' => [
[
'field' => 'field1',
'message' => 'This field is mandatory'
],
[
'field' => 'field2',
'message' => 'This field is mandatory'
],
[
'field' => 'field3',
'message' => 'This field is mandatory'
],
[
'field' => 'field4',
'message' => 'This field is mandatory'
]
]
];
$request = $this->getMock(\TYPO3\CMS\Extbase\Mvc\Request::class);
$request->method('getArgument')
->will(
$this->returnValueMap($submitData)
);
$this->controller->setRequest($request);
/**
* will always fail because message is always null
*
* assigned value:
* [
* 'success' => false,
* 'errors' => [
* [
* 'field' => 'field1',
* 'message' => NULL
* ],
* [
* 'field' => 'field2',
* 'message' => NULL
* ],
* [
* 'field' => 'field3',
* 'message' => NULL
* ],
* [
* 'field' => 'field4',
* 'message' => NULL
* ]
* ]
* ]
*/
$this->view->expects($this->at(0))
->method('assign')
->with('retval', $failRetval);
$this->controller->submitFormAction();
}
}
在这种情况下,您需要做的就是将扩展密钥添加到,以确保您的翻译文件像往常一样加载。太好了,成功了!我必须先清理typo3temp/目录。另一个值得注意的资源是: