首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >作曲家autoload找不到这个类

作曲家autoload找不到这个类
EN

Stack Overflow用户
提问于 2022-06-12 12:54:47
回答 1查看 135关注 0票数 0

我有一个具有以下结构的项目:

代码语言:javascript
复制
- src
  + Guitar.php
  + Type.php
  + ToString.php
- tests
  + GuitarTest.php
composer.json

这就是我是如何在composer.json中定义psr-4自动加载的:

代码语言:javascript
复制
"autoload": {
    "psr-4": {
        "Shop\\Guitar\\": "src/"
    }
}

这是我的Guitar.php:

代码语言:javascript
复制
<?php

namespace Shop\Guitar;

require_once __DIR__ . '/../vendor/autoload.php';

use Shop\Guitar\Type;

class Guitar
{
    public function __construct(public readonly string $serialNumber, public readonly Type $type)
    {
    }
}

这是我的ToString.php:

代码语言:javascript
复制
<?php

namespace Shop\Guitar;

require_once __DIR__ . '/../vendor/autoload.php';

interface ToString
{
    public function toString(): string;
}

这是我的Type.php:

代码语言:javascript
复制
<?php

namespace Shop\Guitar;

require_once __DIR__ . '/../vendor/autoload.php';

enum Type implements ToString
{
    case ACOUSTIC;
    case ELECTRIC;

    public function toString(): string
    {
        return match($this)
        {
            self::ACOUSTIC => 'Acoustic',
            self::ELECTRIC => 'Electric',
        };
    }
}

这是我的GuitarTest.php:

代码语言:javascript
复制
<?php

require_once __DIR__ . '/../vendor/autoload.php';

use PHPUnit\Framework\TestCase;
use Shop\Guitar\Guitar;
use Shop\Guitar\Type;

final class InventoryTest extends TestCase
{
    public function testGuitarConstructor(): void
    {
        $guitar = new Guitar('foo', Type::ELECTRIC);
    }
}

但是,当我运行测试时,我会得到以下错误:

代码语言:javascript
复制
Error: Class "Shop\Guitar\Guitar" not found

有什么问题,我该怎么解决呢?

EN

回答 1

Stack Overflow用户

发布于 2022-06-14 18:09:29

这仅仅是一个关于PSR-4配置中的作曲家自动加载器的问题.

  • 您的composer.json配置看起来是合法的。
  • require_once调用没有。这是一个自动加载程序,类文件不能要求自动加载程序。

如果有疑问,请测试您的自动加载器配置。

下一步:

  1. 删除那些require_once电话,它们只会分散你的注意力,防止麻烦的发生.自动配置要么有效,要么无效。如果您在不同的文件上分发包含点,这是更多的工作需要维护,而根本没有必要去做。
  2. 当您遇到问题时,重新组织您的tests模块以引入一个自动加载程序配置测试:
代码语言:javascript
复制
1. Move the contents from `tests`  into `tests/unit` to have a dedicated directory for your unit tests.
代码语言:javascript
复制
2. Create the `tests/php`  subfolder and create an `autoloading.php`  file there-in. The folder is for PHP tests, that is you execute them with PHP directly (e.g. `php tests/php/autoloading.php`). The files content:
代码语言:javascript
复制
    - [https://php.net/assert](https://php.net/assert)
    - [https://php.net/interface\_exists](https://php.net/interface_exists)
    - [https://php.net/enum\_exists](https://php.net/enum_exists)
    - [https://php.net/class\_exists](https://php.net/class_exists)
  1. 运行自动加载程序配置测试,以检查是否可以加载该文件。 $ php test/php/autooloading.php

(但是,它不能打印任何东西。重要的是它运行,而不是文件-没有找到或类似的错误。

如果您能够以这种方式执行PHP文件,请将其绑定到composer配置中。这是为了让您可以测试您的自动加载程序配置。

composer.json配置中添加一个新脚本:

代码语言:javascript
复制
{
    "script": {
        "post-autoload-dump": [
            "@php -dzend.assertions=1 -dassert.exception=0 tests/php/autoloading.php"
        ]
    }
}

参考文献:

并测试它:

代码语言:javascript
复制
$ composer run-script post-autoload-dump
> @php -dzend.assertions=1 -dassert.exception=0 tests/php/autoloading.php
PHP Warning:  assert(): [ ...

(它不能给出输出,这里我提出了一点警告,以显示它会是什么样的)

这将运行在警告模式下启用断言的新的自动配置测试。也就是说,如果任何***_exists()函数返回false,您将看到错误。这意味着无法加载接口/enum/class。

通过将-dassert.exception=0更改为-dassert.exception=1 (0 -> 1),可以使测试失败。然后,测试将使用非零代码退出(状态255,因为没有异常)。

这就是您想要的,将-dassert.exception=0更改为-dassert.exception=1,然后再次保存composer.json

然后,您可以使用任何转储自动加载程序的composer命令测试自动加载程序:

  • composer dump-autoload
  • composer install
  • composer update

测试愉快。

在进行测试时,请同时检查phpunit是否仍在运行:

代码语言:javascript
复制
$ composer exec phpunit -- tests/unit
PHPUnit ...

再次进行测试,当您发现了罪魁祸首,并且一切都正常时,您可以使用一个编写器脚本运行所有测试。

公共是一个带有单词test的脚本

代码语言:javascript
复制
{
    "script": {
        "post-autoload-dump": [
            "@php -dzend.assertions=1 -dassert.exception=0 tests/php/autoloading.php"
        ],
        "test": [
          "@composer dump-autoload",
          "@composer exec phpunit"
        ]
    }
}

而且你不需要一直写composer run-script test,只要composer test就够了:

代码语言:javascript
复制
$ composer test
Generating autoload files
Generated autoload files
> @php -dzend.assertions=1 -dassert.exception=1 tests/php/autoloading.php
PHPUnit ...
Time: 28 ms, Memory: 4.00MB

No tests executed!

(在这个示例输出中,我还没有配置phpunit测试,但您知道了)

那是什么样的测试?我把它称为配置测试,它只是一个快速检查,您可以在那里验证一些PHP内容。不要添加很多这样的测试,并保持他们的测试计划小。您可以使用它来解决配置项目时遇到的问题。把它保持在一年之内,如果你是完全没有错误的,那么你可以考虑删除它。

然而,这个示例显示了如何使某些检查更接近配置的位置,并将它们绑定在一起。如果存在非零退出状态,Composer将使整个流程失败。

此外,还可以为每个编写器脚本添加一个描述,以便显示一条消息:

代码语言:javascript
复制
{
    "scripts-descriptions": {
        "test": "Runs all tests."
    }
}
代码语言:javascript
复制
$ composer list
...
  test                 Runs all tests.
...

(见:自定义描述-编写器脚本文档)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72592362

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档