Skip to content

Commit

Permalink
feat: update documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Yozhef committed Oct 12, 2024
1 parent 1be53af commit 9a8950e
Show file tree
Hide file tree
Showing 8 changed files with 220 additions and 84 deletions.
103 changes: 19 additions & 84 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
<?php
// app/AppKernel.php
## How Usage in Behat
These methods can be used in Behat scenarios to load fixtures into the database, preparing test data for scenarios. For more detailed information on each method, refer to the links above.

// ...
class AppKernel extends Kernel
{
public function registerBundles()
{
$bundles = array(
// ...
BehatDoctrineFixtures\BehatDoctrineFixturesBundle::class => ['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:
- <path to directory with your fixtures>
```
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:
- <path to directory with your fixtures>
<secondConnectionName>:
run_migrations_command: <customMigrationsCommand>
database_fixtures_paths:
- <path to directory with your fixtures>
```
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
Expand Down
15 changes: 15 additions & 0 deletions docs/DatabaseContext/beforeScenario.md
Original file line number Diff line number Diff line change
@@ -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
22 changes: 22 additions & 0 deletions docs/DatabaseContext/loadFixturesForDefaultConnection.md
Original file line number Diff line number Diff line change
@@ -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"
```
22 changes: 22 additions & 0 deletions docs/DatabaseContext/loadFixturesForGivenConnection.md
Original file line number Diff line number Diff line change
@@ -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
```
61 changes: 61 additions & 0 deletions docs/configure_multiple_databases.md
Original file line number Diff line number Diff line change
@@ -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.
11 changes: 11 additions & 0 deletions docs/configure_postgre.md
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions docs/configure_sqlite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Using SQlite in the Project
69 changes: 69 additions & 0 deletions docs/install.md
Original file line number Diff line number Diff line change
@@ -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
<?php
// app/AppKernel.php

// ...
class AppKernel extends Kernel
{
public function registerBundles()
{
$bundles = array(
// ...
BehatDoctrineFixtures\BehatDoctrineFixturesBundle::class => ['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:
- <path to directory with your fixtures>
```
Step 3: Configure Behat
=============
Go to `behat.yml`

```yaml
...
contexts:
- BehatDoctrineFixtures\Context\DatabaseContext
...
```

0 comments on commit 9a8950e

Please sign in to comment.