PHPackages                             reedware/container-testcase - 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. [Framework](/categories/framework)
4. /
5. reedware/container-testcase

ActiveLibrary[Framework](/categories/framework)

reedware/container-testcase
===========================

Enables unit testing with an empty service container.

v3.0.0(1y ago)12101MITPHPPHP &gt;=8.3CI passing

Since Jun 23Pushed 1y ago1 watchersCompare

[ Source](https://github.com/tylernathanreed/container-testcase)[ Packagist](https://packagist.org/packages/reedware/container-testcase)[ RSS](/packages/reedware-container-testcase/feed)WikiDiscussions master Synced yesterday

READMEChangelog (6)Dependencies (11)Versions (7)Used By (0)

Container TestCase
==================

[](#container-testcase)

[![Laravel Version](https://camo.githubusercontent.com/9e1643675487e14c111a75a4914b12e5d36bd1c42de0f642c2558906eba9b417/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c61726176656c2d31312e7825324631322e782d626c7565)](https://laravel.com/)[![Automated Tests](https://github.com/tylernathanreed/container-testcase/actions/workflows/tests.yml/badge.svg)](https://github.com/tylernathanreed/container-testcase/actions/workflows/tests.yml)[![Coding Standards](https://github.com/tylernathanreed/container-testcase/actions/workflows/coding-standards.yml/badge.svg)](https://github.com/tylernathanreed/container-testcase/actions/workflows/coding-standards.yml)[![Code Coverage](https://github.com/tylernathanreed/container-testcase/actions/workflows/coverage.yml/badge.svg)](https://github.com/tylernathanreed/container-testcase/actions/workflows/coverage.yml)[![Static Analysis](https://github.com/tylernathanreed/container-testcase/actions/workflows/static-analysis.yml/badge.svg)](https://github.com/tylernathanreed/container-testcase/actions/workflows/static-analysis.yml)[![Latest Stable Version](https://camo.githubusercontent.com/20776c0b2436e21b0f018a23b957edaf708057b02ba1378d968304d5090038bd/68747470733a2f2f706f7365722e707567782e6f72672f72656564776172652f636f6e7461696e65722d74657374636173652f762f737461626c65)](https://packagist.org/packages/reedware/container-testcase)

This package enables unit testing with an empty service container.

Table of Contents
-----------------

[](#table-of-contents)

- [Introduction](#introduction)
- [Installation](#installation)
    - [1. Versioning](#versioning)
    - [2. Extension](#extension)
    - [3. Trait](#trait)
- [Usage](#usage)
    - [1. Service Provider](#service-provider)
    - [2. The Application Instance](#application)
    - [3. The Service Container](#container)
        - [Make](#make)
        - [Mock](#mock)
        - [Mock As](#mock-as)
        - [Mock Config](#mock-config)
    - [4. Foundation Helpers](#foundation-helpers)
- [Assertions](#assertions)
    - [1. Array Subset](#array-subset)
- [Pest](#pest)
    - [1. Expectations](#pest-expectations)
    - [2. Pest Helpers](#pest-helpers)

Introduction
------------

[](#introduction)

I'm a huge advocate for unit testing, but when developing packages for Laravel, I often find myself needing the service container within my unit tests. This package simply implements a pattern I've been following for years, but never defined in a shared location.

This approach to testing keeps your test cases light, as you don't have to boot the entire Laravel application, but you still get some quality of life features necessary for testing services created as a part of package development.

Installation
------------

[](#installation)

Install this package using Composer:

```
composer require reedware/container-testcase --dev

```

Take note of the `--dev` flag. This package is intended for testing. As such, it should only be required in dev settings.

### 1. Versioning

[](#1-versioning)

This package is maintained with the latest version of Laravel in mind, but follows Laravel's [Support Policy](https://laravel.com/docs/master/releases#support-policy).

PackageLaravelPHP3.x11.x - 12.x8.3 - 8.4+2.x10.x - 11.x8.2 - 8.4+1.x9.x - 10.x8.1 - 8.3+After deciding which version is right for you, you have two installation options: Extension or Trait.

### 2. Extension

[](#2-extension)

Change your test case to extend from `Reedware\ContainerTestCase\ContainerTestCase`.

```
use Reedware\ContainerTestCase\ContainerTestCase;

class TestCase extends ContainerTestCase
{
    /* ... */
}
```

### 3. Trait

[](#3-trait)

Add the `ServiceContainer` trait to your test case.

```
use PHPUnit\Framework\TestCase as BaseTestCase;
use Reedware\ContainerTestCase\ServiceContainer;

class TestCase extends BaseTestCase
{
    use ServiceContainer;
}
```

Be mindful that the trait overrides the `setUp()` and `tearDown()` methods. If you have your own definition, you'll need to call the setUp/tearDown functionality provided by the trait.

```
protected function setUp(): void
{
    parent::setUp();

    $this->setUpServiceContainer();
}

protected function tearDown(): void
{
    $this->tearDownServiceContainer();

    parent::tearDown();
}
```

Usage
-----

[](#usage)

### 1. Service Provider

[](#1-service-provider)

If you're writing a package that binds into the service container, chances are, you have a service provider. You'll want to register your service provider during the setup process:

```
protected function setUp(): void
{
    parent::setUp();

    $this->registerServiceProvider(MyServiceProvider::class);
}
```

You can do something similar for booting:

```
protected function setUp(): void
{
    parent::setUp();

    $this->bootServiceProvider(MyServiceProvider::class);
}
```

Any dependencies in your `boot()` method will be injected from the service container, exactly how the Laravel Framework does it.

### 2. The Application Instance

[](#2-the-application-instance)

The application instance in your test is not the same as the one provided by the Laravel Framework. The provided application instance offers full container functionality, but throws by default on any non-container method (e.g. `$this->app->version()`). If you need to use these methods in your service providers, you can provide expectations to the application instance, as it also acts as a partial mock instance.

```
/** @test */
public function it_bails_on_production(): void
{
    $this->app
        ->shouldReceive('environment')
        ->withNoArgs()
        ->once()
        ->andReturn('production');

    $this->registerServiceProvider(MyServiceProvider::class);

    $this->assertFalse($this->app->bound(MyService::class));
}
```

### 3. The Service Container

[](#3-the-service-container)

The application instance acts as your service container. Similar to Laravel's feature test, some mocking and container helpers are available as methods on your test cases:

#### Make

[](#make)

Creates a new instance of the specified service using the container. Alias for `$this->app->make(...)`.

Usage:

```
$service = $this->make(MyService::class);
```

#### Mock

[](#mock)

Creates a mock of the specified service and binds it to the container.

Usage:

```
$mock = $this->mock(MyService::class, function (MockInterface $mock) {
    $mock
        ->shouldReceive(...)
        ->...
});
```

or

```
$mock = $this->mock(MyService::class);

$mock
    ->shouldReceive(...)
    ->...
```

Note: This method binds the mock instance to `MyService::class`. If you just want a mocked service without binding it to the container, use `Mockery::mock(MyService::class)`.

#### Mock As

[](#mock-as)

Creates a mock of the specified service and binds it to the container under the given alias. The `$this->mock(...)` method will bind the service to the container using its class name. However, if you wish to bind it under a different name, you can use `$this->mockAs(...)`.

Usage:

```
$mock = $this->mockAs(MyService::class, 'acme.service', function (MockInterface $mock) {
    /* ... */
});
```

Anything that resolves `acme.service` from the container will now receive your mocked service.

#### Mock Config

[](#mock-config)

For packages that ship with a configuration file, and still want to unit test their services, you can use `$this->mockConfig(...)` to bind a basic configuration repository to the container that yields the provided values.

Usage:

```
$this->mockConfig([
    'my-package' => [
        'setting-1' => 'foo'
    ]
]);

config('my-package.setting-1'); // "foo"
```

4. Foundation Helpers
---------------------

[](#4-foundation-helpers)

There are some helper methods that ship with the Laravel Framework that help you interact with the service container. This package replicates a subset of those same methods, so that you get the same quality of life, but without having to include the entire Laravel Framework in your package. Don't worry, if you do decide to use the entire Laravel Framework, Laravel's helpers will take precedence over these.

- [app](https://laravel.com/docs/10.x/helpers#method-app)
- [dd](https://laravel.com/docs/10.x/helpers#method-dd)
- [dump](https://laravel.com/docs/10.x/helpers#method-dump)
- [now](https://laravel.com/docs/10.x/helpers#method-now)
- [resolve](https://laravel.com/docs/10.x/helpers#method-resolve)
- [today](https://laravel.com/docs/10.x/helpers#method-today)

Assertions
==========

[](#assertions)

Laravel's Test Case comes with some basic assertions that are useful in unit tests. These have also been included.

1. Array Subset
---------------

[](#1-array-subset)

Asserts that an array has a specified subset.

Usage:

```
/** @test */
public function it_has_some_attributes(): void
{
    $myObject = $this->newMyObject();

    $this->assertArraySubset([
        'foo' => 'bar'
    ], $myObject->toArray());
}
```

Pest
====

[](#pest)

If you're using [Pest](https://pestphp.com/), this package provides some additional extensions.

1. Expectations
---------------

[](#1-expectations)

The assertions for PHPUnit offered by this package also have their own Pest expectation flavors:

- **PHPUnit =&gt; Pest**
- `$this->assertArraySubset($subset, $actual)` =&gt; `expect($actual)->toContainArraySubset($subset)`

2. Pest Helpers
---------------

[](#2-pest-helpers)

If you're focused on package development, and you want to use Pest, be wary of the [Laravel Pest Plugin](https://github.com/pestphp/pest-plugin-laravel), (`pestphp/pest-plugin-laravel`). This package requires the entire Laravel Framework, which isn't always the best approach for package development. Therefore, this package includes a subset of the helpers offered by the Laravel Pest Plugin, specifically those that interact with the service container.

- `swap($abstract, $instance)`
- `instance($abstract, $instance)`
- `mock($abstract, $mock = null)`
- `mockAs($abstract, $alias, $mock = null)`
- `partialMock($abstract, $mock = null)`
- `spy($abstract, $mock = null)`

###  Health Score

36

—

LowBetter than 79% of packages

Maintenance46

Moderate activity, may be stable

Popularity14

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity64

Established project with proven stability

 Bus Factor1

Top contributor holds 98.7% 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 ~139 days

Recently: every ~174 days

Total

6

Last Release

408d ago

Major Versions

v1.1.0 → v2.0.02025-01-03

v2.0.0 → v3.0.02025-05-21

PHP version history (3 changes)v1.0.0PHP &gt;=8.1

v2.0.0PHP &gt;=8.2

v3.0.0PHP &gt;=8.3

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/6486381?v=4)[Tyler](/maintainers/tylernathanreed)[@tylernathanreed](https://github.com/tylernathanreed)

---

Top Contributors

[![tylernathanreed](https://avatars.githubusercontent.com/u/6486381?v=4)](https://github.com/tylernathanreed "tylernathanreed (76 commits)")[![calebdw](https://avatars.githubusercontent.com/u/4176520?v=4)](https://github.com/calebdw "calebdw (1 commits)")

---

Tags

phptestingunitcontainerframeworktestlaravel

###  Code Quality

TestsPest

Static AnalysisPHPStan

Code StyleLaravel Pint

Type Coverage Yes

### Embed Badge

![Health badge](/badges/reedware-container-testcase/health.svg)

```
[![Health](https://phpackages.com/badges/reedware-container-testcase/health.svg)](https://phpackages.com/packages/reedware-container-testcase)
```

###  Alternatives

[laravel/ai

The official AI SDK for Laravel.

1.0k3.2M194](/packages/laravel-ai)[psalm/plugin-laravel

Psalm plugin for Laravel

3355.3M346](/packages/psalm-plugin-laravel)[roots/acorn

Framework for Roots WordPress projects built with Laravel components.

9762.4M131](/packages/roots-acorn)[laravel/mcp

Rapidly build MCP servers for your Laravel applications.

77022.3M150](/packages/laravel-mcp)[illuminate/database

The Illuminate Database package.

2.8k54.9M11.6k](/packages/illuminate-database)[larastan/larastan

Larastan - Discover bugs in your code without running it. A phpstan/phpstan extension for Laravel

6.5k55.4M8.4k](/packages/larastan-larastan)

PHPackages © 2026

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