Load the framework while testing in order to allow for additional tests

This commit is contained in:
Alex Tselegidis 2023-03-24 08:11:25 +01:00
parent d58dbebc75
commit d4185b2b05
9 changed files with 114 additions and 22 deletions

View file

@ -0,0 +1,20 @@
<?php defined('BASEPATH') or exit('No direct script access allowed');
/*
| -------------------------------------------------------------------------
| TESTING ROUTES
| -------------------------------------------------------------------------
| The following routes are defined in order for CI to be able to process
| test execution requests via the CLI.
|
| The Test controller class is used as a placeholder for this purpose.
|
*/
$route['default_controller'] = 'test/index';
$route['404_override'] = 'test/index'; // when in doubt, use the hammer
$route['translate_uri_dashes'] = FALSE;
/* End of file routes.php */
/* Location: ./application/config/testing/routes.php */

View file

@ -0,0 +1,39 @@
<?php defined('BASEPATH') or exit('No direct script access allowed');
/* ----------------------------------------------------------------------------
* Easy!Appointments - Online Appointment Scheduler
*
* @package EasyAppointments
* @author A.Tselegidis <alextselegidis@gmail.com>
* @copyright Copyright (c) Alex Tselegidis
* @license https://opensource.org/licenses/GPL-3.0 - GPLv3
* @link https://easyappointments.org
* @since v1.5.0
* ---------------------------------------------------------------------------- */
/*
* This file can only be used in a testing environment and only from the termninal.
*/
if (ENVIRONMENT !== 'testing' || ! is_cli())
{
show_404();
}
/**
* Test controller.
*
* This controller does not have or need any logic, it is just used so that CI can be loaded properly during the test
* execution.
*/
class Test extends EA_Controller {
/**
* Placeholder callback.
*
* @return void
*/
public function index(): void
{
//
}
}

View file

@ -45,6 +45,11 @@
"phpunit/phpunit": "^10.0.18"
},
"scripts": {
"test": "php vendor/bin/phpunit"
"test": "APP_ENV=testing php vendor/bin/phpunit"
},
"autoload": {
"psr-4": {
"Tests\\": "tests/"
}
}
}

View file

@ -89,7 +89,17 @@ require_once __DIR__ . '/vendor/autoload.php';
*
* NOTE: If you change these, also change the error_reporting() code below
*/
$app_env = getenv('APP_ENV');
if ($app_env)
{
define('ENVIRONMENT', $app_env);
}
else
{
define('ENVIRONMENT', (Config::DEBUG_MODE) ? 'development' : 'production');
}
/*
*---------------------------------------------------------------

View file

@ -1,7 +1,7 @@
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="tests/bootstrap.php"
bootstrap="index.php"
colors="true"
cacheResult="false"
>

27
tests/TestCase.php Normal file
View file

@ -0,0 +1,27 @@
<?php
namespace Tests;
use CI_Controller;
use EA_Controller;
use PHPUnit\Framework\TestCase as PHPUnitTestCase;
/**
* Parent test case sharing common test functionality.
*/
class TestCase extends PHPUnitTestCase {
/**
* @var EA_Controller|CI_Controller
*/
private static EA_Controller|CI_Controller $CI;
/**
* Load the framework instance.
*
* @return void
*/
public static function setUpBeforeClass(): void
{
self::$CI =& get_instance();
}
}

View file

@ -1,28 +1,26 @@
<?php
namespace Unit\Helper;
namespace Tests\Unit\Helper;
use PHPUnit\Framework\TestCase;
require_once __DIR__ . '/../../../application/helpers/array_helper.php';
use Tests\TestCase;
class ArrayHelperTest extends TestCase {
public function testIsAssocReturnsTrueOnAssociativeArray()
public function testIsAssocReturnsTrueOnAssociativeArray(): void
{
$this->assertTrue(is_assoc(['test' => 'value']));
}
public function testIsAssocReturnsFalseOnIndexedArray()
public function testIsAssocReturnsFalseOnIndexedArray(): void
{
$this->assertFalse(is_assoc(['one', 'two', 'three']));
}
public function testIsAssocReturnsTrueOnMixedArray()
public function testIsAssocReturnsTrueOnMixedArray(): void
{
$this->assertTrue(is_assoc(['one', 'two', 'three' => 'value']));
}
public function testArrayFindReturnsCorrectElement()
public function testArrayFindReturnsCorrectElement(): void
{
$arr = [
[
@ -39,7 +37,7 @@ class ArrayHelperTest extends TestCase {
$this->assertSame($arr[0], array_find($arr, fn($element) => $element['id'] === 1));
}
public function testArrayFieldsReturnsStrippedArray()
public function testArrayFieldsReturnsStrippedArray(): void
{
$arr = [
'name' => 'John',

View file

@ -1,10 +1,8 @@
<?php
namespace Unit\Helper;
namespace Tests\Unit\Helper;
use PHPUnit\Framework\TestCase;
require_once __DIR__ . '/../../../application/helpers/validation_helper.php';
use Tests\TestCase;
class ValidationHelperTest extends TestCase {
public function testValidateDateTimeReturnsTrueOnValidValue()

View file

@ -1,5 +0,0 @@
<?php
require_once __DIR__ . '/../vendor/autoload.php';
define('BASEPATH', __DIR__ . '/../system');