【问题标题】:Why phpspec can not find classes?为什么phpspec找不到类?
【发布时间】:2017-06-02 07:26:43
【问题描述】:

我使用 PHPSPEC 3.4.0。当我使用

运行 phpsec

vendor/bin/phpspec 运行

我收到此错误:

Eastsea\Service\FileStorage\DuplicateFileStorage 类不存在。

这是我关于自动加载部分的 composer.json 文件:

"autoload": {
    "psr-4": {
        "App\\": "app/"
    },
    "psr-0": {
        "EastSea\\": "src/"
    }
}

这是我的文件夹树:

    ./src
`-- EastSea
    `-- Service
        `-- FileStorage
            |-- DuplicateFile.php
            |-- DuplicateFileStorage.php
            `-- Result.php

类:

<?php

namespace EastSea\Service\FileStorage;

class DuplicateFileStorage
{
    public function validate()
    {
        // TODO: write logic here
    }

    public function storage()
    {
        // TODO: write logic here
    }

    public function handle(DuplicateFile $file)
    {
        $file->hash();
    }
}

规格:

<?php

namespace spec\Eastsea\Service\FileStorage;

use \EastSea\Service\FileStorage\DuplicateFileStorage;
use \EastSea\Service\FileStorage\Result;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

class DuplicateFileStorageSpec extends ObjectBehavior
{
    function it_is_initializable()
    {
        $this->shouldHaveType(DuplicateFileStorage::class);
    }

}

【问题讨论】:

  • 删除开始使用的“\”。没有必要。用户需要 FQCN(Fully Qualified Class Name)。

标签: php phpspec


【解决方案1】:

试试这个:

"autoload": {
    "psr-4": {
        "App\\": "app/",
        "": "src/"
    }
}

显然在您的 composer.json 更新后运行 composer dump-autoload

【讨论】:

  • 它有效。非常感谢。但为什么我的配置不起作用?我已经编写了一个 test.php 来创建该对象。'EastSea\\' 和 '""' 都有效。
猜你喜欢
  • 2023-03-21
  • 1970-01-01
  • 2018-05-29
  • 1970-01-01
  • 1970-01-01
  • 2019-01-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多