From d4185b2b0543e2c0640d6fdc417ad7a20209fd8b Mon Sep 17 00:00:00 2001 From: Alex Tselegidis Date: Fri, 24 Mar 2023 08:11:25 +0100 Subject: [PATCH] Load the framework while testing in order to allow for additional tests --- application/config/testing/routes.php | 20 +++++++++++ application/controllers/Test.php | 39 ++++++++++++++++++++++ composer.json | 7 +++- index.php | 12 ++++++- phpunit.xml | 2 +- tests/TestCase.php | 27 +++++++++++++++ tests/Unit/Helper/ArrayHelperTest.php | 18 +++++----- tests/Unit/Helper/ValidationHelperTest.php | 6 ++-- tests/bootstrap.php | 5 --- 9 files changed, 114 insertions(+), 22 deletions(-) create mode 100644 application/config/testing/routes.php create mode 100644 application/controllers/Test.php create mode 100644 tests/TestCase.php delete mode 100644 tests/bootstrap.php diff --git a/application/config/testing/routes.php b/application/config/testing/routes.php new file mode 100644 index 00000000..f5b2c68d --- /dev/null +++ b/application/config/testing/routes.php @@ -0,0 +1,20 @@ + + * @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 + { + // + } +} diff --git a/composer.json b/composer.json index 7bd60c88..10ced5ce 100644 --- a/composer.json +++ b/composer.json @@ -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/" + } } } diff --git a/index.php b/index.php index da730790..120373e4 100644 --- a/index.php +++ b/index.php @@ -89,7 +89,17 @@ require_once __DIR__ . '/vendor/autoload.php'; * * NOTE: If you change these, also change the error_reporting() code below */ -define('ENVIRONMENT', (Config::DEBUG_MODE) ? 'development' : 'production'); + +$app_env = getenv('APP_ENV'); + +if ($app_env) +{ + define('ENVIRONMENT', $app_env); +} +else +{ + define('ENVIRONMENT', (Config::DEBUG_MODE) ? 'development' : 'production'); +} /* *--------------------------------------------------------------- diff --git a/phpunit.xml b/phpunit.xml index 1a2ec8fb..25777ebb 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,7 +1,7 @@ diff --git a/tests/TestCase.php b/tests/TestCase.php new file mode 100644 index 00000000..e075f57e --- /dev/null +++ b/tests/TestCase.php @@ -0,0 +1,27 @@ +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', @@ -49,7 +47,7 @@ class ArrayHelperTest extends TestCase { $stripped = array_fields($arr, ['name']); $this->assertArrayHasKey('name', $stripped); - + $this->assertArrayNotHasKey('email', $stripped); } } diff --git a/tests/Unit/Helper/ValidationHelperTest.php b/tests/Unit/Helper/ValidationHelperTest.php index e13543c6..261eed07 100644 --- a/tests/Unit/Helper/ValidationHelperTest.php +++ b/tests/Unit/Helper/ValidationHelperTest.php @@ -1,10 +1,8 @@