无法找到单元测试出现错误 class

Unit testing getting error class could not be found

我在

dynamoDB 有一个 class
src/Dynamo/shop.php 

我的class如下所示

<?php
    
    namespace App\Dynamo;
    
    
    class Shop
    {
        -----
    }
    
?>

我正在尝试为此 class 实施单元测试,所以我在下面的位置创建了一个文件夹 class dynamo。

app/cake/tests/TestCase/Dynamo

在 Dynamo 文件夹中,我创建了一个文件名为 ShopTest.php

的 class

为了创建单元测试,我写了这个class,如下所示

<?php
declare(strict_types=1);

namespace App\Test\TestCase\Dynamo;

use App\Dynamo\Shop;
use Cake\TestSuite\TestCase;

class ShopTest{
    
    public function setUp()
    {
        $this->shop = new Shop;
    }

    public function testconnectDynamoDB()
    {
        debug($this->shop->connectDynamoDB());

        $this->assertNotEmpty($this->shop->connectDynamoDB());

    }
}

现在在 运行 phpunit 命令之后

vendor/bin/phpunit tests/TestCase/Dynamo/ShopTest.php 

我得到

Class 'App\Test\TestCase\Dynamo\ShopTest' could not be found in '/var/www/html/tests/TestCase/Dynamo/ShopTest.php'.

Class 存在于此位置,为什么我找不到 Class?

root@0ceda1df4444:/var/www/html# cd /var/www/html/tests/TestCase/Dynamo/
    root@0ceda1df4444:/var/www/html/tests/TestCase/Dynamo# ls
    ShopTest.php

root@0ceda1df4444:/var/www/html/tests/TestCase/Dynamo# cat ShopTest.php 
<?php
declare(strict_types=1);

namespace App\Test\TestCase\Dynamo;

use App\Dynamo\Shop;
use Cake\TestSuite\TestCase;

class ShopTest{

我还试图通过以下命令 运行 所有测试用例,我收到警告。

root@0ceda1df4444:/var/www/html# vendor/bin/phpunit                
PHPUnit 9.5.9 by Sebastian Bergmann and contributors.

Warning:       Your XML configuration validates against a deprecated schema.
Suggestion:    Migrate your XML configuration using "--migrate-configuration"!

No tests executed!

没有测试class

那里的错误信息有点混乱,但关键是这不是测试 class:

<?php
declare(strict_types=1);

namespace App\Test\TestCase\Dynamo;

use App\Dynamo\Shop;
use Cake\TestSuite\TestCase;

class ShopTest{ # <--

它只是一个class(巧合的是它的名字中有Test这个词)。

the documentation 中的示例进行比较:

namespace App\Test\TestCase\View\Helper;

use App\View\Helper\ProgressHelper;
use Cake\TestSuite\TestCase;
use Cake\View\View;

class ProgressHelperTest extends TestCase # <--
{

要作为测试检测到 class 必须扩展 TestClass - 因此要更正此问题:

...
use App\Dynamo\Shop;
use Cake\TestSuite\TestCase;

class ShopTest extends TestCase # <--
{

通过该更改,测试 class 将加载,一些更容易解决的问题将变得明显:

$ vendor/bin/phpunit tests/TestCase/Dynamo/ShopTest.php
PHP Fatal error:  Declaration of ShopTest::setUp() must be compatible with Cake\TestSuite\TestCase::setUp(): void in ~/repos/cakephp/app/tests/TestCase/Dynamo/ShopTest.php on line 11

Fatal error: Declaration of ShopTest::setUp() must be compatible with Cake\TestSuite\TestCase::setUp(): void in ~/repos/cakephp/app/tests/TestCase/Dynamo/ShopTest.php on line 11