承接 xp-framework/test 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

xp-framework/test

Composer 安装命令:

composer require xp-framework/test

包简介

Testing for the XP Framework

关键字:

README 文档

README

Build status on GitHub XP Framework Module BSD Licence Requires PHP 7.4+ Supports PHP 8.0+ Latest Stable Version

Unit and integration tests for the XP Framework

Writing a test

Tests reside inside a class suffixed by "Test" (a test group) and consist of methods annotated with the Test attribute (the test cases). The convention for test method naming is to use lowercase, words separated by underscores, though this is not strictly necessary.

use test\{Assert, Test};

class CalculatorTest {

  #[Test]
  public function addition() {
    Assert::equals(2, (new Calculator())->add(1, 1));
  }
}

To run these tests, use the test subcommand:

$ xp test CalculatorTest.class.php
> [PASS] CalculatorTest
  ✓ addition

Tests cases: 1 succeeded, 0 skipped, 0 failed
Memory used: 1556.36 kB (1610.49 kB peak)
Time taken:  0.001 seconds

Assertions

The following shorthand methods exist on the Assert class:

  • equals(mixed $expected, mixed $actual) - check two values are equal. Uses the util.Objects::equal() method internally, which allows overwriting object comparison.
  • notEquals(mixed $expected, mixed $actual) - opposite of above
  • true(mixed $actual) - check a given value is equal to the true boolean
  • false(mixed $actual) - check a given value is equal to the false boolean
  • null(mixed $actual) - check a given value is null
  • instance(string|lang.Type $expected, mixed $actual) - check a given value is an instance of the given type.
  • matches(string $pattern, mixed $actual) - verify the given value matches a given regular expression.
  • throws(string|lang.Type $expected, callable $actual) - verify the given callable raises an exception.

Expected failures

Using the Expect annotation, we can write tests that assert a given exception is raised:

use test\{Assert, Expect, Test};

class CalculatorTest {

  #[Test, Expect(DivisionByZero::class)]
  public function division_by_zero() {
    (new Calculator())->divide(1, 0);
  }
}

To check the expected exceptions' messages, use the following:

  • Any message: Expect(DivisionByZero::class)
  • Exact message: Expect(DivisionByZero::class, 'Division by zero')
  • Message matching regular expression: Expect(DivisionByZero::class, '/Division by (0|zero)/i')

Value-driven tests

To keep test code short and concise, tests may be value-driven. Values can be provided either directly inline:

use test\{Assert, Test, Values};

class CalculatorTest {

  #[Test, Values([[0, 0], [1, 1], [-1, 1]])]
  public function addition($a, $b) {
    Assert::equals($a + $b, (new Calculator())->add($a, $b));
  }
}

...or by referencing a provider method as follows:

use test\{Assert, Test, Values};

class CalculatorTest {

  private function operands(): iterable {
    yield [0, 0];
    yield [1, 1];
    yield [-1, 1];
  }

  #[Test, Values(from: 'operands')]
  public function addition($a, $b) {
    Assert::equals($a + $b, (new Calculator())->add($a, $b));
  }
}

Prerequisites

Test classes and methods may have prerequisites, which must successfully verify in order for the tests to run:

use test\verify\Runtime;
use test\{Assert, Test};

#[Runtime(extensions: ['bcmath'])]
class CalculatorTest {

  #[Test]
  public function addition() {
    Assert::equals(3, (int)bcadd(1, 2));
  }
}

The following verifications are included:

  • Runtime(os: '^WIN', php: '^8.0', extensions: ['bcmath']) - runtime verifications.
  • Condition(assert: 'function_exists("random_int")') - verify given expression in the context of the test class.

Passing arguments to tests

Especially for integration tests, passing values like a connection string from the command line to the test class is important. Add the Args annotation to the class as follows:

use test\Args;
use com\mongodb\MongoConnection;

#[Args('use')]
class IntegrationTest {
  private $conn;

  public function __construct(string $dsn) {
    $this->conn= new MongoConnection($dsn);
  }

  // ...shortened for brevity
}

...then pass the arguments as follows:

$ xp test IntegrationTest --use=mongodb://locahost
# ...

See also

xp-framework/test 适用场景与选型建议

xp-framework/test 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 228.5k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 01 月 16 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「module」 「xp」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

围绕 xp-framework/test 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 228.5k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 8
  • 依赖项目数: 66
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2023-01-16