### Install Behat ORM Context Bundle with Composer Source: https://github.com/macpaw/behat-orm-context/blob/develop/docs/install.md This command installs the Behat ORM Context bundle as a development dependency using Composer. It is the primary method for adding the bundle to your project. ```bash composer require --dev macpaw/behat-orm-context ``` -------------------------------- ### Gherkin: Complete Feature Test Example for Order Processing Source: https://context7.com/macpaw/behat-orm-context/llms.txt A comprehensive Behat feature demonstrating a full user workflow, including API requests and database state verification. It tests order creation, status updates, and entity property validation across multiple steps. ```gherkin Feature: E-commerce Order Processing In order to verify order handling As a test suite I need to check database state after operations Background: Given the database is clean And I see 0 entities "App\Entity\Order" Scenario: Create and process an order When I send a POST request to "/api/orders" with: """ { "customerId": "cust-789", "items": [ {"productId": "prod-123", "quantity": 2}, {"productId": "prod-456", "quantity": 1} ] } """ Then the response status code should be 201 And I see 1 entities "App\Entity\Order" And I see entity "App\Entity\Order" with properties: """ { "customerId": "cust-789", "status": "pending", "itemCount": 3 } """ When I process the order "ord-001" Then I see entity "App\Entity\Order" with properties: """ { "id": "ord-001", "status": "completed", "processedAt": null } """ ``` -------------------------------- ### Register Behat ORM Context Bundle in Symfony Source: https://github.com/macpaw/behat-orm-context/blob/develop/docs/install.md This PHP code snippet shows how to manually register the Behat ORM Context bundle in the `config/bundles.php` file. This is necessary if not using Symfony Flex or if the bundle does not provide an automatic recipe. The bundle is enabled only for the 'test' environment. ```php ['test' => true], ]; ``` -------------------------------- ### Configure Behat ORM Context in behat.yml Source: https://github.com/macpaw/behat-orm-context/blob/develop/docs/install.md This YAML configuration adds the ORM context to your Behat setup. It specifies the `ORMContext` class to be used within the default suite, enabling ORM-related Behat features. ```yaml default: suites: default: contexts: - BehatOrmContext\Context\ORMContext ``` -------------------------------- ### Custom Entity Manager Configuration (YAML) Source: https://context7.com/macpaw/behat-orm-context/llms.txt Overrides the default Doctrine entity manager used by the ORMContext. This configuration snippet allows specifying a custom entity manager service, which is particularly useful when working with multiple database connections or custom manager setups in your test environment. ```yaml # config/services_test.yaml when@test: services: BehatOrmContext\Context\ORMContext: arguments: $manager: '@doctrine.orm.analytics_entity_manager' ``` -------------------------------- ### Inject Custom ObjectManager for Behat ORM Context Source: https://github.com/macpaw/behat-orm-context/blob/develop/docs/install.md This YAML configuration demonstrates how to override the default Doctrine ObjectManager used by `ORMContext`. By specifying a custom service (e.g., `@doctrine.orm.other_entity_manager`) in `config/services.yaml` under the 'test' environment, you can inject a different ObjectManager. ```yaml when@test: services: BehatOrmContext\Context\ORMContext: arguments: $manager: '@doctrine.orm.other_entity_manager' ``` -------------------------------- ### PHP Usage: ORMContext Constructor and Injection Source: https://context7.com/macpaw/behat-orm-context/llms.txt Demonstrates how to instantiate the ORMContext class with an EntityManagerInterface. It shows automatic wiring by Symfony's dependency injection and how to inject ORMContext into custom Behat contexts for testing entity states. ```php get('doctrine.orm.entity_manager'); $ormContext = new ORMContext($entityManager); // Can be injected into custom contexts class MyCustomContext implements Context { private ORMContext $ormContext; public function __construct(ORMContext $ormContext) { $this->ormContext = $ormContext; } /** * @Then the user should be saved */ public function theUserShouldBeSaved(): void { $this->ormContext->thenISeeInRepository(1, User::class); } } ``` -------------------------------- ### Gherkin: Testing Null Properties Source: https://context7.com/macpaw/behat-orm-context/llms.txt Illustrates how to test entities with intentionally null properties using Gherkin syntax. The bundle translates these null checks into SQL `IS NULL` conditions for accurate database assertions. ```gherkin Feature: User Profile Scenario: User without optional fields When I create a user with minimal information Then I see entity "App\Entity\User" with properties: """ { "email": "minimal@example.com", "middleName": null, "phoneNumber": null } """ ``` -------------------------------- ### Gherkin: Testing JSON Field Properties Source: https://context7.com/macpaw/behat-orm-context/llms.txt Shows how to test entities with Doctrine JSON field types. The bundle normalizes JSON values for consistent comparison across PostgreSQL and MySQL, ensuring reliable testing. ```gherkin Feature: Product Metadata Scenario: Product with JSON attributes When I create a product with custom attributes Then I see entity "App\Entity\Product" with properties: """ { "sku": "PROD-001", "metadata": { "color": "blue", "size": "large", "tags": ["electronics", "gadgets"] } } """ ``` -------------------------------- ### Verify Entity Existence with Properties (Gherkin) Source: https://github.com/macpaw/behat-orm-context/blob/develop/docs/ORMContext/see-entity-in-repository-with-properties.md This Gherkin step definition allows you to check if a specific entity exists in the database and matches the provided property key-value pairs. It's useful for validating entity states after certain operations. ```gherkin Then I see entity "App\Entity\User" with properties: """ { "email": "user@example.com", "firstName": "John", "lastName": "Doe", "active": true } """ ``` -------------------------------- ### Configure Behat Context (YAML) Source: https://context7.com/macpaw/behat-orm-context/llms.txt Adds the ORMContext class to your Behat suite configuration file. This step enables the provided Behat step definitions for testing Doctrine ORM interactions within your BDD scenarios. Ensure the path to the context is correct. ```yaml # behat.yml default: suites: default: contexts: - BehatOrmContext\Context\ORMContext - App\Tests\Behat\ApiContext ``` -------------------------------- ### Verify Entity with Multiple Properties (Gherkin) Source: https://context7.com/macpaw/behat-orm-context/llms.txt Checks for the existence of an entity that matches a set of specified property values. This step supports various data types including strings, numbers, booleans, nulls, and JSON fields, provided as a JSON string. It's essential for validating complex entity states. ```gherkin Feature: User Registration Scenario: Register a new user with profile When I register a user with email "john.doe@example.com" Then I see entity "App\Entity\User" with properties: """ { "email": "john.doe@example.com", "firstName": "John", "lastName": "Doe", "active": true, "roles": ["ROLE_USER"] } """ And I see 1 entities "App\Entity\User" ``` -------------------------------- ### Find Entity by ID (Gherkin) Source: https://context7.com/macpaw/behat-orm-context/llms.txt Confirms the existence of an entity with a specific ID in the database. This step definition is valuable for validating successful entity creation or persistence after various application actions. It helps ensure that individual records are correctly managed. ```gherkin Feature: Product Catalog Scenario: Create a product When I send a POST request to "/api/products" with: """ { "id": "prod-12345", "name": "Wireless Mouse", "price": 29.99 } """ Then the response status code should be 201 And I see entity "App\Entity\Product" with id "prod-12345" ``` -------------------------------- ### Check Entity Count in Repository (Gherkin) Source: https://context7.com/macpaw/behat-orm-context/llms.txt Verifies the number of entities of a given class in the database. This Gherkin step executes a count query and raises an exception if the actual count does not match the expected value. It's useful for ensuring data integrity after operations. ```gherkin Feature: User Management Scenario: Create multiple users Given the database is empty When I create 5 new users via the API Then I see 5 entities "App\Entity\User" And I see 0 entities "App\Entity\Admin" ``` -------------------------------- ### Verify Entity Existence by ID (Gherkin) Source: https://github.com/macpaw/behat-orm-context/blob/develop/docs/ORMContext/see-entity-in-repository-with-id.md This Gherkin step definition allows you to assert the presence of a specific entity within the database. It takes the fully-qualified entity class name and the entity's unique ID as parameters to perform the check. ```gherkin And I see entity "App\Entity\Product" with id "abc123" ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.