{"id":19783144,"url":"https://github.com/edmondscommerce/behat-db-context","last_synced_at":"2026-04-15T11:34:27.093Z","repository":{"id":56975102,"uuid":"141426197","full_name":"edmondscommerce/behat-db-context","owner":"edmondscommerce","description":"A Behat context for managing your testing database.","archived":false,"fork":false,"pushed_at":"2018-07-20T05:54:37.000Z","size":15,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-11T02:44:55.693Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/edmondscommerce.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-07-18T11:33:57.000Z","updated_at":"2018-07-20T05:54:39.000Z","dependencies_parsed_at":"2022-08-21T11:50:47.980Z","dependency_job_id":null,"html_url":"https://github.com/edmondscommerce/behat-db-context","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/edmondscommerce%2Fbehat-db-context","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/edmondscommerce%2Fbehat-db-context/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/edmondscommerce%2Fbehat-db-context/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/edmondscommerce%2Fbehat-db-context/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/edmondscommerce","download_url":"https://codeload.github.com/edmondscommerce/behat-db-context/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241114109,"owners_count":19912020,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-12T06:07:25.238Z","updated_at":"2026-04-15T11:34:22.072Z","avatar_url":"https://github.com/edmondscommerce.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Behat DB Context\n## By [Edmonds Commerce](https://www.edmondscommerce.co.uk)\n\nA Behat context for managing your testing database.\n\n### Installation\n\nInstall via composer\n\n\"edmondscommerce/behat-db-context\": \"dev-master@dev\"\n\n\n### Include Context in Behat Configuration\n\n```yaml\ndefault:\n    # ...\n    suites:\n        default:\n        # ...\n            contexts:\n                - # ...\n                - EdmondsCommerce\\BehatDbContext\\DbContext\n            parameters:\n              databaseSettings:\n                importTestingDatabase: true\n                databaseName: some_testing_db\n                pathToSqlDump: '../some_testing_db.sql'\n                customAssertions:\n                  # Must be wrapped in double quotes\n                  - \"SELECT COUNT(*) FROM some_table WHERE some_thing = some_value\"\n                  - \"SELECT COUNT(*) FROM some_table WHERE another_thing = another_value\"\n```\n\n### Features\n\n#### Ensures You're Using the Testing Database\n\nThe context employs platform detection to ensure that your application is currently using\nthe test database you configured in `behat.yml` using `databaseName`.\n\nIf you're not using the correct database then you'll receive an exception and the tests won't\nproceed.\n\nTo add support for you're platform you can follow the steps detailed below in the\n'Adding Support For New Platforms' section.\n\n#### Import Fresh Testing Database\n\nThe context imports a fresh version of the testing database each time you run your test\nsuite. It imports the SQL dump configured in your `behat.yml` using `pathToSqlDump` and\nimports this into the database `databaseName`.\n\nNOTE: this context assumes you have your MYSQL credentials configured in `.my.cnf`.\n\n#### Optional Testing Database Import\n\nWhen `importTestingDatabase` is set to `false` the import step will be skipped. This is useful\nwhile working on the tests locally but should always be set to `true` when finally running\nthe test suite.\n\nEven when this is set to `false` the check to confirm you're using the correct database\nand your custom assertions (see below) will still be run to ensure everything is\nconfigured correctly.\n\n#### Custom Assertions\n\nIn order to flexibly confirm the database is in the correct state the context supports\ncustom assertions. These are simple `COUNT` SQL queries which need to return `1` in order\nto pass. You can provide as many of these as you like.\n\nFor example:\n\n```sql\nSELECT COUNT(*) FROM core_config_data WHERE path = 'web/unsecure/base_url' AND value = 'https://www.base.url.com/'\n```\n\n### Adding Support For New Platforms\n\nAll platform code is contained within the [Platform](src/Util/Platform.php) class. In order to extend\nthis you need to provide two functions; one which handles platform detection and one which handles\ndatabase detection.\n\n#### Platform Detection\n\n##### Add Detection Method\n\nYou need to add a method that can detect the platform from the project root. The Magento platform detection\nsimply does this by looking for `local.xml`:\n\n```php\n    // ...\n\n    const MAGENTO_ONE_PATH_TO_LOCAL_XML = '/public/app/etc/local.xml';\n\n    private static function detectMagentoOnePlatform($projectRoot)\n    {\n        return is_file($projectRoot . self::MAGENTO_ONE_PATH_TO_LOCAL_XML);\n    }\n    \n    // ...\n```\n\n##### Add Method to Detect\n\nYou then need to add this to the detect method:\n\n```php\n    // ...\n    \n    public static function detect()\n    {\n        // ...\n\n        while (true) {\n            if (self::detectMagentoOnePlatform($searchPath)) {\n                return [self::MAGENTO_ONE, $searchPath];\n            }\n\n            // ...\n\n            $searchPath = realpath($searchPath . '/../');\n\n            if ($searchPath === false) {\n                break;\n            }\n        }\n\n        throw new \\RuntimeException('Failed finding project root.');\n    }\n    \n    // ...\n```\n\nAnd add your platform as a constant:\n\n```php\n// ...\n\n    const MAGENTO_ONE = 'magento';\n    \n// ...\n```\n\n#### Database Detection\n\nNow that you have platform detection in place you need to handle database configuration detection.\n\n##### Add Assertion Method\n\nYou need to add a method that can assert that the platforms database is currently configured to use\nthe testing database. If the platform isn't configured correctly it should throw an exception.\n\nHere's the Magento 1 assertion for example:\n\n```php\n    // ...\n    \n    private static function assertMagentoOneUsingTestingDatabase($projectRoot, $databaseName)\n    {\n        $localXml = simplexml_load_string(\n            file_get_contents($projectRoot . Platform::MAGENTO_ONE_PATH_TO_LOCAL_XML)\n        );\n\n        if (! isset($localXml-\u003eglobal-\u003eresources-\u003edefault_setup-\u003econnection-\u003edbname)) {\n            throw new \\RuntimeException(\n                'You need to configure a dbname in your local.xml'\n            );\n        }\n\n        if ((string) $localXml-\u003eglobal-\u003eresources-\u003edefault_setup-\u003econnection-\u003edbname === $databaseName) {\n            return;\n        }\n\n        throw new \\InvalidArgumentException(\n            \"You need to configure Magento to use the testing database '$databaseName' in local.xml\"\n        );\n    }\n    \n    // ...\n```\n\n##### Add Method to Assert\n\nYou then need to add this platform specific assertion to the generic assertion method:\n\n```php\n    // ...\n    \n    public static function assertTestingDatabaseIsBeingUsed($databaseName)\n    {\n        list($platform, $projectRoot) = self::detect();\n\n        switch ($platform) {\n            case self::MAGENTO_ONE:\n                self::assertMagentoOneUsingTestingDatabase($projectRoot, $databaseName);\n                break;\n            // Add your platform specific assertion here...\n        }\n    }\n    \n    // ...\n```\n\nYour platform is now supported!","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fedmondscommerce%2Fbehat-db-context","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fedmondscommerce%2Fbehat-db-context","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fedmondscommerce%2Fbehat-db-context/lists"}