Skip to content

Commit

Permalink
Add documentation for OpenSearch
Browse files Browse the repository at this point in the history
Fix reported issues by Rector and PHPStan

Add missing unit tests for OpenSearch related code

Add OpenSearch to test application used in integration tests

Add integration tests for OpenSearch

Try to run integration tests in GHA pipeline
  • Loading branch information
hugo-goncalves-kununu committed Jan 22, 2025
1 parent 7bace09 commit 66e0035
Show file tree
Hide file tree
Showing 39 changed files with 622 additions and 98 deletions.
84 changes: 42 additions & 42 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
php-version:
- 8.3
mysql-version:
- 5.7
- 8.0
elasticsearch-version:
- 7.9.3
opensearch-version:
Expand Down Expand Up @@ -59,7 +59,7 @@ jobs:
http.port: 9201
DISABLE_INSTALL_DEMO_CONFIG: true
DISABLE_SECURITY_PLUGIN: true
OPENSEARCH_JAVA_OPTS: -Xms1g -Xmx1g
OPENSEARCH_JAVA_OPTS: -Xms512m -Xmx512m
options: >-
--health-cmd "curl http://localhost:9201/_cluster/health"
--health-interval 10s
Expand All @@ -72,46 +72,46 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v4
#
# - name: Setup PHP
# uses: shivammathur/setup-php@v2
# with:
# php-version: ${{ matrix.php-version }}
# coverage: pcov
# extensions: ${{ matrix.extensions }}
#
# - name: Install Composer Dependencies
# uses: ramsey/composer-install@v3
# with:
# dependency-versions: ${{ matrix.dependencies }}
# composer-options: "--prefer-stable"

# - name: Setup databases
# run: ./tests/App/bin/setup_databases.sh
# env:
# DEF_DATABASE_URL: mysql://root:root@127.0.0.1:${{ job.services.mysql.ports['3306'] }}/default_db
# PERSISTENCE_DATABASE_URL: mysql://root:root@127.0.0.1:${{ job.services.mysql.ports['3306'] }}/persistence_db
# MONOLITHIC_DATABASE_URL: mysql://root:root@127.0.0.1:${{ job.services.mysql.ports['3306'] }}/monolithic_db
# ELASTICSEARCH_URL: http://localhost:9200
# OPENSEARCH_URL: http://localhost:9201
# DATABASE_SERVER_VERSION: ${{ matrix.mysql-version }}
#
# - name: Run PHPUnit
# run: vendor/bin/phpunit --log-junit tests/.results/tests-junit.xml --coverage-clover tests/.results/tests-clover.xml
# env:
# DEF_DATABASE_URL: mysql://root:root@127.0.0.1:${{ job.services.mysql.ports['3306'] }}/default_db
# PERSISTENCE_DATABASE_URL: mysql://root:root@127.0.0.1:${{ job.services.mysql.ports['3306'] }}/persistence_db
# MONOLITHIC_DATABASE_URL: mysql://root:root@127.0.0.1:${{ job.services.mysql.ports['3306'] }}/monolithic_db
# ELASTICSEARCH_URL: http://localhost:9200
# OPENSEARCH_URL: http://localhost:9201
# DATABASE_SERVER_VERSION: ${{ matrix.mysql-version }}
#
# - name: Upload coverage files
# uses: actions/upload-artifact@v4
# with:
# name: ${{ github.job }}-${{ matrix.php-version }}-${{ matrix.dependencies }}-coverage
# include-hidden-files: true
# path: tests/.results/

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-version }}
coverage: pcov
extensions: ${{ matrix.extensions }}

- name: Install Composer Dependencies
uses: ramsey/composer-install@v3
with:
dependency-versions: ${{ matrix.dependencies }}
composer-options: "--prefer-stable"

- name: Setup databases
run: ./tests/App/bin/setup_databases.sh
env:
DEF_DATABASE_URL: mysql://root:root@127.0.0.1:${{ job.services.mysql.ports['3306'] }}/default_db
PERSISTENCE_DATABASE_URL: mysql://root:root@127.0.0.1:${{ job.services.mysql.ports['3306'] }}/persistence_db
MONOLITHIC_DATABASE_URL: mysql://root:root@127.0.0.1:${{ job.services.mysql.ports['3306'] }}/monolithic_db
ELASTICSEARCH_URL: http://localhost:9200
OPENSEARCH_URL: http://localhost:9201
DATABASE_SERVER_VERSION: ${{ matrix.mysql-version }}

- name: Run PHPUnit
run: vendor/bin/phpunit --log-junit tests/.results/tests-junit.xml --coverage-clover tests/.results/tests-clover.xml
env:
DEF_DATABASE_URL: mysql://root:root@127.0.0.1:${{ job.services.mysql.ports['3306'] }}/default_db
PERSISTENCE_DATABASE_URL: mysql://root:root@127.0.0.1:${{ job.services.mysql.ports['3306'] }}/persistence_db
MONOLITHIC_DATABASE_URL: mysql://root:root@127.0.0.1:${{ job.services.mysql.ports['3306'] }}/monolithic_db
ELASTICSEARCH_URL: http://localhost:9200
OPENSEARCH_URL: http://localhost:9201
DATABASE_SERVER_VERSION: ${{ matrix.mysql-version }}

- name: Upload coverage files
uses: actions/upload-artifact@v4
with:
name: ${{ github.job }}-${{ matrix.php-version }}-${{ matrix.dependencies }}-coverage
include-hidden-files: true
path: tests/.results/

sonarcloud:
if: false
Expand Down
10 changes: 8 additions & 2 deletions docs/FixturesTypes/cache-pool-fixtures.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,21 @@ You can also disable the creation of orchestrators services for cache pools if y

## Symfony Command to load Cache fixtures

This bundle can automatically create a Symfony Command to load default fixtures for any cache pool. This can be useful for example when you want to have default fixtures for a cache pool that are loaded when your service spins up. At kununu we make use of this and when one of our services starts, we call a script, *run_startup.sh*, that on the *dev* and *test* environments calls this commands so that each cache pool starts with a set of a default fixtures.
This bundle can automatically create a Symfony Command to load default fixtures for any cache pool.

This can be useful for example when you want to have default fixtures for a cache pool that are loaded when your service spins up.

At kununu we make use of this and when one of our services starts, we call a script, *run_startup.sh*, that on the *dev* and *test* environments calls this commands so that each cache pool starts with a set of a default fixtures.

```shell
php bin/console kununu_testing:load_fixtures:cache_pools:MY_CACHE_ID [--append]
```

### 1. Enable Symfony Command for a Cache Pool

By default, Symfony Commands are not created for any cache pool. If you want to enable the creation of a Symfony Command for a specific cache pool you will need to enable it the configuration of the bundle by setting the option `load_command_fixtures_classes_namespace` where you specify the classes names of the fixtures that the command should run.
By default, Symfony Commands are not created for any cache pool.

If you want to enable the creation of a Symfony Command for a specific cache pool you will need to enable it in the configuration of the bundle by setting the option `load_command_fixtures_classes_namespace` where you specify the classes names of the fixtures that the command should run.

```yaml
kununu_testing:
Expand Down
10 changes: 8 additions & 2 deletions docs/FixturesTypes/doctrine-dbal-connection-fixtures.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,11 @@ final class IntegrationTest extends FixturesAwareTestCase

## Symfony Command to load Connection fixtures

This bundle can automatically create a Symfony Command to load default fixtures for any connection. This can be useful for example when you want to have default fixtures for a database that are loaded when your service spins up. At kununu we make use of this and when one of our services starts, we call a script, *run_startup.sh*, that on the *dev* and *test* environments calls this commands so that each database starts with a set of a default fixtures.
This bundle can automatically create a Symfony Command to load default fixtures for any connection.

This can be useful for example when you want to have default fixtures for a database that are loaded when your service spins up.

At kununu we make use of this and when one of our services starts, we call a script, *run_startup.sh*, that on the *dev* and *test* environments calls this commands so that each database starts with a set of a default fixtures.

```shell
php bin/console kununu_testing:load_fixtures:connections:CONNECTION_NAME [--append]
Expand All @@ -110,7 +114,9 @@ php bin/console kununu_testing:load_fixtures:non_transactional_connections:CONNE

### 1. Enable Symfony Command for a Doctrine Connection

By default, Symfony Commands are not created for any Doctrine Connection. If you want to enable the creation of a Symfony Command for a specific Connection you will need to enable it the configuration of the bundle by setting the option `load_command_fixtures_classes_namespace` where you specify the classes names of the fixtures that the command should run.
By default, Symfony Commands are not created for any Doctrine Connection.

If you want to enable the creation of a Symfony Command for a specific Connection you will need to enable it in the configuration of the bundle by setting the option `load_command_fixtures_classes_namespace` where you specify the classes names of the fixtures that the command should run.

```yaml
kununu_testing:
Expand Down
12 changes: 9 additions & 3 deletions docs/FixturesTypes/elasticsearch.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,21 @@ final class IntegrationTest extends FixturesAwareTestCase

## Symfony Command to load Elasticsearch fixtures

This bundle can automatically create a Symfony Command to load default fixtures for any configured Elasticsearch Index. This can be useful for example when you want to have default fixtures for a Elasticsearch Index that are loaded when your service spins up. At kununu we make use of this and when one of our services starts, we call a script, *run_startup.sh*, that on the *dev* and *test* environments calls this commands so that each Elasticsearch Index starts with a set of a default fixtures.
This bundle can automatically create a Symfony Command to load default fixtures for any configured Elasticsearch Index.

This can be useful for example when you want to have default fixtures for a Elasticsearch Index that are loaded when your service spins up.

At kununu we make use of this and when one of our services starts, we call a script, *run_startup.sh*, that on the *dev* and *test* environments calls this commands so that each Elasticsearch Index starts with a set of a default fixtures.

```shell
php bin/console kununu_testing:load_fixtures:elastic_search:MY_INDEX_ALIAS [--append]
```

### 1. Enable Symfony Command for an Elasticsearch Index

By default, Symfony Commands are not created for any Elasticsearch Index. If you want to enable the creation of a Symfony Command for a specific Index you will need to enable it the configuration of the bundle by setting the option `load_command_fixtures_classes_namespace` where you specify the classes names of the fixtures that the command should run.
By default, Symfony Commands are not created for any Elasticsearch Index.

If you want to enable the creation of a Symfony Command for a specific Index you will need to enable it in the configuration of the bundle by setting the option `load_command_fixtures_classes_namespace` where you specify the classes names of the fixtures that the command should run.

```yaml
kununu_testing:
Expand All @@ -91,7 +97,7 @@ The fixtures can be loaded for an Elasticsearch Index by running:
php bin/console kununu_testing:load_fixtures:elastic_search:my_index_alias --append
```

If `--append` option is not used then the cache pool will be purged.
If `--append` option is not used then the index will be purged.

------------------------------

Expand Down
149 changes: 149 additions & 0 deletions docs/FixturesTypes/opensearch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
# OpenSearch Fixtures

This bundle integrates seamless with *OpenSearch Fixtures* from [kununu/data-fixtures](https://github.com/kununu/data-fixtures).

----------------------------------

## How to load OpenSearch Fixtures?

First you will need to configure the bundle. In this example, we will configure an OpenSearch index named (aliased) *my_index_alias* that we will use in the rest of the documentation.

```yaml
kununu_testing:
open_search:
my_index_alias:
service: 'OpenSearch\Client' # Your OpenSearch client service id
index_name: 'my_index_name'
```
In your tests you can extend the classes [FixturesAwareTestCase](../../src/Test/FixturesAwareTestCase.php) or [WebTestCase](../../src/Test/WebTestCase.php) which expose the following method:
```php
protected function loadOpenSearchFixtures(string $alias, OptionsInterface $options, string ...$classNames): void
```
- `$alias` - Alias defined above
- `$classNames` - Array with classes names of fixtures to load
- `$options` - [Options](options.md) for the fixtures load process
- `$classNames` - Classes names of fixtures to load

**Example of loading fixtures in an Integration Test**

```php
use Kununu\TestingBundle\Test\FixturesAwareTestCase;
use Kununu\TestingBundle\Test\Options\Options;
final class IntegrationTest extends FixturesAwareTestCase
{
public function testIntegration()
{
// Start with an empty index and loading data from Fixture1
$this->loadOpenSearchFixtures(
'my_index_alias',
Options::create(),
Fixture1::class
);
// Start from a empty index
$this->loadOpenSearchFixtures(
'my_index_alias',
Options::create()
);
// Do not purge index before loading fixtures
$this->loadOpenSearchFixtures(
'my_index_alias',
Options::create()->withAppend(),
Fixture1::class
);
}
}
```

-----------------------

## Symfony Command to load OpenSearch fixtures

This bundle can automatically create a Symfony Command to load default fixtures for any configured OpenSearch Index.

This can be useful for example when you want to have default fixtures for a OpenSearch Index that are loaded when your service spins up.

At kununu we make use of this and when one of our services starts, we call a script, *run_startup.sh*, that on the *dev* and *test* environments calls this commands so that each OpenSearch Index starts with a set of a default fixtures.

```shell
php bin/console kununu_testing:load_fixtures:open_search:MY_INDEX_ALIAS [--append]
```

### 1. Enable Symfony Command for an OpenSearch Index

By default, Symfony Commands are not created for any OpenSearch Index.

If you want to enable the creation of a Symfony Command for a specific Index you will need to enable it in the configuration of the bundle by setting the option `load_command_fixtures_classes_namespace` where you specify the classes names of the fixtures that the command should run.

```yaml
kununu_testing:
open_search:
my_index_alias:
load_command_fixtures_classes_namespace:
- 'Kununu\TestingBundle\Tests\App\Fixtures\OpenSearch\OpenSearchFixture1'
- 'Kununu\TestingBundle\Tests\App\Fixtures\OpenSearch\OpenSearchFixture2'
```

### 2. Run Symfony Command

The fixtures can be loaded for an OpenSearch Index by running:

```shell
php bin/console kununu_testing:load_fixtures:open_search:my_index_alias --append
```

If `--append` option is not used then the index will be purged.

------------------------------

## Initializable Fixtures

Since this bundle is using the [kununu/data-fixtures](https://github.com/kununu/data-fixtures) package, it also has support for initializable features, allowing you to inject arguments into your feature classes (see [documentation](https://github.com/kununu/data-fixtures) of the kununu/data-fixtures package).

In order to do that, your Fixtures classes must implement the *[InitializableFixtureInterface](https://github.com/kununu/data-fixtures/blob/master/src/InitializableFixtureInterface.php)*, and before loading the fixtures you will need to initialize the arguments.

```php
use Kununu\TestingBundle\Test\FixturesAwareTestCase;
use Kununu\TestingBundle\Test\Options\Options;
final class IntegrationTest extends FixturesAwareTestCase
{
public function testIntegration()
{
$this->registerInitializableFixtureForOpenSearch(
'my_index_alias',
YourOpenSearchFixtureClass::class,
$yourArg1,
// ...,
$yourArgN
);
$this->loadOpenSearchFixtures(
'my_index_alias',
Options::create(),
YourOpenSearchFixtureClass::class
);
}
}
```

-------------------------

## Configuration

Bellow you can find all configuration options for OpenSearch fixtures.

```yaml
kununu_testing:
open_search:
my_index_alias: # Alias to be used to load fixtures for the configured index using the defined service
load_command_fixtures_classes_namespace:
- 'Kununu\TestingBundle\Tests\App\Fixtures\OpenSearch\OpenSearchFixture2' # FQDN for a fixtures class
service: 'Kununu\TestingBundle\Tests\App\OpenSearch' # Service Id of an instance of OpenSearch\Client
index_name: 'my_index_name' # name of your index
```
3 changes: 2 additions & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ parameters:
- src
- tests
errorFormat: table
treatPhpDocTypesAsCertain: false
excludePaths:
- example
- src/DependencyInjection/Configuration.php
- tests/App/
2 changes: 2 additions & 0 deletions rector-ci.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
])
->withSkip([
__DIR__ . '/rector-ci.php',
__DIR__ . '/tests/App/config/bundles.php',
AddOverrideAttributeToOverriddenMethodsRector::class,
])
->withSkipPath(__DIR__ . '/tests/App/var/*')
->withImportNames();
7 changes: 4 additions & 3 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ public function getConfigTreeBuilder(): TreeBuilder
$rootNode,
'non_transactional_connections'
)
->addElasticsearchSection($rootNode)
->addSearchEngineSection($rootNode, 'elastic_search')
->addSearchEngineSection($rootNode, 'open_search')
->addCacheSection($rootNode)
->addHttpClientSection($rootNode);

Expand Down Expand Up @@ -58,11 +59,11 @@ private function addConnectionsSection(ArrayNodeDefinition $node, string $nodeNa
return $this;
}

private function addElasticsearchSection(ArrayNodeDefinition $node): self
private function addSearchEngineSection(ArrayNodeDefinition $node, string $name): self
{
$node
->children()
->arrayNode('elastic_search')
->arrayNode($name)
->requiresAtLeastOneElement()
->useAttributeAsKey('name')
->arrayPrototype()
Expand Down
4 changes: 2 additions & 2 deletions src/Service/SchemaCopy/Factory/AdapterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
namespace Kununu\TestingBundle\Service\SchemaCopy\Factory;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Platforms\MySqlPlatform;
use Doctrine\DBAL\Platforms\MySQLPlatform;
use Kununu\TestingBundle\Service\SchemaCopy\Adapter\MySqlAdapter;
use Kununu\TestingBundle\Service\SchemaCopy\Exception\UnsupportedDatabasePlatformException;
use Kununu\TestingBundle\Service\SchemaCopy\SchemaCopyAdapterFactoryInterface;
Expand All @@ -16,7 +16,7 @@ public function createAdapter(Connection $connection): SchemaCopyAdapterInterfac
{
$databasePlatform = $connection->getDatabasePlatform();

if ($databasePlatform instanceof MySqlPlatform) {
if ($databasePlatform instanceof MySQLPlatform) {
return new MySqlAdapter($connection);
}

Expand Down
Loading

0 comments on commit 66e0035

Please sign in to comment.