draft individual module test environments

This commit is contained in:
Dennis Eichhorn 2021-02-09 18:22:20 +01:00
parent c4157c4d21
commit 8ce7e41f66
4 changed files with 601 additions and 2 deletions

View File

@ -41,6 +41,34 @@ jobs:
code-tests:
runs-on: ubuntu-latest
if: "!contains(github.event.head_commit.message, 'NO_CI')"
services:
mysql:
image: mysql:5.7
env:
MYSQL_ALLOW_EMPTY_PASSWORD: false
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: oms
ports:
- 3306:3306
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
postgres:
image: postgres:10.8
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: root
POSTGRES_DB: oms
ports:
- 5432:5432
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 3
redis:
image: redis
ports:
- 6379:6379
options: --entrypoint redis-server
memcached:
image: memcached
ports:
- 11211:11211
strategy:
fail-fast: false
max-parallel: 3
@ -58,12 +86,34 @@ jobs:
ref: develop
repository: Orange-Management/Build
path: Build
- name: Checkout Resource Repository
uses: actions/checkout@master
with:
fetch-depth: 1
ref: develop
repository: Orange-Management/Resources
path: Resources
- name: Checkout phpOMS Repository
uses: actions/checkout@master
with:
fetch-depth: 1
ref: develop
repository: Orange-Management/phpOMS
path: phpOMS
- name: Checkout Orange-Management Repository
uses: actions/checkout@master
with:
fetch-depth: 1
ref: develop
repository: Orange-Management/Orange-Management
path: Orange-Management
- name: Setup PHP, with composer and extensions
uses: shivammathur/setup-php@master
with:
php-version: ${{ matrix.php-versions }}
extensions: mbstring, gd, zip, dom, mysql, pgsql, sqlite, imap, bcmath, redis, memcached
ini-values: opcache.jit_buffer_size=256M, opcache.jit=1235, pcre.jit=1
coverage: pcov
- name: Get Composer Cache Directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
@ -75,8 +125,12 @@ jobs:
restore-keys: ${{ runner.os }}-composer-
- name: Setup Composer
run: composer install
- name: phpcs
run: vendor/bin/phpcs ./ --standard="Build/Config/phpcs.xml" -s --report=full
- name: phpunit
run: vendor/bin/phpunit --coverage-clover tests/coverage.xml --configuration tests/phpunit_default.xml
- name: codecov
env:
CODECOV_TOKEN: "${{ secrets.CODECOV_TOKEN }}"
run: "bash <(curl -s https://codecov.io/bash) -f tests/coverage.xml"
custom:
runs-on: ubuntu-latest
if: "!contains(github.event.head_commit.message, 'NO_CI')"

94
tests/Autoloader.php Normal file
View File

@ -0,0 +1,94 @@
<?php
/**
* Orange Management
*
* PHP Version 8.0
*
* @package Modules/tests
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://orange-management.org
*/
declare(strict_types=1);
namespace tests;
\spl_autoload_register('\tests\Autoloader::defaultAutoloader');
/**
* Autoloader class.
*
* @package tests
* @license OMS License 1.0
* @link https://orange-management.org
* @since 1.0.0
*/
final class Autoloader
{
/**
* Base paths for autoloading
*
* @var string[]
* @since 1.0.0
*/
private static $paths = [
__DIR__ . '/../',
__DIR__ . '/../Orange-Management/',
__DIR__ . '/../../',
];
/**
* Constructor.
*
* @since 1.0.0
* @codeCoverageIgnore
*/
private function __construct()
{
}
/**
* Add base path for autoloading
*
* @param string $path Absolute base path with / at the end
*
* @return void
*
* @since 1.0.0
*/
public static function addPath(string $path) : void
{
self::$paths[] = $path;
}
/**
* Loading classes by namespace + class name.
*
* @param string $class Class path
*
* @example Autoloader::defaultAutoloader('\phpOMS\Autoloader') // void
*
* @return void
*
* @throws AutoloadException Throws this exception if the class to autoload doesn't exist. This could also be related to a wrong namespace/file path correlation.
*
* @since 1.0.0
*/
public static function defaultAutoloader(string $class) : void
{
$class = \ltrim($class, '\\');
$class = \str_replace(['_', '\\'], '/', $class);
foreach (self::$paths as $path) {
$file = $path . $class . '.php';
$file = \str_replace('/Modules/', '/', $file);
if (\is_file($file)) {
include_once $file;
return;
}
}
}
}

395
tests/Bootstrap.php Normal file
View File

@ -0,0 +1,395 @@
<?php declare(strict_types=1);
// Modules/tests
\ini_set('memory_limit', '2048M');
\ini_set('display_errors', '1');
\ini_set('display_startup_errors', '1');
\error_reporting(\E_ALL);
\setlocale(\LC_ALL, 'en_US.UTF-8');
require_once __DIR__ . '/../vendor/autoload.php';
require_once __DIR__ . '/Autoloader.php';
use phpOMS\DataStorage\Database\DatabasePool;
use phpOMS\DataStorage\Database\DataMapperAbstract;
use phpOMS\DataStorage\Session\HttpSession;
$CONFIG = [
'db' => [
'core' => [
'masters' => [
'admin' => [
'db' => 'mysql', /* db type */
'host' => '127.0.0.1', /* db host address */
'port' => '3306', /* db host port */
'login' => 'root', /* db login name */
'password' => 'root', /* db login password */
'database' => 'oms', /* db name */
'weight' => 1000, /* db table prefix */
],
'insert' => [
'db' => 'mysql', /* db type */
'host' => '127.0.0.1', /* db host address */
'port' => '3306', /* db host port */
'login' => 'root', /* db login name */
'password' => 'root', /* db login password */
'database' => 'oms', /* db name */
'weight' => 1000, /* db table prefix */
],
'select' => [
'db' => 'mysql', /* db type */
'host' => '127.0.0.1', /* db host address */
'port' => '3306', /* db host port */
'login' => 'root', /* db login name */
'password' => 'root', /* db login password */
'database' => 'oms', /* db name */
'weight' => 1000, /* db table prefix */
],
'update' => [
'db' => 'mysql', /* db type */
'host' => '127.0.0.1', /* db host address */
'port' => '3306', /* db host port */
'login' => 'root', /* db login name */
'password' => 'root', /* db login password */
'database' => 'oms', /* db name */
'weight' => 1000, /* db table prefix */
],
'delete' => [
'db' => 'mysql', /* db type */
'host' => '127.0.0.1', /* db host address */
'port' => '3306', /* db host port */
'login' => 'root', /* db login name */
'password' => 'root', /* db login password */
'database' => 'oms', /* db name */
'weight' => 1000, /* db table prefix */
],
'schema' => [
'db' => 'mysql', /* db type */
'host' => '127.0.0.1', /* db host address */
'port' => '3306', /* db host port */
'login' => 'root', /* db login name */
'password' => 'root', /* db login password */
'database' => 'oms', /* db name */
'weight' => 1000, /* db table prefix */
],
],
'postgresql' => [
'admin' => [
'db' => 'pgsql', /* db type */
'host' => '127.0.0.1', /* db host address */
'port' => '5432', /* db host port */
'login' => 'postgres', /* db login name */
'password' => 'root', /* db login password */
'database' => 'oms', /* db name */
'weight' => 1000, /* db table prefix */
],
'insert' => [
'db' => 'pgsql', /* db type */
'host' => '127.0.0.1', /* db host address */
'port' => '5432', /* db host port */
'login' => 'postgres', /* db login name */
'password' => 'root', /* db login password */
'database' => 'oms', /* db name */
'weight' => 1000, /* db table prefix */
],
'select' => [
'db' => 'pgsql', /* db type */
'host' => '127.0.0.1', /* db host address */
'port' => '5432', /* db host port */
'login' => 'postgres', /* db login name */
'password' => 'root', /* db login password */
'database' => 'oms', /* db name */
'weight' => 1000, /* db table prefix */
],
'update' => [
'db' => 'pgsql', /* db type */
'host' => '127.0.0.1', /* db host address */
'port' => '5432', /* db host port */
'login' => 'postgres', /* db login name */
'password' => 'root', /* db login password */
'database' => 'oms', /* db name */
'weight' => 1000, /* db table prefix */
],
'delete' => [
'db' => 'pgsql', /* db type */
'host' => '127.0.0.1', /* db host address */
'port' => '5432', /* db host port */
'login' => 'postgres', /* db login name */
'password' => 'root', /* db login password */
'database' => 'oms', /* db name */
'weight' => 1000, /* db table prefix */
],
'schema' => [
'db' => 'pgsql', /* db type */
'host' => '127.0.0.1', /* db host address */
'port' => '5432', /* db host port */
'login' => 'postgres', /* db login name */
'password' => 'root', /* db login password */
'database' => 'oms', /* db name */
'weight' => 1000, /* db table prefix */
],
],
'sqlite' => [
'admin' => [
'db' => 'sqlite', /* db type */
'database' => __DIR__ . '/test.sqlite', /* db name */
'weight' => 1000, /* db table prefix */
],
'insert' => [
'db' => 'sqlite', /* db type */
'database' => __DIR__ . '/test.sqlite', /* db name */
'weight' => 1000, /* db table prefix */
],
'select' => [
'db' => 'sqlite', /* db type */
'database' => __DIR__ . '/test.sqlite', /* db name */
'weight' => 1000, /* db table prefix */
],
'update' => [
'db' => 'sqlite', /* db type */
'database' => __DIR__ . '/test.sqlite', /* db name */
'weight' => 1000, /* db table prefix */
],
'delete' => [
'db' => 'sqlite', /* db type */
'database' => __DIR__ . '/test.sqlite', /* db name */
'weight' => 1000, /* db table prefix */
],
'schema' => [
'db' => 'sqlite', /* db type */
'database' => __DIR__ . '/test.sqlite', /* db name */
'weight' => 1000, /* db table prefix */
],
],
'mssql' => [
'admin' => [
'db' => 'mssql', /* db type */
'host' => '127.0.0.1', /* db host address */
'port' => '1433', /* db host port */
'login' => 'sa', /* db login name */
'password' => 'R00troot', /* db login password */
'database' => 'oms', /* db name */
'weight' => 1000, /* db table prefix */
],
'insert' => [
'db' => 'mssql', /* db type */
'host' => '127.0.0.1', /* db host address */
'port' => '1433', /* db host port */
'login' => 'sa', /* db login name */
'password' => 'R00troot', /* db login password */
'database' => 'oms', /* db name */
'weight' => 1000, /* db table prefix */
],
'select' => [
'db' => 'mssql', /* db type */
'host' => '127.0.0.1', /* db host address */
'port' => '1433', /* db host port */
'login' => 'sa', /* db login name */
'password' => 'R00troot', /* db login password */
'database' => 'oms', /* db name */
'weight' => 1000, /* db table prefix */
],
'update' => [
'db' => 'mssql', /* db type */
'host' => '127.0.0.1', /* db host address */
'port' => '1433', /* db host port */
'login' => 'sa', /* db login name */
'password' => 'R00troot', /* db login password */
'database' => 'oms', /* db name */
'weight' => 1000, /* db table prefix */
],
'delete' => [
'db' => 'mssql', /* db type */
'host' => '127.0.0.1', /* db host address */
'port' => '1433', /* db host port */
'login' => 'sa', /* db login name */
'password' => 'R00troot', /* db login password */
'database' => 'oms', /* db name */
'weight' => 1000, /* db table prefix */
],
'schema' => [
'db' => 'mssql', /* db type */
'host' => '127.0.0.1', /* db host address */
'port' => '1433', /* db host port */
'login' => 'sa', /* db login name */
'password' => 'R00troot', /* db login password */
'database' => 'oms', /* db name */
'weight' => 1000, /* db table prefix */
],
],
],
],
'cache' => [
'redis' => [
'db' => 1,
'host' => '127.0.0.1',
'port' => 6379,
],
'memcached' => [
'host' => '127.0.0.1',
'port' => 11211,
],
],
'mail' => [
'imap' => [
'host' => '127.0.0.1',
'port' => 143,
'ssl' => false,
'user' => 'test',
'password' => '123456',
],
'pop3' => [
'host' => '127.0.0.1',
'port' => 25,
'ssl' => false,
'user' => 'test',
'password' => '123456',
],
],
'log' => [
'file' => [
'path' => __DIR__ . '/Logs',
],
],
'page' => [
'root' => '/',
'https' => false,
],
'socket' => [
'master' => [
'host' => '127.0.0.1',
'limit' => 300,
'port' => 4310,
],
],
'language' => [
'en',
],
'apis' => [
],
];
// Reset database
if (\defined('RESET') && RESET === '1') {
if (\extension_loaded('pdo_mysql')) {
try {
$db = new \PDO('mysql:host=' .
$CONFIG['db']['core']['masters']['admin']['host'],
$CONFIG['db']['core']['masters']['admin']['login'],
$CONFIG['db']['core']['masters']['admin']['password']
);
$db->exec('DROP DATABASE IF EXISTS ' . $CONFIG['db']['core']['masters']['admin']['database']);
$db->exec('CREATE DATABASE IF NOT EXISTS ' . $CONFIG['db']['core']['masters']['admin']['database']);
$db = null;
} catch (\Throwable $t) {
echo "\nCouldn't connect to MYSQL DB\n";
}
}
if (\extension_loaded('pdo_pgsql')) {
try {
$db = new \PDO('pgsql:host=' .
$CONFIG['db']['core']['postgresql']['admin']['host'],
$CONFIG['db']['core']['postgresql']['admin']['login'],
$CONFIG['db']['core']['postgresql']['admin']['password']
);
$db->exec('DROP DATABASE IF EXISTS ' . $CONFIG['db']['core']['postgresql']['admin']['database']);
$db->exec('CREATE DATABASE ' . $CONFIG['db']['core']['postgresql']['admin']['database']);
$db = null;
} catch (\Throwable $t) {
echo "\nCouldn't connect to POSTGRESQL DB\n";
}
}
if (\extension_loaded('pdo_sqlsrv')) {
try {
$db = new \PDO('sqlsrv:Server=' .
$CONFIG['db']['core']['mssql']['admin']['host'],
$CONFIG['db']['core']['mssql']['admin']['login'],
$CONFIG['db']['core']['mssql']['admin']['password']
);
$db->exec('DROP DATABASE IF EXISTS ' . $CONFIG['db']['core']['mssql']['admin']['database']);
$db->exec('CREATE DATABASE ' . $CONFIG['db']['core']['mssql']['admin']['database']);
$db = null;
} catch (\Throwable $t) {
echo "\nCouldn't connect to MSSQL DB\n";
}
}
}
$httpSession = new HttpSession();
$GLOBALS['session'] = $httpSession;
$GLOBALS['dbpool'] = new DatabasePool();
$GLOBALS['dbpool']->create('admin', $CONFIG['db']['core']['masters']['admin']);
$GLOBALS['dbpool']->create('select', $CONFIG['db']['core']['masters']['select']);
$GLOBALS['dbpool']->create('update', $CONFIG['db']['core']['masters']['update']);
$GLOBALS['dbpool']->create('delete', $CONFIG['db']['core']['masters']['delete']);
$GLOBALS['dbpool']->create('insert', $CONFIG['db']['core']['masters']['insert']);
$GLOBALS['dbpool']->create('schema', $CONFIG['db']['core']['masters']['schema']);
DataMapperAbstract::setConnection($GLOBALS['dbpool']->get());
$GLOBALS['frameworkpath'] = '/phpOMS/';
function phpServe() : void
{
// OS detection
$isWindows = \stristr(\php_uname('s'), 'Windows') !== false;
// Command that starts the built-in web server
if ($isWindows) {
$command = \sprintf(
'wmic process call create "php -S %s:%d -t %s" | find "ProcessId"',
WEB_SERVER_HOST,
WEB_SERVER_PORT,
__DIR__ . '/../' . WEB_SERVER_DOCROOT
);
$killCommand = 'taskkill /f /pid ';
} else {
$command = \sprintf(
'php -S %s:%d -t %s >/dev/null 2>&1 & echo $!',
WEB_SERVER_HOST,
WEB_SERVER_PORT,
WEB_SERVER_DOCROOT
);
$killCommand = 'kill ';
}
// Execute the command and store the process ID
$output = [];
echo \sprintf('Starting server...') . \PHP_EOL;
echo \sprintf(' Current directory: %s', \getcwd()) . \PHP_EOL;
echo \sprintf(' %s', $command);
\exec($command, $output);
// Get PID
if ($isWindows) {
$pid = \explode('=', $output[0]);
$pid = \str_replace(' ', '', $pid[1]);
$pid = \str_replace(';', '', $pid);
} else {
$pid = (int) $output[0];
}
// Log
echo \sprintf(
' %s - Web server started on %s:%d with PID %d',
\date('r'),
WEB_SERVER_HOST,
WEB_SERVER_PORT,
$pid
) . \PHP_EOL;
// Kill the web server when the process ends
\register_shutdown_function(function() use ($killCommand, $pid) : void {
echo \PHP_EOL . \sprintf('Stopping server...') . \PHP_EOL;
echo \sprintf(' %s - Killing process with ID %d', \date('r'), $pid) . \PHP_EOL;
\exec($killCommand . $pid);
});
}
phpServe();

56
tests/phpunit_default.xml Normal file
View File

@ -0,0 +1,56 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="Bootstrap.php" colors="true" stopOnError="true" stopOnFailure="false" stopOnIncomplete="false" stopOnSkipped="false" beStrictAboutTestsThatDoNotTestAnything="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage includeUncoveredFiles="true" processUncoveredFiles="false">
<exclude>
<directory>*vendor*</directory>
<directory>vendor</directory>
<directory>../vendor</directory>
<directory>../phpOMS</directory>
<directory>../phpOMS*</directory>
<directory>../phpOMS/*</directory>
<directory>../tests*</directory>
<directory>../*/tests*</directory>
<directory>../**/tests*</directory>
<directory>*/tests*</directory>
<directory suffix="tpl.php">../*</directory>
<directory suffix="lang.php">../*</directory>
<directory suffix="Test.php">../*</directory>
<directory suffix="Routes.php">../*</directory>
<directory suffix="Hooks.php">../*</directory>
<directory>../**/test*</directory>
<directory>../**/Theme*</directory>
<directory>../**/Admin/Routes*</directory>
<directory>../**/Admin/Install*</directory>
<directory>../Media/Files*</directory>
</exclude>
<report>
<clover outputFile="coverage.xml"/>
<html outputDirectory="../tests" lowUpperBound="75" highLowerBound="95"/>
</report>
</coverage>
<testsuites>
<testsuite name="Install">
<directory>../Orange-Management/Install/tests*</directory>
</testsuite>
<testsuite name="Module">
<directory>../tests*</directory>
</testsuite>
</testsuites>
<groups>
<exclude>
<group>volume</group>
<group>maybe</group>
</exclude>
</groups>
<logging>
<junit outputFile="../tests/junit_php.xml"/>
<testdoxHtml outputFile="../tests/index.html"/>
<testdoxText outputFile="../tests/testdox.txt"/>
</logging>
<php>
<const name="WEB_SERVER_HOST" value="localhost"/>
<const name="WEB_SERVER_PORT" value="1234"/>
<const name="WEB_SERVER_DOCROOT" value="./"/>
<const name="RESET" value="1"/>
</php>
</phpunit>