PHPackages                             thatsus/test-api-protect - 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. thatsus/test-api-protect

ActiveLibrary

thatsus/test-api-protect
========================

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

v1.0.1(7y ago)170.7k—0%1MITPHPPHP &gt;=7.0

Since Jul 17Pushed 7y ago2 watchersCompare

[ Source](https://github.com/thatsus/test-api-protect)[ Packagist](https://packagist.org/packages/thatsus/test-api-protect)[ RSS](/packages/thatsus-test-api-protect/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (2)Dependencies (1)Versions (3)Used By (0)

Test API Protect
================

[](#test-api-protect)

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
====================================

[](#protect-any-classes-in-laravel-tests)

Install
-------

[](#install)

```
composer require thatsus/test-api-protect
```

Add to TestCase
---------------

[](#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
-----------------------------

[](#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
==========

[](#no-trouble)

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

Contribution
============

[](#contribution)

If you find a bug or want to contribute to the code or documentation, you can help by submitting an [issue](https://github.com/thatsus/test-api-protect/issues) or a [pull request](https://github.com/thatsus/test-api-protect/pulls).

License
=======

[](#license)

[MIT](http://opensource.org/licenses/MIT)

###  Health Score

33

—

LowBetter than 75% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity32

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% 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 ~62 days

Total

2

Last Release

2791d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/8b3b4c01d8549581c94d638549ddf875fd2bcd46f28ea72533ad7b93e5b25d8a?d=identicon)[dankuck](/maintainers/dankuck)

---

Top Contributors

[![dankuck](https://avatars.githubusercontent.com/u/277837?v=4)](https://github.com/dankuck "dankuck (3 commits)")

### Embed Badge

![Health badge](/badges/thatsus-test-api-protect/health.svg)

```
[![Health](https://phpackages.com/badges/thatsus-test-api-protect/health.svg)](https://phpackages.com/packages/thatsus-test-api-protect)
```

PHPackages © 2026

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