定制 mattiasgeniar/phpunit-query-count-assertions 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

mattiasgeniar/phpunit-query-count-assertions

最新稳定版本:1.1.5

Composer 安装命令:

composer require mattiasgeniar/phpunit-query-count-assertions

包简介

A custom assertion for phpunit that allows you to count the amount of SQL queries used in a test. Can be used to enforce certain performance characteristics (ie: limit queries to X for a certain action).

README 文档

README

Tests

A custom assertion for phpunit that allows you to count the number of SQL queries used in a test. Can be used to enforce certain performance characteristics (ie: limit queries to X for a certain action).

This works for Laravel only at the moment.

Installation

You can install the package via composer:

composer require --dev mattiasgeniar/phpunit-query-count-assertions

Usage

Add the AssertsQueryCounts trait to your test-class, initialize it in the setup() and you can start asserting queries.

use Mattiasgeniar\PhpunitQueryCountAssertions\AssertsQueryCounts;

class YourTest extends TestCase
{
    use AssertsQueryCounts;

    public function setUp(): void
    {
        parent::setUp();

        AssertsQueryCounts::trackQueries();
    }

    /** @test */
    public function your_tests_go_here()
    {
        $this->assertNoQueriesExecuted();
    }
}

Available assertions/methods

You can use the following methods, their names should be self-explanatory:

$this->assertNoQueriesExecuted();       // No queries should have been run

$this->assertQueryCountMatches(5);      // Query count should be exactly 5

$this->assertQueryCountLessThan(6);     // Should be less than 6 queries

$this->assertQueryCountGreaterThan(4);  // Should be more than 4 queries

All these methods can accept a closure as an extra argument. The assertion will only take in account the queries performed inside the closure. If you use this way of testing, you don't need to call trackQueries yourself.

$this->assertQueryCountMatches(2, function() {
    // assertion will pass if exactly 2 queries happen here.
});

Testing

composer test

License

The MIT License (MIT). Please see License File for more information.

统计信息

  • 总下载量: 653.54k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 96
  • 点击次数: 1
  • 依赖项目数: 3
  • 推荐数: 0

GitHub 信息

  • Stars: 96
  • Watchers: 3
  • Forks: 6
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-09-28