cspray/labrador-async-unit 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

cspray/labrador-async-unit

Composer 安装命令:

composer require cspray/labrador-async-unit

包简介

An async unit and integration testing framework written on top of PHP8

README 文档

README

Unit Tests

A testing framework, with a focus on integration testing, that treats Amp's Loop as a first-class citizen!

  • Extend Cspray\Labrador\AsyncUnit\TestCase and annotate your tests with #[Test] to get started
  • Setup and teardown your tests using a variety of hooks by annotating methods with attributes like #[BeforeEach] and #[AfterEach]
  • Embrace a test suite as a first-class citizen with Cspray\Labrador\AsyncUnit\TestSuite and bring a layer of extra functionality to integration tests
  • Assertion API with first-class async support and a clear
  • Disable tests, TestCase, or a TestSuite with the #[Disabled] Attribute
  • Utilize #[DataProvider] to reduce test duplication
  • Expect exceptions with, or without, specific messages to be thrown by your tests
  • Includes a bundled CLI application for running your tests

AsyncUnit is under active development! The project has a defined Roadmap and is currently implementing features for version 0.5.0.

Installation

composer require --dev cspray/labrador-async-unit

Hello, AsyncUnit

Although AsyncUnit can satisfy the needs of most unit and integration tests it was really designed for a specific type of test which can be challenging to run properly even in synchronous contexts. The "canonical" AsyncUnit test example is below and demonstrates the core functionality of the framework.

<?php

namespace Acme\MyApp;

use Cspray\Labrador\AsyncUnit\Attribute\AfterAll;
use Cspray\Labrador\AsyncUnit\Attribute\BeforeAll;
use Cspray\Labrador\AsyncUnit\Attribute\BeforeEachTest;
use Cspray\Labrador\AsyncUnit\Attribute\AfterEachTest;
use Cspray\Labrador\AsyncUnit\Attribute\BeforeEach;
use Cspray\Labrador\AsyncUnit\Attribute\Test;
use Cspray\Labrador\AsyncUnit\Attribute\AttachToTestSuite as UseTestSuite;
use Cspray\Labrador\AsyncUnit\TestCase;
use Cspray\Labrador\AsyncUnit\TestSuite;
use Amp\Success;
use Amp\Delayed;
use function Amp\Postgres\pool;

class DatabaseTestSuite extends TestSuite {

    #[BeforeAll]
    public function connectToDatabase() {
        // In test situations we want to make sure we're dealing with the same connection so we can properly clean up data
        $pool = pool(connectionConfig(), maxConnections: 1, resetConnections: false);
        $this->set('pool', $pool);
    }
    
    #[BeforeEachTest]
    public function startTransaction() {
        yield $this->get('pool')->query('START TRANSACTION');
    }
    
    #[AfterEachTest]
    public function rollback() {
        yield $this->get('pool')->query('ROLLBACK');
    }
    
    #[AfterAll]
    public function closeDatabase() {
        $this->get('pool')->close();
    }
    
}

// The name of this class doesn't matter... you only need to ensure you extend TestCase
#[UseTestSuite(DatabaseTestSuite::class)]
class MyDatabaseTestCase extends TestCase {

    // Again, none of the method names matter... just make sure you're annotating with the correct Attribute
    #[BeforeEach]
    public function loadFixture() {
        yield someMethodThatLoadsData($this->testSuite()->get('pool'));
    }
    
    #[Test]
    public function ensureSomethingHappens() {
        yield new Delayed(100); // just to show you we're on the loop
        // These values could be retrieved from the database
        $this->assert()->stringEquals('foo', 'foo');
    }
    
    #[Test]
    public function ensureSomethingAsyncHappens() {
        yield new Delayed(100);
        yield $this->asyncAssert()->stringEquals('foo', new Success('foo'));
    }
    
    #[Test]
    public function makeSureYouAssertSomething() {
        // a failed test because you didn't assert anything!
    }
    
}
    
class MyNormalTestCase extends TestCase {
    
    #[Test]
    public function ensurePoolNotAvailable() {
        $this->assert()->isNull($this->testSuite()->get('pool'));
    }
    
}

I hope you were able to see as much neatness in the above testing example as I do! If you're interested in seeing more examples there are two places to find them; the examples/ and acme_src/ directories. Otherwise, please check out the rest of this README for how to get started with the project.

Documentation

Whether you're a user learning how to write tests with the framework or you're a contributor wanting to make the library better our documentation should have what you're looking for! We walk you through everything you need to do get started, teach you about all the important concepts to know, and list out the assertions available. For contributors we give you a thorough overview of how everything works.

Online documentation at https://docs.labrador-kennel.io/asyncunit.

Documentation for Labrador AsyncUnit is sponsored by GitBook. Should absolutely check them out if you have the ned for developer-friendly, managed documentation hosting!

Discussion

Have an idea for the framework? Wondering how something works and have a question? Wanna interact with the maintainers? You're in the right place! This is the "social" part of AsyncUnit... go add to the Discussion!

Roadmap

AsyncUnit has a fairly well-defined roadmap leading to a stable API and a 1.0 release. Our Roadmap is not dated because the framework is currently maintained and implemented by 1 person in their free time. Instead, we have a series of 0.x releases with functionality that should enable increasingly complex tests until our canonical example can be executed. Check out the Roadmap to see what's in store for AsyncUnit! The features we're currently working on implemented can be tracked in our Active Sprint.

cspray/labrador-async-unit 适用场景与选型建议

cspray/labrador-async-unit 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 7 次下载、GitHub Stars 达 14, 最近一次更新时间为 2021 年 04 月 19 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 cspray/labrador-async-unit 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 cspray/labrador-async-unit 我们能提供哪些服务?
定制开发 / 二次开发

基于 cspray/labrador-async-unit 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 7
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 14
  • 点击次数: 5
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 14
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2021-04-19