Skip to content

Commit

Permalink
Release/1.1.0 (#11)
Browse files Browse the repository at this point in the history
* refactor: Refactors database initialization for testing.
  • Loading branch information
gustavofreze authored Dec 2, 2024
1 parent fb7e1ab commit d3a06a4
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 18 deletions.
27 changes: 22 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
name: CI

on:
push:
pull_request:

permissions:
contents: read

env:
PHP_VERSION: '8.3'

jobs:
auto-review:
name: Auto review
Expand All @@ -18,7 +22,7 @@ jobs:
- name: Configure PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.3'
php-version: ${{ env.PHP_VERSION }}

- name: Install dependencies
run: composer update --no-progress --optimize-autoloader
Expand All @@ -37,12 +41,25 @@ jobs:
- name: Configure PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.3'
php-version: ${{ env.PHP_VERSION }}

- name: Install dependencies
run: composer update --no-progress --optimize-autoloader

- name: Clean up Docker
run: docker system prune -f

- name: Create Docker network
run: docker network create account_default

- name: Create Docker volume for migrations
run: docker volume create account-adm-migrations

- name: Run tests
env:
XDEBUG_MODE: coverage
run: composer unit-test
run: |
docker run -u root --name account-test --network=account_default \
-v ${PWD}:/app \
-v ${PWD}/config/database/mysql/migrations:/account-adm-migrations \
-v /var/run/docker.sock:/var/run/docker.sock \
-w /app \
gustavofreze/php:${{ env.PHP_VERSION }} bash -c "composer tests"
22 changes: 14 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,20 @@ PHP_IMAGE = gustavofreze/php:8.3
FLYWAY_IMAGE = flyway/flyway:10.20.1

APP_RUN = docker run ${PLATFORM} -u root --rm -it -v ${PWD}:/app -w /app ${PHP_IMAGE}
APP_TEST_RUN = docker run ${PLATFORM} -u root --rm -it --name account-test --link account-adm --network=account_default -v ${PWD}:/app -w /app ${PHP_IMAGE}
APP_TEST_RUN = docker run ${PLATFORM} -u root --rm -it \
--name account-test \
--network=account_default \
-v ${PWD}:/app \
-v ${PWD}/config/database/mysql/migrations:/account-adm-migrations \
-v /var/run/docker.sock:/var/run/docker.sock \
-w /app \
${PHP_IMAGE}

FLYWAY_RUN = docker run ${PLATFORM} --rm -v ${PWD}/config/database/mysql/migrations:/flyway/sql --env-file=config/local.env --network=account_default ${FLYWAY_IMAGE}
MIGRATE_DB = ${FLYWAY_RUN} -locations=filesystem:/flyway/sql -schemas=account_adm -connectRetries=15
MIGRATE_TEST_DB = ${FLYWAY_RUN} -locations=filesystem:/flyway/sql -schemas=account_adm_test -connectRetries=15

.DEFAULT_GOAL := help
.PHONY: start stop configure migrate-database clean-database migrate-test-database test test-no-coverage review show-reports help show-logs
.PHONY: start stop configure create-volume migrate-database clean-database test test-no-coverage review show-reports help show-logs

start: ## Start application containers
@docker compose up -d --build
Expand All @@ -33,10 +39,10 @@ stop: ## Stop application containers
configure: ## Configure development environment
@${APP_RUN} composer update --optimize-autoloader

test: migrate-test-database ## Run all tests with coverage
test: create-volume ## Run all tests with coverage
@${APP_TEST_RUN} composer run tests

test-no-coverage: migrate-test-database ## Run all tests without coverage
test-no-coverage: create-volume ## Run all tests without coverage
@${APP_TEST_RUN} composer run tests-no-coverage

review: ## Run static code analysis
Expand All @@ -45,15 +51,15 @@ review: ## Run static code analysis
show-reports: ## Open static analysis reports (e.g., coverage, lints) in the browser
@sensible-browser report/coverage/coverage-html/index.html report/coverage/mutation-report.html

create-volume: ## Create database migrations volume
@docker volume create account-adm-migrations

migrate-database: ## Run database migrations
@${MIGRATE_DB} migrate

clean-database: ## Clean database
@${MIGRATE_DB} clean

migrate-test-database: ## Run test database migrations
@${MIGRATE_TEST_DB} migrate

show-logs: ## Display application logs
@docker logs -f account

Expand Down
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,21 @@
"phpunit/phpunit": "^11",
"dg/bypass-finals": "^1.8",
"infection/infection": "^0.29",
"squizlabs/php_codesniffer": "^3.10"
"squizlabs/php_codesniffer": "^3.10",
"tiny-blocks/docker-container": "^1"
},
"config": {
"sort-packages": true,
"allow-plugins": {
"infection/extension-installer": true
}
},
"process-timeout": 400
},
"scripts": {
"test": "phpunit -c phpunit.xml --log-junit=report/coverage/junit.xml --coverage-xml=report/coverage/coverage-xml --coverage-html=report/coverage/coverage-html tests",
"phpcs": "phpcs --standard=PSR12 --extensions=php ./src",
"phpmd": "phpmd ./src text phpmd.xml --suffixes php --exclude /src/Routes.php --ignore-violations-on-exit",
"phpstan": "phpstan analyse -c phpstan.neon.dist --quiet --no-progress",
"unit-test": "phpunit -c phpunit.xml --no-coverage --testsuite unit",
"mutation-test": "infection --only-covered --logger-html=report/coverage/mutation-report.html --coverage=report/coverage --min-msi=100 --min-covered-msi=100 --threads=4",
"test-no-coverage": "phpunit -c phpunit.xml --no-coverage tests",
"review": [
Expand Down
2 changes: 1 addition & 1 deletion infection.json.dist
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"timeout": 15,
"timeout": 400,
"testFramework": "phpunit",
"tmpDir": "report/infection/",
"source": {
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
<php>
<env name="APP_NAME" value="account"/>

<env name="DATABASE_HOST" value="account-adm"/>
<env name="DATABASE_HOST" value="account-adm-test"/>
<env name="DATABASE_PORT" value="3306"/>
<env name="DATABASE_NAME" value="account_adm_test"/>
<env name="DATABASE_USER" value="root"/>
Expand Down
72 changes: 72 additions & 0 deletions tests/Integration/Database.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

declare(strict_types=1);

namespace Test\Integration;

use Account\Environment;
use TinyBlocks\DockerContainer\GenericDockerContainer;
use TinyBlocks\DockerContainer\MySQLDockerContainer;
use TinyBlocks\DockerContainer\Waits\Conditions\MySQL\MySQLReady;
use TinyBlocks\DockerContainer\Waits\ContainerWaitForDependency;
use TinyBlocks\DockerContainer\Waits\ContainerWaitForTime;

final readonly class Database
{
private string $host;

private string $database;

private string $username;

private string $password;

public function __construct()
{
$this->host = Environment::get(variable: 'DATABASE_HOST')->toString();
$this->database = Environment::get(variable: 'DATABASE_NAME')->toString();
$this->username = Environment::get(variable: 'DATABASE_USER')->toString();
$this->password = Environment::get(variable: 'DATABASE_PASSWORD')->toString();
}

public function start(): void
{
$mySQLContainer = MySQLDockerContainer::from(image: 'mysql:8.1', name: $this->host)
->withNetwork(name: 'account_default')
->withTimezone(timezone: 'America/Sao_Paulo')
->withUsername(user: $this->username)
->withPassword(password: $this->password)
->withDatabase(database: $this->database)
->withRootPassword(rootPassword: $this->password)
->withGrantedHosts()
->withVolumeMapping(pathOnHost: '/var/lib/mysql', pathOnContainer: '/var/lib/mysql')
->runIfNotExists();

$jdbcUrl = $mySQLContainer->getJdbcUrl();

GenericDockerContainer::from(image: 'flyway/flyway:11.0.0')
->withNetwork(name: 'account_default')
->copyToContainer(pathOnHost: '/account-adm-migrations', pathOnContainer: '/flyway/sql')
->withVolumeMapping(pathOnHost: '/account-adm-migrations', pathOnContainer: '/flyway/sql')
->withWaitBeforeRun(
wait: ContainerWaitForDependency::untilReady(
condition: MySQLReady::from(
container: $mySQLContainer
)
)
)
->withEnvironmentVariable(key: 'FLYWAY_URL', value: $jdbcUrl)
->withEnvironmentVariable(key: 'FLYWAY_USER', value: $this->username)
->withEnvironmentVariable(key: 'FLYWAY_TABLE', value: 'schema_history')
->withEnvironmentVariable(key: 'FLYWAY_SCHEMAS', value: $this->database)
->withEnvironmentVariable(key: 'FLYWAY_EDITION', value: 'community')
->withEnvironmentVariable(key: 'FLYWAY_PASSWORD', value: $this->password)
->withEnvironmentVariable(key: 'FLYWAY_LOCATIONS', value: 'filesystem:/flyway/sql')
->withEnvironmentVariable(key: 'FLYWAY_CLEAN_DISABLED', value: 'false')
->withEnvironmentVariable(key: 'FLYWAY_VALIDATE_MIGRATION_NAMING', value: 'true')
->run(
commands: ['-connectRetries=15', 'clean', 'migrate'],
waitAfterStarted: ContainerWaitForTime::forSeconds(seconds: 5)
);
}
}
4 changes: 4 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
<?php

use DG\BypassFinals;
use Test\Integration\Database;

require_once __DIR__ . '/../vendor/autoload.php';

BypassFinals::enable();

$database = new Database();
$database->start();

0 comments on commit d3a06a4

Please sign in to comment.