【问题标题】:How to use the LocalizationUtility in test context如何在测试上下文中使用 LocalizationUtility
【发布时间】:2018-12-15 20:10:10
【问题描述】:

我正在尝试测试返回本地化错误消息的控制器操作。我正在使用静态 TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate 方法来获取本地化错误消息。当我通过浏览器调用操作时,它可以工作。但是当在测试上下文中调用控制器操作时,它总是会返回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

供参考,我的测试类:

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

    }
}

【问题讨论】:

    标签: testing phpunit typo3 functional-testing typo3-7.6.x


    【解决方案1】:

    在这种情况下,您只需将扩展密钥添加到$testExtensionsToLoad list,这样可以确保您的翻译文件正常加载。

    【讨论】:

    • 太好了,成功了!我必须先清理typo3temp/ 目录。也是一个值得注意的资源:@​​987654322@
    猜你喜欢
    • 1970-01-01
    • 2018-08-12
    • 2021-07-25
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 2014-07-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多