### Install Behat Redis Context Bundle (Bash) Source: https://context7.com/macpaw/behat-redis-context/llms.txt Installs the Behat Redis Context Bundle using Composer. This command should be run in a development environment. ```bash # Install the bundle composer require --dev macpaw/behat-redis-context ``` -------------------------------- ### Install Behat Redis Context with Composer Source: https://github.com/macpaw/behat-redis-context/blob/develop/docs/install.md Installs the Behat Redis Context bundle using Composer. This command is used for both applications with and without Symfony Flex. It requires Composer to be installed globally. ```console $ composer require --dev macpaw/behat-redis-context ``` -------------------------------- ### Configure Redis Client for Behat Redis Context Source: https://github.com/macpaw/behat-redis-context/blob/develop/docs/install.md Configures the Redis client interface to be used by the Behat Redis Context. This involves setting the appropriate service definition, often in `config/services_test.yaml`. An example using the Symfony Redis Bundle is also provided. ```yaml Predis\ClientInterface: 'Your Redis Client' Example if you use [Symfony Redis Bundle](https://github.com/symfony-bundles/redis-bundle): Predis\ClientInterface: '@SymfonyBundles\RedisBundle\Redis\ClientInterface' ``` -------------------------------- ### Enable Behat Redis Context Bundle in AppKernel Source: https://github.com/macpaw/behat-redis-context/blob/develop/docs/install.md Enables the Behat Redis Context bundle by adding it to the list of registered bundles in the `app/AppKernel.php` file. This ensures the bundle is active for your test environment. ```php ['test' => true], ); // ... } // ... } ``` -------------------------------- ### Install Symfony Behat Redis Context using Composer Source: https://github.com/macpaw/behat-redis-context/blob/develop/README.md Installs the Symfony Behat Redis Context package into your project using Composer. This command adds the package as a development dependency, making its features available for your Behat tests. ```bash composer require --dev macpaw/behat-redis-context ``` -------------------------------- ### Automatic Database Cleanup Source: https://context7.com/macpaw/behat-redis-context/llms.txt Demonstrates automatic Redis database flushing before each scenario for test isolation. The Gherkin feature shows how a scenario can be run with the guarantee that Redis is empty, and how a subsequent scenario will also start with an empty database. ```gherkin Feature: User authentication with clean state Scenario: Login creates new session # Redis is automatically flushed before this scenario When I save string value "new_session_123" to redis by "session:user:1" Then I see in redis value "new_session_123" by key "session:user:1" Scenario: Second scenario starts with empty Redis # Redis is flushed again - previous scenario data is gone Then I don't see in redis key "session:user:1" ``` -------------------------------- ### Configure Behat Contexts Source: https://github.com/macpaw/behat-redis-context/blob/develop/docs/install.md Configures Behat to use the Redis contexts provided by the Behat Redis Context bundle. This is done by adding the context class paths to the `contexts` section in your `behat.yml` file. ```yaml ... contexts: - BehatRedisContext\Context\RedisContext - BehatRedisContext\Context\RedisFixturesContext ... ``` -------------------------------- ### Configure Behat Redis Context Data Fixtures Path Source: https://github.com/macpaw/behat-redis-context/blob/develop/docs/install.md Sets the path to the directory containing your data fixtures for the Behat Redis Context. This configuration is typically done in `config/packages/test/behat_redis_context.yaml`. ```yaml behat_redis_context: dataFixturesPath: "your path" ``` -------------------------------- ### Load Redis Fixtures from YAML Files Source: https://context7.com/macpaw/behat-redis-context/llms.txt Demonstrates loading predefined test data from YAML fixture files into Redis. The Gherkin feature illustrates how to load single or multiple fixtures and then verify specific values or structures within Redis. ```gherkin Feature: Test with predefined data Scenario: Load and verify user fixtures Given I load redis fixtures "users" Then I see in redis any value by key "users" Then I see in redis array by key "users": """ { "1": "John Doe", "2": "Jane Smith", "3": "Alice Cooper" } """ Scenario: Load multiple fixtures Given I load redis fixtures "users, orders" Then I see in redis value "token123" by key "user:1:token" Then I see in redis array by key "orders": """ { "order_123": { "id": "123", "status": "pending", "total": "250.50" } } """ ``` -------------------------------- ### Load Redis Fixtures from YAML Files Using PHP Source: https://context7.com/macpaw/behat-redis-context/llms.txt Loads predefined test data from YAML fixture files into Redis. This function parses YAML files specified by aliases and loads the data into Redis, supporting both simple key-value pairs and hash structures. It requires the path to fixture files and a comma-separated string of aliases. ```php dataFixturesPath, $alias); if (!is_file($fixture)) { throw new InvalidArgumentException(sprintf('The "%s" redis fixture not found.', $alias)); } $fixtures[] = $fixture; } $this->loadFixtures($fixtures); } private function loadFile(array $params): void { foreach ($params as $key => $value) { if (is_array($value)) { $this->redis->hmset($key, $value); } else { $this->redis->set($key, $value); } } } ``` -------------------------------- ### Configure Behat Redis Context (YAML) Source: https://context7.com/macpaw/behat-redis-context/llms.txt Configures the Behat Redis Context Bundle and its dependencies within the Symfony testing environment. This includes setting the path for Redis fixtures and aliasing the Predis client. ```yaml # config/packages/test/behat_redis_context.yaml behat_redis_context: dataFixturesPath: "tests/Fixtures/Redis" # config/services_test.yaml Predis\ClientInterface: '@SymfonyBundles\RedisBundle\Redis\ClientInterface' # behat.yml default: suites: default: contexts: - BehatRedisContext\Context\RedisContext - BehatRedisContext\Context\RedisFixturesContext ``` -------------------------------- ### Automatic Database Cleanup Using PHP Source: https://context7.com/macpaw/behat-redis-context/llms.txt Ensures Redis database flushing before each scenario for test isolation. This PHP code defines a method that runs automatically before each scenario, clearing the Redis database to provide a clean state for tests. It also includes a cleanup method that runs after the feature. ```php redis->flushdb(); } /** * @AfterFeature */ public static function afterFeature(): void { gc_collect_cycles(); } ``` -------------------------------- ### Check Any Value Exists by Key in Redis Source: https://context7.com/macpaw/behat-redis-context/llms.txt Verifies if a key exists in Redis, irrespective of its associated value. This is useful for confirming the presence of cached data. It takes a key as input and throws an exception if the key is not found. ```gherkin Feature: Check cache presence Scenario: Verify cache key exists When I save string value "cached_data" to redis by "cache:product:123" Then I see in redis any value by key "cache:product:123" ``` -------------------------------- ### Check String Value Existence in Redis (PHP) Source: https://context7.com/macpaw/behat-redis-context/llms.txt PHP implementation for checking string value existence and non-existence in Redis. It retrieves values by key and validates them against expected strings, or asserts the absence of a key. ```php redis->get($key); if (!$found) { throw new InvalidArgumentException(sprintf('In Redis does not exist data for key "%s"', $key)); } if ($value !== $found) { throw new InvalidArgumentException(sprintf('Value in key "%s" do not match "%s" actual "%s"', $key, $value, $found)); } } public function iDontSeeInRedisKey(string $key): void { $found = $this->redis->get($key); if ($found) { throw new InvalidArgumentException(sprintf('Redis contains data for key "%s"', $key)); } } ``` -------------------------------- ### Save String Value to Redis (PHP) Source: https://context7.com/macpaw/behat-redis-context/llms.txt PHP implementation of a Behat step to save a string value to Redis using a given key. It utilizes the Predis client to interact with the Redis server. ```php redis->set($key, $value); } ``` -------------------------------- ### Save and Check Serialized Value in Redis (PHP) Source: https://context7.com/macpaw/behat-redis-context/llms.txt PHP implementation for saving and checking serialized values in Redis. It handles the serialization and unserialization of data, throwing exceptions if values do not match or keys are not found. ```php redis->set($key, serialize($value)); } public function iSeeInRedisSerializedValueByKey(string $value, string $key): void { $found = $this->redis->get($key); if (null === $found) { throw new InvalidArgumentException(sprintf('In Redis does not exist data for key "%s"', $key)); } $found = unserialize($found); if ($value !== $found) { throw new InvalidArgumentException(sprintf('Value in key "%s" do not match "%s" actual "%s"', $key, $value, $found)); } } ``` -------------------------------- ### Register Behat Redis Context Bundle in Symfony (PHP) Source: https://context7.com/macpaw/behat-redis-context/llms.txt Registers the BehatRedisContextBundle within the Symfony application kernel for testing environments. This ensures the bundle's services are available during Behat tests. ```php ['test' => true], ); return $bundles; } } ``` -------------------------------- ### Store String Value in Redis (Gherkin) Source: https://context7.com/macpaw/behat-redis-context/llms.txt Defines Gherkin steps to save a simple string value to Redis with a specified key. These steps are used in Behat scenarios to populate Redis with test data. ```gherkin Feature: Store user session data Scenario: Save user token to Redis When I save string value "abc123token" to redis by "user:1:token" When I save string value "active" to redis by "user:1:status" Then I see in redis value "abc123token" by key "user:1:token" Then I see in redis value "active" by key "user:1:status" ``` -------------------------------- ### Check String Value in Redis (Gherkin) Source: https://context7.com/macpaw/behat-redis-context/llms.txt Gherkin steps to validate that a specific string value exists in Redis for a given key. It also includes a step to assert that a key does not exist. ```gherkin Feature: Validate cached data Scenario: Verify user session exists When I save string value "session_xyz" to redis by "session:current" Then I see in redis value "session_xyz" by key "session:current" # Negative test - key should not exist Then I don't see in redis key "session:expired" ``` -------------------------------- ### Store Serialized Value in Redis (Gherkin) Source: https://context7.com/macpaw/behat-redis-context/llms.txt Gherkin steps to serialize and store complex data structures in Redis. This is useful for testing scenarios involving session data or other complex objects. ```gherkin Feature: Store serialized user preferences Scenario: Save serialized user settings When I save serialized value "dark_mode_enabled" to redis by "user:1:preference" Then I see in redis serialized value "dark_mode_enabled" by key "user:1:preference" ``` -------------------------------- ### Check Array Value in Redis Using PHP Source: https://context7.com/macpaw/behat-redis-context/llms.txt Validates hash structures stored in Redis against expected JSON data. This function compares the actual Redis hash data with the provided JSON. It requires the Redis client connection and a key, then compares the fetched hash against the expected JSON structure. ```php redis->hgetall($key); $expectedResponse = (array) json_decode(trim($string->getRaw()), true, 512, JSON_THROW_ON_ERROR); if (array_diff($actualResponse, $expectedResponse)) { $prettyJSON = json_encode($actualResponse, JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT, 512); $message = sprintf("Expected JSON does not match actual JSON:\n%s\n", $prettyJSON); throw new RuntimeException($message); } } ``` -------------------------------- ### Check Serialized Redis Value (Gherkin) Source: https://github.com/macpaw/behat-redis-context/blob/develop/docs/RedisContext/check-serialized-value.md This Gherkin step defines a scenario to check if a serialized value stored in Redis at a specific key matches the expected value. It involves a 'When' action to trigger the check. ```gherkin When I see in redis serialized value "testSerializedValue" by key "serializedKey" ``` -------------------------------- ### Check Redis Array with JSON Structure (Gherkin) Source: https://github.com/macpaw/behat-redis-context/blob/develop/docs/RedisContext/check-array.md This Gherkin step definition allows you to assert that a hash stored in Redis at a specified key matches a given JSON object. It is useful for integration testing scenarios where Redis is used for data storage. ```gherkin Then I see in redis array by key "arrayKey": """ { "key1": "value1", "key2": "value2" } """ ``` -------------------------------- ### Check Redis Value with Behat Context Source: https://github.com/macpaw/behat-redis-context/blob/develop/docs/RedisContext/check-value-in-redis.md This Gherkin step definition allows you to assert that a specific value exists within Redis for a given key. It is crucial for testing data persistence and integrity in Redis. If the value is not found or does not match, the test will fail. ```gherkin When I see in redis value "testValue" by key "testKey" ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.