【问题标题】:Silverstripe 4 SapphireTest class can't be found找不到 Silverstripe 4 SapphireTest 类
【发布时间】:2018-09-08 17:50:57
【问题描述】:

我已从 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”类

【问题讨论】:

    标签: phpunit silverstripe silverstripe-4


    【解决方案1】:

    我在 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

    干杯

    戈登

    【讨论】:

      【解决方案2】:

      您可能缺少测试引导。 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-10-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-11-18
        • 1970-01-01
        • 2012-12-13
        • 2019-04-17
        相关资源
        最近更新 更多