-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
220 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Using SQlite in the Project |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
... | ||
``` |