我有一个具有以下结构的项目:
- src
+ Guitar.php
+ Type.php
+ ToString.php
- tests
+ GuitarTest.php
composer.json这就是我是如何在composer.json中定义psr-4自动加载的:
"autoload": {
"psr-4": {
"Shop\\Guitar\\": "src/"
}
}这是我的Guitar.php:
<?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:
<?php
namespace Shop\Guitar;
require_once __DIR__ . '/../vendor/autoload.php';
interface ToString
{
public function toString(): string;
}这是我的Type.php:
<?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:
<?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);
}
}但是,当我运行测试时,我会得到以下错误:
Error: Class "Shop\Guitar\Guitar" not found有什么问题,我该怎么解决呢?
发布于 2022-06-14 18:09:29
这仅仅是一个关于PSR-4配置中的作曲家自动加载器的问题.
require_once调用没有。这是一个自动加载程序,类文件不能要求自动加载程序。如果有疑问,请测试您的自动加载器配置。
下一步:
require_once电话,它们只会分散你的注意力,防止麻烦的发生.自动配置要么有效,要么无效。如果您在不同的文件上分发包含点,这是更多的工作需要维护,而根本没有必要去做。tests模块以引入一个自动加载程序配置测试:1. Move the contents from `tests` into `tests/unit` to have a dedicated directory for your unit tests.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: - [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)(但是,它不能打印任何东西。重要的是它运行,而不是文件-没有找到或类似的错误。
如果您能够以这种方式执行PHP文件,请将其绑定到composer配置中。这是为了让您可以测试您的自动加载程序配置。
向composer.json配置中添加一个新脚本:
{
"script": {
"post-autoload-dump": [
"@php -dzend.assertions=1 -dassert.exception=0 tests/php/autoloading.php"
]
}
}参考文献:
并测试它:
$ 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-autoloadcomposer installcomposer update测试愉快。
在进行测试时,请同时检查phpunit是否仍在运行:
$ composer exec phpunit -- tests/unit
PHPUnit ...再次进行测试,当您发现了罪魁祸首,并且一切都正常时,您可以使用一个编写器脚本运行所有测试。
公共是一个带有单词test的脚本
{
"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就够了:
$ 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将使整个流程失败。
此外,还可以为每个编写器脚本添加一个描述,以便显示一条消息:
{
"scripts-descriptions": {
"test": "Runs all tests."
}
}$ composer list
...
test Runs all tests.
...(见:自定义描述-编写器脚本文档)
https://stackoverflow.com/questions/72592362
复制相似问题