PHPackages                             dbx12/yii2-mock-database - 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. dbx12/yii2-mock-database

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

dbx12/yii2-mock-database
========================

Mock connection for the Yii2 database connection. This connection can be configured to fail operations for testing purposes.

v1.0.2(3mo ago)050MITPHPPHP ^8.0CI passing

Since Sep 1Pushed 3mo ago1 watchersCompare

[ Source](https://github.com/DBX12/yii2-mock-database)[ Packagist](https://packagist.org/packages/dbx12/yii2-mock-database)[ RSS](/packages/dbx12-yii2-mock-database/feed)WikiDiscussions master Synced yesterday

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

Mock database failures for Yii 2
================================

[](#mock-database-failures-for-yii-2)

This library provides adapted connections and command classes for Yii2 database connections. With it, you can force database failures in your tests to check how your application behaves when the database is not reachable for example.

Setup and usage
---------------

[](#setup-and-usage)

1. Install with `composer require --dev dbx12/yii2-mock-database`
2. Define the `\dbx12\yii2MockDatabase\Connection` class as your database connection class.
3. During the tests, call `Yii::$app->db->failAlways()` to fail all subsequent commands. Make sure you've loaded your fixtures before doing that.
4. Return to normal behavior by calling `Yii::$app->db->passAlways()`
5. Optionally register the CleanupExtension in your phpunit.xml to get rid of old `mockDatabase.dat` files (see below)

Other methods
-------------

[](#other-methods)

- `failNextCommand($count = 1)` allows you to only **fail** a given number of commands and then return to "normal behavior"
- `passNextCommand($count = 1)` allows you to only **pass** a given number of commands and then return to "always fail"

Cleanup Extension
-----------------

[](#cleanup-extension)

The cleanup extension deletes all files given to it in the first argument after the last test. You can use it to clean your output directory and prevent old files from affecting future test runs. All paths must be either absolute or relative to the working directory of phpunit. A configuration example is in the code block below, it assumes your working directory is the project root (you call phpunit as `vendor/bin/phpunit`).

```

            tests/_output/mockDatabase.dat

```

###  Health Score

41

—

FairBetter than 89% of packages

Maintenance81

Actively maintained with recent releases

Popularity8

Limited adoption so far

Community7

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 ~805 days

Total

3

Last Release

103d ago

PHP version history (2 changes)v1.0.0PHP &gt;7.4

v1.0.1PHP ^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/7dd9e7c6d0c101b23d35ddde728a50a7263b43f2103416c3022cb5f6ca0fa7ab?d=identicon)[DBX12](/maintainers/DBX12)

---

Top Contributors

[![DBX12](https://avatars.githubusercontent.com/u/6484542?v=4)](https://github.com/DBX12 "DBX12 (10 commits)")

---

Tags

testingyii2

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/dbx12-yii2-mock-database/health.svg)

```
[![Health](https://phpackages.com/badges/dbx12-yii2-mock-database/health.svg)](https://phpackages.com/packages/dbx12-yii2-mock-database)
```

###  Alternatives

[phpspec/prophecy

Highly opinionated mocking framework for PHP 5.3+

8.5k551.7M682](/packages/phpspec-prophecy)[vimeo/psalm

A static analysis tool for finding errors in PHP applications

5.8k77.5M6.7k](/packages/vimeo-psalm)[brianium/paratest

Parallel testing for PHP

2.5k118.8M754](/packages/brianium-paratest)[beberlei/assert

Thin assertion library for input validation in business models.

2.4k96.9M571](/packages/beberlei-assert)[mikey179/vfsstream

Virtual file system to mock the real file system in unit tests.

1.4k108.0M2.7k](/packages/mikey179-vfsstream)[orchestra/testbench

Laravel Testing Helper for Packages Development

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

PHPackages © 2026

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