3

我已经从 SilverStripe 3 升级到 4,现在我的 phpUnit 测试无法运行,因为它们找不到我的任何自定义类。

自动加载器或其他东西必须缺少某些东西。

我有一个像这样的简单测试

  use SilverStripe\Dev\SapphireTest;

class EntityTest extends SapphireTest
{
    var $Entity;
    function setUp()/* The :void return type declaration that should be here would cause a BC issue */
    {
        parent::setUp(); // TODO: Change the autogenerated stub
        $this->Entity = new \My\API\Client\Model\Entity();
    }

    function testMethods(){

        $this->assertMethodExist($this->Entity,'setName');
    }


    function assertMethodExist($class, $method) {
        $oReflectionClass = new ReflectionClass($class);
        assertThat("method exist", true, $oReflectionClass->hasMethod($method));
    }
}

运行时我得到: $ php vendor/phpunit/phpunit/phpunit mysite/tests/EntityTest.php

致命错误:找不到类“SilverStripe\Dev\SapphireTest”

4

2 回答 2

3

我在 SilverStripe 4.1 中遇到了类似的问题,这就是我发现(并解决)的问题。

1) 从 4.1 开始,您需要使用 --prefer-source 而不是 --prefer-dist 来获取测试代码。现在已从分布式包中省略了测试代码,请参阅https://github.com/silverstripe/silverstripe-framework/issues/7845

2) phpunit 必须在 ^ 5.7 版本的 require-dev 中 - 我有不同的值,这就是自动加载问题的原因。

我创建了一个测试模块供参考,请参阅https://github.com/gordonbanderson/travistestmodule

干杯

戈登

于 2018-04-11T05:17:23.260 回答
0

您可能错过了测试引导。SS4 仍然依赖 SilverStripe 类清单来注册可用的类(不仅仅是 PSR-4 自动加载器),因此您需要包含它。尝试以下任何一种:

$ vendor/bin/phpunit --bootstrap vendor/silverstripe/framework/tests/bootstrap.php mysite/tests

phpunit.xml或在您的根项目中创建一个文件:

<phpunit bootstrap="vendor/silverstripe/framework/tests/bootstrap.php" colors="true">
</phpunit>

您也可以使用 CMS 模块中的等效文件,但在开始将您的测试套件集成到 CI 提供程序之前,您可能不会看到任何差异。

于 2018-04-02T23:17:37.133 回答