Skip to content

Commit

Permalink
test(feat): add DB_CONNECTION environment variable for test differe…
Browse files Browse the repository at this point in the history
…nt database platform

- Update GitHub actions build with new steps
  • Loading branch information
chiqui3d committed May 7, 2023
1 parent d748748 commit 5bec106
Show file tree
Hide file tree
Showing 12 changed files with 131 additions and 44 deletions.
27 changes: 20 additions & 7 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,6 @@ MOUNT_APP_DIRECTORY=${APP_DIRECTORY}
#HOST_GID=1001# id -g $(whoami)
#HOST_DOCKER_GID=1001# getent group docker | cut -d: -f3

# MariaDB/Mysql
MARIA_VERSION=10.6.5
MYSQL_PORT=3306
MYSQL_ROOT_PASSWORD=${PROJECT_NAME}-root-password
MYSQL_DATABASE=${PROJECT_NAME}-db
MYSQL_USER=${PROJECT_NAME}-user
MYSQL_PASSWORD=${PROJECT_NAME}-pass

# PHP
PHP_VERSION=8.1
Expand All @@ -33,3 +26,23 @@ PHP_IDE_CONFIG=serverName=${VIRTUAL_HOST}
XDEBUG_CONFIG="discover_client_host=1 idekey=PHPSTORM client_host=host.docker.internal client_port=9003"
XDEBUG_MODE=off

# Database connection: default, postgres
DB_CONNECTION=default

## MariaDB/Mysql
MARIA_VERSION=10.6.5
MYSQL_PORT=3306
MYSQL_ROOT_PASSWORD=${PROJECT_NAME}-root-password
MYSQL_DATABASE=${PROJECT_NAME}-db
MYSQL_USER=${PROJECT_NAME}-user
MYSQL_PASSWORD=${PROJECT_NAME}-pass
MYSQL_DATABASE_URL=mysql://root:${MYSQL_ROOT_PASSWORD}@mariadb/${MYSQL_DATABASE}?serverVersion=mariadb-${MARIA_VERSION}

## PostgreSQL
# https://github.com/docker-library/docs/blob/master/postgres/README.md#postgres_user
POSTGRES_VERSION=14.7
POSTGRES_PORT=5432
POSTGRES_DB=${MYSQL_DATABASE}
POSTGRES_USER=${MYSQL_USER}
POSTGRES_PASSWORD=${MYSQL_PASSWORD}
POSTGRES_DATABASE_URL=postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@postgres/${POSTGRES_DB}?serverVersion=${POSTGRES_VERSION}&charset=utf8
4 changes: 4 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ jobs:
run: make phpunit-ci
- name: Run Behat tests
run: make behat-ci
- name: Run PHPUnit tests with Postgres
run: DB_CONNECTION=postgres make phpunit-ci
- name: Run Behat tests with Postgres
run: DB_CONNECTION=postgres make behat-ci
- name: Stop and remove containers
if: always()
run: make docker-down
25 changes: 13 additions & 12 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ ifneq ($(TMP_ENV),)
override DOCKER_ENV = $(TMP_ENV)
endif

ifneq ($(DB_CONNECTION_OVERRIDE),)
override DB_CONNECTION = $(DB_CONNECTION_OVERRIDE)
endif

.EXPORT_ALL_VARIABLES:

## Configuration ##
Expand All @@ -40,13 +44,13 @@ ifeq ($(CI_ENABLED),true)
HOST_UID = 1001
HOST_GID = 1001
DOCKER_COMPOSE := DOCKER_ENV=$(DOCKER_ENV) HOST_UID=$(HOST_UID) HOST_GID=$(HOST_GID) BUILDKIT_PROGRESS=plain DOCKER_BUILDKIT=1 docker compose -f docker-compose.yml -f docker-compose.ci.yml --env-file $(ENV_FILE)
DOCKER_EXEC_PHP := $(DOCKER_COMPOSE) exec -T php
DOCKER_EXEC_ROOT_PHP := $(DOCKER_COMPOSE) exec -T -u root php
DOCKER_EXEC_PHP := $(DOCKER_COMPOSE) exec -e DB_CONNECTION=$(DB_CONNECTION) -T php
DOCKER_EXEC_ROOT_PHP := $(DOCKER_COMPOSE) exec -e DB_CONNECTION=$(DB_CONNECTION) -T -u root php
else
SHELL = /bin/zsh
DOCKER_COMPOSE := DOCKER_ENV=$(DOCKER_ENV) HOST_UID=$(HOST_UID) HOST_GID=$(HOST_GID) BUILDKIT_PROGRESS=plain DOCKER_BUILDKIT=1 docker compose -f docker-compose.yml --env-file $(ENV_FILE)
DOCKER_EXEC_PHP := $(DOCKER_COMPOSE) exec php
DOCKER_EXEC_ROOT_PHP := $(DOCKER_COMPOSE) exec -u root php
DOCKER_EXEC_PHP := $(DOCKER_COMPOSE) exec -e DB_CONNECTION=$(DB_CONNECTION) php
DOCKER_EXEC_ROOT_PHP := $(DOCKER_COMPOSE) exec -e DB_CONNECTION=$(DB_CONNECTION) -u root php
endif

.DEFAULT_GOAL = help
Expand Down Expand Up @@ -81,18 +85,15 @@ test-variables: ## Show all variables
@echo "ENV_FILE: $(ENV_FILE)"
@echo "HOST_UID: $(HOST_UID)"
@echo "HOST_GID: $(HOST_GID)"
@echo "PROJECT_NAME: $$PROJECT_NAME"
@echo "APP_DIRECTORY: $$APP_DIRECTORY"
@echo "SYMFONY APP_ENV: $$APP_ENV"
@echo "SYMFONY APP_DEBUG: $$APP_DEBUG"
@echo "PHP_VERSION: $$PHP_VERSION"
@echo "HOST IP: $(HOST_IP)"
@echo $(MAKECMDGOALS)
make docker -- config
@echo "MYSQL_DATABASE_URL: $(MYSQL_DATABASE_URL)"
@echo "POSTGRES_DATABASE_URL: $(POSTGRES_DATABASE_URL)"
@echo "DB_CONNECTION: $(DB_CONNECTION)"


##@ General
ci: lint test composer-validate ## All in one
lint: phpstan-clear phpstan php-cs-fixer ## All in one for lint tools
test: phpunit behat ## All in one for test tools

# remember add *.Makefile in Configuration -> Editor -> Files Types in PHPSTORM (GNU Makefile)
-include tools/make/docker.Makefile
Expand Down
34 changes: 33 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,33 @@ x-logging: &logging
driver: json-file

services:
postgres:
image: postgres:${POSTGRES_VERSION}-alpine
container_name: ${PROJECT_NAME}-postgres
hostname: postgres
mem_limit: 300m
memswap_limit: 100m
restart: always
<<: *logging
environment:
POSTGRES_DB: ${POSTGRES_DB}
POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
POSTGRES_INITDB_ARGS: '--encoding=UTF-8'
LC_COLLATE: C
LC_CTYPE: C
LANG: C.UTF-8
TZ: ${TZ}
PGTZ: ${TZ}
expose:
- 5432
networks:
- db
healthcheck:
test: [ "CMD-SHELL", "pg_isready -U ${POSTGRES_USER}" ]
interval: 20s
timeout: 10s
retries: 7
mariadb:
container_name: ${PROJECT_NAME}-mariadb
hostname: mariadb
Expand Down Expand Up @@ -62,20 +89,25 @@ services:
- APP_DIRECTORY=${APP_DIRECTORY}
- DOCKER_ENV=${DOCKER_ENV}
- CI=${CI:-false}
- MARIA_VERSION=${MARIA_VERSION}
- DB_CONNECTION=${DB_CONNECTION}
- MYSQL_DATABASE_URL=${MYSQL_DATABASE_URL}
- POSTGRES_DATABASE_URL=${POSTGRES_DATABASE_URL}
- VIRTUAL_HOST=${VIRTUAL_HOST}
- PHP_IDE_CONFIG=${PHP_IDE_CONFIG}
- XDEBUG_CONFIG=${XDEBUG_CONFIG}
- XDEBUG_MODE=${XDEBUG_MODE}
- COMPOSER_ALLOW_SUPERUSER=${COMPOSER_ALLOW_SUPERUSER}
volumes:
- ${PWD}:${MOUNT_APP_DIRECTORY}
- ../:/var/www/bundles
expose:
- "9000" # php-fpm
- "9003" # xdebug
depends_on:
mariadb:
condition: service_healthy
postgres:
condition: service_healthy
healthcheck:
test: ["CMD", "php-fpm", "-t"]
interval: 20s
Expand Down
2 changes: 1 addition & 1 deletion tests/.env
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ APP_DEBUG=0
KERNEL_CLASS='Ranky\MediaBundle\Tests\TestKernel'
APP_SECRET='secreto_iberico'
SYMFONY_DEPRECATIONS_HELPER=999999
DATABASE_URL=mysql://root:ranky-media-bundle-root-password@mariadb/ranky-media-bundle-db?serverVersion=mariadb-10.6.5
SITE_URL=http://ranky-media-bundle.test
###! DATABASE_URL is defined in docker env
61 changes: 47 additions & 14 deletions tests/config/doctrine.php
Original file line number Diff line number Diff line change
@@ -1,25 +1,58 @@
<?php

declare(strict_types=1);

use Ranky\MediaBundle\Infrastructure\Persistence\Dql\Mysql\MimeSubType;
use Ranky\MediaBundle\Infrastructure\Persistence\Dql\Mysql\MimeType;
use Ranky\MediaBundle\Infrastructure\Persistence\Dql\Mysql\Month;
use Ranky\MediaBundle\Infrastructure\Persistence\Dql\Mysql\Year;
use Ranky\MediaBundle\Infrastructure\Persistence\Dql\Postgresql\MimeSubType as PostgresqlMimeSubType;
use Ranky\MediaBundle\Infrastructure\Persistence\Dql\Postgresql\MimeType as PostgresqlMimeType;
use Ranky\MediaBundle\Infrastructure\Persistence\Dql\Postgresql\Month as PostgresqlMonth;
use Ranky\MediaBundle\Infrastructure\Persistence\Dql\Postgresql\Year as PostgresqlYear;
use Symfony\Config\DoctrineConfig;

return static function (DoctrineConfig $doctrineConfig): void {
# "TEST_TOKEN" is typically set by ParaTest
$doctrineConfig->dbal()
->defaultConnection('default')
->connection('default')
->url('%env(resolve:DATABASE_URL)%')
->dbnameSuffix('_test%env(default::TEST_TOKEN)%')
->logging(false)
->charset('utf8')
$dbal = $doctrineConfig->dbal();
$dbConnection = $_SERVER['DB_CONNECTION'] ?? 'default';

$dbalConnection = $dbal->connection('default');
$dbal->defaultConnection('default');
if ($dbConnection === 'postgres') {
$dbalConnection
->charset('utf8')
->url(getenv('POSTGRES_DATABASE_URL'))
;
}else{
$dbalConnection
->charset('utf8mb4')
->url(getenv('MYSQL_DATABASE_URL'))
;

}

$emDefault = $doctrineConfig->orm()->autoGenerateProxyClasses(true)->entityManager('default');
$emDefault->autoMapping(true);
$emDefault
->mapping('Tests')
$orm = $doctrineConfig->orm();
$orm->defaultEntityManager('default')->autoGenerateProxyClasses(true);

$em = $orm->entityManager('default');
$em->connection('default');
$em->autoMapping(true);
$em->namingStrategy('doctrine.orm.naming_strategy.underscore');
$em->mapping('Tests')
->dir('%kernel.project_dir%/src/Dummy')
->prefix('Ranky\MediaBundle\Tests\Dummy')
;
->prefix('Ranky\MediaBundle\Tests\Dummy');
$dql = $em->dql();

if ($dbConnection === 'postgres') {
$dql->stringFunction('MIME_TYPE', PostgresqlMimeType::class);
$dql->stringFunction('MIME_SUBTYPE', PostgresqlMimeSubType::class);
$dql->datetimeFunction('YEAR', PostgresqlYear::class);
$dql->datetimeFunction('MONTH', PostgresqlMonth::class);
}else{
$dql->stringFunction('MIME_TYPE', MimeType::class);
$dql->stringFunction('MIME_SUBTYPE', MimeSubType::class);
$dql->datetimeFunction('YEAR', Year::class);
$dql->datetimeFunction('MONTH', Month::class);
}

};
10 changes: 7 additions & 3 deletions tools/docker/php-fpm/build.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
ARG PHP_VERSION=8.1
# -fpm-alpine3.16
FROM php:${PHP_VERSION}-fpm-alpine as build
ARG DOCKER_ENV=dev
ARG APP_DIRECTORY=/var/www/ranky-media-bundle
Expand All @@ -13,6 +14,8 @@ RUN apk add --no-cache --update-cache $PHPIZE_DEPS \
git \
### usermod change uid & gid
shadow \
### pdo_pgsql
libpq libpq-dev \
### Intl
icu icu-dev icu-libs icu-data-full \
### Not remember. Encryption maybe
Expand All @@ -29,13 +32,14 @@ RUN apk add --no-cache --update-cache $PHPIZE_DEPS \
jpegoptim optipng pngquant gifsicle libwebp libwebp-tools \
### Xml
libxml2-dev && \
### php extensions
### php extensions \
#docker-php-ext-install -j$(nproc) iconv && \
docker-php-ext-configure pgsql -with-pgsql=/usr/local/pgsql && \
docker-php-ext-configure gd --with-jpeg=/usr/include/ --with-freetype=/usr/include/ && \
docker-php-ext-install -j$(nproc) gd && \
docker-php-ext-configure intl && \
docker-php-ext-configure zip && \
docker-php-ext-install zip intl xml opcache pdo pdo_mysql && \
docker-php-ext-install zip intl xml opcache pdo pdo_mysql pgsql pdo_pgsql && \
### enable imagick
pecl install imagick && \
docker-php-ext-enable imagick && \
Expand All @@ -50,7 +54,7 @@ fi

### Clean ###
RUN apk del $PHPIZE_DEPS && \
apk del --no-cache icu-dev libxml2-dev freetype-dev libpng-dev libjpeg-turbo-dev imagemagick-dev && \
apk del --no-cache icu-dev libxml2-dev libpq-dev freetype-dev libpng-dev libjpeg-turbo-dev imagemagick-dev && \
rm -rf /var/cache/apk/* /tmp/* /var/tmp/* /usr/share/doc/*

### composer ###
Expand Down
4 changes: 2 additions & 2 deletions tools/make/git.Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ git-change-log: ## generate change log and make commit
git commit -m "docs: update change log"

git-publish: ## fix with php-cs-fixer, create tag version && push
make tools-generate-change-log
make git-change-log
make git-php-cs-fixer
make git-tag

Expand All @@ -113,7 +113,7 @@ git-tag: ## create new tag version && push
@echo "Pushing tag $(NEXT_GIT_TAG_VERSION)..."
git push --atomic origin HEAD --tags

## with date: https://stackoverflow.com/a/21759466/2046442
## other option with date: https://stackoverflow.com/a/21759466/2046442
git-tag-from-last-commit: ## create new tag version from last commit && push
@echo "Updating tags from remote..."
git fetch --all --tags --force
Expand Down
2 changes: 0 additions & 2 deletions tools/make/lint.Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ phpstan-single: ## Run PHPStan Example: -c phpstan.neon --memory-limit 1G --no-p
phpstan-ci: ## Run PHPStan as root for CI
$(DOCKER_EXEC_ROOT_PHP) bash -c "$(PHPSTAN_BIN) analyse -c $(PHPSTAN_DIRECTORY)/phpstan.neon --no-progress --no-interaction"

lint: phpstan-clear phpstan php-cs-fixer ## All in one for lint tools

##@ Validate

validate-composer: ## Composer validate && diagnose && audit
Expand Down
2 changes: 0 additions & 2 deletions tools/make/test.Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,3 @@ behat-s3: ## behat test in aws s3 storage

behat-ci: ## behat as root for CI
$(DOCKER_EXEC_ROOT_PHP) bash -c "$(BEHAT_BIN) --config $(BEHAT_DIRECTORY)/behat.yml --stop-on-failure -vv"

test: phpunit behat ## All in one for test tools
1 change: 1 addition & 0 deletions tools/phpstan/phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
parameters:
excludePaths:
- ../../tests/var/*
- ../../tests/config/*
- ../../tests/src/TestKernel.php
- ../../src/Infrastructure/DependencyInjection/MediaCompilerPass.php
Expand Down
3 changes: 3 additions & 0 deletions tools/phpunit/phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
<directory suffix="Test.php">../../tests/src/Domain</directory>
<directory suffix="Test.php">../../tests/src/Infrastructure</directory>
<directory suffix="Test.php">../../tests/src/Presentation</directory>
<!-- <exclude>
<directory suffix="Test.php">../../tests/src/Infrastructure/Persistence/Orm/Dql</directory>
</exclude>-->
</testsuite>
<testsuite name="clean">
<file>../../tests/src/CleanTest.php</file>
Expand Down

0 comments on commit 5bec106

Please sign in to comment.