【问题标题】:TYPO3\CMS\Core\Tests\UnitTestCase can't be loaded in extbase unit test filesTYPO3\CMS\Core\Tests\UnitTestCase 无法加载到 extbase 单元测试文件中
【发布时间】:2016-12-02 09:53:30
【问题描述】:

我正在使用 phpunit 对 extbase 扩展进行独立的单元测试。

这是我的 phpunt.xml 位于typo3conf/

<?xml version="1.0" encoding="UTF-8"?>
<phpunit colors="true">
    <testsuites>
        <testsuite name="Application Test Suite">
            <directory>./ext/test_extension/Tests/</directory>
        </testsuite>
    </testsuites>
</phpunit>

我的文件夹结构如下图

DummyControllerTest 文件在这里

<?php
namespace Ricky\TestExtension\Tests\Unit\Controller;
/***************************************************************
 *  Copyright notice
 *
 *  (c) 2016 Ricky Mathew <ricky.mk@pitsolutions.com>, Pits
 *              
 *  All rights reserved
 *
 *  This script is part of the TYPO3 project. The TYPO3 project is
 *  free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  The GNU General Public License can be found at
 *  http://www.gnu.org/copyleft/gpl.html.
 *
 *  This script is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  This copyright notice MUST APPEAR in all copies of the script!
 ***************************************************************/

/**
 * Test case for class Ricky\TestExtension\Controller\DummyController.
 *
 * @author Ricky Mathew <ricky.mk@pitsolutions.com>
 */
class DummyControllerTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
{

    /**
     * @var \Ricky\TestExtension\Controller\DummyController
     */
    protected $subject = NULL;

    public function setUp()
    {
        $this->subject = $this->getMock('Ricky\\TestExtension\\Controller\\DummyController', array('redirect', 'forward', 'addFlashMessage'), array(), '', FALSE);
    }

    public function tearDown()
    {
        unset($this->subject);
    }

    /**
     * @test
     */
    public function listActionFetchesAllDummiesFromRepositoryAndAssignsThemToView()
    {

        $allDummies = $this->getMock('TYPO3\\CMS\\Extbase\\Persistence\\ObjectStorage', array(), array(), '', FALSE);

        $dummyRepository = $this->getMock('Ricky\\TestExtension\\Domain\\Repository\\DummyRepository', array('findAll'), array(), '', FALSE);
        $dummyRepository->expects($this->once())->method('findAll')->will($this->returnValue($allDummies));
        $this->inject($this->subject, 'dummyRepository', $dummyRepository);

        $view = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\View\\ViewInterface');
        $view->expects($this->once())->method('assign')->with('dummies', $allDummies);
        $this->inject($this->subject, 'view', $view);

        $this->subject->listAction();
    }

    /**
     * @test
     */
    public function showActionAssignsTheGivenDummyToView()
    {
        $dummy = new \Ricky\TestExtension\Domain\Model\Dummy();

        $view = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\View\\ViewInterface');
        $this->inject($this->subject, 'view', $view);
        $view->expects($this->once())->method('assign')->with('dummy', $dummy);

        $this->subject->showAction($dummy);
    }
}

但是在通过命令行运行 phpunit 时会抛出

致命错误:在中找不到类 'TYPO3\CMS\Core\Tests\UnitTestCase' /opt/xampp/htdocs/typo3testpro/typo3conf/ext/test_extension/Tests/Unit/Controller/DummyControllerTest.php 第 37 行

为什么它不自动加载?我试图在我的控制器类中实例化 TYPO3\CMS\Core\Tests\UnitTestCase 只是为了测试,它在那里完美地自动加载。

【问题讨论】:

    标签: php phpunit typo3 extbase


    【解决方案1】:

    您需要在 PHPUnit 中引导自动加载(不仅适用于 TYPO3)。为此,请将属性bootstrap 添加到配置文件中的&lt;phpunit&gt; 元素。它是在测试之前执行的文件的路径,它应该设置自动加载。

    在 TYPO3 上下文中,您可以使用文件typo3/sysext/core/Build/UnitTestsBootstrap.php,它由 TYPO3 内核提供。

    如果你运行一个没有 TYPO3 的项目,你通常需要包含 composer 生成的自动加载文件,默认是文件vendor/autoload.php

    之后您的配置文件应如下所示:

    <phpunit
        colors="true"
        bootstrap="../typo3/sysext/core/Build/UnitTestsBootstrap.php"
    >
        <testsuites>
            <testsuite name="Application Test Suite">
                <directory>./ext/test_extension/Tests/</directory>
            </testsuite>
        </testsuites>
    </phpunit>
    

    【讨论】:

    • 我按照你说的做了。然后我在命令行中收到以下消息“无法确定进入脚本的路径。请检查您的路径或设置环境变量 'TYPO3_PATH_WEB'”@Jost
    • 不知道为什么会发生这种情况,该设置适用于我的 TYPO3 实例 - 你在测试中做什么?它们是单元测试,还是更多的东西?
    • @RickyMathew 我遇到了同样的问题,在启用作曲家的 TYPO3 中运行功能测试,定义了 web-dir 并加载了 typo3-console 扩展名。通过简单地重载 TYPO3_PATH_WEB 以指向安装的根目录来修复它 - 而不是在测试执行期间指向真实的 Web 路径(由 web-dir 定义),如下所示:TYPO3_PATH_WEB=/home/my_project ./bin/phpunit -c FunctionalTests.xml
    猜你喜欢
    • 2016-05-09
    • 1970-01-01
    • 1970-01-01
    • 2019-03-13
    • 2020-01-02
    • 1970-01-01
    • 1970-01-01
    • 2015-03-24
    • 1970-01-01
    相关资源
    最近更新 更多