From 41b44c28a4d06f01e3ff788e98ea9948fe16f64a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20B=C3=BCrk?= Date: Sat, 15 Oct 2022 19:44:09 +0200 Subject: [PATCH] [TASK] Avoid using testing-framework boilerplate files typo3/testing-framework provides some template files as kickstart for project and extension based testing. They are properly marked to be copied and not used directly from the package. The reason for this recommendation is, that project should make adjustments to theire concrete setup, like coverage settings and so on. This change clonses the corresponding template files from the testing-framework to folders in this extension, adjusts needed paths and ensure testing is still working. With that this best practice example follows the recommendation and best-practice for typo3/testing-framework usage. Tasks * provided cloned unit- and function test configuration and bootstrap files in `Build/phpunit/` * updated cloned phpunit configuration files to be phpunit v9 compatible, removing old coverage tag as this is done by cli options in this repository anyway * add proper xml namespacing to cloned phpunit configurations * adjustes config paths in unit and functional testing calls provided as composer scripts * adjusted phpunit configuration files in documentation Resolves #533 --- Build/phpunit/FunctionalTests.xml | 52 ++++++++++++++ Build/phpunit/FunctionalTestsBootstrap.php | 30 ++++++++ Build/phpunit/UnitTests.xml | 45 ++++++++++++ Build/phpunit/UnitTestsBootstrap.php | 84 ++++++++++++++++++++++ Documentation/Running.rst | 4 +- composer.json | 8 +-- 6 files changed, 217 insertions(+), 6 deletions(-) create mode 100644 Build/phpunit/FunctionalTests.xml create mode 100644 Build/phpunit/FunctionalTestsBootstrap.php create mode 100644 Build/phpunit/UnitTests.xml create mode 100644 Build/phpunit/UnitTestsBootstrap.php diff --git a/Build/phpunit/FunctionalTests.xml b/Build/phpunit/FunctionalTests.xml new file mode 100644 index 0000000..80156d2 --- /dev/null +++ b/Build/phpunit/FunctionalTests.xml @@ -0,0 +1,52 @@ + + + + + + ../../Tests/Functional/ + + + + + + + + + + diff --git a/Build/phpunit/FunctionalTestsBootstrap.php b/Build/phpunit/FunctionalTestsBootstrap.php new file mode 100644 index 0000000..a95bc52 --- /dev/null +++ b/Build/phpunit/FunctionalTestsBootstrap.php @@ -0,0 +1,30 @@ +defineOriginalRootPath(); + $testbase->createDirectory(ORIGINAL_ROOT . 'typo3temp/var/tests'); + $testbase->createDirectory(ORIGINAL_ROOT . 'typo3temp/var/transient'); +})(); diff --git a/Build/phpunit/UnitTests.xml b/Build/phpunit/UnitTests.xml new file mode 100644 index 0000000..d09d8ef --- /dev/null +++ b/Build/phpunit/UnitTests.xml @@ -0,0 +1,45 @@ + + + + + + ../../Tests/Unit/ + + + + + + + diff --git a/Build/phpunit/UnitTestsBootstrap.php b/Build/phpunit/UnitTestsBootstrap.php new file mode 100644 index 0000000..5b5bbd0 --- /dev/null +++ b/Build/phpunit/UnitTestsBootstrap.php @@ -0,0 +1,84 @@ +getWebRoot(), '/')); + } + if (!getenv('TYPO3_PATH_WEB')) { + putenv('TYPO3_PATH_WEB=' . rtrim($testbase->getWebRoot(), '/')); + } + + $testbase->defineSitePath(); + + $requestType = \TYPO3\CMS\Core\Core\SystemEnvironmentBuilder::REQUESTTYPE_BE | \TYPO3\CMS\Core\Core\SystemEnvironmentBuilder::REQUESTTYPE_CLI; + \TYPO3\TestingFramework\Core\SystemEnvironmentBuilder::run(0, $requestType); + + $testbase->createDirectory(\TYPO3\CMS\Core\Core\Environment::getPublicPath() . '/typo3conf/ext'); + $testbase->createDirectory(\TYPO3\CMS\Core\Core\Environment::getPublicPath() . '/typo3temp/assets'); + $testbase->createDirectory(\TYPO3\CMS\Core\Core\Environment::getPublicPath() . '/typo3temp/var/tests'); + $testbase->createDirectory(\TYPO3\CMS\Core\Core\Environment::getPublicPath() . '/typo3temp/var/transient'); + + // Retrieve an instance of class loader and inject to core bootstrap + $classLoader = require $testbase->getPackagesPath() . '/autoload.php'; + \TYPO3\CMS\Core\Core\Bootstrap::initializeClassLoader($classLoader); + + // Initialize default TYPO3_CONF_VARS + $configurationManager = new \TYPO3\CMS\Core\Configuration\ConfigurationManager(); + $GLOBALS['TYPO3_CONF_VARS'] = $configurationManager->getDefaultConfiguration(); + + $cache = new \TYPO3\CMS\Core\Cache\Frontend\PhpFrontend( + 'core', + new \TYPO3\CMS\Core\Cache\Backend\NullBackend('production', []) + ); + + // Set all packages to active + if (interface_exists(\TYPO3\CMS\Core\Package\Cache\PackageCacheInterface::class)) { + $packageManager = \TYPO3\CMS\Core\Core\Bootstrap::createPackageManager(\TYPO3\CMS\Core\Package\UnitTestPackageManager::class, \TYPO3\CMS\Core\Core\Bootstrap::createPackageCache($cache)); + } else { + // v10 compatibility layer + // @deprecated Will be removed when v10 compat is dropped from testing-framework + $packageManager = \TYPO3\CMS\Core\Core\Bootstrap::createPackageManager(\TYPO3\CMS\Core\Package\UnitTestPackageManager::class, $cache); + } + + \TYPO3\CMS\Core\Utility\GeneralUtility::setSingletonInstance(\TYPO3\CMS\Core\Package\PackageManager::class, $packageManager); + \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::setPackageManager($packageManager); + + $testbase->dumpClassLoadingInformation(); + + \TYPO3\CMS\Core\Utility\GeneralUtility::purgeInstances(); +})(); diff --git a/Documentation/Running.rst b/Documentation/Running.rst index 532e36b..84630c6 100644 --- a/Documentation/Running.rst +++ b/Documentation/Running.rst @@ -232,7 +232,7 @@ settings so this configuration can serve as a template: - Directory: use the `Tests/Unit` directory in your project. - (*) Use alternative configuration file. -- Use `.Build/vendor/typo3/testing-framework/Resources/Core/Build/UnitTests.xml` +- Use `.Build/public/typo3conf/tea/Build/phpunit/UnitTests.xml` in your project folder. - Add the following environment variables: @@ -258,4 +258,4 @@ settings: - Directory: use the `Tests/Functional` directory in your project. - (*) Use alternative configuration file. - Use - `.Build/vendor/typo3/testing-framework/Resources/Core/Build/FunctionalTests.xml`. + `.Build/public/typo3conf/tea/Build/phpunit/FunctionalTests.xml`. diff --git a/composer.json b/composer.json index 157ed1d..0bff378 100644 --- a/composer.json +++ b/composer.json @@ -123,7 +123,7 @@ ], "ci:coverage:functional": [ "@coverage:create-directories", - ".Build/vendor/bin/phpunit -c .Build/vendor/typo3/testing-framework/Resources/Core/Build/FunctionalTests.xml --whitelist Classes --coverage-php=.Build/coverage/functional.cov Tests/Functional" + ".Build/vendor/bin/phpunit -c .Build/public/typo3conf/ext/tea/Build/phpunit/FunctionalTests.xml --whitelist Classes --coverage-php=.Build/coverage/functional.cov Tests/Functional" ], "ci:coverage:merge": [ "@coverage:create-directories", @@ -131,7 +131,7 @@ ], "ci:coverage:unit": [ "@coverage:create-directories", - ".Build/vendor/bin/phpunit -c .Build/vendor/typo3/testing-framework/Resources/Core/Build/UnitTests.xml --whitelist Classes --coverage-php=.Build/coverage/unit.cov Tests/Unit" + ".Build/vendor/bin/phpunit -c .Build/public/typo3conf/ext/tea/Build/phpunit/UnitTests.xml --whitelist Classes --coverage-php=.Build/coverage/unit.cov Tests/Unit" ], "ci:dynamic": [ "@ci:tests" @@ -164,8 +164,8 @@ "@ci:tests:unit", "@ci:tests:functional" ], - "ci:tests:functional": "find 'Tests/Functional' -wholename '*Test.php' | parallel --gnu 'echo; echo \"Running functional test suite {}\"; .Build/vendor/bin/phpunit -c .Build/vendor/typo3/testing-framework/Resources/Core/Build/FunctionalTests.xml {}';", - "ci:tests:unit": ".Build/vendor/bin/phpunit -c .Build/vendor/typo3/testing-framework/Resources/Core/Build/UnitTests.xml Tests/Unit", + "ci:tests:functional": "find 'Tests/Functional' -wholename '*Test.php' | parallel --gnu 'echo; echo \"Running functional test suite {}\"; .Build/vendor/bin/phpunit -c .Build/public/typo3conf/ext/tea/Build/phpunit/FunctionalTests.xml {}';", + "ci:tests:unit": ".Build/vendor/bin/phpunit -c .Build/public/typo3conf/ext/tea/Build/phpunit/UnitTests.xml Tests/Unit", "ci:ts:lint": "typoscript-lint -c Configuration/TsLint.yml --ansi -n --fail-on-warnings -vvv Configuration/TypoScript", "ci:yaml:lint": "find . ! -path '*.Build/*' ! -path '*node_modules/*' -regextype egrep -regex '.*.ya?ml$' | xargs -r php ./.Build/vendor/bin/yaml-lint", "coverage:create-directories": "mkdir -p .Build/logs .Build/coverage",