致命错误无法声明类,因为该名称已被使用

时间:2018-08-04 09:41:08

标签: php unit-testing testing phpunit phpunit-testing

我正在尝试设置phpunit并将其用于测试。这是我第一次使用此功能,因此我可能会丢失一些东西。通过搜索看来,其他人也遇到了这个问题,但是答案对我而言没有用。我试过调整名称空间,并在测试所调用的类中检查require / includes,但是这些都没有改变。

我有一个与以下相似的课程:

<?php

namespace TM470_project\Test\php;

use \vendor\Autoload;

class myClassTest extends \PHPUnit\Framework\TestCase
{
     protected function setUP()
     {
         $temp = $this -> getMockBuilder('\My_project\php\myClassScript')
                       -> setConstructorArgs(
                          array(
                              "key" => "value",
                              "key" => "value",
                              "key" => "value"
                          ))
                       -> getMock();
 }

protected function tearDown()
{
    $temp = null;
}

public function removeTest()
{

}

当我尝试运行phpunit时,响应如下:

Fatal error: Cannot declare class myClass, because the name is already in use     in /Library/WebServer/Documents/mysites/My_project/php/myClass.php on line 3

Call Stack:
    0.0003     356720   1. {main}() /Library/WebServer/Documents/mysites/vendor/phpunit/phpunit/phpunit:0
    0.0032     727752   2. PHPUnit\TextUI\Command::main() /Library/WebServer/Documents/mysites/vendor/phpunit/phpunit/phpunit:53
    0.0032     727864   3. PHPUnit\TextUI\Command->run() /Library/WebServer/Documents/mysites/vendor/phpunit/phpunit/src/TextUI/Command.php:148
    0.0171    2257584   4. PHPUnit\TextUI\TestRunner->doRun() /Library/WebServer/Documents/mysites/vendor/phpunit/phpunit/src/TextUI/Command.php:195
    0.0230    2410320   5. PHPUnit\Framework\TestSuite->run() /Library/WebServer/Documents/mysites/vendor/phpunit/phpunit/src/TextUI/TestRunner.php:546
    0.0276    2417088   6. PHPUnit\Framework\TestCase->run() /Library/WebServer/Documents/mysites/vendor/phpunit/phpunit/src/Framework/TestSuite.php:755
    0.0276    2417088   7. PHPUnit\Framework\TestResult->run() /Library/WebServer/Documents/mysites/vendor/phpunit/phpunit/src/Framework/TestCase.php:894
    0.0281    2439208   8. PHPUnit\Framework\TestCase->runBare() /Library/WebServer/Documents/mysites/vendor/phpunit/phpunit/src/Framework/TestResult.php:698
    0.0283    2455896   9. vendor\AutoLoad\mortgageTest->setUP() /Library/WebServer/Documents/mysites/vendor/phpunit/phpunit/src/Framework/TestCase.php:935
    0.0300    2633496  10. PHPUnit\Framework\MockObject\MockBuilder->getMock() /Library/WebServer/Documents/mysites/TM470_Project/Test/php/myTest.php:61
    0.0300    2633496  11. PHPUnit\Framework\MockObject\Generator->getMock() /Library/WebServer/Documents/mysites/vendor/phpunit/phpunit-mock-objects/src/MockBuilder.php:118
    0.0301    2633496  12. PHPUnit\Framework\MockObject\Generator->generate() /Library/WebServer/Documents/mysites/vendor/phpunit/phpunit-mock-objects/src/Generator.php:189
    0.0301    2633560  13. PHPUnit\Framework\MockObject\Generator->generateMock() /Library/WebServer/Documents/mysites/vendor/phpunit/phpunit-mock-objects/src/Generator.php:488
    0.0313    2780728  14. interface_exists() /Library/WebServer/Documents/mysites/vendor/phpunit/phpunit-mock-objects/src/Generator.php:657
    0.0313    2780784  15. spl_autoload_call() /Library/WebServer/Documents/mysites/vendor/phpunit/phpunit-mock-objects/src/Generator.php:657
    0.0313    2780920  16. Composer\Autoload\ClassLoader->loadClass() /Library/WebServer/Documents/mysites/vendor/phpunit/phpunit-mock-objects/src/Generator.php:657
    0.0314    2781048  17. Composer\Autoload\includeFile() /Library/WebServer/Documents/mysites/vendor/composer/ClassLoader.php:322
    0.0319    2841600  18. include('/Library/WebServer/Documents/mysites/TM470_project/php/mortgage.php') /Library/WebServer/Documents/mysites/vendor/composer/ClassLoader.php:444

第3行是myClass.php中的类声明,因此我看不到为什么出现此消息。

其他一些帖子提到了自动装带器是问题,但是我不知道要寻找什么。这是我的autoloader.php文件:

<?php
// autoload.php @generated by Composer
require_once __DIR__ . '/composer/autoload_real.php';
return ComposerAutoloaderInit5d05186b2ab974ea0fca942edb705943::getLoader();

如果我删除测试功能,则由于我认为该类中没有测试,因此该消息消失了。

我的问题是这个: 根据提供的代码,请告知为什么出现此致命错误消息。另外,如何清除此错误,即我应该在代码中查找什么?

0 个答案:

没有答案