From 3b1940201e991f6f1bd4ec445f0384a4426be139 Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Thu, 9 Oct 2025 10:25:28 -0300 Subject: [PATCH 1/4] feat(tests): Refactor test imports for consistency and add support files. --- .gitignore | 3 +++ CHANGELOG.md | 1 + phpstan.neon | 4 ++-- phpunit.xml.dist | 2 +- tests/adapter/CookiesPsr7Test.php | 3 +-- tests/adapter/HeadersPsr7Test.php | 3 +-- tests/adapter/ResponseAdapterTest.php | 3 +-- tests/adapter/ServerParamsPsr7Test.php | 3 +-- tests/adapter/ServerRequestAdapterTest.php | 3 +-- tests/adapter/UploadedFilesPsr7Test.php | 3 +-- tests/creator/ServerRequestCreatorTest.php | 3 +-- tests/creator/UploadedFileCreatorTest.php | 3 +-- tests/emitter/SapiEmitterTest.php | 3 +-- tests/http/ErrorHandlerTest.php | 2 +- tests/http/RequestTest.php | 2 +- tests/http/ResponseTest.php | 3 +-- tests/http/ServerExitCodeTest.php | 2 +- tests/http/UploadedFileTest.php | 3 +-- tests/http/stateless/ApplicationAuthTest.php | 3 +-- tests/http/stateless/ApplicationContentTypesTest.php | 3 +-- tests/http/stateless/ApplicationCookieTest.php | 3 +-- tests/http/stateless/ApplicationCoreTest.php | 9 +++++---- tests/http/stateless/ApplicationErrorHandlerTest.php | 3 +-- tests/http/stateless/ApplicationEventTest.php | 3 +-- tests/http/stateless/ApplicationMemoryTest.php | 3 +-- tests/http/stateless/ApplicationRoutingTest.php | 3 +-- tests/http/stateless/ApplicationServerParamsTest.php | 3 +-- tests/http/stateless/ApplicationSessionTest.php | 3 +-- tests/http/stateless/ApplicationUploadedTest.php | 3 +-- tests/{ => support}/TestCase.php | 10 +++------- tests/{ => support}/bootstrap.php | 6 ++++-- tests/{ => support}/phpstan-config.php | 0 32 files changed, 43 insertions(+), 61 deletions(-) rename tests/{ => support}/TestCase.php (94%) rename tests/{ => support}/bootstrap.php (58%) rename tests/{ => support}/phpstan-config.php (100%) diff --git a/.gitignore b/.gitignore index 33918c05..eb0ad5bd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,9 @@ #code coverage /code_coverage +#copilot +copilot + # composer vendor dir /vendor /composer.lock diff --git a/CHANGELOG.md b/CHANGELOG.md index a5dd301b..7b0f1261 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## 0.1.2 Under development - Bug #190: Update action versions to use `yii2-framework` for consistency (@terabytesoftw) +- Bug #191: Refactor test imports for consistency and add support files (@terabytesoftw) ## 0.1.1 October 6, 2025 diff --git a/phpstan.neon b/phpstan.neon index 7adfced3..26c9b853 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -3,7 +3,7 @@ includes: parameters: bootstrapFiles: - - tests/bootstrap.php + - tests/support/bootstrap.php level: max @@ -14,7 +14,7 @@ parameters: tmpDir: %currentWorkingDirectory%/runtime yii2: - config_path: %currentWorkingDirectory%/tests/phpstan-config.php + config_path: %currentWorkingDirectory%/tests/support/phpstan-config.php # Enable strict advanced checks checkImplicitMixed: true diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 87ad9924..ec653d1a 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -2,7 +2,7 @@ logFile = dirname(__DIR__, 2) . '/runtime/log/flush-test.log'; + $this->logFile = dirname(__DIR__, 3) . '/runtime/logs/flush-test.log'; if (file_exists($this->logFile)) { unlink($this->logFile); @@ -610,7 +611,7 @@ public function testSetWebAndWebrootAliasesAfterHandleRequest(): void "'@web' alias should be set to an empty string after handling a request.", ); self::assertSame( - dirname(__DIR__, 2), + dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'support', Yii::getAlias('@webroot'), "'@webroot' alias should be set to the parent directory of the test directory after handling a request.", ); diff --git a/tests/http/stateless/ApplicationErrorHandlerTest.php b/tests/http/stateless/ApplicationErrorHandlerTest.php index fc807600..9d806f1c 100644 --- a/tests/http/stateless/ApplicationErrorHandlerTest.php +++ b/tests/http/stateless/ApplicationErrorHandlerTest.php @@ -10,8 +10,7 @@ use yii\log\{FileTarget, Logger}; use yii2\extensions\psrbridge\http\{Response, StatelessApplication}; use yii2\extensions\psrbridge\tests\provider\StatelessApplicationProvider; -use yii2\extensions\psrbridge\tests\support\FactoryHelper; -use yii2\extensions\psrbridge\tests\TestCase; +use yii2\extensions\psrbridge\tests\support\{FactoryHelper, TestCase}; use function array_filter; use function is_array; diff --git a/tests/http/stateless/ApplicationEventTest.php b/tests/http/stateless/ApplicationEventTest.php index 37e080f6..7524718a 100644 --- a/tests/http/stateless/ApplicationEventTest.php +++ b/tests/http/stateless/ApplicationEventTest.php @@ -8,9 +8,8 @@ use ReflectionException; use yii\base\{Event, InvalidConfigException}; use yii2\extensions\psrbridge\http\StatelessApplication; -use yii2\extensions\psrbridge\tests\support\FactoryHelper; +use yii2\extensions\psrbridge\tests\support\{FactoryHelper, TestCase}; use yii2\extensions\psrbridge\tests\support\stub\EventComponent; -use yii2\extensions\psrbridge\tests\TestCase; use function count; diff --git a/tests/http/stateless/ApplicationMemoryTest.php b/tests/http/stateless/ApplicationMemoryTest.php index ec7bc266..5b49ac76 100644 --- a/tests/http/stateless/ApplicationMemoryTest.php +++ b/tests/http/stateless/ApplicationMemoryTest.php @@ -9,8 +9,7 @@ use stdClass; use yii\base\InvalidConfigException; use yii2\extensions\psrbridge\tests\provider\StatelessApplicationProvider; -use yii2\extensions\psrbridge\tests\support\FactoryHelper; -use yii2\extensions\psrbridge\tests\TestCase; +use yii2\extensions\psrbridge\tests\support\{FactoryHelper, TestCase}; use function array_fill; use function end; diff --git a/tests/http/stateless/ApplicationRoutingTest.php b/tests/http/stateless/ApplicationRoutingTest.php index cde4d658..8877dfde 100644 --- a/tests/http/stateless/ApplicationRoutingTest.php +++ b/tests/http/stateless/ApplicationRoutingTest.php @@ -6,8 +6,7 @@ use PHPUnit\Framework\Attributes\Group; use yii\base\InvalidConfigException; -use yii2\extensions\psrbridge\tests\support\FactoryHelper; -use yii2\extensions\psrbridge\tests\TestCase; +use yii2\extensions\psrbridge\tests\support\{FactoryHelper, TestCase}; /** * Test suite for {@see \yii2\extensions\psrbridge\http\StatelessApplication} routing and parameter handling in diff --git a/tests/http/stateless/ApplicationServerParamsTest.php b/tests/http/stateless/ApplicationServerParamsTest.php index 8b75cdfb..83ee5fca 100644 --- a/tests/http/stateless/ApplicationServerParamsTest.php +++ b/tests/http/stateless/ApplicationServerParamsTest.php @@ -7,8 +7,7 @@ use PHPUnit\Framework\Attributes\{DataProviderExternal, Group}; use yii\base\InvalidConfigException; use yii2\extensions\psrbridge\tests\provider\StatelessApplicationProvider; -use yii2\extensions\psrbridge\tests\support\FactoryHelper; -use yii2\extensions\psrbridge\tests\TestCase; +use yii2\extensions\psrbridge\tests\support\{FactoryHelper, TestCase}; /** * Test suite for {@see \yii2\extensions\psrbridge\http\StatelessApplication} server parameter handling in stateless diff --git a/tests/http/stateless/ApplicationSessionTest.php b/tests/http/stateless/ApplicationSessionTest.php index d5393f54..56f41c48 100644 --- a/tests/http/stateless/ApplicationSessionTest.php +++ b/tests/http/stateless/ApplicationSessionTest.php @@ -7,8 +7,7 @@ use PHPUnit\Framework\Attributes\Group; use yii\base\InvalidConfigException; use yii\helpers\Json; -use yii2\extensions\psrbridge\tests\support\FactoryHelper; -use yii2\extensions\psrbridge\tests\TestCase; +use yii2\extensions\psrbridge\tests\support\{FactoryHelper, TestCase}; use function array_filter; use function array_key_exists; diff --git a/tests/http/stateless/ApplicationUploadedTest.php b/tests/http/stateless/ApplicationUploadedTest.php index dd308d3a..5569a74d 100644 --- a/tests/http/stateless/ApplicationUploadedTest.php +++ b/tests/http/stateless/ApplicationUploadedTest.php @@ -8,8 +8,7 @@ use yii\base\InvalidConfigException; use yii2\extensions\psrbridge\creator\ServerRequestCreator; use yii2\extensions\psrbridge\http\UploadedFile; -use yii2\extensions\psrbridge\tests\support\FactoryHelper; -use yii2\extensions\psrbridge\tests\TestCase; +use yii2\extensions\psrbridge\tests\support\{FactoryHelper, TestCase}; use function filesize; diff --git a/tests/TestCase.php b/tests/support/TestCase.php similarity index 94% rename from tests/TestCase.php rename to tests/support/TestCase.php index ecbc64b2..d8a1af21 100644 --- a/tests/TestCase.php +++ b/tests/support/TestCase.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace yii2\extensions\psrbridge\tests; +namespace yii2\extensions\psrbridge\tests\support; use HttpSoft\Message\{ResponseFactory, StreamFactory}; use PHPForge\Support\TestSupport; @@ -145,7 +145,7 @@ protected function statelessApplication(array $config = []): StatelessApplicatio ArrayHelper::merge( [ 'id' => 'stateless-app', - 'basePath' => __DIR__, + 'basePath' => dirname(__DIR__, 2), 'bootstrap' => ['log'], 'controllerNamespace' => '\yii2\extensions\psrbridge\tests\support\stub', 'components' => [ @@ -162,7 +162,6 @@ protected function statelessApplication(array $config = []): StatelessApplicatio 'info', 'warning', ], - 'logFile' => '@runtime/log/app.log', ], ], ], @@ -196,8 +195,6 @@ protected function statelessApplication(array $config = []): StatelessApplicatio StreamFactoryInterface::class => StreamFactory::class, ], ], - 'runtimePath' => dirname(__DIR__) . '/runtime', - 'vendorPath' => dirname(__DIR__) . '/vendor', ], $config, ), @@ -213,8 +210,7 @@ protected function webApplication(array $config = []): void ArrayHelper::merge( [ 'id' => 'web-app', - 'basePath' => __DIR__, - 'vendorPath' => dirname(__DIR__) . '/vendor', + 'basePath' => dirname(__DIR__, 2), 'aliases' => [ '@bower' => '@vendor/bower-asset', '@npm' => '@vendor/npm-asset', diff --git a/tests/bootstrap.php b/tests/support/bootstrap.php similarity index 58% rename from tests/bootstrap.php rename to tests/support/bootstrap.php index c77ed27a..439c066b 100644 --- a/tests/bootstrap.php +++ b/tests/support/bootstrap.php @@ -7,6 +7,8 @@ defined('YII_DEBUG') || define('YII_DEBUG', true); define('YII_ENV', 'test'); +$rootDir = dirname(__DIR__, 2); + // require composer autoloader if available -require(__DIR__ . '/../vendor/autoload.php'); -require(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php'); +require "{$rootDir}/vendor/autoload.php"; +require "{$rootDir}/vendor/yiisoft/yii2/Yii.php"; diff --git a/tests/phpstan-config.php b/tests/support/phpstan-config.php similarity index 100% rename from tests/phpstan-config.php rename to tests/support/phpstan-config.php From 5d8535904e91fe1cacfe8b95997e502e7a7010e1 Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Thu, 9 Oct 2025 10:34:53 -0300 Subject: [PATCH 2/4] Apply fixed review coderabbitai nitpick comments. --- tests/http/stateless/ApplicationCoreTest.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/http/stateless/ApplicationCoreTest.php b/tests/http/stateless/ApplicationCoreTest.php index a5e40f43..a6376fb7 100644 --- a/tests/http/stateless/ApplicationCoreTest.php +++ b/tests/http/stateless/ApplicationCoreTest.php @@ -29,8 +29,6 @@ use function str_contains; use function unlink; -use const DIRECTORY_SEPARATOR; - /** * Test suite for {@see StatelessApplication} core functionality in stateless mode. * From ebca2c75142709da8132cf2fccc779c890e4892f Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Thu, 9 Oct 2025 10:48:07 -0300 Subject: [PATCH 3/4] fix(tests): Update documentation for `TestCase` class to clarify its purpose and features. --- tests/support/TestCase.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/support/TestCase.php b/tests/support/TestCase.php index d8a1af21..eb286c91 100644 --- a/tests/support/TestCase.php +++ b/tests/support/TestCase.php @@ -22,6 +22,24 @@ use function stream_get_meta_data; use function tmpfile; +/** + * Base test case providing common helpers and utilities for the test suite. + * + * Provides utilities to create and tear down Yii2 stateless and web application instances, manage temporary files used + * during tests, sign cookies for cookie-validation scenarios, and reset PHP superglobals to ensure test isolation. + * + * Tests that require HTTP request/response factories, stream factories or application scaffolding should extend this + * class. + * + * Key features. + * - Creates `StatelessApplication` and `Application` instances with a sane test configuration. + * - Manages temporary file resources and ensures cleanup during `tearDown()`. + * - Provides `signCookies()` helper for creating signed cookie values. + * - Resets `$_SERVER`, `$_GET`, `$_POST`, `$_FILES` and `$_COOKIE` between tests to avoid cross-test contamination. + * + * @copyright Copyright (C) 2025 Terabytesoftw. + * @license https://opensource.org/license/bsd-3-clause BSD 3-Clause License. + */ abstract class TestCase extends \PHPUnit\Framework\TestCase { use TestSupport; From 49865e6442f29c3cd928c8f922cc790049409261 Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Thu, 9 Oct 2025 10:57:16 -0300 Subject: [PATCH 4/4] Apply fixed review coderabbitai nitpick comments. --- tests/http/stateless/ApplicationCoreTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/http/stateless/ApplicationCoreTest.php b/tests/http/stateless/ApplicationCoreTest.php index a6376fb7..c3fb4978 100644 --- a/tests/http/stateless/ApplicationCoreTest.php +++ b/tests/http/stateless/ApplicationCoreTest.php @@ -611,7 +611,7 @@ public function testSetWebAndWebrootAliasesAfterHandleRequest(): void self::assertSame( dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'support', Yii::getAlias('@webroot'), - "'@webroot' alias should be set to the parent directory of the test directory after handling a request.", + "'@webroot' alias should be set to the 'tests/support' directory after handling a request.", ); } }