### Install Behat Doctrine Fixtures using Composer Source: https://github.com/macpaw/behat-doctrine-fixtures/blob/develop/docs/install.md This command installs the Behat Doctrine Fixtures bundle using Composer. It is compatible with both Symfony Flex and non-Symfony Flex applications. Ensure Composer is installed globally before running this command. ```bash composer require --dev macpaw/behat-doctrine-fixtures ``` -------------------------------- ### PostgreSQL Client Installation - Bash Source: https://context7.com/macpaw/behat-doctrine-fixtures/llms.txt Installs the PostgreSQL client on a Debian-based system (like Ubuntu) using apt-get. This command is necessary for performing database operations with PostgreSQL from the command line. ```bash # Install PostgreSQL client for database operations apt-get install -y postgresql-client ``` -------------------------------- ### Example Alice Fixture File for Product Entity Source: https://context7.com/macpaw/behat-doctrine-fixtures/llms.txt An example Alice YAML file defining fixtures for the `App\Entity\Product` entity. It demonstrates setting properties, referencing other entities, and using data generation helpers. ```yaml # Fixture file: tests/Fixtures/ORM/Product.yml App\Entity\Product: product_laptop: name: 'MacBook Pro' price: 1999.99 category: '@category_electronics' createdAt: '' product_mouse: name: 'Magic Mouse' price: 79.99 category: '@category_electronics' createdAt: '' ``` -------------------------------- ### Configure Behat Doctrine Fixtures Database Context Source: https://github.com/macpaw/behat-doctrine-fixtures/blob/develop/docs/install.md This YAML configuration sets up the database context for Behat Doctrine Fixtures. It specifies the path to the directory containing your database fixtures. This configuration should be placed in `config/packages/test/behat_doctrine_fixtures.yaml`. ```yaml behat_doctrine_fixtures: connections: default: database_fixtures_paths: - ``` -------------------------------- ### Fixture Definition Example - YAML Source: https://context7.com/macpaw/behat-doctrine-fixtures/llms.txt An example of a fixture file defined in YAML format. This demonstrates how to define entities, their properties, and use dynamic data generation functions for various fields. ```yaml # Fixture file: tests/Fixtures/ORM/User.yml App\Entity\User: user_admin: username: 'admin' email: 'admin@example.com' password: '' roles: ['ROLE_ADMIN', 'ROLE_USER'] isActive: true createdAt: '' # Example of dynamic date generation user_{1..10}: username: '' # Example of generating multiple users with dynamic data email: '' password: '' roles: ['ROLE_USER'] isActive: '' createdAt: '' ``` -------------------------------- ### Example Gherkin Step to Load Multiple Fixtures Source: https://context7.com/macpaw/behat-doctrine-fixtures/llms.txt An example of a Behat Gherkin scenario demonstrating how to use the 'I load fixtures' step to load multiple Alice data fixtures before executing test steps. ```gherkin # Example: Load multiple fixtures for testing Feature: Product Catalog Scenario: Display products with categories Given I load fixtures "Category, Product, ProductImage" When I visit the catalog page Then I should see 10 products And each product should have a category ``` -------------------------------- ### Configure Behat to use DatabaseContext Source: https://github.com/macpaw/behat-doctrine-fixtures/blob/develop/docs/install.md This YAML snippet shows how to configure Behat to use the `DatabaseContext` provided by the Behat Doctrine Fixtures bundle. Add this to your `behat.yml` file to enable the database context for your Behat tests. ```yaml ... contexts: - BehatDoctrineFixtures\Context\DatabaseContext ... ``` -------------------------------- ### Register BehatDoctrineFixturesBundle in AppKernel.php Source: https://github.com/macpaw/behat-doctrine-fixtures/blob/develop/docs/install.md This PHP code snippet shows how to manually register the BehatDoctrineFixturesBundle within the `registerBundles` method of your `AppKernel.php` file. This is necessary for applications not using Symfony Flex. ```php ['test' => true] ); // ... } // ... } ``` -------------------------------- ### Define Entity Relationships with References in YAML Source: https://context7.com/macpaw/behat-doctrine-fixtures/llms.txt This example demonstrates how to define entity relationships using references in Behat Doctrine Fixtures. It shows the creation of categories and products, where products reference categories using '@category_*'. This allows for complex data structures and relational integrity in test fixtures. ```yaml # tests/Fixtures/ORM/Category.yml App\Entity\Category: category_electronics: name: 'Electronics' slug: 'electronics' category_books: name: 'Books' slug: 'books' ``` ```yaml # tests/Fixtures/ORM/Product.yml App\Entity\Product: product_{1..20}: name: '' description: '' price: '' stock: '' category: '@category_*' # Random category reference createdAt: '' updatedAt: '' ``` ```yaml # tests/Fixtures/ORM/Order.yml App\Entity\Order: order_{1..5}: orderNumber: 'ORD-' customer: '@user_' status: '' totalAmount: '' createdAt: '' items: '5x @product_*' # 5 random products ``` -------------------------------- ### Load Fixtures for Specific Connection - PHP Source: https://context7.com/macpaw/behat-doctrine-fixtures/llms.txt Loads specified fixtures into a named database connection. This method is used within Behat scenarios to enable multi-database test setups by allowing different sets of fixtures to be loaded into different connections. ```php loadFixtures($connectionName, $fixtureAliases); } ``` -------------------------------- ### PHP: `beforeScenario` Behat Hook for Database Fixture Loading Source: https://github.com/macpaw/behat-doctrine-fixtures/blob/develop/docs/DatabaseContext/beforeScenario.md This PHP method, annotated with `@BeforeScenario`, serves as a hook in Behat to execute before each test scenario. Its primary function is to load database fixtures for all configured connections, preparing the database for scenario execution. It has no direct inputs or outputs and relies on Behat's event system. ```php /** * @BeforeScenario */ public function beforeScenario(): void ``` -------------------------------- ### Load Fixtures for Default Connection (PHP/Gherkin) Source: https://github.com/macpaw/behat-doctrine-fixtures/blob/develop/docs/DatabaseContext/loadFixturesForDefaultConnection.md The `loadFixturesForDefaultConnection` method is a Behat step definition written in PHP. It accepts a string of fixture names as input and loads them into the default database connection. This is typically used in Gherkin feature files to set up test data. ```php /** * @Given I load fixtures :fixtures */ public function loadFixturesForDefaultConnection(string $fixtures): void ``` ```gherkin Given I load fixtures "UserFixture, ProductFixture" ``` -------------------------------- ### Core Fixture Loading with Backup/Restore - PHP Source: https://context7.com/macpaw/behat-doctrine-fixtures/llms.txt The core fixture loading method that includes intelligent backup and restore capabilities for optimized test performance. It checks for existing backups of fixture combinations, restoring from backup if available, or preparing the schema, loading fixtures, and creating a backup if not. ```php databaseHelper = $databaseHelper; } /** * @Given I have a clean database with :fixtures */ public function loadCustomFixtures(string $fixtures): void { $fixtureAliases = array_map('trim', explode(',', $fixtures)); // Loads fixtures with automatic backup/restore: // 1. Checks if backup exists for this fixture combination // 2. If yes: restores from backup (fast) // 3. If no: prepares schema, loads fixtures, creates backup $this->databaseHelper->loadFixtures($fixtureAliases); } } ``` -------------------------------- ### Configure Multiple Database Connections (YAML) Source: https://github.com/macpaw/behat-doctrine-fixtures/blob/develop/docs/configure_multiple_databases.md Defines how to configure multiple database connections in Behat. It specifies the fixture paths and migration commands for each connection. This allows for isolated testing against different databases. ```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 ``` -------------------------------- ### PostgreSQL Environment Variable for Test DB - .env Source: https://context7.com/macpaw/behat-doctrine-fixtures/llms.txt Defines the DATABASE_URL environment variable for testing with PostgreSQL. This string specifies the connection parameters, including user, password, host, port, database name, server version, and character set. ```env # .env.test DATABASE_URL="postgresql://testuser:testpass@localhost:5432/test_db?serverVersion=14&charset=utf8" ``` -------------------------------- ### DatabaseContext::beforeScenario() - Automatic Fixture Loading Source: https://context7.com/macpaw/behat-doctrine-fixtures/llms.txt This PHP method, automatically triggered by Behat's `@BeforeScenario` hook, loads fixtures for all configured database connections before each scenario. It ensures a clean database state for every test. ```php databaseHelperCollection->getConnectionNameList(); foreach ($connectionNameList as $connectionName) { $this->loadFixtures($connectionName, []); } } ``` -------------------------------- ### Behat Feature File for Shopping Cart Management Source: https://context7.com/macpaw/behat-doctrine-fixtures/llms.txt This Gherkin feature file outlines scenarios for managing a shopping cart. It includes background steps for loading fixtures and user authentication, followed by scenarios for adding and removing products from the cart. It demonstrates interaction with fixtures loaded by Behat Doctrine Fixtures. ```gherkin # features/shopping_cart.feature Feature: Shopping Cart Management As a customer I want to manage items in my shopping cart So that I can purchase products Background: Given I load fixtures "Category, Product, User" Scenario: Add product to empty cart Given I am logged in as "user_1" When I add product "product_laptop" to my cart with quantity 2 Then my cart should contain 2 items And the cart total should be "$3999.98" Scenario: Remove product from cart Given I am logged in as "user_1" And I load fixtures "Cart" When I remove product "product_mouse" from my cart Then my cart should contain 1 item And the product "product_mouse" should not be in my cart ``` -------------------------------- ### SQLite Doctrine Configuration - YAML Source: https://context7.com/macpaw/behat-doctrine-fixtures/llms.txt Configures Doctrine DBAL to use SQLite as the database driver for testing. This YAML snippet defines the connection URL, pointing to a file-based SQLite database within the project's var directory. ```yaml # config/packages/test/doctrine.yaml doctrine: dbal: connections: default: url: 'sqlite:///%kernel.project_dir%/var/test.db' driver: 'pdo_sqlite' ``` -------------------------------- ### Load Fixtures for Specific Connection (PHP) Source: https://github.com/macpaw/behat-doctrine-fixtures/blob/develop/docs/configure_multiple_databases.md Provides a Behat step definition in PHP to load fixtures for a specified database connection. It parses fixture names and connection names to facilitate targeted fixture loading. ```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); } ``` -------------------------------- ### Retrieve DatabaseHelperCollection and DatabaseHelper (PHP) Source: https://github.com/macpaw/behat-doctrine-fixtures/blob/develop/UPGRADE-2.0.md Shows how to access the DatabaseHelperCollection and then a specific DatabaseHelper instance for a given connection name using PHP. This replaces the direct usage of DatabaseHelper in previous versions when loading fixtures outside of a Behat context. ```php $databaseHelperCollection = $container->get(DatabaseHelperCollection::class); $databaseHelper = $databaseHelperCollection->getDatabaseHelperByConnectionName($connectionName); ``` -------------------------------- ### Manage Multiple Database Helpers - PHP Source: https://context7.com/macpaw/behat-doctrine-fixtures/llms.txt Manages multiple DatabaseHelper instances for multi-database configurations, providing connection-based lookup. This class allows for granular control over fixture loading across different database connections within a test environment. ```php collection = $collection; } public function setupTestEnvironment(): void { // Get all configured connection names $connections = $this->collection->getConnectionNameList(); // Returns: ['default', 'inventory', 'billing'] // Load specific fixtures for each connection foreach ($connections as $connectionName) { $helper = $this->collection->getDatabaseHelperByConnectionName($connectionName); switch ($connectionName) { case 'inventory': $helper->loadFixtures(['Warehouse', 'Product']); break; case 'billing': $helper->loadFixtures(['Customer', 'Invoice']); break; default: $helper->loadFixtures(['Base']); } } } public function resetConnection(string $connectionName): void { $helper = $this->collection->getDatabaseHelperByConnectionName($connectionName); $helper->loadFixtures([]); // Reset to empty database with schema } } ``` -------------------------------- ### Update Configuration for Multiple Database Connections (YAML) Source: https://github.com/macpaw/behat-doctrine-fixtures/blob/develop/UPGRADE-2.0.md Demonstrates the 'before' and 'after' YAML configuration for behat-doctrine-fixtures when migrating to version 2.0. This highlights the introduction of the 'connections' option for managing multiple database connections and the change in how fixture paths are defined. ```yaml behat_doctrine_fixtures: database_context: dataFixturesPath: '%kernel.project_dir%/tests/DataFixtures/ORM' ``` ```yaml behat_doctrine_fixtures: connections: default: database_fixtures_paths: - '%kernel.project_dir%/tests/DataFixtures/ORM' ``` -------------------------------- ### PostgreSQL Doctrine Configuration - YAML Source: https://context7.com/macpaw/behat-doctrine-fixtures/llms.txt Configures Doctrine DBAL to use PostgreSQL as the database driver. This YAML snippet specifies connection details like URL, driver, server version, and character set for the 'default' connection. ```yaml # config/packages/test/doctrine.yaml doctrine: dbal: connections: default: url: '%env(resolve:DATABASE_URL)%' driver: 'pdo_pgsql' server_version: '14' charset: utf8 ``` -------------------------------- ### DatabaseContext::loadFixturesForDefaultConnection() - Load Specific Fixtures Source: https://context7.com/macpaw/behat-doctrine-fixtures/llms.txt Loads specified fixtures into the default database connection using a Gherkin step. Fixtures are provided as a comma-separated string and are mapped to Alice data fixtures. ```php // Used in Behat scenarios with the step definition: // @Given I load fixtures :fixtures use BehatDoctrineFixtures\Context\DatabaseContext; public function loadFixturesForDefaultConnection(string $fixtures): void { $fixtureAliases = array_map('trim', explode(',', $fixtures)); $this->loadFixtures('default', $fixtureAliases); } ``` -------------------------------- ### Load Fixtures for Given Connection (PHP) Source: https://github.com/macpaw/behat-doctrine-fixtures/blob/develop/docs/DatabaseContext/loadFixturesForGivenConnection.md Loads specified fixtures into a named Doctrine database connection. This step definition is written in PHP and uses the Behat testing framework. It requires fixtures to be specified as a comma-separated string and the connection name. ```php /** * @Given I load fixtures :fixtures for :connectionName connection */ public function loadFixturesForGivenConnection(string $fixtures, string $connectionName): void { // Method implementation would go here } ``` ```gherkin Given I load fixtures "UserFixture, ProductFixture" for "test_connection" connection ``` -------------------------------- ### Configure Behat Doctrine Fixtures for Single Database Source: https://context7.com/macpaw/behat-doctrine-fixtures/llms.txt Basic configuration for the Behat Doctrine Fixtures bundle for a single database connection. Specifies fixture paths and the command to run migrations. ```yaml behat_doctrine_fixtures: connections: default: database_fixtures_paths: - '%kernel.project_dir%/tests/Fixtures/ORM' run_migrations_command: 'bin/console doctrine:migrations:migrate --env=test --no-interaction --allow-no-migration' preserve_migrations_data: false ``` -------------------------------- ### PHP Behat Context for Shopping Cart Logic Source: https://context7.com/macpaw/behat-doctrine-fixtures/llms.txt This PHP code defines a Behat context class for managing shopping cart operations. It implements methods for logging in users, adding products to the cart, and interacting with the entity manager to persist changes. This context is used in conjunction with Gherkin feature files to drive BDD tests. ```php databaseContext = $databaseContext; $this->entityManager = $entityManager; } /** * @Given I am logged in as :username */ public function iAmLoggedInAs(string $username): void { $this->currentUser = $this->entityManager ->getRepository(User::class) ->findOneBy(['username' => $username]); if (!$this->currentUser) { throw new \RuntimeException("User $username not found"); } } /** * @When I add product :productName to my cart with quantity :quantity */ public function iAddProductToMyCart(string $productName, int $quantity): void { $product = $this->entityManager ->getRepository(Product::class) ->findOneBy(['name' => $productName]); $cart = $this->currentUser->getCart() ?? new Cart($this->currentUser); $cart->addItem($product, $quantity); $this->entityManager->persist($cart); $this->entityManager->flush(); } } ``` -------------------------------- ### Update Behat Doctrine Fixtures Dependency (JSON) Source: https://github.com/macpaw/behat-doctrine-fixtures/blob/develop/UPGRADE-2.0.md Illustrates the change needed in the composer.json file to specify the version constraint for the behat-doctrine-fixtures package. It shows how to update the requirement to '^2.0' for the latest major version. ```json { "require-dev": { "macpaw/behat-doctrine-fixtures": "^2.0" } } ``` -------------------------------- ### Register Behat Doctrine Fixtures Bundle in Symfony Source: https://context7.com/macpaw/behat-doctrine-fixtures/llms.txt Registers the Behat Doctrine Fixtures bundle within your Symfony application's kernel for testing environments. This is required for non-Flex applications. ```php ['test' => true] ); return $bundles; } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.