srd2010/testing-tools

Laravel特定测试辅助工具和断言。用于Laravel 10.x版本的分叉版

10.0.0 2024-03-05 07:14 UTC

This package is auto-updated.

Last update: 2024-09-08 10:46:56 UTC


README

Laravel-specific Testing Helpers and Assertions

Laravel测试工具

这是从原始仓库分叉出来以支持Laravel v10.x的版本

Buy Me A Coffee

StyleCI Build Status codecov

Packagist Version Packagist Stars Packagist Downloads Packagist License

Laravel特定测试辅助工具和断言。

用法

  1. 通过Composer安装包

    composer require --dev srd2010/testing-tools
  2. 使用 Illuminated\Testing\TestingTools 特性

    use Illuminated\Testing\TestingTools;
    
    abstract class TestCase extends Illuminate\Foundation\Testing\TestCase
    {
        use TestingTools;
    
        // ...
    }
  3. 在测试中使用提供的辅助工具和断言

    class ExampleTest extends TestCase
    {
        /** @test */
        public function it_has_lots_of_useful_assertions()
        {
            $this->assertDatabaseHasMany('posts', [
                ['title' => 'Awesome!'],
                ['title' => 'Check multiple rows'],
                ['title' => 'In one simple assertion 🤟'],
            ]);
        }
    }

可用的辅助工具

欢迎贡献力量。

可用的断言

欢迎贡献力量。

辅助工具

应用程序辅助工具

emulateLocal()

模拟应用程序在 local 环境下运行

$this->emulateLocal();

emulateProduction()

模拟应用程序在 production 环境下运行

$this->emulateProduction();

emulateEnvironment()

模拟应用程序在指定环境下运行

$this->emulateEnvironment('demo');

断言

集合断言

assertCollectionsEqual()

基于指定键断言给定的集合相等

$this->assertCollectionsEqual($collection1, $collection2, 'id');

assertCollectionsNotEqual()

基于指定键断言给定的集合不相等

$this->assertCollectionsNotEqual($collection1, $collection2, 'id');

数据库断言

assertDatabaseHasTable()

断言数据库有给定的表

$this->assertDatabaseHasTable('users');

assertDatabaseMissingTable()

断言数据库没有给定的表

$this->assertDatabaseMissingTable('unicorns');

assertDatabaseHasMany()

断言数据库有所有给定的行

$this->assertDatabaseHasMany('posts', [
    ['title' => 'First Post'],
    ['title' => 'Second Post'],
    ['title' => 'Third Post'],
]);

assertDatabaseMissingMany()

断言数据库没有所有给定的行

$this->assertDatabaseMissingMany('posts', [
    ['title' => 'Fourth Post'],
    ['title' => 'Fifth Post'],
]);

文件系统断言

assertDirectoryEmpty()

断言给定的目录为空

$this->assertDirectoryEmpty('./my/dir/');

assertDirectoryNotEmpty()

断言给定的目录不为空

$this->assertDirectoryNotEmpty('./my/dir/');

assertFilesCount()

断言目录有给定的文件数

$this->assertFilesCount('./my/dir/', 3);

assertNotFilesCount()

断言目录没有给定的文件数

$this->assertNotFilesCount('./my/dir/', 5);

日志文件断言

seeLogFile()

断言给定的日志文件存在。

路径相对于 storage/logs 文件夹

$this->seeLogFile('example.log');

dontSeeLogFile()

断言给定的日志文件不存在。

路径相对于 storage/logs 文件夹

$this->dontSeeLogFile('foobarbaz.log');

seeInLogFile()

断言日志文件包含给定的消息。

路径相对于 storage/logs 文件夹

$this->seeInLogFile('example.log', 'Sample log message!');

您还可以指定消息数组

$this->seeInLogFile('example.log', [
    'Sample log message 1!',
    'Sample log message 2!',
    'Sample log message 3!',
]);

您可以在消息中使用以下占位符

  • %datetime% - 任何日期时间字符串。
$this->seeInLogFile('example.log', '[%datetime%]: Sample log message!');

dontSeeInLogFile()

断言日志文件不包含给定的消息。

路径相对于 storage/logs 文件夹

$this->dontSeeInLogFile('example.log', 'Non-existing log message!');

您还可以指定消息数组

$this->dontSeeInLogFile('example.log', [
    'Non-existing log message 1!',
    'Non-existing log message 2!',
    'Non-existing log message 3!',
]);

调度断言

seeScheduleCount()

断言调度计数等于给定的值

$this->seeScheduleCount(3);

dontSeeScheduleCount()

断言调度计数不等于给定的值

$this->dontSeeScheduleCount(5);

seeInSchedule()

断言给定的命令已被调度

$this->seeInSchedule('foo', 'everyFiveMinutes');
$this->seeInSchedule('bar', 'hourly');
$this->seeInSchedule('baz', 'twiceDaily');

您还可以使用cron表达式

$this->seeInSchedule('foo', '*/5 * * * * *');
$this->seeInSchedule('bar', '0 * * * * *');
$this->seeInSchedule('baz', '0 1,13 * * * *');

dontSeeInSchedule()

断言给定的命令未被调度

$this->dontSeeInSchedule('foobarbaz');

原始赞助商

Laravel Idea
Material Theme UI Plugin

许可证

Laravel测试工具是开源软件,许可证为MIT许可