Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Git
.git

# IntelliJ
.idea
*.iml

# Composer
vendor

# PHP code sniffer
.php_cs.cache

# PHP Unit
build
.phpunit.result.cache

# Dev / Testing
test.php
coverage/*
18 changes: 18 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# This file is for unifying the coding style for different editors and IDEs.
# More information at http://editorconfig.org

root = true

[*]
charset = utf-8
indent_size = 4
indent_style = space
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true

[Dockerfile]
indent_size = 2

[*.md]
trim_trailing_whitespace = false
14 changes: 14 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
version: 2
updates:
- package-ecosystem: "composer"
directory: "/"
schedule:
interval: "daily"
time: "08:00"
timezone: "Europe/Paris"
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "daily"
time: "08:00"
timezone: "Europe/Paris"
105 changes: 19 additions & 86 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,91 +1,24 @@
name: test

on:
push:
pull_request:
push:
pull_request:

jobs:
test:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os:
- ubuntu-latest

services:
mysql:
image: mysql:5.7
env:
MYSQL_ALLOW_EMPTY_PASSWORD: yes
MYSQL_USER: root
MYSQL_PASSWORD: ''
MYSQL_DATABASE: test_database
ports:
- 3306:3306
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3

redis:
image: redis
options: >-
--health-cmd "redis-cli ping"
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 6379:6379

steps:
- name: Verify MySQL connection from host
run: |
sudo apt-get install -y mysql-client
mysql --host 127.0.0.1 --port 3306 -uroot --password="" -e "SHOW DATABASES"

- uses: actions/checkout@v2

- uses: shivammathur/setup-php@v2
with:
php-version: '7.4'
coverage: xdebug

- name: Set files permission
run: |
sudo chown -R www-data:www-data /var/lib/php/sessions/
sudo chmod -R 777 /var/lib/php/sessions/

- name: Validate composer.json and composer.lock
run: composer validate

- name: Cache Composer packages
id: composer-cache
uses: actions/cache@v2
with:
path: vendor
key: ${{ runner.os }}-node-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-node-

- name: Install dependencies
if: steps.composer-cache.outputs.cache-hit != 'true'
run: composer install --prefer-dist --no-progress --no-suggest

- name: PHPCS
if: ${{ runner.os != 'Windows' }}
run: |
vendor/bin/php-cs-fixer fix --diff --dry-run

- name: PHPUnit
run: |
vendor/bin/phpunit --colors --coverage-text --coverage-clover coverage/clover.xml
env:
MYSQL_HOST: 127.0.0.1
MYSQL_PORT: ${{ job.services.mysql.ports[3306] }}
MYSQL_USER: root
MYSQL_PASSWORD:

- name: Upload coverage
uses: codecov/codecov-action@v1.0.7
if: success()
with:
token: ${{ secrets.CODECOV_TOKEN }}
file: ./coverage/clover.xml
test:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Pull
run: docker-compose pull --quiet
- name: Build
run: docker-compose build
- name: Test
run: docker-compose run lib composer ci
- name: Upload coverage
uses: codecov/codecov-action@v1.0.14
if: success()
with:
token: ${{ secrets.CODECOV_TOKEN }}
file: ./coverage/clover.xml
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ build/

# Dev / Testing
test.php
coverage/
coverage/*
!coverage/.gitkeep
22 changes: 22 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
ARG PHPVERSION="7.4"
FROM php:$PHPVERSION-cli-alpine

RUN apk --update --no-cache add \
mysql-client \
&& rm -rf /tmp/* /var/cache/apk/*

RUN docker-php-ext-install \
pdo_mysql

RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/bin --filename=composer

WORKDIR /app

COPY composer.json .
COPY composer.lock .
RUN composer validate
RUN composer install --no-interaction --no-progress --no-suggest

COPY . .

ENTRYPOINT ["./entrypoint.sh"]
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,4 @@ composer require predis/predis
```

## How to Dev
`composer ci` for php-cs-fixer and phpunit and coverage
`composer lint` for php-cs-fixer
`composer test` for phpunit and coverage
`docker-compose run lib composer ci` for launching tests
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"php-cs-fixer fix"
],
"test": [
"phpunit --colors --coverage-html ./coverage"
"phpdbg -qrr vendor/bin/phpunit --colors --coverage-text --coverage-clover ./coverage/clover.xml"
]
}
}
Empty file added coverage/.gitkeep
Empty file.
25 changes: 25 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
version: "3.8"

services:
mariadb:
image: mariadb:10.4
environment:
- "MYSQL_ALLOW_EMPTY_PASSWORD=yes"
- "MYSQL_DATABASE=test_database"
restart: "on-failure"

redis:
image: redis:6
restart: "on-failure"

lib:
build: ./
depends_on:
- mariadb
- redis
environment:
- "MYSQL_HOST=mariadb"
- "REDIS_HOST=redis"
volumes:
- "./coverage:/app/coverage"
restart: "no"
19 changes: 19 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/sh

DB_TIMEOUT=${DB_TIMEOUT:-45}

# Check MySQL up
MYSQL_CMD="mysql -h ${MYSQL_HOST} -P ${MYSQL_PORT:-3306} -u ${MYSQL_USER:-root}"
echo "Waiting ${DB_TIMEOUT}s for MySQL database to be ready..."
counter=1
while ! ${MYSQL_CMD} -e "show databases;" > /dev/null 2>&1; do
sleep 1
counter=$((counter + 1))
if [ ${counter} -gt "${DB_TIMEOUT}" ]; then
>&2 echo "ERROR: Failed to connect to MySQL database on $MYSQL_HOST"
exit 1
fi;
done
echo "MySQL database ready!"

exec "$@"
4 changes: 2 additions & 2 deletions phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@

<file>src</file>

<rule ref="PSR2"></rule>
<rule ref="PSR2"/>

</ruleset>
</ruleset>
23 changes: 17 additions & 6 deletions tests/DatabaseEncryptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,15 @@ public static function setUpBeforeClass(): void
{
$conf = new Configurator([
'driver' => 'mysql',
'host' => '127.0.0.1',
'host' => 'mariadb',
'user' => 'root',
'password' => '',
'database' => 'test_database'
]);

$mysqlHost = getenv('MYSQL_HOST', true);
$conf->setHost(($mysqlHost !== false) ? $mysqlHost : '127.0.0.1');

static::$db = new \Rancoud\Database\Database($conf);

$sql = '
Expand Down Expand Up @@ -319,11 +323,15 @@ public function testSetNewDatabaseWithArray(): void

$params = [
'driver' => 'mysql',
'host' => '127.0.0.1',
'host' => 'mariadb',
'user' => 'root',
'password' => '',
'database' => 'test_database'
];

$mysqlHost = getenv('MYSQL_HOST', true);
$params['host'] = ($mysqlHost !== false) ? $mysqlHost : '127.0.0.1';

$database->setNewDatabase($params);

$sessionId = 'sessionId';
Expand All @@ -348,14 +356,17 @@ public function testSetNewDatabaseWithConfigurator(): void
$database = new DatabaseEncryption();
$database->setKey('randomKey');

$params = [
$conf = new Configurator([
'driver' => 'mysql',
'host' => '127.0.0.1',
'host' => 'mariadb',
'user' => 'root',
'password' => '',
'database' => 'test_database'
];
$conf = new Configurator($params);
]);

$mysqlHost = getenv('MYSQL_HOST', true);
$conf->setHost(($mysqlHost !== false) ? $mysqlHost : '127.0.0.1');

$database->setNewDatabase($conf);

$sessionId = 'sessionId';
Expand Down
23 changes: 17 additions & 6 deletions tests/DatabaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,15 @@ public static function setUpBeforeClass(): void
{
$conf = new Configurator([
'driver' => 'mysql',
'host' => '127.0.0.1',
'host' => 'mariadb',
'user' => 'root',
'password' => '',
'database' => 'test_database'
]);

$mysqlHost = getenv('MYSQL_HOST', true);
$conf->setHost(($mysqlHost !== false) ? $mysqlHost : '127.0.0.1');

static::$db = new \Rancoud\Database\Database($conf);

$sql = '
Expand Down Expand Up @@ -288,11 +292,15 @@ public function testSetNewDatabaseWithArray(): void
$database = new Database();
$params = [
'driver' => 'mysql',
'host' => '127.0.0.1',
'host' => 'mariadb',
'user' => 'root',
'password' => '',
'database' => 'test_database'
];

$mysqlHost = getenv('MYSQL_HOST', true);
$params['host'] = ($mysqlHost !== false) ? $mysqlHost : '127.0.0.1';

$database->setNewDatabase($params);

$sessionId = 'sessionId';
Expand All @@ -314,14 +322,17 @@ public function testSetNewDatabaseWithArray(): void
public function testSetNewDatabaseWithConfigurator(): void
{
$database = new Database();
$params = [
$conf = new Configurator([
'driver' => 'mysql',
'host' => '127.0.0.1',
'host' => 'mariadb',
'user' => 'root',
'password' => '',
'database' => 'test_database'
];
$conf = new Configurator($params);
]);

$mysqlHost = getenv('MYSQL_HOST', true);
$conf->setHost(($mysqlHost !== false) ? $mysqlHost : '127.0.0.1');

$database->setNewDatabase($conf);

$sessionId = 'sessionId';
Expand Down
Loading