ralphjsmit/pest-plugin-filesystem

为Pest提供的一套便捷的文件系统测试助手。

1.2.0 2023-02-17 17:24 UTC

This package is auto-updated.

Last update: 2024-09-19 09:40:51 UTC


README

使用Pest进行测试绝对是一种乐趣,但有时你确实需要一点点额外的功能。在我的情况下,这个额外功能是一套用于与文件系统交互和对其内容进行断言的期望和函数。以下列出了目前所有可用的助手。祝您使用愉快!

重要

此包仅适用于Pest V1,不适用于Pest V2。

内容

  1. 期望
    1. expect(...)->toHaveContents()
    2. expect(...)->toExist()
    3. expect(...)->toHaveNamespace()
  2. 高阶期望
    1. expect(...)->contents->toBe(...)
  3. 函数
    1. rm($path, $allowNonExisting)
    2. rmdir_recursive($dir)
    3. contents($path)
    4. expectFailedAssertion()

期望

expect(...)->toHaveContents()

测试文件是否包含特定内容

file_put_contents(__DIR__ . '/tmp/fileA.php', 'I\'m a test file!');
file_put_contents(__DIR__ . '/tmp/fileB.php', 'I\'m a second test file!');

expect(__DIR__ . '/tmp/fileA.php')->toHaveContents('I\'m a test file!');
expect(__DIR__ . '/tmp/fileB.php')->not->toHaveContents('I\'m a test file!');

expect(...)->toExist()

测试文件是否存在

expect(__DIR__ . '/tmp/fileA.php')->toExist();

expect(...)->toHaveNamespace()

测试文件是否具有特定的命名空间(仅限PHP)

expect(__DIR__ . '/tmp/fileA.php')->toHaveNamespace('App\Models');

高阶期望

expect(...)->contents->toBe(...)

对文件内容进行断言

file_put_contents(__DIR__ . '/tmp/fileA.php', 'I\'m a test file!');

expect(__DIR__ . '/tmp/fileA.php')
    ->contents->toBe('I\'m a test file!')
    ->toContain('test file')
    ->not->toContain('Hello world');

函数

rm($path, $allowNonExisting)

完全删除文件或目录

use function RalphJSmit\PestPluginFilesystem\rm;

rm(__DIR__ . '/tmp'); // Make sure that this file or directory doesn't exist

file_put_contents(__DIR__ . '/tmp/fileA.php', 'I\'m a test file!');
file_put_contents(__DIR__ . '/tmp/fileB.php', 'I\'m a second test file!');

// Other code

rmdir_recursive($dir)

此函数递归删除文件夹及其内容。在内部,它被 rm() 函数使用

use function RalphJSmit\PestPluginFilesystem\rmdir_recursive;

rmdir_recursive(__DIR__ . '/tmp'); // Recursively remove this directory

contents($path)

获取特定文件的文件内容。它只是 file_get_contents() 的简短包装

use function RalphJSmit\PestPluginFilesystem\contents;

expect(
    contents(__DIR__ . '/tmp/fileA.php')
)->toBe(
    contents(__DIR__ . '/tmp/fileB.php')
);

expectFailedAssertion()

请注意,此助手将很快添加到另一个包中,因此将在这里删除。

期望断言失败。这对于测试您自己的自定义Pest断言很有帮助

use function RalphJSmit\PestPluginFilesystem\expectFailedAssertion;

expectFailedAssertion();
expect(true)->toBe(false);

// This test will pass
// Somewhere
expect()->extend('toBeHello', function () {
    return $this->toBe('Hello there');
});

// In your test
use function RalphJSmit\PestPluginFilesystem\expectFailedAssertion;

expect('Hello there')->toBeHello(); // This will pass

expectFailedAssertion();
expect('Bye')->toBeHello(); // This will pass

expectFailedAssertion();
expect('Hello there')->toBeHello(); // This will fail

通用

🐞 如果您发现错误,请提交详细的 issue,我将尽快修复。

🔐 如果您发现漏洞,请查看 我们的安全策略

🙌 如果您想贡献力量,请提交 pull request。所有PR都将得到完全认可。如果您不确定我是否会接受您的想法,请随时联系我!

🙋‍♂️ Ralph J. Smit