定制 webxid/phpunitsandbox 二次开发

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

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

webxid/phpunitsandbox

Composer 安装命令:

composer require webxid/phpunitsandbox

包简介

This package helps to make a mockups for unit-testing

README 文档

README

This package helps to make a mockups for unit-testing in PHP v5.4 - 8.x

Source https://github.com/webxid/PHPUnitSandbox

Install

PHPUnitSandbox does not require any special installation.

Requirements

  • PHP v5.4 or later,
  • PHPUnitSandbox has to be includes directly (no autoloaders supports, including Composer),
  • PHP config has to support functions exec() and eval().

Installation steps

  1. Run composer require webxid/phpunitsandbox
  2. Register additional autoloaders, if you have any one
UnitSandbox::init([
        __DIR__ . '/../autoloader_1.php', // it has to be absolute route of autoloader file, and it has to be .php file
        ...
        __DIR__ . '/../autoloader_n.php',
    ])
    ->registerAutoloader();

!!! Important !!! to make sandbox works correctly, the all autoloaders have to be register via UnitSandbox, otherwise you get failed. Also, please, check ./bootstrap.php

How To Use

This lib works as sandbox:

  • First, you setup a mock up of a class and of the class methods.
  • Then, you call UnitSandbox::execute(function() {}); and pass mocked class using inside the function() {}.

Example

// Setup a mock up of a class `DB` and of the static method `DB::query()`
UnitSandbox::mockClass('DB')
    ->mockStaticMethod('query', UnitSandbox::SELF_INSTANCE); //return self instance

// Then call the mocked method inside sandbox
$result = UnitSandbox::execute(function () {
    return \DB::query();
});

Code examples

Mock up a static method and an object method

UnitSandbox::mockClass('DB')
    ->mockStaticMethod('query', UnitSandbox::SELF_INSTANCE) // returns self instance
    ->mockMethod('execute', [1,2,3]); // returns array(1,2,3)

Mocked classes are working inside another classes too

// This class should be able by autoloader
class TestClass
{
    public static function init()
    {
        return DB::query() // the usage of the mocked class
            ->execute();
    }
}
// Get result of TestClass::init();
$result = UnitSandbox::execute(function () {
    return \TestClass::init();
});

echo json_encode($result); // returns `[1,2,3]` 

Spy class

Spy class uses to mock up a part of a class.

  1. We need a TestClass for example:
class TestClass
{
    private static $my_property = 'Hello world!';

    public static function getProperty()
    {
        return static::$my_property;
    }
}
  1. Let's rewrite private property of class TestClass;
UnitSandbox::spyClass('\TestClass')
    ->defineStaticProperty('my_property', 'value');

$result_private_property = UnitSandbox::execute(function () {
    return \Spy\TestClass::getProperty();
});

echo $result_private_property; // it'll prints `value` instead `Hello world!`


*Please, see all examples in ./tests/ExampleUnitTest.php *

Issues

To see errors, occurred inside sandbox, needs to set up debug mode:

UnitSandbox::init()
   ->debugMode(true, false);

License

PHPUnitSandbox is licensed under the Apache v2.0.

Version log

v0.3

  • Make composer lib
  • Minor fixes

v0.2

  • Mocked class properties defining
  • Pass parameters to mocked methods
  • Spy class logic
  • Minor fixes

v0.1

  • Methods mock up features

webxid/phpunitsandbox 适用场景与选型建议

webxid/phpunitsandbox 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 02 月 04 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 webxid/phpunitsandbox 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: Apache-2.0
  • 更新时间: 2022-02-04