diff --git a/README.md b/README.md index 2d85d13..2c891dc 100644 --- a/README.md +++ b/README.md @@ -1,102 +1,37 @@ -Behat Doctrine Fixtures -================================= +# Behat Doctrine Fixtures | Version | Build Status | Code Coverage | |:---------:|:-------------:|:-----:| | `master`| [![CI][master Build Status Image]][master Build Status] | [![Coverage Status][master Code Coverage Image]][master Code Coverage] | | `develop`| [![CI][develop Build Status Image]][develop Build Status] | [![Coverage Status][develop Code Coverage Image]][develop Code Coverage] | -## Migrate from 1.x to 2.0 - -[To migrate from 1.x to 2.0, follow our guide.](https://github.com/MacPaw/behat-doctrine-fixtures/blob/master/UPGRADE-2.0.md) - -Installation -============ - -Step 1: Install Bundle ----------------------------------- -Open a command console, enter your project directory and execute: - -```console -$ composer require --dev macpaw/behat-doctrine-fixtures -``` - -This command requires you to have Composer installed globally, as explained -in the [installation chapter](https://getcomposer.org/doc/00-intro.md) -of the Composer documentation. - -If you use PostgreSQL database, you also need to install postgresql-client: +## Installation +To install the Behat Doctrine Fixtures and integrate it with your Behat setup, follow the instructions provided in the [Installation Guide](docs/install.md). -```console -$ apt-get install -y postgresql-client -``` +## Database Support +This library allows you to easily work with fixtures for testing in Behat using Doctrine. It supports two types of databases: -Step 2: Enable the Bundle ----------------------------------- -Then, enable the bundle by adding it to the list of registered bundles -in the `app/AppKernel.php` file of your project: +* [How to configure SQLite](docs/configure_sqlite.md) – a lightweight and fast database for testing. +* [How to configure PostgreSQL](docs/configure_postgre.md) – a powerful and popular relational database for production-like environments. +* [How multiple databases](docs/configure_multiple_databases.md) - To configure multiple databases, define separate connection settings for each database in your Doctrine configuration and reference them appropriately in your application. -```php - ['test' => true] - ); +### Methods - // ... - } +#### [beforeScenario](docs/DatabaseContext/beforeScenario.md) +The `beforeScenario` method loads fixtures for all configured database connections before each Behat scenario. This ensures that the database is in a clean state before testing. - // ... -} -``` +#### [loadFixturesForDefaultConnection](docs/DatabaseContext/loadFixturesForDefaultConnection.md) +The `loadFixturesForDefaultConnection` method loads fixtures into the default database connection. It accepts a comma-separated list of fixture names and loads them into the default connection. -Step 3: Create Behat Doctrine Fixtures Config: ----------------------------------- -`config/packages/test/behat_doctrine_fixtures.yaml ` +#### [loadFixturesForGivenConnection](docs/DatabaseContext/loadFixturesForGivenConnection.md) +The `loadFixturesForGivenConnection` method loads fixtures into a specified database connection. This method allows flexibility by letting you choose which database connection the fixtures should be loaded into, which is useful in multi-database environments. -Configurating behat database context - -```yaml -behat_doctrine_fixtures: - connections: - default: - database_fixtures_paths: - - -``` - -You don't need to explicitly pass database url here, since this bundle uses doctrine connection under the hood. For now, we support PostgreSQL and Sqlite databases. - -If you want to use multiple databases in your project, just add one more connection under ``behat_doctrine_fixtures.connections`` with its own configuration: - -```yaml -behat_doctrine_fixtures: - connections: - default: - database_fixtures_paths: - - - : - run_migrations_command: - database_fixtures_paths: - - -``` - -Step 4: Configure Behat -============= -Go to `behat.yml` +## Migrate from 1.x to 2.0 -```yaml -... - contexts: - - BehatDoctrineFixtures\Context\DatabaseContext -... -``` +[To migrate from 1.x to 2.0, follow our guide.](https://github.com/MacPaw/behat-doctrine-fixtures/blob/master/UPGRADE-2.0.md) [master Build Status]: https://github.com/macpaw/behat-doctrine-fixtures/actions?query=workflow%3ACI+branch%3Amaster [master Build Status Image]: https://github.com/macpaw/behat-doctrine-fixtures/workflows/CI/badge.svg?branch=master diff --git a/docs/DatabaseContext/beforeScenario.md b/docs/DatabaseContext/beforeScenario.md new file mode 100644 index 0000000..50fc9c8 --- /dev/null +++ b/docs/DatabaseContext/beforeScenario.md @@ -0,0 +1,15 @@ +# Method Documentation: `beforeScenario` + +## Description + +The `beforeScenario` method is a hook that is executed before each scenario in Behat. It prepares the database by loading fixtures for all the configured database connections. This method is annotated with `@BeforeScenario`, which ensures it is triggered automatically by Behat before any scenario runs. + +## Method Details + +### Method Signature + +```php +/** + * @BeforeScenario + */ +public function beforeScenario(): void diff --git a/docs/DatabaseContext/loadFixturesForDefaultConnection.md b/docs/DatabaseContext/loadFixturesForDefaultConnection.md new file mode 100644 index 0000000..b24ba28 --- /dev/null +++ b/docs/DatabaseContext/loadFixturesForDefaultConnection.md @@ -0,0 +1,22 @@ +# Method Documentation: `loadFixturesForDefaultConnection` + +## Description + +The `loadFixturesForDefaultConnection` method is a Behat step definition that loads a specified list of fixtures into the default database connection. This step can be used in Behat scenarios to load predefined data into the database before running tests. + +## Method Details + +### Method Signature + +```php +/** + * @Given I load fixtures :fixtures + */ +public function loadFixturesForDefaultConnection(string $fixtures): void +``` + +### Use in tests + +```gherkin +Given I load fixtures "UserFixture, ProductFixture" +``` diff --git a/docs/DatabaseContext/loadFixturesForGivenConnection.md b/docs/DatabaseContext/loadFixturesForGivenConnection.md new file mode 100644 index 0000000..c267c1b --- /dev/null +++ b/docs/DatabaseContext/loadFixturesForGivenConnection.md @@ -0,0 +1,22 @@ +# Method Documentation: `loadFixturesForGivenConnection` + +## Description + +The `loadFixturesForGivenConnection` method is a Behat step definition that loads a specified list of fixtures into a given database connection. This step allows flexibility in choosing the database connection to load the fixtures into, which is useful in multi-database environments. + +## Method Details + +### Method Signature + +```php +/** + * @Given I load fixtures :fixtures for :connectionName connection + */ +public function loadFixturesForGivenConnection(string $fixtures, string $connectionName): void +``` + +### Use in tests + +```gherkin +Given I load fixtures "UserFixture, ProductFixture" for "test_connection" connection +``` \ No newline at end of file diff --git a/docs/configure_multiple_databases.md b/docs/configure_multiple_databases.md new file mode 100644 index 0000000..b6895ff --- /dev/null +++ b/docs/configure_multiple_databases.md @@ -0,0 +1,61 @@ + +# Using Multiple Database Connections + +To work with multiple databases in your Behat project, you can define additional connections under `behat_doctrine_fixtures.connections`. Each connection can have its own configuration, migrations, and fixtures. This setup allows you to run tests on different databases within the same project. + +## Configuration Example + +Below is an example configuration that demonstrates how to set up two database connections: + +```yaml +behat_doctrine_fixtures: + connections: + default: + database_fixtures_paths: + - path/to/fixtures/default + run_migrations_command: doctrine:migrations:migrate --connection=default + second_connection: + database_fixtures_paths: + - path/to/fixtures/second + run_migrations_command: doctrine:migrations:migrate --connection=second +``` + +In this configuration: + +- **default**: The default database connection. + - `database_fixtures_paths`: Points to the directory containing fixtures for this connection. + - `run_migrations_command`: The custom command to run migrations for this connection. +- **second_connection**: A secondary database connection. + - `database_fixtures_paths`: Points to a different set of fixtures. + - `run_migrations_command`: A custom migration command for the second connection. + +## Usage Example in Tests + +To use these multiple connections in your Behat tests, you may want to load fixtures or perform database operations on different databases depending on the context. Here’s an example Behat step definition: + +```php +/** + * @Given I load fixtures :fixtures for :connectionName connection + */ +public function loadFixturesForGivenConnection(string $fixtures, string $connectionName): void +{ + $fixtureAliases = array_map('trim', explode(',', $fixtures)); + $this->loadFixtures($connectionName, $fixtureAliases); +} +``` + +You can then use this step in your Behat scenario: + +```gherkin +Scenario: Load fixtures for multiple databases + Given I load fixtures "Base, User" for "default" connection + And I load fixtures "Order" for "second_connection" connection +``` + +## How it Works + +- **Multiple Connections**: Each connection (e.g., `default`, `second_connection`) can have its own set of fixtures and migrations. +- **Fixture Loading**: Behat allows you to specify which connection to load fixtures for, enabling testing against multiple databases within the same test suite. +- **Custom Migrations**: The `run_migrations_command` ensures that the correct migrations are applied to each respective database before the tests run. + +This setup is useful when your application interacts with more than one database, such as in microservices architectures or when testing different database configurations. diff --git a/docs/configure_postgre.md b/docs/configure_postgre.md new file mode 100644 index 0000000..19a9466 --- /dev/null +++ b/docs/configure_postgre.md @@ -0,0 +1,11 @@ +# Using PostgreSQL in the Project + +## Additional Requirements for PostgreSQL + +If you are using a PostgreSQL database in your project, you need to install the PostgreSQL client on your machine. + +### Installation Steps: + +To install the PostgreSQL client, use the following command: +```bash +apt-get install -y postgresql-client diff --git a/docs/configure_sqlite.md b/docs/configure_sqlite.md new file mode 100644 index 0000000..b1f3558 --- /dev/null +++ b/docs/configure_sqlite.md @@ -0,0 +1 @@ +# Using SQlite in the Project diff --git a/docs/install.md b/docs/install.md new file mode 100644 index 0000000..77a1be6 --- /dev/null +++ b/docs/install.md @@ -0,0 +1,69 @@ +# Installation Guide + +## Step 1: Install the Bundle +To begin, navigate to your project directory and use Composer to download the bundle. + +### For applications using Symfony Flex: +Simply run the following command: + +```bash +composer require --dev macpaw/behat-doctrine-fixtures +``` + +### For applications without Symfony Flex: +If your project doesn't use Symfony Flex, run the same command: + +```bash +composer require --dev macpaw/behat-doctrine-fixtures +``` + +Make sure that Composer is installed globally on your machine. If not, refer to the [Composer installation guide](https://getcomposer.org/doc/00-intro.md) for assistance. + +Next, you'll need to manually register the bundle in the `AppKernel.php` file. Add the following line to the `registerBundles` method: + +```php + ['test' => true] + ); + + // ... + } + + // ... +} +``` + +Step 2: Create Behat Doctrine Fixtures Config: +---------------------------------- +`config/packages/test/behat_doctrine_fixtures.yaml ` + +Configurating behat database context + +```yaml +behat_doctrine_fixtures: + connections: + default: + database_fixtures_paths: + - +``` + + +Step 3: Configure Behat +============= +Go to `behat.yml` + +```yaml +... + contexts: + - BehatDoctrineFixtures\Context\DatabaseContext +... +``` \ No newline at end of file