定制 thatsus/test-api-protect 二次开发

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

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

thatsus/test-api-protect

Composer 安装命令:

composer require thatsus/test-api-protect

包简介

Avoid hitting third party API's by forcing their client classes to be mocked in tests

README 文档

README

Avoid hitting third party API's by forcing their client classes to be mocked in Laravel and Lumen tests.

Protect any classes in Laravel tests

Install

composer require thatsus/test-api-protect

Add to TestCase

  • Use the ProtectClasses trait,
  • list the $protected_classes, and
  • call protectClasses in the setUp.
use ThatsUs\ProtectClasses;

class TestCase extends \Illuminate\Foundation\Testing\TestCase
{
    
    use ProtectClasses;

    protected $protected_classes = [
        \Facebook\Facebook::class,
        \GuzzleHttp\Client::class,
        \App\SomeExpensiveClass::class,
    ];

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

        $this->protectClasses();
    }

    ...
}

Make Mistakes with Confidence

Imagine you forgot that this Innocent class uses \App\SomeExpensiveClass.

class InnocentTest extends TestCase
{

    public function testInnocentMethod()
    {
        $good_guy = new Innocent();
        $this->assertTrue($good_guy->everythingIsFine());
    }
}
class Innocent 
{

    public function everythingIsFine()
    {
        app('App\SomeExpensiveClass')->doThing();
        return true;
    }
}

Now you get this error.

PHPUnit 5.7.16 by Sebastian Bergmann and contributors.

..E..                                                               5 / 5 (100%)

Time: 152 ms, Memory: 14.00MB

There was 1 error:

1) ThatsUs\InnocentTest::testInnocentMethod
Exception: The App\SomeExpensiveClass instance is protected for tests. Setup 
a mock object using App::bind('App\SomeExpensiveClass', Closure). Method 
called: doThing.

./vendor/ThatsUs/FakeProtectedClass.php:21
./src/Innocent.php:10
./tests/InnocentTest.php:15

ERRORS!
Tests: 5, Assertions: 6, Errors: 1.

You just saved yourself from doing something dangerous! Pat yourself on the back and go edit that test file.

use Illuminate\Support\Facades\App;
use Mockery;

class InnocentTest extends TestCase
{

    public function testInnocentMethod()
    {
        App::bind(\App\SomeExpensiveClass::class, function () {
            $mock = Mockery::mock(\App\SomeExpensiveClass::class);
            $mock->shouldReceive('doThing')->once();
            return $mock;
        });

        $good_guy = new Innocent();
        $this->assertTrue($good_guy->everythingIsFine());
    }
}

No Trouble

If the code under test only instantiates a class without calling any methods on it, no exception is thrown.

Contribution

If you find a bug or want to contribute to the code or documentation, you can help by submitting an issue or a pull request.

License

MIT

thatsus/test-api-protect 适用场景与选型建议

thatsus/test-api-protect 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 74.45k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2018 年 07 月 17 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-07-17