PHPackages                             yunbuye/thinkphp-testing - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Testing &amp; Quality](/categories/testing)
4. /
5. yunbuye/thinkphp-testing

ActiveLibrary[Testing &amp; Quality](/categories/testing)

yunbuye/thinkphp-testing
========================

ThinkPHP 友好的测试扩展

0.8.2(6y ago)2671MITPHPPHP &gt;=7.1.0CI failing

Since Dec 25Pushed 3y ago2 watchersCompare

[ Source](https://github.com/yunbuye/thinkphp-testing)[ Packagist](https://packagist.org/packages/yunbuye/thinkphp-testing)[ Docs](http://github.com/yunbuye/thinkphp-testing)[ RSS](/packages/yunbuye-thinkphp-testing/feed)WikiDiscussions develop Synced 1w ago

READMEChangelogDependencies (5)Versions (5)Used By (0)

yunbuye/thinkphp-testing
========================

[](#yunbuyethinkphp-testing)

一个测试友好的ThinkPHP测试扩展

安装
--

[](#安装)

```
composer require yunbuye/thinkphp-testing --dev
```

修改 phpunit.xml 文件,在 phpunit 标签加入 bootstrap="vendor/autoload.php"

```

...

```

使用时注意
-----

[](#使用时注意)

1. 测试类必须继承 Yunbuye\\ThinkTesting\\TestCase 测试类
2. 如果不是使用thinkPHP命令（php think unit） 运行的测试，需要设置 $app\_path 和加载基础文件。
    例： ```
    namespace Tests;

    use Yunbuye\ThinkTesting\TestCase as BaseTestCase;

    abstract class TestCase extends BaseTestCase
    {
        protected $app_path = __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'application';//指定应用目录
        protected $baseUrl = 'http://localhost';

        public function __construct($name = null, array $data = [], $dataName = '')
        {
            require_once __DIR__ . '/../thinkphp/base.php';//加载基础文件
            parent::__construct($name, $data, $dataName);
        }
    }
    ```

功能
--

[](#功能)

1. 模拟对象
    只要是使用容器进行管理的对象，都可以使用以下方法进行模拟(具体的 [Mockery](http://docs.mockery.io/en/latest/) 使用方法，请参考 [文档](http://docs.mockery.io/en/latest/)：

    ```
    use Mockery;
    use Mockery\Mock;

    $this->instance('think\Cache', Mockery::mock('think\Cache', function ($mock) {
        /**
         * @var Mock $mock
         */
        $return='return';
        $key='key';
        return $mock->shouldReceive('get')->with($key)->andReturn($return);
    }));
    ```

    为了让以上过程更加便捷：

    ```
    use Mockery;
    use Mockery\Mock;

    $this->mock('think\Cache', function ($mock) {
        /**
         * @var Mock $mock
         */
        $return='return';
        $key='key';
        return $mock->shouldReceive('get')->with($key)->andReturn($return);
     });
    ```

    同样，如果你想侦查一个对象，基本测试用例类提供了一个便捷的 spy 方法作为 Mockery::spy 的替代方法:

    ```
    use App\Service;
    use Mockery\Mock;

    $this->spy('think\Cache', function ($mock) {
        /**
         * @var Mock $mock
         */
        $return='return';
        $key='key';
        return $mock->shouldReceive('get')->with($key)->andReturn($return);
    });
    ```
2. 每次测试后数据库回滚
    每次运行测试用例后，为了不互相污染数据，可以选择数据库回滚。
    例：

    ```
    namespace Tests;

    use Yunbuye\ThinkTesting\TestCase as BaseTestCase;
    use Yunbuye\ThinkTesting\Traits\DatabaseTransactions;

    abstract class TestCase extends BaseTestCase
    {
        use DatabaseTransactions;//每次测试回滚数据
        protected $app_path = __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'application';//指定应用目录
        protected $baseUrl = 'http://localhost';

        public function __construct($name = null, array $data = [], $dataName = '')
        {
            require_once __DIR__ . '/../thinkphp/base.php';//加载基础文件
            parent::__construct($name, $data, $dataName);
        }
    }
    ```
3. Facade mock 模拟
    安装扩展(不要加--dev)

    ```
    composer require yunbuye/thinkphp-facade
    ```

    具体使用，请参考 [yunbuye/thinkphp-facade](https://github.com/yunbuye/thinkphp-facade)
4. 模型工厂
    安装扩展(加--dev)

    ```
    composer require yunbuye/thinkphp-model-factory
    ```

    具体使用，请参考 [yunbuye/thinkphp-model-factory](https://github.com/yunbuye/thinkphp-model-factory)

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity12

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity45

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 90.9% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~7 days

Total

3

Last Release

2322d ago

PHP version history (2 changes)0.8.0PHP &gt;=5.6.0

0.8.1PHP &gt;=7.1.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/b1d5aa6e1769a531d3d4cbfd980198fddb57bce6f21bf343faf477b8fc0d3699?d=identicon)[yunbuye](/maintainers/yunbuye)

---

Top Contributors

[![xwpd](https://avatars.githubusercontent.com/u/17465049?v=4)](https://github.com/xwpd "xwpd (10 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (1 commits)")

---

Tags

testingmockerymockthinkphp

### Embed Badge

![Health badge](/badges/yunbuye-thinkphp-testing/health.svg)

```
[![Health](https://phpackages.com/badges/yunbuye-thinkphp-testing/health.svg)](https://phpackages.com/packages/yunbuye-thinkphp-testing)
```

###  Alternatives

[mockery/mockery

Mockery is a simple yet flexible PHP mock object framework

10.7k497.0M23.6k](/packages/mockery-mockery)[orchestra/testbench

Laravel Testing Helper for Packages Development

2.2k39.1M32.1k](/packages/orchestra-testbench)[brain/monkey

Mocking utility for PHP functions and WordPress plugin API

33812.5M350](/packages/brain-monkey)[php-mock/php-mock-phpunit

Mock built-in PHP functions (e.g. time()) with PHPUnit. This package relies on PHP's namespace fallback policy. No further extension is needed.

1718.2M399](/packages/php-mock-php-mock-phpunit)[php-mock/php-mock-mockery

Mock built-in PHP functions (e.g. time()) with Mockery. This package relies on PHP's namespace fallback policy. No further extension is needed.

392.1M96](/packages/php-mock-php-mock-mockery)[polishsymfonycommunity/symfony-mocker-container

Provides base Symfony dependency injection container enabling service mocking.

1468.0M237](/packages/polishsymfonycommunity-symfony-mocker-container)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
