如何在测试上下文中使用 LocalizationUtility

How to use the LocalizationUtility in test context

我正在尝试测试 return 本地化错误消息的控制器操作。我正在为本地化错误消息使用静态 TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate 方法。当我通过浏览器调用操作时,它会起作用。但是当在测试上下文中调用控制器操作时,它总是 return NULL 。似乎 localang.xml 没有正确加载...

我需要做一些准备工作才能使用 LocalizationUtility 吗?

我的 phpunit 调用:

/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

作为参考,我的测试class:

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

    }
}

在这种情况下,您需要做的就是将您的扩展密钥添加到 $testExtensionsToLoad list,以确保您的翻译文件像往常一样加载。