sokolnikov911/testing-tools-updated

Laravel 专用测试助手和断言。


README

Laravel-specific Testing Helpers and Assertions

Laravel 测试工具

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 测试工具是开源软件,根据 MIT 许可证 许可。

https://github.com/dmitry-ivanov/laravel-testing-tools 分支