illuminated/testing-tools

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


README

Laravel-specific Testing Helpers and Assertions

Laravel 测试工具

Buy me a coffee

StyleCI Build Status Coverage Status

Packagist Version Packagist Stars Packagist Downloads Packagist License

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

用法

  1. 使用 Composer 安装包

    composer require --dev illuminated/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 协议

Buy me a coffee