我正在使用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时它会抛出
致命错误:Class&#39; TYPO3 \ CMS \ Core \ Tests \ UnitTestCase&#39;找不到 /opt/xampp/htdocs/typo3testpro/typo3conf/ext/test_extension/Tests/Unit/Controller/DummyControllerTest.php 第37行
为什么它不是自动加载?我试图在我的控制器类中实例化TYPO3\CMS\Core\Tests\UnitTestCase
仅用于测试,并且它在那里完全自动加载。
答案 0 :(得分:2)
您需要在PHPUnit中引导自动加载(不仅适用于TYPO3)。为此,请将属性bootstrap
添加到配置文件中的<phpunit>
元素。它是在测试之前执行的文件的路径,它应该设置自动加载。
在TYPO3上下文中,您可以使用文件typo3/sysext/core/Build/UnitTestsBootstrap.php
,它由TYPO3核心提供。
如果您正在运行没有TYPO3的项目,通常需要包含作曲家生成的自动加载文件,默认情况下它是文件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>