自动加载PHPUnit的类文件

时间:2019-12-21 20:10:56

标签: php composer-php phpunit

所以,我只是在玩PHPUnit,我并不是真的需要它,但是我想学习它的工作原理,所以我在我的插件中的一个随机类上尝试了它。 我遇到的问题是,每当我运行phpunit时,都会说找不到测试中的Plot类。在我的composer.json文件中,

{
    "require": {
        "phpunit/phpunit": "^8.5"
    },

    "autoload":{
        "psr-4" : { "\\mohagames\\PlotArea\\utils\\" : "src/mohagames/PlotArea/utils/"}
    }
}

我的Plot.php文件位于C:\ Users \ moham \ Documents \ GitHub \ PlotArea \ src \ mohagames \ PlotArea \ utils \目录中,并具有mohagames\PlotArea\utils命名空间

但是出于某种原因它仍然会说

C:\Users\moham\Documents\GitHub\PlotArea>C:\Users\moham\Documents\Github\PlotArea\vendor\bin\phpunit --debug
PHPUnit 8.5.0 by Sebastian Bergmann and contributors.

Runtime:       PHP 7.3.4
Configuration: C:\Users\moham\Documents\GitHub\PlotArea\phpunit.xml

Test 'SampleTest::testPlot' started
Test 'SampleTest::testPlot' ended


Time: 95 ms, Memory: 4.00 MB

There was 1 error:

1) SampleTest::testPlot
Error: Class 'Plot' not found

C:\Users\moham\Documents\GitHub\PlotArea\tests\unit\SampleTest.php:8

ERRORS!
Tests: 1, Assertions: 0, Errors: 1.

和SampleTest测试类:

<?php

use PHPUnit\Framework\TestCase;

class SampleTest extends TestCase{

    public function testPlot(){
        $plot = new Plot();

    }
}

我已经尝试了Internet上的各种解决方案,但都无济于事

1 个答案:

答案 0 :(得分:0)

就像Alister指出的那样,首先导入类是可行的,否则PHP不知道代码所指的是什么类。

<?php

use PHPUnit\Framework\TestCase;
use mohagames\PlotArea\utils\Plot;

class SampleTest extends TestCase{

    public function testPlot(){
        $plot = new Plot();

    }
}
相关问题