Skip to content

Commit

Permalink
Merge pull request #9 from WebFiori/dev
Browse files Browse the repository at this point in the history
test: Added Files to Help in Bootstraping Testing
  • Loading branch information
usernane authored Mar 7, 2024
2 parents b1e1e32 + 167919e commit 295a32d
Show file tree
Hide file tree
Showing 6 changed files with 241 additions and 2 deletions.
32 changes: 32 additions & 0 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: PHP Build

on:
push:
branches: [ main , dev]
pull_request:
branches: [ main ]

jobs:
build:

runs-on: ubuntu-latest

strategy:
fail-fast: true

steps:

- uses: actions/checkout@v3

- name: Setup PHP
uses: shivammathur/[email protected]
with:
php-version: 8.3
extensions: mbstring, sqlsrv, iconv
tools: phpunit, composer

- name: Install dependencies
run: composer install --prefer-dist --no-progress

- name: Execute Tests
run: phpunit --configuration tests/phpunit.xml
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ app/storage/*
vendor/*
app/sto
.idea/*
app/config/AppConfig.php
app/config/app-config.json
.vscode/*
tests/.phpunit.result.cache
*/config/*
122 changes: 122 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?php

//Bootstrap file which is used to boot testing process.
use webfiori\framework\AutoLoader;
use webfiori\framework\App;

$DS = DIRECTORY_SEPARATOR;

//TODO: Set the name of JSON configuration file to use inrunning test cases.
define('JSON_CONFIG','app-config-testing.json');


//TODO: Set application directory name. Update as needed.
define('APP_DIR', 'app');

//This constant is used to indicate that application is in testing env.
//Leave as is
define('UNIT_TESTING', true);

$Root = trim(__DIR__, $DS.'tests');

//an array that contains possible locations at which
//WebFiori Framework might exist.
//Add and remove directories as needed.
$WebFioriFrameworkDirs = [
$Root.$DS.'webfiori',
$Root.$DS.'vendor'.$DS.'webfiori'.$DS.'framework'.$DS.'webfiori'
];

//Printing informative messages in the terminal
fprintf(STDOUT, "Bootstrap Path: '".__DIR__."'\n");
fprintf(STDOUT,'Include Path: \''.get_include_path().'\''."\n");
fprintf(STDOUT,"Tryning to load the class 'AutoLoader'...\n");
$isAutoloaderLoaded = false;


if (explode($DS, __DIR__)[0] == 'home' || explode($DS, __DIR__)[1] == 'home') {
fprintf(STDOUT,"Run Environment: Linux.\n");

foreach ($WebFioriFrameworkDirs as $dir) {
//linux
$file = $DS.$dir.$DS.'framework'.$DS.'AutoLoader.php';
fprintf(STDOUT,"Checking if file '$file' is exist...\n");

if (file_exists($file)) {
require_once $file;
$isAutoloaderLoaded = true;
break;
}
}
} else {
fprintf(STDOUT,"Run Environment: Other.\n");

foreach ($WebFioriFrameworkDirs as $dir) {
//other
$file = $dir.$DS.'framework'.$DS.'AutoLoader.php';
fprintf(STDOUT,"Checking if file '$file' is exist...\n");

if (file_exists($file)) {
require_once $file;
$isAutoloaderLoaded = true;
break;
}
}
}

if ($isAutoloaderLoaded === false) {
fprintf(STDERR, "Error: Unable to find the class 'AutoLoader'.\n");
exit(-1);
} else {
fprintf(STDOUT,"Class 'AutoLoader' successfully loaded.\n");
}
fprintf(STDOUT,"Initializing autoload directories...\n");
AutoLoader::get([
'search-folders' => [
'tests',
'webfiori',
'vendor',
APP_DIR,
],
'define-root' => true,
'root' => $Root,
'on-load-failure' => 'do-nothing'
]);
fprintf(STDOUT,'Autoloader Initialized.'."\n");

fprintf(STDOUT,"Initializing application...\n");
define('APP_PATH', AutoLoader::get()->root().$DS.APP_DIR.$DS);
fprintf(STDOUT,'App Path: '.APP_PATH."\n");
$driver = "\\webfiori\\framework\\config\\JsonDriver";
fprintf(STDOUT,"Setting application configuration driver to '$driver'\n");
App::setConfigDriver($driver);
$configFileName = 'app-config-testing.json';
fprintf(STDOUT,"Setting application configuration file to '$configFileName'\n");
App::getConfig()->setConfigFileName($configFileName);
App::getConfig()->initialize();
App::start();
fprintf(STDOUT,'Done.'."\n");
fprintf(STDOUT,'Root Directory: \''.AutoLoader::get()->root().'\'.'."\n");
define('TESTS_PATH', AutoLoader::get()->root().$DS.'tests');
fprintf(STDOUT,'Stored Connections:'."\n");
if (count(App::getConfig()->getDBConnections()) != 0) {
foreach (App::getConfig()->getDBConnections() as $conn) {
fprintf(STDOUT, $conn->getName()."\n");
}
} else {
fprintf(STDOUT, "<<NO DATABSE CONNECTIONS>>\n");
}
fprintf(STDOUT, "Registering shutdown function...\n");
register_shutdown_function(function()
{
//TODO: Run extra code after tests completion.
fprintf(STDOUT, "Testing Finished.\n");
});
fprintf(STDOUT, "Registering shutdown function completed.\n");
require_once 'query-runner.php';

//TODO: Include your own custom bootstrap scripts here.
//require_once 'my-script.php';

fprintf(STDOUT, "Done\n");
fprintf(STDOUT,"Starting to run tests...\n");
22 changes: 22 additions & 0 deletions tests/phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit colors="true" bootstrap="bootstrap.php">
<php>
</php>
<filter>
<whitelist addUncoveredFilesFromWhitelist="true">
<!-- TODO: Specify files to be covered by tests-->
<!--<file>./webfiori/framework/Access.php</file>-->
</whitelist>
</filter>
<logging>
<log type="coverage-clover" target="clover.xml"/>
</logging>
<testsuites>
<!-- TODO: Specify test suits. Each folder should have one suite.-->

<testsuite name="General Tests">
<directory>./sample</directory>
</testsuite>

</testsuites>
</phpunit>
45 changes: 45 additions & 0 deletions tests/query-runner.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php


use webfiori\framework\App;
/**
* Run a command to execute certain SQL query on specific database.
*
* This function can be used during bootstrap stage of running unit tests
* to initialize application's database.
*
* @param string $connection The name of the connection that will be used.
* It must exist in application configuration.
*
* @param string $queryFileOrSchema A string that represents the namespace of database
* class (Schema). This will initialize all tables linked to database class.
* This also can be the path to SQL file to be executed.
*
* @param bool $isFile If set to true, the function will assumes that the
* previous parameter is SQL file.
*/
function runInitializationQuery(string $connection, string $queryFileOrSchema, bool $isFile = false) {
if ($isFile) {
fprintf(STDOUT, "Using file ".$queryFileOrSchema."...\n");
App::getRunner()->setArgsVector([
'webfiori',
'run-query',
'--no-confirm',
'--create',
'--file' => $queryFileOrSchema,
'--connection' => $connection,
'--ansi',
]);
} else {
fprintf(STDOUT, "Using Schema ".$queryFileOrSchema."...\n");
App::getRunner()->setArgsVector([
'webfiori',
'run-query',
'--no-confirm',
'--create',
'--schema' => $queryFileOrSchema,
'--ansi',
]);
}
App::getRunner()->start();
}
17 changes: 17 additions & 0 deletions tests/sample/SampleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
namespace sample;

use PHPUnit\Framework\TestCase;
use webfiori\framework\App;
use webfiori\framework\config\JsonDriver;
/**
* A sample unit test class.
*/
class SampleTest extends TestCase {
/**
* @test
*/
public function test00() {
$this->assertTrue(App::getConfig() instanceof JsonDriver);
}
}

0 comments on commit 295a32d

Please sign in to comment.