Skip to content

Commit

Permalink
Support only PHP 8.3 (#66)
Browse files Browse the repository at this point in the history
* **BREAKING CHANGES**
- Only support PHP 8.3
    - Make classes read-only when possible
	- Add types to constants
    - Upgrade PHPUnit to v11.3
		- Use static versions of asserts and expectations of invocations
	- Change CI pipeline to use PHP 8.3
	- Bump to use `kununu/data-fixtures` 12.0
- Changes on `AbstractTestCase` and children (`FixturesAwareTestCase` and `WebTestCase`):
	- `getFixturesContainer` now has the Symfony `ContainerInterface` and not PSR interface as its return type
	- Add `getServiceFromContainer`
	- Add `getParameterFromContainer`
- Add `FixturesContainerGetterTrait` to help getting common services from the test container:
	- `getConnection` to get a Doctrine DBAL connection
	- `getCachePool` to get a CacheItemPoolInterface
	- `getElasticsearchClient` to get an Elasticsearch client
	- `getHttpClient` to get a Symfony Http client
- Update documentation

* Only use highest on CI pipeline

* Use pcov as coverage driver in CI pipeline

* Add tests to AbstractTestCase::getServiceFromContainer and  AbstractTestCase::getParameterFromContainer
  • Loading branch information
hugo-goncalves-kununu authored Sep 18, 2024
1 parent d476723 commit b21210c
Show file tree
Hide file tree
Showing 68 changed files with 613 additions and 431 deletions.
15 changes: 6 additions & 9 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,13 @@ jobs:
strategy:
matrix:
php-version:
- 8.1
- 8.3
mysql-version:
- 5.7
elasticsearch-version:
- 6.8.15
dependencies:
- lowest
- highest
exclude:
- php-version: 8.1
dependencies: lowest

services:
mysql:
Expand Down Expand Up @@ -60,7 +56,7 @@ jobs:
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-version }}
coverage: xdebug
coverage: pcov
extensions: ${{ matrix.extensions }}

- name: Install Composer Dependencies
Expand All @@ -79,7 +75,7 @@ jobs:
DATABASE_SERVER_VERSION: ${{ matrix.mysql-version }}

- name: Run PHPUnit
run: vendor/bin/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:[email protected]:${{ job.services.mysql.ports['3306'] }}/default_db
PERSISTENCE_DATABASE_URL: mysql://root:[email protected]:${{ job.services.mysql.ports['3306'] }}/persistence_db
Expand All @@ -91,6 +87,7 @@ jobs:
uses: actions/upload-artifact@v4
with:
name: ${{ github.job }}-${{ matrix.php-version }}-${{ matrix.dependencies }}-coverage
include-hidden-files: true
path: tests/.results/

sonarcloud:
Expand All @@ -104,7 +101,7 @@ jobs:

- uses: actions/download-artifact@v4
with:
name: build-8.1-highest-coverage
name: build-8.3-highest-coverage
path: tests/.results/

- name: Fix Code Coverage Paths
Expand All @@ -114,7 +111,7 @@ jobs:
sed -i 's@'$GITHUB_WORKSPACE'@/github/workspace/@g' tests-junit.xml
- name: SonarCloud Scan
uses: sonarsource/sonarcloud-github-action@master
uses: sonarsource/sonarcloud-github-action@v3.0.0
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ It also provides some utilities that makes testing easier, like a `RequestBuilde

**Please be aware that this bundle should not be used in production mode!**

```bash
```shell
composer require --dev kununu/testing-bundle
```

Expand Down
12 changes: 6 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
}
],
"require": {
"php": ">=8.1",
"php": ">=8.3",
"symfony/framework-bundle": "^5.4|^6.4",
"kununu/data-fixtures": "^11.0",
"kununu/data-fixtures": "^12.0",
"symfony/config": "^5.4|^6.4",
"symfony/dependency-injection": "^5.4|^6.4",
"symfony/http-kernel": "^5.4|^6.4"
Expand All @@ -36,8 +36,8 @@
"doctrine/orm": "^3.0",
"elasticsearch/elasticsearch": "^7.10",
"kununu/scripts": ">=5.0",
"matthiasnoback/symfony-dependency-injection-test": "^5.1",
"phpunit/phpunit": "^10.5",
"matthiasnoback/symfony-dependency-injection-test": "^6.0",
"phpunit/phpunit": "^11.3",
"psr/cache": "^1.0|^2.0",
"symfony/browser-kit": "^6.4",
"symfony/dotenv": "^6.4",
Expand Down Expand Up @@ -84,12 +84,12 @@
"test": [
"rm -rf tests/App/var/*",
"tests/App/bin/setup_databases.sh test",
"phpunit --log-events-text phpunit.log --no-coverage --no-logging --no-progress --testsuite Full"
"phpunit --log-events-text phpunit.log --no-coverage --no-logging --no-progress --testsuite Unit,Integration"
],
"test-coverage": [
"rm -rf tests/App/var/*",
"tests/App/bin/setup_databases.sh test",
"XDEBUG_MODE=coverage phpunit --log-events-text phpunit.log --no-progress --testsuite Full"
"XDEBUG_MODE=coverage phpunit --log-events-text phpunit.log --no-progress --testsuite Unit,Integration --log-junit tests/.results/tests-junit.xml --coverage-clover tests/.results/tests-clover.xml --coverage-html tests/.results/html"
]
},
"scripts-descriptions": {
Expand Down
12 changes: 6 additions & 6 deletions docs/FixturesTypes/cache-pool-fixtures.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ framework:
## How to load Cache Pool Fixtures?
In your tests you can extend the classes [FixturesAwareTestCase](/src/Test/FixturesAwareTestCase.php) or [WebTestCase](/src/Test/WebTestCase.php) which expose the following method:
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 loadCachePoolFixtures(string $cachePoolServiceId, OptionsInterface $options, string ...$classNames): void
```
- `$cachePoolServiceId` - Name of your pool as configured in the config above
- `$options` - [Options](options.md) for the fixtures load process
- `...$classNames` - Classes names of fixtures to load
- `$classNames` - Classes names of fixtures to load

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

```php
use Kununu\TestingBundle\Test\FixturesAwareTestCase;
Expand Down Expand Up @@ -59,15 +59,15 @@ final class IntegrationTest extends FixturesAwareTestCase
}
```

You can also disable the creation of orchestrators services for cache pools if you don't want to use fixtures on cache pools (see [configuration](#Configuration)).
You can also disable the creation of orchestrators services for cache pools if you don't want to use fixtures on cache pools (see [configuration](#configuration)).

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

## 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.

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

Expand All @@ -89,7 +89,7 @@ kununu_testing:

The fixtures can be loaded for a cache pool by running:

```bash
```shell
php bin/console kununu_testing:load_fixtures:cache_pools:app.cache.first --append
```

Expand Down
14 changes: 7 additions & 7 deletions docs/FixturesTypes/doctrine-dbal-connection-fixtures.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ doctrine:
## How to load Doctrine Connection Fixtures?
In your tests you can extend the classes [FixturesAwareTestCase](/src/Test/FixturesAwareTestCase.php) or [WebTestCase](/src/Test/WebTestCase.php) which expose the following method:
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 loadDbFixtures(string $connectionName, DbOptionsInterface $options, string ...$classNames)
```

- `$connectionName` - Name of your connection
- `$options` - [Options](options.md) for the fixtures load process
- `...$classNames` - Classes names of fixtures to load
- `$classNames` - Classes names of fixtures to load

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

```php
use Kununu\TestingBundle\Test\FixturesAwareTestCase;
Expand Down Expand Up @@ -98,13 +98,13 @@ final class IntegrationTest extends FixturesAwareTestCase

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.

```bash
```shell
php bin/console kununu_testing:load_fixtures:connections:CONNECTION_NAME [--append]
```

Or for non-transactional fixtures:

```bash
```shell
php bin/console kununu_testing:load_fixtures:non_transactional_connections:CONNECTION_NAME [--append]
```

Expand All @@ -130,13 +130,13 @@ kununu_testing:
The fixtures can be loaded for a Connection by running:
```bash
```shell
php bin/console kununu_testing:load_fixtures:connections:default --append
```

Or for non-transactional fixtures:

```bash
```shell
php bin/console kununu_testing:load_fixtures:non_transactional_connections:default --append
```

Expand Down
12 changes: 6 additions & 6 deletions docs/FixturesTypes/elasticsearch.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ kununu_testing:
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:
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 loadElasticsearchFixtures(string $alias, OptionsInterface $options, string ...$classNames): void
Expand All @@ -25,9 +25,9 @@ protected function loadElasticsearchFixtures(string $alias, OptionsInterface $op
- `$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
- `$classNames` - Classes names of fixtures to load

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

```php
use Kununu\TestingBundle\Test\FixturesAwareTestCase;
Expand Down Expand Up @@ -66,11 +66,11 @@ final class IntegrationTest extends FixturesAwareTestCase

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.

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

### 1. Enable Symfony Command for a Elasticsearch Index
### 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.

Expand All @@ -87,7 +87,7 @@ kununu_testing:

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

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

Expand Down
10 changes: 5 additions & 5 deletions docs/FixturesTypes/options.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# FixturesAwareTestCase Options

The methods to load fixtures on [FixturesAwareTestCase](/src/Test/FixturesAwareTestCase.php) need to receive a parameter `$options`.
The methods to load fixtures on [FixturesAwareTestCase](../../src/Test/FixturesAwareTestCase.php) need to receive a parameter `$options`.

## OptionsInterface

That object must implement the interface [OptionsInterface](/src/Test/Options/OptionsInterface.php) which defines the following methods:
That object must implement the interface [OptionsInterface](../../src/Test/Options/OptionsInterface.php) which defines the following methods:

```php
public function append(): bool;
Expand All @@ -22,7 +22,7 @@ If this method returns `true` then it will clear any previous loaded fixtures cl

## Options

The class [Options](/src/Test/Options/Options.php) is provided, and it implements the `OptionsInterface` and also provides a builder pattern.
The class [Options](../../src/Test/Options/Options.php) is provided, and it implements the `OptionsInterface` and also provides a builder pattern.

```php
use Kununu\TestingBundle\Test\Options\Options;
Expand Down Expand Up @@ -55,7 +55,7 @@ $options = Options::create()->withoutClear()->withAppend();

To load fixtures with database connections (`loadDbFixtures`) it is required to pass a more specialized instance of options.

That object must implement the interface [DbOptionsInterface](/src/Test/Options/DbOptionsInterface.php) which is an extension of `OptionsInterface` (thus having all the same methods) and adds the following method:
That object must implement the interface [DbOptionsInterface](../../src/Test/Options/DbOptionsInterface.php) which is an extension of `OptionsInterface` (thus having all the same methods) and adds the following method:

```php
public function transactional(): bool;
Expand All @@ -67,7 +67,7 @@ If this method returns `true` then it will use a transactional executor, otherwi

## DbOptions

The class [DbOptions](/src/Test/Options/DbOptions.php) is provided, and it implements the `DbOptionsInterface` and also provides a builder pattern.
The class [DbOptions](../../src/Test/Options/DbOptions.php) is provided, and it implements the `DbOptionsInterface` and also provides a builder pattern.

It extends the `Options` class so all the methods available there are still in this class.

Expand Down
29 changes: 27 additions & 2 deletions docs/FixturesTypes/symfony-http-client.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ In the rest of the documentation we will assume that you are using the [Symfony

## How to load Symfony Http Client Fixtures?

In your tests you can extend the classes [FixturesAwareTestCase](/src/Test/FixturesAwareTestCase.php) or [WebTestCase](/src/Test/WebTestCase.php) which expose the following method:
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 loadHttpClientFixtures(string $httpClientServiceId, OptionsInterface $options, string ...$classNames): void
```

- `$httpClientServiceId` - Name of your Symfony Http Client service
- `$options` - [Options](options.md) for the fixtures load process
- `...$classNames` - Classes names of fixtures to load
- `$classNames` - Classes names of fixtures to load

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

Expand Down Expand Up @@ -97,3 +97,28 @@ Also be mindful that **if you inject the same client on several services** you m
Example: you are testing service A which uses component B. Component B is also injected with the same Http client but is calling totally different endpoints.
To solve those cases create dedicated Http clients services for the service you are testing.
### Common Problems and Solutions
#### The mocked Http Client is not being used
- Try to create an alias of the Symfony interface to your client in your *test* environment configuration:
```yaml
services:
http_client:
class: Kununu\DataFixtures\Tools\HttpClient
public: true

Symfony\Contracts\HttpClient\HttpClientInterface: '@http_client'
```
- If the mock Http client is still not loaded make sure to add to your *test* environment `framework.yaml` file:

```yaml
framework:
http_client:
enabled: false
```

To disable Symfony Framework bundle creating its own Http clients.
2 changes: 1 addition & 1 deletion docs/SchemaCopier/schema-copier.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ This bundle will provide you with a command that can fetch the schema for that e

This bundle will automatically create a Symfony Command allow copying databases schemas (if you have at least a Doctrine connection configured).

```bash
```shell
php bin/console kununu_testing:connections:schema:copy --from SOURCE --to DESTINATION
```

Expand Down
14 changes: 1 addition & 13 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,34 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- https://phpunit.readthedocs.io/en/10.5/ -->
<!-- https://docs.phpunit.de/en/11.3/ -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" bootstrap="tests/bootstrap.php"
colors="true" beStrictAboutChangesToGlobalState="true" cacheDirectory=".phpunit.cache" testdox="true">
<coverage>
<report>
<clover outputFile="tests/.results/tests-clover.xml"/>
<html outputDirectory="tests/.results/html/"/>
</report>
</coverage>
<php>
<ini name="error_reporting" value="-1"/>
<server name="APP_ENV" value="test" force="true"/>
<env name="APP_DEBUG" value="0" force="true"/>
<server name="KERNEL_CLASS" value="Kununu\TestingBundle\Tests\App\Kernel"/>
</php>
<testsuites>
<testsuite name="Full">
<directory>tests</directory>
</testsuite>
<testsuite name="Integration">
<directory>tests/Integration</directory>
</testsuite>
<testsuite name="Unit">
<directory>tests/Unit</directory>
</testsuite>
</testsuites>
<logging>
<junit outputFile="tests/.results/tests-junit.xml"/>
</logging>
<source>
<include>
<directory>src</directory>
Expand Down
Loading

0 comments on commit b21210c

Please sign in to comment.