TYPO3\CMS\Core\Tests\UnitTestCase 无法加载到 extbase 单元测试文件中

TYPO3\CMS\Core\Tests\UnitTestCase can't be loaded in extbase unit test files

我正在使用 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 上通过命令行抛出

Fatal error: Class 'TYPO3\CMS\Core\Tests\UnitTestCase' not found in /opt/xampp/htdocs/typo3testpro/typo3conf/ext/test_extension/Tests/Unit/Controller/DummyControllerTest.php on line 37

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

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

在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>