PHPackages                             edmondscommerce/behat-db-context - 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. edmondscommerce/behat-db-context

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

edmondscommerce/behat-db-context
================================

A Behat Context for managing the testing database

0.0.1(7y ago)026MITPHP

Since Jul 19Pushed 7y ago1 watchersCompare

[ Source](https://github.com/edmondscommerce/behat-db-context)[ Packagist](https://packagist.org/packages/edmondscommerce/behat-db-context)[ Docs](https://www.edmondscommerce.co.uk)[ RSS](/packages/edmondscommerce-behat-db-context/feed)WikiDiscussions master Synced 3d ago

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

Behat DB Context
================

[](#behat-db-context)

By [Edmonds Commerce](https://www.edmondscommerce.co.uk)
--------------------------------------------------------

[](#by-edmonds-commerce)

A Behat context for managing your testing database.

### Installation

[](#installation)

Install via composer

"edmondscommerce/behat-db-context": "dev-master@dev"

### Include Context in Behat Configuration

[](#include-context-in-behat-configuration)

```
default:
    # ...
    suites:
        default:
        # ...
            contexts:
                - # ...
                - EdmondsCommerce\BehatDbContext\DbContext
            parameters:
              databaseSettings:
                importTestingDatabase: true
                databaseName: some_testing_db
                pathToSqlDump: '../some_testing_db.sql'
                customAssertions:
                  # Must be wrapped in double quotes
                  - "SELECT COUNT(*) FROM some_table WHERE some_thing = some_value"
                  - "SELECT COUNT(*) FROM some_table WHERE another_thing = another_value"
```

### Features

[](#features)

#### Ensures You're Using the Testing Database

[](#ensures-youre-using-the-testing-database)

The context employs platform detection to ensure that your application is currently using the test database you configured in `behat.yml` using `databaseName`.

If you're not using the correct database then you'll receive an exception and the tests won't proceed.

To add support for you're platform you can follow the steps detailed below in the 'Adding Support For New Platforms' section.

#### Import Fresh Testing Database

[](#import-fresh-testing-database)

The context imports a fresh version of the testing database each time you run your test suite. It imports the SQL dump configured in your `behat.yml` using `pathToSqlDump` and imports this into the database `databaseName`.

NOTE: this context assumes you have your MYSQL credentials configured in `.my.cnf`.

#### Optional Testing Database Import

[](#optional-testing-database-import)

When `importTestingDatabase` is set to `false` the import step will be skipped. This is useful while working on the tests locally but should always be set to `true` when finally running the test suite.

Even when this is set to `false` the check to confirm you're using the correct database and your custom assertions (see below) will still be run to ensure everything is configured correctly.

#### Custom Assertions

[](#custom-assertions)

In order to flexibly confirm the database is in the correct state the context supports custom assertions. These are simple `COUNT` SQL queries which need to return `1` in order to pass. You can provide as many of these as you like.

For example:

```
SELECT COUNT(*) FROM core_config_data WHERE path = 'web/unsecure/base_url' AND value = 'https://www.base.url.com/'
```

### Adding Support For New Platforms

[](#adding-support-for-new-platforms)

All platform code is contained within the [Platform](src/Util/Platform.php) class. In order to extend this you need to provide two functions; one which handles platform detection and one which handles database detection.

#### Platform Detection

[](#platform-detection)

##### Add Detection Method

[](#add-detection-method)

You need to add a method that can detect the platform from the project root. The Magento platform detection simply does this by looking for `local.xml`:

```
    // ...

    const MAGENTO_ONE_PATH_TO_LOCAL_XML = '/public/app/etc/local.xml';

    private static function detectMagentoOnePlatform($projectRoot)
    {
        return is_file($projectRoot . self::MAGENTO_ONE_PATH_TO_LOCAL_XML);
    }

    // ...
```

##### Add Method to Detect

[](#add-method-to-detect)

You then need to add this to the detect method:

```
    // ...

    public static function detect()
    {
        // ...

        while (true) {
            if (self::detectMagentoOnePlatform($searchPath)) {
                return [self::MAGENTO_ONE, $searchPath];
            }

            // ...

            $searchPath = realpath($searchPath . '/../');

            if ($searchPath === false) {
                break;
            }
        }

        throw new \RuntimeException('Failed finding project root.');
    }

    // ...
```

And add your platform as a constant:

```
// ...

    const MAGENTO_ONE = 'magento';

// ...
```

#### Database Detection

[](#database-detection)

Now that you have platform detection in place you need to handle database configuration detection.

##### Add Assertion Method

[](#add-assertion-method)

You need to add a method that can assert that the platforms database is currently configured to use the testing database. If the platform isn't configured correctly it should throw an exception.

Here's the Magento 1 assertion for example:

```
    // ...

    private static function assertMagentoOneUsingTestingDatabase($projectRoot, $databaseName)
    {
        $localXml = simplexml_load_string(
            file_get_contents($projectRoot . Platform::MAGENTO_ONE_PATH_TO_LOCAL_XML)
        );

        if (! isset($localXml->global->resources->default_setup->connection->dbname)) {
            throw new \RuntimeException(
                'You need to configure a dbname in your local.xml'
            );
        }

        if ((string) $localXml->global->resources->default_setup->connection->dbname === $databaseName) {
            return;
        }

        throw new \InvalidArgumentException(
            "You need to configure Magento to use the testing database '$databaseName' in local.xml"
        );
    }

    // ...
```

##### Add Method to Assert

[](#add-method-to-assert)

You then need to add this platform specific assertion to the generic assertion method:

```
    // ...

    public static function assertTestingDatabaseIsBeingUsed($databaseName)
    {
        list($platform, $projectRoot) = self::detect();

        switch ($platform) {
            case self::MAGENTO_ONE:
                self::assertMagentoOneUsingTestingDatabase($projectRoot, $databaseName);
                break;
            // Add your platform specific assertion here...
        }
    }

    // ...
```

Your platform is now supported!

###  Health Score

23

—

LowBetter than 27% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community4

Small or concentrated contributor base

Maturity52

Maturing project, gaining track record

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

Unknown

Total

1

Last Release

2857d ago

### Community

Maintainers

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

---

Tags

testingdatabasebrowser-testingBehat

### Embed Badge

![Health badge](/badges/edmondscommerce-behat-db-context/health.svg)

```
[![Health](https://phpackages.com/badges/edmondscommerce-behat-db-context/health.svg)](https://phpackages.com/packages/edmondscommerce-behat-db-context)
```

###  Alternatives

[imbo/behat-api-extension

API extension for Behat

1082.5M9](/packages/imbo-behat-api-extension)[jonathanjfshaw/phpunitbehat

Use Behat from within PHPUnit.

2143.8k1](/packages/jonathanjfshaw-phpunitbehat)

PHPackages © 2026

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