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.3(1mo ago)050MITPHPPHP ^8.0CI passing

Since Sep 1Pushed 1mo 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 3w ago

READMEChangelog (4)Dependencies (4)Versions (5)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

44

—

FairBetter than 90% of packages

Maintenance90

Actively maintained with recent releases

Popularity8

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity59

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

Total

4

Last Release

48d 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 (12 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

[dms/phpunit-arraysubset-asserts

This package provides ArraySubset and related asserts once deprecated in PHPUnit 8

14429.2M360](/packages/dms-phpunit-arraysubset-asserts)

PHPackages © 2026

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