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

时间:2018-07-07 13:13:00

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

我正在尝试测试返回本地化错误消息的控制器操作。我正在对本地化的错误消息使用静态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();

    }
}

1 个答案:

答案 0 :(得分:1)

在这种情况下,您要做的就是将扩展名添加到$testExtensionsToLoad list,以确保您的翻译文件照常加载。