diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 1abc966..d28cd90 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -13,6 +13,8 @@ jobs: matrix: php: - '8.2' + - '8.3' + - '8.4' coverage: ['none'] symfony-versions: - '6.4.*' diff --git a/README.md b/README.md index 486e738..19a4043 100644 --- a/README.md +++ b/README.md @@ -1,102 +1,65 @@ -Symfony Behat Redis Context -================================= - +# Symfony Behat Redis Context | Version | Build Status | Code Coverage | |:---------:|:-------------:|:-----:| | `main`| [![CI][main Build Status Image]][main Build Status] | [![Coverage Status][main Code Coverage Image]][main Code Coverage] | | `develop`| [![CI][develop Build Status Image]][develop Build Status] | [![Coverage Status][develop Code Coverage Image]][develop Code Coverage] | -Installation -============ - -Step 1: Download the Bundle ----------------------------------- -Open a command console, enter your project directory and execute: - -### Applications that use Symfony Flex [in progress](https://github.com/MacPaw/BehatRedisContext/issues/2) - -```console -$ composer require --dev macpaw/behat-redis-context -``` - -### Applications that don't use Symfony Flex - -Open a command console, enter your project directory and execute the -following command to download the latest stable version of this bundle: - -```console -$ composer require --dev macpaw/behat-redis-context -``` - -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. - - -Then, enable the bundle by adding it to the list of registered bundles -in the `app/AppKernel.php` file of your project: - -```php - ['test' => true], - ); - - // ... - } - - // ... -} -``` - -Create configuration for behat redis context: - -`config/packages/test/behat_redis_context.yaml ` -```yaml -behat_redis_context: - dataFixturesPath: "" -``` - - -Step 2: Change path to directory with your fixtures ----------------------------------- -`config/packages/test/behat_redis_context.yaml ` -```yaml -behat_redis_context: - dataFixturesPath: "your path" -``` - -Step 3: Change path to directory with your fixtures ----------------------------------- -`config/services_test.yaml` -```yaml -Predis\ClientInterface: 'Your Redis Client' -``` - -Example if you use [Symfony Redis Bundle](https://github.com/symfony-bundles/redis-bundle): -```yaml -Predis\ClientInterface: '@SymfonyBundles\RedisBundle\Redis\ClientInterface' -``` - -Step 4: Configure Behat -============= -Go to `behat.yml` - -```yaml -... - contexts: - - BehatRedisContext\Context\RedisContext - - BehatRedisContext\Context\RedisFixturesContext -... -``` +Symfony Behat Redis Context is a package that integrates Redis operations with Behat for behavior-driven development (BDD). This context allows you to store, retrieve, and validate data in Redis as part of your Behat testing scenarios. It's useful when testing applications that depend on Redis for caching, session storage, or data management. + +This documentation provides step-by-step guides for installing the package and utilizing each Redis-related step within Behat scenarios. + +## How to Install Symfony Behat Redis Context + +To install Symfony Behat Redis Context, follow these steps: + +1. Add the package to your project using composer: + ```bash + composer require --dev macpaw/behat-redis-context + +For detailed steps and configuration, refer to the [Installation Steps](docs/install.md) + +## RedisContext Documentation + +Below are the available Redis operations that you can use in your Behat tests. Each step integrates seamlessly with Redis to ensure data is stored, retrieved, or validated as expected. + +### Redis Step Definitions: + +* [Check Any Value by Redis Key](docs/RedisContext/check-any-value-by-key.md) + Verifies if any value is stored in Redis under a specific key. + +* [Check Array Value Stored in Redis](docs/RedisContext/check-array.md) + Ensures that the stored array or hash in Redis matches the expected structure. + +* [Check if Key Exists in Redis](docs/RedisContext/check-key-exist.md) + Checks whether a specific key exists in Redis. + +* [Check Serialized Value in Redis](docs/RedisContext/check-serialized-value.md) + Verifies that a serialized value stored in Redis matches the expected serialized value. + +* [Check String Value in Redis](docs/RedisContext/check-value-in-redis.md) + Validates if a string value in Redis matches the expected value. + +* [Clean Redis Database in Test](docs/RedisContext/clean-db.md) + Automatically flushes the Redis database before running a scenario to ensure a clean state. + +* [Store Serialized Value in Redis](docs/RedisContext/store-seralized-value.md) + Serializes and stores a value in Redis with a given key. + +* [Store String Value in Redis](docs/RedisContext/store-string-value.md) + Stores a simple string value in Redis under the specified key. + +## RedisFixtureContext Documentation + +Here you can find detailed documentation about using Redis fixtures in Behat: + +1. **[How It Works](docs/RedisFixtures/how-works.md)** + Learn about the inner workings of the RedisFixtureContext and how it integrates with your Behat tests. + +2. **[How to Load Fixture Data into Redis](docs/RedisFixtures/how-load-fixture-in-redis.md)** + A step-by-step guide on how to load predefined data fixtures into Redis using YAML files in Behat. + +3. **[Handling Missing Fixture Files](docs/RedisFixtures/handling-missing-fixtures.md)** + What to do when a specified fixture file is missing and how to handle such errors in your tests. [main Build Status]: https://github.com/macpaw/BehatRedisContext/actions?query=workflow%3ACI+branch%3Amain [main Build Status Image]: https://github.com/macpaw/BehatRedisContext/workflows/CI/badge.svg?branch=main diff --git a/composer.json b/composer.json index 6ac92a6..9632c46 100644 --- a/composer.json +++ b/composer.json @@ -22,7 +22,7 @@ "name": "Yozhef Hisem", "email": "hisemjo@gmail.com", "homepage": "https://macpaw.com/", - "role": "Software Engineer" + "role": "Staff Software Engineer" } ], "license": "MIT", diff --git a/docs/RedisContext/check-any-value-by-key.md b/docs/RedisContext/check-any-value-by-key.md new file mode 100644 index 0000000..f257606 --- /dev/null +++ b/docs/RedisContext/check-any-value-by-key.md @@ -0,0 +1,10 @@ +### Check Any Value in Redis + +#### Step Definition: + +This step checks if any value exists in Redis under a specified key. + +#### Gherkin Example: + +```gherkin +When I see in redis any value by key "testKey" \ No newline at end of file diff --git a/docs/RedisContext/check-array.md b/docs/RedisContext/check-array.md new file mode 100644 index 0000000..3b089bb --- /dev/null +++ b/docs/RedisContext/check-array.md @@ -0,0 +1,16 @@ +### Check an Array in Redis + +#### Step Definition: + +This step checks if the data stored in Redis as a hash matches the expected JSON structure. + +#### Gherkin Example: + +```gherkin +Then I see in redis array by key "arrayKey": + """ + { + "key1": "value1", + "key2": "value2" + } + """ \ No newline at end of file diff --git a/docs/RedisContext/check-key-exist.md b/docs/RedisContext/check-key-exist.md new file mode 100644 index 0000000..2bc6210 --- /dev/null +++ b/docs/RedisContext/check-key-exist.md @@ -0,0 +1,10 @@ +### Check the Absence of a Value in Redis + +#### Step Definition: + +This step checks that no exists under a specific key in Redis. + +#### Gherkin Example: + +```gherkin +When I don't see in redis key "testKey" \ No newline at end of file diff --git a/docs/RedisContext/check-serialized-value.md b/docs/RedisContext/check-serialized-value.md new file mode 100644 index 0000000..d28d9eb --- /dev/null +++ b/docs/RedisContext/check-serialized-value.md @@ -0,0 +1,10 @@ +### Check a Serialized Value in Redis + +#### Step Definition: + +This step checks if a serialized value in Redis matches the expected value. It unserializes the value before comparing. + +#### Gherkin Example: + +```gherkin +When I see in redis serialized value "testSerializedValue" by key "serializedKey" \ No newline at end of file diff --git a/docs/RedisContext/check-value-in-redis.md b/docs/RedisContext/check-value-in-redis.md new file mode 100644 index 0000000..3a795ca --- /dev/null +++ b/docs/RedisContext/check-value-in-redis.md @@ -0,0 +1,10 @@ +### Check a Value in Redis + +#### Step Definition: + +This step checks if a specific value exists in Redis under the specified key. If the value is missing or does not match, the step will throw an error. + +#### Gherkin Example: + +```gherkin +When I see in redis value "testValue" by key "testKey" \ No newline at end of file diff --git a/docs/RedisContext/clean-db.md b/docs/RedisContext/clean-db.md new file mode 100644 index 0000000..28ebe0d --- /dev/null +++ b/docs/RedisContext/clean-db.md @@ -0,0 +1,17 @@ +## Redis Database Cleanup + +### Before Each Scenario + +The Redis database is automatically flushed before each scenario using the `@BeforeScenario` hook. This ensures that each scenario starts with a clean database, preventing test pollution from previous scenarios. + +#### Example Usage: + +There is no specific Gherkin step required; this happens automatically before each test scenario. + +### After Each Feature + +After all scenarios in a feature are completed, the system performs garbage collection to free up memory. + +#### Example Usage: + +There is no specific Gherkin step required; this happens automatically after the last test in a feature. \ No newline at end of file diff --git a/docs/RedisContext/store-seralized-value.md b/docs/RedisContext/store-seralized-value.md new file mode 100644 index 0000000..48cf20d --- /dev/null +++ b/docs/RedisContext/store-seralized-value.md @@ -0,0 +1,10 @@ +### Save a Serialized Value to Redis + +#### Step Definition: + +This step serializes a value and saves it in Redis under the given key. + +#### Gherkin Example: + +```gherkin +When I save serialized value "testSerializedValue" to redis by "serializedKey" diff --git a/docs/RedisContext/store-string-value.md b/docs/RedisContext/store-string-value.md new file mode 100644 index 0000000..272181b --- /dev/null +++ b/docs/RedisContext/store-string-value.md @@ -0,0 +1,10 @@ +### Save a String Value to Redis + +#### Step Definition: + +This step saves a string value in Redis with a specified key. + +#### Gherkin Example: + +```gherkin +When I save string value "testValue" to redis by "testKey" diff --git a/docs/RedisFixtures/handling-missing-fixtures.md b/docs/RedisFixtures/handling-missing-fixtures.md new file mode 100644 index 0000000..c3ccab9 --- /dev/null +++ b/docs/RedisFixtures/handling-missing-fixtures.md @@ -0,0 +1,7 @@ +# Handling Missing Fixtures +If the specified fixture file is not found, an exception is thrown, indicating that the fixture is missing. Make sure the fixtures exist in the correct directory specified by data_fixtures_path. + +Example Error Message: +```gherkin +The "orders" redis fixture not found. +``` \ No newline at end of file diff --git a/docs/RedisFixtures/how-load-fixture-in-radis.md b/docs/RedisFixtures/how-load-fixture-in-radis.md new file mode 100644 index 0000000..73b7e4f --- /dev/null +++ b/docs/RedisFixtures/how-load-fixture-in-radis.md @@ -0,0 +1,109 @@ +# How loading fixture date in Redis + +## 1. Example Directory Structure +Here's how you can organize your project with fixture files for Redis: +``` +project-root/ +│ +├── tests/ +│ └── Fixtures/ +│ └── Redis/ +│ ├── Users.yml +│ └── Orders.yml +│ +├── behat.yml +└── composer.json +``` + +## 2. Create Your Fixture Files +Example of `users.yml`: +```yaml +users: + 1: "John Doe" + 2: "Jane Smith" + 3: "Alice Cooper" +``` +In this example: + +The users hash contains keys for user IDs (1, 2, 3) and their corresponding names. +When this fixture is loaded, these values will be stored in Redis under the hash key users. + +Example of `orders.yml`: +```yaml +orders: + order_123: + id: 123 + status: "pending" + total: 250.50 + order_456: + id: 456 + status: "completed" + total: 99.99 +``` +In this example: + +The orders hash contains keys representing specific orders with associated data such as id, status, and total. +When this fixture is loaded, Redis will store each order with the given values. + +## 3. Using Redis Fixture Context +Once you have created your fixture files, you can reference them in your Behat feature files using the step definition Given I load redis fixtures. + +Example Behat Scenario: +```gherkin +Feature: Test Redis Data in Behat + + Scenario: Check if Redis contains predefined users + Given I load redis fixtures "users" + When I see in redis any value by key "users" + Then I should see in redis array by key "users": + """ + { + "1": "John Doe", + "2": "Jane Smith", + "3": "Alice Cooper" + } + """ +``` +In this scenario: + +The fixture users.yml is loaded into Redis. +The test checks that the users key exists in Redis and contains the expected data. + +## 4. Load Multiple Fixtures in One Scenario + You can load multiple fixtures in a single step by providing a comma-separated list of fixture names. + +Example Behat Scenario: +```gherkin +Scenario: Load users and orders fixtures + Given I load redis fixtures "users, orders" + When I see in redis any value by key "users" + Then I should see in redis array by key "users": + """ + { + "1": "John Doe", + "2": "Jane Smith" + } + """ + When I see in redis any value by key "orders" + Then I should see in redis array by key "orders": + """ + { + "order_123": { + "id": 123, + "status": "pending", + "total": 250.50 + }, + "order_456": { + "id": 456, + "status": "completed", + "total": 99.99 + } + } + """ +``` +In this scenario: + +Both users.yml and orders.yml fixtures are loaded into Redis. +The test checks that both users and orders contain the correct data. + +4. Handling Missing Fixtures \ No newline at end of file diff --git a/docs/RedisFixtures/how-works.md b/docs/RedisFixtures/how-works.md new file mode 100644 index 0000000..79114b4 --- /dev/null +++ b/docs/RedisFixtures/how-works.md @@ -0,0 +1,6 @@ +# How Redis Fixtures Work +When the Given I load redis fixtures step is called: + +* The system looks for the specified YAML files in the directory defined by data_fixtures_path. +* Redis is populated with the key-value pairs or hash structures defined in these files. +* Simple key-value pairs are stored using the SET command, and hash sets are stored using the HMSET command. \ No newline at end of file diff --git a/docs/install.md b/docs/install.md new file mode 100644 index 0000000..1c2b92e --- /dev/null +++ b/docs/install.md @@ -0,0 +1,91 @@ +Installation +============ + +Step 1: Download the Bundle +---------------------------------- +Open a command console, enter your project directory and execute: + +### Applications that use Symfony Flex [in progress](https://github.com/MacPaw/BehatRedisContext/issues/2) + +```console +$ composer require --dev macpaw/behat-redis-context +``` + +### Applications that don't use Symfony Flex + +Open a command console, enter your project directory and execute the +following command to download the latest stable version of this bundle: + +```console +$ composer require --dev macpaw/behat-redis-context +``` + +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. + + +Then, enable the bundle by adding it to the list of registered bundles +in the `app/AppKernel.php` file of your project: + +```php + ['test' => true], + ); + + // ... + } + + // ... +} +``` + +Create configuration for behat redis context: + +`config/packages/test/behat_redis_context.yaml ` +```yaml +behat_redis_context: + dataFixturesPath: "" +``` + + +Step 2: Change path to directory with your fixtures +---------------------------------- +`config/packages/test/behat_redis_context.yaml ` +```yaml +behat_redis_context: + dataFixturesPath: "your path" +``` + +Step 3: Change path to directory with your fixtures +---------------------------------- +`config/services_test.yaml` +```yaml +Predis\ClientInterface: 'Your Redis Client' +``` + +Example if you use [Symfony Redis Bundle](https://github.com/symfony-bundles/redis-bundle): +```yaml +Predis\ClientInterface: '@SymfonyBundles\RedisBundle\Redis\ClientInterface' +``` + +Step 4: Configure Behat +============= +Go to `behat.yml` + +```yaml +... + contexts: + - BehatRedisContext\Context\RedisContext + - BehatRedisContext\Context\RedisFixturesContext +... +``` \ No newline at end of file diff --git a/src/Context/RedisContext.php b/src/Context/RedisContext.php index debee4b..0aa986d 100644 --- a/src/Context/RedisContext.php +++ b/src/Context/RedisContext.php @@ -82,9 +82,9 @@ public function iSeeInRedisValueByKey(string $value, string $key): void } /** - * @When /^I don't see in redis value by key "([^"]*)"$/ + * @When /^I don't see in redis key "([^"]*)"$/ */ - public function iDontSeeInRedisValueByKey(string $key): void + public function iDontSeeInRedisKey(string $key): void { $found = $this->redis->get($key);