diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index c94aa09..937d31d 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -9,7 +9,7 @@ jobs: strategy: fail-fast: false matrix: - php: ['7.2', '7.3', '7.4', '8.0', '8.1', '8.2', '8.3', '8.4', '8.5'] + php: ['8.2', '8.3', '8.4', '8.5'] steps: - uses: actions/checkout@v2 diff --git a/.gitignore b/.gitignore index 5fe6285..5907911 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ /.ddev -/.php_cs.cache +/.php-cs-fixer.cache +/.phpunit.cache /.idea /composer.lock /vendor diff --git a/.php-cs-fixer.php b/.php-cs-fixer.php new file mode 100644 index 0000000..d4845bf --- /dev/null +++ b/.php-cs-fixer.php @@ -0,0 +1,118 @@ +setParallelConfig(\PhpCsFixer\Runner\Parallel\ParallelConfigFactory::detect()) + ->setFinder( + (new PhpCsFixer\Finder()) + ->ignoreVCSIgnored(true) + ->in(__DIR__) + ->exclude('.github') + ->exclude('vendor'), + ) + ->setRiskyAllowed(true) + ->setRules([ + '@DoctrineAnnotation' => true, + // @todo: Switch to @PER-CS2x0 once php-cs-fixer's todo list is done: https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/issues/7247 + '@PER-CS1x0' => true, + 'array_indentation' => true, + 'array_syntax' => ['syntax' => 'short'], + 'cast_spaces' => ['space' => 'none'], + // @todo: Can be dropped once we enable @PER-CS2x0 + 'concat_space' => ['spacing' => 'one'], + 'declare_equal_normalize' => ['space' => 'none'], + 'declare_parentheses' => true, + 'dir_constant' => true, + // @todo: Can be dropped once we enable @PER-CS2x0 + 'function_declaration' => [ + 'closure_fn_spacing' => 'none', + ], + 'function_to_constant' => [ + 'functions' => [ + 'get_called_class', + 'get_class', + 'get_class_this', + 'php_sapi_name', + 'phpversion', + 'pi', + ], + ], + 'type_declaration_spaces' => true, + 'global_namespace_import' => [ + 'import_classes' => false, + 'import_constants' => false, + 'import_functions' => false, + ], + 'list_syntax' => ['syntax' => 'short'], + // @todo: Can be dropped once we enable @PER-CS2x0 + 'method_argument_space' => true, + 'modernize_strpos' => true, + 'modernize_types_casting' => true, + 'native_function_casing' => true, + 'native_function_invocation' => [ + 'include' => [], + 'scope' => 'all', + 'strict' => true, + ], + 'no_alias_functions' => true, + 'no_blank_lines_after_phpdoc' => true, + 'no_empty_phpdoc' => true, + 'no_empty_statement' => true, + 'no_extra_blank_lines' => true, + 'no_leading_namespace_whitespace' => true, + 'no_null_property_initialization' => true, + 'no_short_bool_cast' => true, + 'no_singleline_whitespace_before_semicolons' => true, + 'no_superfluous_elseif' => true, + 'no_trailing_comma_in_singleline' => true, + 'no_unneeded_control_parentheses' => true, + 'no_unused_imports' => true, + 'no_useless_else' => true, + 'no_useless_nullsafe_operator' => true, + 'nullable_type_declaration' => [ + 'syntax' => 'question_mark', + ], + 'nullable_type_declaration_for_default_null_value' => true, + 'ordered_class_elements' => ['order' => ['use_trait', 'case', 'constant', 'property']], + 'ordered_imports' => ['imports_order' => ['class', 'function', 'const'], 'sort_algorithm' => 'alpha'], + 'php_unit_construct' => ['assertions' => ['assertEquals', 'assertSame', 'assertNotEquals', 'assertNotSame']], + 'php_unit_mock_short_will_return' => true, + 'php_unit_test_case_static_method_calls' => [ + 'call_type' => 'self', + 'methods' => [ + 'any' => 'this', + 'atLeast' => 'this', + 'atLeastOnce' => 'this', + 'atMost' => 'this', + 'exactly' => 'this', + 'never' => 'this', + 'onConsecutiveCalls' => 'this', + 'once' => 'this', + 'returnArgument' => 'this', + 'returnCallback' => 'this', + 'returnSelf' => 'this', + 'returnValue' => 'this', + 'returnValueMap' => 'this', + 'throwException' => 'this', + ], + ], + 'phpdoc_no_access' => true, + 'phpdoc_no_empty_return' => true, + 'phpdoc_no_package' => true, + 'phpdoc_scalar' => true, + 'phpdoc_trim' => true, + 'phpdoc_types' => true, + 'phpdoc_types_order' => ['null_adjustment' => 'always_last', 'sort_algorithm' => 'none'], + 'protected_to_private' => true, + 'return_type_declaration' => ['space_before' => 'none'], + 'single_quote' => true, + 'single_space_around_construct' => true, + 'single_line_comment_style' => ['comment_types' => ['hash']], + // @todo: Can be dropped once we enable @PER-CS2x0 + 'single_line_empty_body' => true, + 'trailing_comma_in_multiline' => ['elements' => ['arrays']], + 'whitespace_after_comma_in_array' => ['ensure_single_space' => true], + 'yoda_style' => ['equal' => false, 'identical' => false, 'less_and_greater' => false], + ]); diff --git a/.php_cs.dist b/.php_cs.dist deleted file mode 100644 index cad8850..0000000 --- a/.php_cs.dist +++ /dev/null @@ -1,49 +0,0 @@ -setRiskyAllowed(true) - ->setRules([ - '@PSR2' => true, - 'concat_space' => [ - 'spacing' => 'one', - ], - 'function_typehint_space' => true, - 'hash_to_slash_comment' => true, - 'lowercase_cast' => true, - 'ordered_imports' => true, - 'native_function_casing' => true, - 'no_alias_functions' => true, - 'no_blank_lines_after_phpdoc' => true, - 'no_leading_namespace_whitespace' => true, - 'no_trailing_comma_in_singleline_array' => true, - 'no_singleline_whitespace_before_semicolons' => true, - 'no_unused_imports' => true, - 'no_whitespace_in_blank_line' => true, - 'no_empty_statement' => true, - 'no_extra_consecutive_blank_lines' => [ - 'continue', - 'curly_brace_block', - 'extra', - 'parenthesis_brace_block', - 'square_brace_block', - 'throw', - ], - 'no_short_bool_cast' => true, - 'no_unneeded_control_parentheses' => [ - 'break', - 'clone', - 'continue', - 'echo_print', - 'return', - 'switch_case', - ], - 'phpdoc_no_package' => true, - 'phpdoc_scalar' => true, - 'self_accessor' => true, - 'single_quote' => true, - 'whitespace_after_comma_in_array' => true, - ]) - ->setFinder( - PhpCsFixer\Finder::create() - ->exclude('vendor') - ->in(__DIR__) - ); diff --git a/README.md b/README.md index 1584a08..b183090 100644 --- a/README.md +++ b/README.md @@ -42,37 +42,8 @@ return array( ); ``` -The '::class' constant is not available before PHP 5.5. Under a PHP before 5.5 the mapping file can look like this: -``` - 'TYPO3\\CMS\\About\\Controller\\AboutController', - 'Tx_About_Domain_Model_Extension' => 'TYPO3\\CMS\\About\\Domain\\Model\\Extension', - 'Tx_About_Domain_Repository_ExtensionRepository' => 'TYPO3\\CMS\\About\\Domain\\Repository\\ExtensionRepository', - 'Tx_Aboutmodules_Controller_ModulesController' => 'TYPO3\\CMS\\Aboutmodules\\Controller\\ModulesController', -); -``` - -In your *root* `composer.json` file, you can decide whether to allow classes to be found that are requested with wrong casing. -Since PHP is case insensitive for class names, but PSR class loading standards bound file names to class names, class names de facto -become case sensitive. For legacy packages it may be useful however to allow class names to be loaded even if wrong casing is provided. -For this to work properly, you need to use the composer [optimize class loading information feature](https://getcomposer.org/doc/03-cli.md#global-options). - - -You can activate this feature like this: - -``` - "extra": { - "typo3/class-alias-loader": { - "autoload-case-sensitivity": false - } - }, -``` - -The default value of this option is `true`. - -If no alias mapping is found and case sensitivity is set to `true` then by default this package does nothing. It means no additional class loading information is dumped +If no alias mapping is found then by default this package does nothing. It means no additional class loading information is dumped and the `vendor/autoload.php` is not changed. This enables library vendors to deliver compatibility packages which provide such aliases for backwards compatibility, but keep the library clean (and faster) for new users. diff --git a/composer.json b/composer.json index e9d6ea8..93a8123 100644 --- a/composer.json +++ b/composer.json @@ -20,13 +20,14 @@ "psr-4": { "TYPO3\\ClassAliasLoader\\Test\\": "tests/"} }, "require": { - "php": ">=7.1", + "php": ">=8.2", "composer-plugin-api": "^2.0" }, "require-dev": { "composer/composer": "^2.0@dev", "mikey179/vfsstream": "~1.4.0@dev", - "phpunit/phpunit": "^8 || ^9" + "phpunit/phpunit": "^11", + "friendsofphp/php-cs-fixer": "^3.95" }, "replace": { "helhum/class-alias-loader": "*" @@ -34,7 +35,7 @@ "extra": { "class": "TYPO3\\ClassAliasLoader\\Plugin", "branch-alias": { - "dev-main": "1.1.x-dev" + "dev-main": "2.0.x-dev" } } } diff --git a/phpunit.xml.dist b/phpunit.xml.dist index f36c8f3..36a0d72 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,29 +1,24 @@ - - - - tests/Unit/ - - - - - + + + + + tests/Unit/ + + + + + diff --git a/res/php/alias-loader-include.tmpl.php b/res/php/alias-loader-include.tmpl.php index 40f09db..e58ec71 100644 --- a/res/php/alias-loader-include.tmpl.php +++ b/res/php/alias-loader-include.tmpl.php @@ -1,9 +1,10 @@ setAliasMap($classAliasMap); -$classAliasLoader->setCaseSensitiveClassLoading('{$sensitive-loading}'); $classAliasLoader->register('{$prepend}'); TYPO3\ClassAliasLoader\ClassAliasMap::setClassAliasLoader($classAliasLoader); diff --git a/src/ClassAliasLoader.php b/src/ClassAliasLoader.php index be732e8..d06a557 100644 --- a/src/ClassAliasLoader.php +++ b/src/ClassAliasLoader.php @@ -1,4 +1,7 @@ [], + 'classNameToAliasMapping' => [], + ]; - /** - * @var array - */ - protected $aliasMap = array( - 'aliasToClassNameMapping' => array(), - 'classNameToAliasMapping' => array() - ); - - /** - * @deprecated - * @var bool - */ - protected $caseSensitiveClassLoading = true; - - /** - * @param ComposerClassLoader $composerClassLoader - */ - public function __construct(ComposerClassLoader $composerClassLoader) - { - $this->composerClassLoader = $composerClassLoader; - } + public function __construct(private readonly ComposerClassLoader $composerClassLoader) {} /** * Set the alias map - * - * @param array $aliasMap */ - public function setAliasMap(array $aliasMap) + public function setAliasMap(array $aliasMap): void { $this->aliasMap = $aliasMap; } - /** - * @deprecated - * @param bool $caseSensitiveClassLoading - */ - public function setCaseSensitiveClassLoading($caseSensitiveClassLoading) - { - $this->caseSensitiveClassLoading = $caseSensitiveClassLoading; - } - /** * Adds an alias map and merges it with already available map - * - * @param array $aliasMap */ - public function addAliasMap(array $aliasMap) + public function addAliasMap(array $aliasMap): void { foreach ($aliasMap['aliasToClassNameMapping'] as $alias => $originalClassName) { $lowerCaseAlias = strtolower($alias); @@ -80,79 +50,52 @@ public function addAliasMap(array $aliasMap) /** * Get final class name of alias - * - * @param string $aliasOrClassName - * @return string */ - public function getClassNameForAlias($aliasOrClassName) + public function getClassNameForAlias(string $aliasOrClassName): string { - $lookUpClassName = strtolower($aliasOrClassName); - - return isset($this->aliasMap['aliasToClassNameMapping'][$lookUpClassName]) ? $this->aliasMap['aliasToClassNameMapping'][$lookUpClassName] : $aliasOrClassName; + return $this->aliasMap['aliasToClassNameMapping'][strtolower($aliasOrClassName)] ?? $aliasOrClassName; } /** - * Registers this instance as an autoloader. - * - * @param bool $prepend Whether to prepend the autoloader or not + * Registers this instance as autoloader. */ - public function register($prepend = false) + public function register(bool $prepend = false): void { - spl_autoload_unregister(array($this->composerClassLoader, 'loadClass')); - spl_autoload_register(array($this, 'loadClassWithAlias'), true, $prepend); + spl_autoload_unregister([$this->composerClassLoader, 'loadClass']); + spl_autoload_register([$this, 'loadClassWithAlias'], true, $prepend); } /** * Unregisters this instance as an autoloader. */ - public function unregister() + public function unregister(): void { - spl_autoload_unregister(array($this, 'loadClassWithAlias')); + spl_autoload_unregister([$this, 'loadClassWithAlias']); } /** * Main class loading method registered with spl_autoload_register() - * - * @param string $className - * @return bool */ - public function loadClassWithAlias($className) + public function loadClassWithAlias(string $className): ?bool { $originalClassName = $this->getOriginalClassName($className); - return $originalClassName - ? $this->loadOriginalClassAndSetAliases($originalClassName) - : $this->loadClass($className); - } - - /** - * Load class with the option to respect case insensitivity - * @deprecated - * - * @param string $className - * @return bool|null - */ - public function loadClass($className) - { - $classFound = $this->composerClassLoader->loadClass($className); - if (!$classFound && !$this->caseSensitiveClassLoading) { - $classFound = $this->composerClassLoader->loadClass(strtolower($className)); - } - return $classFound; + return $originalClassName === null + ? $this->composerClassLoader->loadClass($className) + : $this->loadOriginalClassAndSetAliases($originalClassName); } /** * Looks up the original class name from the alias map - * - * @param string $aliasOrClassName - * @return string|NULL NULL if no alias mapping is found or the original class name as string + * returns null if no alias mapping is found or the original class name as string */ - protected function getOriginalClassName($aliasOrClassName) + private function getOriginalClassName(string $aliasOrClassName): ?string { // Is an original class which has an alias if (array_key_exists($aliasOrClassName, $this->aliasMap['classNameToAliasMapping'])) { return $aliasOrClassName; } + // Is an alias (we're graceful ignoring casing for alias definitions) return $this->aliasMap['aliasToClassNameMapping'][strtolower($aliasOrClassName)] ?? null; } @@ -161,13 +104,10 @@ protected function getOriginalClassName($aliasOrClassName) * Load classes and set aliases. * The class_exists calls are safety guards to avoid fatals when * class files were included or aliases were set manually in userland code. - * - * @param string $originalClassName - * @return bool|null */ - protected function loadOriginalClassAndSetAliases($originalClassName) + private function loadOriginalClassAndSetAliases(string $originalClassName): ?bool { - if ($this->classExists($originalClassName) || $this->loadClass($originalClassName)) { + if ($this->classExists($originalClassName) || $this->composerClassLoader->loadClass($originalClassName)) { foreach ($this->aliasMap['classNameToAliasMapping'][$originalClassName] as $aliasClassName) { if (!$this->classExists($aliasClassName)) { class_alias($originalClassName, $aliasClassName); @@ -180,11 +120,7 @@ class_alias($originalClassName, $aliasClassName); return null; } - /** - * @param string $className - * @return bool - */ - protected function classExists($className) + private function classExists(string $className): bool { return class_exists($className, false) || interface_exists($className, false) diff --git a/src/ClassAliasMap.php b/src/ClassAliasMap.php index 009a51e..346bff6 100644 --- a/src/ClassAliasMap.php +++ b/src/ClassAliasMap.php @@ -1,4 +1,7 @@ getClassNameForAlias($classNameOrAlias); } /** * Whether or not alias maps are already registered - * - * @return bool */ - public static function hasAliasMaps() + public static function hasAliasMaps(): bool { return is_object(static::$classAliasLoader); } @@ -51,10 +50,9 @@ public static function hasAliasMaps() /** * Adds an alias map if the alias loader is registered, throws an exception otherwise. * - * @param array $aliasMap * @throws \RuntimeException */ - public static function addAliasMap(array $aliasMap) + public static function addAliasMap(array $aliasMap): void { if (!static::$classAliasLoader) { throw new \RuntimeException('Cannot set an alias map as the alias loader is not registered!', 1439228111); @@ -63,10 +61,7 @@ public static function addAliasMap(array $aliasMap) static::$classAliasLoader->addAliasMap($aliasMap); } - /** - * @param ClassAliasLoader $classAliasLoader - */ - public static function setClassAliasLoader(ClassAliasLoader $classAliasLoader) + public static function setClassAliasLoader(ClassAliasLoader $classAliasLoader): void { if (static::$classAliasLoader) { throw new \RuntimeException('Cannot set the alias loader, as it is already registered!', 1439228112); diff --git a/src/ClassAliasMapGenerator.php b/src/ClassAliasMapGenerator.php index ad59130..83fc00d 100644 --- a/src/ClassAliasMapGenerator.php +++ b/src/ClassAliasMapGenerator.php @@ -1,4 +1,7 @@ composer = $composer; - $this->io = $io ?: new NullIO(); - if (\is_bool($config)) { - // Happens during upgrade from older versions, so try to be graceful - $config = new Config($this->composer->getPackage()); - } - $this->config = $config ?: new Config($this->composer->getPackage()); + $this->config = new Config($this->composer->getPackage()); } /** - * @deprecated * @throws \Exception */ - public function generateAliasMap() - { - // Is called during upgrade from older plugin versions, so try to be graceful, but output verbose message - $this->io->writeError(' ┌─────────────────────────────────────────────────────────────┐ '); - $this->io->writeError(' │ Upgraded typo3/class-alias-loader from older plugin version.│ '); - $this->io->writeError(' │ Please run "composer dumpautoload" to complete the upgrade. │ '); - $this->io->writeError(' └─────────────────────────────────────────────────────────────┘ '); - } - - /** - * @throws \Exception - * @return bool - */ - public function generateAliasMapFiles() + public function generateAliasMapFiles(): bool { $config = $this->composer->getConfig(); @@ -89,14 +52,14 @@ public function generateAliasMapFiles() $localRepo = $this->composer->getRepositoryManager()->getLocalRepository(); $packageMap = $autoLoadGenerator->buildPackageMap($this->composer->getInstallationManager(), $mainPackage, $localRepo->getCanonicalPackages()); - $aliasToClassNameMapping = array(); - $classNameToAliasMapping = array(); + $aliasToClassNameMapping = []; + $classNameToAliasMapping = []; $classAliasMappingFound = false; foreach ($packageMap as $item) { /** @var PackageInterface $package */ - list($package, $installPath) = $item; - $aliasLoaderConfig = new Config($package, $this->io); + [$package, $installPath] = $item; + $aliasLoaderConfig = new Config($package); if ($aliasLoaderConfig->get('class-alias-maps') !== null) { if (!is_array($aliasLoaderConfig->get('class-alias-maps'))) { throw new \Exception('Configuration option "class-alias-maps" must be an array'); @@ -124,9 +87,8 @@ public function generateAliasMapFiles() } $alwaysAddAliasLoader = $this->config->get('always-add-alias-loader'); - $caseSensitiveClassLoading = $this->config->get('autoload-case-sensitivity'); - if (!$alwaysAddAliasLoader && !$classAliasMappingFound && $caseSensitiveClassLoading) { + if (!$alwaysAddAliasLoader && !$classAliasMappingFound) { // No mapping found in any package and no insensitive class loading active. We return early and skip rewriting // Unless user configured alias loader to be always added return false; @@ -135,16 +97,11 @@ public function generateAliasMapFiles() $includeFile = new IncludeFile( $this->io, $this->composer, - array( - new CaseSensitiveToken( - $this->io, - $this->config - ), + [ new PrependToken( - $this->io, $this->composer->getConfig() ), - ) + ] ); $includeFile->register(); @@ -154,37 +111,12 @@ public function generateAliasMapFiles() return true; } - /** - * @deprecated will be removed with 2.0 - * @param $optimizeAutoloadFiles - * @return bool - */ - public function modifyComposerGeneratedFiles($optimizeAutoloadFiles = false) - { - $caseSensitiveClassLoading = $this->config->get('autoload-case-sensitivity'); - $vendorPath = $this->composer->getConfig()->get('vendor-dir'); - if (!$caseSensitiveClassLoading) { - $this->io->writeError('Re-writing class map to support case insensitive class loading is deprecated'); - if (!$optimizeAutoloadFiles) { - $this->io->writeError('Case insensitive class loading only works reliably if you use the optimize class loading feature of composer'); - } - $this->rewriteClassMapWithLowerCaseClassNames($vendorPath . '/composer'); - } - - return true; - } - - /** - * @param array $aliasToClassNameMapping - * @param array $classNameToAliasMapping - * @param string $targetDir - */ - protected function generateAliasMapFile(array $aliasToClassNameMapping, array $classNameToAliasMapping, $targetDir) + private function generateAliasMapFile(array $aliasToClassNameMapping, array $classNameToAliasMapping, string $targetDir): void { - $exportArray = array( + $exportArray = [ 'aliasToClassNameMapping' => $aliasToClassNameMapping, - 'classNameToAliasMapping' => $classNameToAliasMapping - ); + 'classNameToAliasMapping' => $classNameToAliasMapping, + ]; $fileContent = ' /', function ($match) { diff --git a/src/Config.php b/src/Config.php index bbd06f3..373c0e3 100644 --- a/src/Config.php +++ b/src/Config.php @@ -1,4 +1,7 @@ null, 'always-add-alias-loader' => false, - 'autoload-case-sensitivity' => true - ); - - /** - * @var IOInterface - */ - protected $io; + ]; /** * @param PackageInterface $package - * @param IOInterface $io */ - public function __construct(PackageInterface $package, ?IOInterface $io = null) + public function __construct(PackageInterface $package) { - $this->io = $io ?: new NullIO(); $this->setAliasLoaderConfigFromPackage($package); } @@ -55,7 +46,7 @@ public function get($configKey) throw new \InvalidArgumentException('Configuration key must not be empty', 1444039407); } // Extract parts of the path - $configParts = str_getcsv($configKey, '.', '"', '\\'); + $configParts = str_getcsv($configKey, '.', '"', '\\'); // Loop through each part and extract its value $value = $this->config; @@ -67,64 +58,18 @@ public function get($configKey) return null; } } + return $value; } - /** - * @param PackageInterface $package - */ - protected function setAliasLoaderConfigFromPackage(PackageInterface $package) + protected function setAliasLoaderConfigFromPackage(PackageInterface $package): void { - $extraConfig = $this->handleDeprecatedConfigurationInPackage($package); + $extraConfig = $package->getExtra(); if (isset($extraConfig['typo3/class-alias-loader']['class-alias-maps'])) { $this->config['class-alias-maps'] = (array)$extraConfig['typo3/class-alias-loader']['class-alias-maps']; } if (isset($extraConfig['typo3/class-alias-loader']['always-add-alias-loader'])) { $this->config['always-add-alias-loader'] = (bool)$extraConfig['typo3/class-alias-loader']['always-add-alias-loader']; } - if (isset($extraConfig['typo3/class-alias-loader']['autoload-case-sensitivity'])) { - $this->config['autoload-case-sensitivity'] = (bool)$extraConfig['typo3/class-alias-loader']['autoload-case-sensitivity']; - } - } - - /** - * Ensures backwards compatibility for packages which used helhum/class-alias-loader - * - * @param PackageInterface $package - * @return array - */ - protected function handleDeprecatedConfigurationInPackage(PackageInterface $package) - { - $extraConfig = $package->getExtra(); - $messages = array(); - if (!isset($extraConfig['typo3/class-alias-loader'])) { - if (isset($extraConfig['helhum/class-alias-loader'])) { - $extraConfig['typo3/class-alias-loader'] = $extraConfig['helhum/class-alias-loader']; - $messages[] = sprintf( - 'The package "%s" uses "helhum/class-alias-loader" section to define class alias maps, which is deprecated. Please use "typo3/class-alias-loader" instead!', - $package->getName() - ); - } else { - $extraConfig['typo3/class-alias-loader'] = array(); - if (isset($extraConfig['class-alias-maps'])) { - $extraConfig['typo3/class-alias-loader']['class-alias-maps'] = $extraConfig['class-alias-maps']; - $messages[] = sprintf( - 'The package "%s" uses "class-alias-maps" section on top level, which is deprecated. Please move this config below the top level key "typo3/class-alias-loader" instead!', - $package->getName() - ); - } - if (isset($extraConfig['autoload-case-sensitivity'])) { - $extraConfig['typo3/class-alias-loader']['autoload-case-sensitivity'] = $extraConfig['autoload-case-sensitivity']; - $messages[] = sprintf( - 'The package "%s" uses "autoload-case-sensitivity" section on top level, which is deprecated. Please move this config below the top level key "typo3/class-alias-loader" instead!', - $package->getName() - ); - } - } - } - if (!empty($messages)) { - $this->io->writeError($messages); - } - return $extraConfig; } } diff --git a/src/IncludeFile.php b/src/IncludeFile.php index a606e7c..189e4d2 100644 --- a/src/IncludeFile.php +++ b/src/IncludeFile.php @@ -1,4 +1,7 @@ io = $io; $this->composer = $composer; $this->tokens = $tokens; - $this->filesystem = $filesystem ?: new Filesystem(); + $this->filesystem = $filesystem ?? new Filesystem(); } public function register() diff --git a/src/IncludeFile/CaseSensitiveToken.php b/src/IncludeFile/CaseSensitiveToken.php deleted file mode 100644 index 78e9eea..0000000 --- a/src/IncludeFile/CaseSensitiveToken.php +++ /dev/null @@ -1,65 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -use Composer\IO\IOInterface; -use TYPO3\ClassAliasLoader\Config; - -/** - * @deprecated - */ -class CaseSensitiveToken implements TokenInterface -{ - /** - * @var string - */ - private $name = 'sensitive-loading'; - - /** - * @var IOInterface - */ - private $io; - - /** - * @var Config - */ - private $config; - - /** - * BaseDirToken constructor. - * - * @param IOInterface $io - * @param Config $config - */ - public function __construct(IOInterface $io, Config $config) - { - $this->io = $io; - $this->config = $config; - } - - /** - * @return string - */ - public function getName() - { - return $this->name; - } - - /** - * @param string $includeFilePath - * @throws \InvalidArgumentException - * @return string - */ - public function getContent($includeFilePath) - { - return $this->config->get('autoload-case-sensitivity') ? 'true' : 'false'; - } -} diff --git a/src/IncludeFile/PrependToken.php b/src/IncludeFile/PrependToken.php index dc7246d..3b70fcb 100644 --- a/src/IncludeFile/PrependToken.php +++ b/src/IncludeFile/PrependToken.php @@ -1,4 +1,7 @@ io = $io; $this->config = $config; } - /** - * @return string - */ - public function getName() + public function getName(): string { return $this->name; } - /** - * @param string $includeFilePath - * @throws \InvalidArgumentException - * @return string - */ - public function getContent($includeFilePath) + public function getContent(string $includeFilePath): string { return $this->config->get('prepend-autoloader') === false ? 'false' : 'true'; } diff --git a/src/IncludeFile/TokenInterface.php b/src/IncludeFile/TokenInterface.php index 15b975a..8a9d0b8 100644 --- a/src/IncludeFile/TokenInterface.php +++ b/src/IncludeFile/TokenInterface.php @@ -1,4 +1,7 @@ composer = $composer; - $this->io = $io; $this->aliasMapGenerator = new ClassAliasMapGenerator( - $this->composer, - $this->io + $composer, + $io ); } @@ -62,30 +53,11 @@ public function uninstall(Composer $composer, IOInterface $io) // Nothing to do } - /** - * Returns an array of event names this subscriber wants to listen to. - * - * The array keys are event names and the value can be: - * - * * The method name to call (priority defaults to 0) - * * An array composed of the method name to call and the priority - * * An array of arrays composed of the method names to call and respective - * priorities, or 0 if unset - * - * For instance: - * - * * array('eventName' => 'methodName') - * * array('eventName' => array('methodName', $priority)) - * * array('eventName' => array(array('methodName1', $priority), array('methodName2')) - * - * @return array The event names to listen to - */ - public static function getSubscribedEvents() + public static function getSubscribedEvents(): array { - return array( - 'pre-autoload-dump' => array('onPreAutoloadDump'), - 'post-autoload-dump' => array('onPostAutoloadDump'), - ); + return [ + 'pre-autoload-dump' => ['onPreAutoloadDump'], + ]; } /** @@ -93,21 +65,8 @@ public static function getSubscribedEvents() * @throws \Exception * @return bool */ - public function onPreAutoloadDump(Event $event) + public function onPreAutoloadDump(Event $event): bool { return $this->aliasMapGenerator->generateAliasMapFiles(); } - - /** - * @param Event $event - * @return bool - */ - public function onPostAutoloadDump(Event $event) - { - $flags = $event->getFlags(); - $config = $event->getComposer()->getConfig(); - $optimizeAutoloadFiles = !empty($flags['optimize']) || $config->get('optimize-autoloader') || $config->get('classmap-authoritative'); - - return $this->aliasMapGenerator->modifyComposerGeneratedFiles($optimizeAutoloadFiles); - } } diff --git a/tests/Unit/ClassAliasLoaderTest.php b/tests/Unit/ClassAliasLoaderTest.php index f1247e7..7e4d681 100644 --- a/tests/Unit/ClassAliasLoaderTest.php +++ b/tests/Unit/ClassAliasLoaderTest.php @@ -1,4 +1,7 @@ subject->unregister(); } - /** - * @test - */ + #[Test] public function composerLoadClassIsCalledOnlyOnceWhenCaseSensitiveClassLoadingIsOn(): void - { - $this->composerClassLoaderMock->expects($this->once())->method('loadClass'); - $this->subject->loadClassWithAlias('TestClass'); - } - - /** - * @test - */ - public function composerLoadClassIsCalledOnlyOnceWhenCaseSensitiveClassLoadingIsOffButClassIsFound(): void { $this->composerClassLoaderMock->expects($this->once())->method('loadClass')->willReturn(true); - $this->subject->setCaseSensitiveClassLoading(false); $this->subject->loadClassWithAlias('TestClass'); } - /** - * @test - */ - public function composerLoadClassIsCalledTwiceWhenCaseSensitiveClassLoadingIsOffAndClassIsNotFound(): void - { - $this->composerClassLoaderMock->expects($this->exactly(2))->method('loadClass'); - $this->subject->setCaseSensitiveClassLoading(false); - $this->subject->loadClassWithAlias('TestClass'); - } - - /** - * @test - */ + #[Test] public function loadsClassIfNoAliasIsFound(): void { $testClassName = 'TestClass' . md5(uniqid('bla', true)); $this->composerClassLoaderMock->expects($this->once())->method('loadClass')->willReturnCallback(function ($className) { eval('class ' . $className . ' {}'); + return true; }); $this->subject->loadClassWithAlias($testClassName); - $this->assertTrue(class_exists($testClassName, false)); + self::assertTrue(class_exists($testClassName, false)); } - /** - * @test - */ + #[Test] public function callingLoadClassMultipleTimesInEdgeCasesWillStillWork(): void { $this->composerClassLoaderMock ->expects($this->exactly(2)) ->method('loadClass') ->willReturnOnConsecutiveCalls(false, true); - $this->assertFalse($this->subject->loadClassWithAlias('TestClass')); - $this->assertTrue($this->subject->loadClassWithAlias('TestClass')); + self::assertFalse($this->subject->loadClassWithAlias('TestClass')); + self::assertTrue($this->subject->loadClassWithAlias('TestClass')); } - /** - * @test - */ + #[Test] public function loadClassWithOriginalClassNameSetsAliases(): void { $testClassName = 'TestClass' . md5(uniqid('bla', true)); @@ -108,109 +85,100 @@ public function loadClassWithOriginalClassNameSetsAliases(): void $this->composerClassLoaderMock->expects($this->once())->method('loadClass')->willReturnCallback(function ($className) { eval('class ' . $className . ' {}'); + return true; }); - $this->subject->setAliasMap(array( - 'aliasToClassNameMapping' => array( + $this->subject->setAliasMap([ + 'aliasToClassNameMapping' => [ strtolower($testAlias1) => $testClassName, strtolower($testAlias2) => $testClassName, - ), - 'classNameToAliasMapping' => array( - $testClassName => array(strtolower($testAlias1), strtolower($testAlias2)) - ), - )); + ], + 'classNameToAliasMapping' => [ + $testClassName => [strtolower($testAlias1), strtolower($testAlias2)], + ], + ]); $this->subject->loadClassWithAlias($testClassName); - $this->assertTrue(class_exists($testAlias1, false)); - $this->assertTrue(class_exists($testAlias2, false)); + self::assertTrue(class_exists($testAlias1, false)); + self::assertTrue(class_exists($testAlias2, false)); } - /** - * @test - */ + #[Test] public function getClassNameForAliasReturnsClassNameForEachAlias(): void { $testClassName = 'TestClass' . md5(uniqid('bla', true)); $testAlias1 = 'TestAlias' . md5(uniqid('bla', true)); $testAlias2 = 'TestAlias' . md5(uniqid('bla', true)); - $this->subject->setAliasMap(array( - 'aliasToClassNameMapping' => array( + $this->subject->setAliasMap([ + 'aliasToClassNameMapping' => [ strtolower($testAlias1) => $testClassName, strtolower($testAlias2) => $testClassName, - ), - 'classNameToAliasMapping' => array( - $testClassName => array(strtolower($testAlias1), strtolower($testAlias2)) - ), - )); - - $this->assertEquals($testClassName, $this->subject->getClassNameForAlias($testAlias1)); - $this->assertEquals($testClassName, $this->subject->getClassNameForAlias($testAlias2)); + ], + 'classNameToAliasMapping' => [ + $testClassName => [strtolower($testAlias1), strtolower($testAlias2)], + ], + ]); + + self::assertEquals($testClassName, $this->subject->getClassNameForAlias($testAlias1)); + self::assertEquals($testClassName, $this->subject->getClassNameForAlias($testAlias2)); } - /** - * @test - */ + #[Test] public function addAliasMapAddsAliasesCorrectlyToTheMap(): void { $testClassName = 'TestClass' . md5(uniqid('bla', true)); $testAlias1 = 'TestAlias' . md5(uniqid('bla', true)); $testAlias2 = 'TestAlias' . md5(uniqid('bla', true)); - $this->subject->setAliasMap(array( - 'aliasToClassNameMapping' => array( + $this->subject->setAliasMap([ + 'aliasToClassNameMapping' => [ strtolower($testAlias1) => $testClassName, - ), - 'classNameToAliasMapping' => array( - $testClassName => array(strtolower($testAlias1)) - ), - )); - - $this->subject->addAliasMap(array( - 'aliasToClassNameMapping' => array( + ], + 'classNameToAliasMapping' => [ + $testClassName => [strtolower($testAlias1)], + ], + ]); + + $this->subject->addAliasMap([ + 'aliasToClassNameMapping' => [ $testAlias2 => $testClassName, - ), - )); + ], + ]); - $this->assertEquals($testClassName, $this->subject->getClassNameForAlias($testAlias1)); - $this->assertEquals($testClassName, $this->subject->getClassNameForAlias($testAlias2)); + self::assertEquals($testClassName, $this->subject->getClassNameForAlias($testAlias1)); + self::assertEquals($testClassName, $this->subject->getClassNameForAlias($testAlias2)); } - /** - * @test - */ + #[Test] public function getClassNameForAliasReturnsClassNameForClassName(): void { $testClassName = 'TestClass' . md5(uniqid('bla', true)); $testAlias1 = 'TestAlias' . md5(uniqid('bla', true)); $testAlias2 = 'TestAlias' . md5(uniqid('bla', true)); - $this->subject->setAliasMap(array( - 'aliasToClassNameMapping' => array( + $this->subject->setAliasMap([ + 'aliasToClassNameMapping' => [ strtolower($testAlias1) => $testClassName, strtolower($testAlias2) => $testClassName, - ), - 'classNameToAliasMapping' => array( - $testClassName => array(strtolower($testAlias1), strtolower($testAlias2)) - ), - )); + ], + 'classNameToAliasMapping' => [ + $testClassName => [strtolower($testAlias1), strtolower($testAlias2)], + ], + ]); - $this->assertEquals($testClassName, $this->subject->getClassNameForAlias($testClassName)); + self::assertEquals($testClassName, $this->subject->getClassNameForAlias($testClassName)); } - /** - * @test - */ + #[Test] public function getClassNameForAliasReturnsClassNameForClassNameWithNoAliasMapSet(): void { $testClassName = 'TestClass' . md5(uniqid('bla', true)); - $this->assertEquals($testClassName, $this->subject->getClassNameForAlias($testClassName)); + self::assertEquals($testClassName, $this->subject->getClassNameForAlias($testClassName)); } - /** - * @test - */ + #[Test] public function loadClassWithAliasClassNameSetsAliasesAndLoadsOriginalClass(): void { $testClassName = 'TestClass' . md5(uniqid('bla', true)); @@ -219,28 +187,27 @@ public function loadClassWithAliasClassNameSetsAliasesAndLoadsOriginalClass(): v $this->composerClassLoaderMock->expects($this->once())->method('loadClass')->willReturnCallback(function ($className) { eval('class ' . $className . ' {}'); + return true; }); - $this->subject->setAliasMap(array( - 'aliasToClassNameMapping' => array( + $this->subject->setAliasMap([ + 'aliasToClassNameMapping' => [ strtolower($testAlias1) => $testClassName, strtolower($testAlias2) => $testClassName, - ), - 'classNameToAliasMapping' => array( - $testClassName => array(strtolower($testAlias1), strtolower($testAlias2)) - ), - )); + ], + 'classNameToAliasMapping' => [ + $testClassName => [strtolower($testAlias1), strtolower($testAlias2)], + ], + ]); $this->subject->loadClassWithAlias($testAlias1); - $this->assertTrue(class_exists($testClassName, false), 'Class name is not loaded'); - $this->assertTrue(class_exists($testAlias1, false), 'First alias is not loaded'); - $this->assertTrue(class_exists($testAlias2, false), 'Second alias is not loaded'); + self::assertTrue(class_exists($testClassName, false), 'Class name is not loaded'); + self::assertTrue(class_exists($testAlias1, false), 'First alias is not loaded'); + self::assertTrue(class_exists($testAlias2, false), 'Second alias is not loaded'); } - /** - * @test - */ + #[Test] public function aliasesInstancesHaveOriginalClassName(): void { $testClassName = 'TestClass' . md5(uniqid('bla', true)); @@ -249,31 +216,30 @@ public function aliasesInstancesHaveOriginalClassName(): void $this->composerClassLoaderMock->expects($this->once())->method('loadClass')->willReturnCallback(function ($className) { eval('class ' . $className . ' {}'); + return true; }); - $this->subject->setAliasMap(array( - 'aliasToClassNameMapping' => array( + $this->subject->setAliasMap([ + 'aliasToClassNameMapping' => [ strtolower($testAlias1) => $testClassName, strtolower($testAlias2) => $testClassName, - ), - 'classNameToAliasMapping' => array( - $testClassName => array(strtolower($testAlias1), strtolower($testAlias2)) - ), - )); + ], + 'classNameToAliasMapping' => [ + $testClassName => [strtolower($testAlias1), strtolower($testAlias2)], + ], + ]); $this->subject->loadClassWithAlias($testClassName); $testObject1 = new $testAlias1(); $testObject2 = new $testAlias2(); - $this->assertSame($testClassName, get_class($testObject1)); - $this->assertSame($testClassName, get_class($testObject2)); + self::assertSame($testClassName, get_class($testObject1)); + self::assertSame($testClassName, get_class($testObject2)); } - /** - * @test - */ + #[Test] public function classAliasesAreGracefullySetIfClassAlreadyExists(): void { $testClassName = 'TestClass' . md5(uniqid('bla', true)); @@ -281,15 +247,15 @@ public function classAliasesAreGracefullySetIfClassAlreadyExists(): void $testAlias2 = 'TestAlias' . md5(uniqid('bla', true)); $this->composerClassLoaderMock->expects($this->never())->method('loadClass'); - $this->subject->setAliasMap(array( - 'aliasToClassNameMapping' => array( + $this->subject->setAliasMap([ + 'aliasToClassNameMapping' => [ strtolower($testAlias1) => $testClassName, strtolower($testAlias2) => $testClassName, - ), - 'classNameToAliasMapping' => array( - $testClassName => array(strtolower($testAlias1), strtolower($testAlias2)) - ), - )); + ], + 'classNameToAliasMapping' => [ + $testClassName => [strtolower($testAlias1), strtolower($testAlias2)], + ], + ]); eval('class ' . $testClassName . ' {}'); @@ -298,13 +264,11 @@ public function classAliasesAreGracefullySetIfClassAlreadyExists(): void $testObject1 = new $testAlias1(); $testObject2 = new $testAlias2(); - $this->assertSame($testClassName, get_class($testObject1)); - $this->assertSame($testClassName, get_class($testObject2)); + self::assertSame($testClassName, get_class($testObject1)); + self::assertSame($testClassName, get_class($testObject2)); } - /** - * @test - */ + #[Test] public function interfaceAliasesAreGracefullySetIfInterfaceAlreadyExists(): void { $testClassName = 'TestClass' . md5(uniqid('bla', true)); @@ -312,27 +276,25 @@ public function interfaceAliasesAreGracefullySetIfInterfaceAlreadyExists(): void $testAlias2 = 'TestAlias' . md5(uniqid('bla', true)); $this->composerClassLoaderMock->expects($this->never())->method('loadClass'); - $this->subject->setAliasMap(array( - 'aliasToClassNameMapping' => array( + $this->subject->setAliasMap([ + 'aliasToClassNameMapping' => [ strtolower($testAlias1) => $testClassName, strtolower($testAlias2) => $testClassName, - ), - 'classNameToAliasMapping' => array( - $testClassName => array(strtolower($testAlias1), strtolower($testAlias2)) - ), - )); + ], + 'classNameToAliasMapping' => [ + $testClassName => [strtolower($testAlias1), strtolower($testAlias2)], + ], + ]); eval('interface ' . $testClassName . ' {}'); $this->subject->loadClassWithAlias($testClassName); - $this->assertTrue(interface_exists($testAlias1, false), 'First alias is not loaded'); - $this->assertTrue(interface_exists($testAlias2, false), 'Second alias is not loaded'); + self::assertTrue(interface_exists($testAlias1, false), 'First alias is not loaded'); + self::assertTrue(interface_exists($testAlias2, false), 'Second alias is not loaded'); } - /** - * @test - */ + #[Test] public function classAliasesAreNotReEstablishedIfTheyAlreadyExist(): void { $testClassName = 'TestClass' . md5(uniqid('bla', true)); @@ -340,53 +302,49 @@ public function classAliasesAreNotReEstablishedIfTheyAlreadyExist(): void $testAlias2 = 'TestAlias' . md5(uniqid('bla', true)); $this->composerClassLoaderMock->expects($this->never())->method('loadClass'); - $this->subject->setAliasMap(array( - 'aliasToClassNameMapping' => array( + $this->subject->setAliasMap([ + 'aliasToClassNameMapping' => [ strtolower($testAlias1) => $testClassName, strtolower($testAlias2) => $testClassName, - ), - 'classNameToAliasMapping' => array( - $testClassName => array(strtolower($testAlias1), strtolower($testAlias2)) - ), - )); + ], + 'classNameToAliasMapping' => [ + $testClassName => [strtolower($testAlias1), strtolower($testAlias2)], + ], + ]); eval('class ' . $testClassName . ' {}'); class_alias($testClassName, $testAlias1); $this->subject->loadClassWithAlias($testClassName); - $this->assertTrue(class_exists($testAlias2, false), 'Second alias is not loaded'); + self::assertTrue(class_exists($testAlias2, false), 'Second alias is not loaded'); } - /** - * @test - */ + #[Test] public function loadClassWithAliasReturnsNullIfComposerClassLoaderCannotFindClass(): void { $this->composerClassLoaderMock->expects($this->once())->method('loadClass'); - $this->assertNull($this->subject->loadClassWithAlias('TestClass')); + self::assertNull($this->subject->loadClassWithAlias('TestClass')); } - /** - * @test - */ + #[Test] public function loadClassWithAliasReturnsNullIfComposerClassLoaderCannotFindClassEvenIfItExistsInMap(): void { $testClassName = 'TestClass' . md5(uniqid('bla', true)); $testAlias1 = 'TestAlias' . md5(uniqid('bla', true)); $testAlias2 = 'TestAlias' . md5(uniqid('bla', true)); - $this->subject->setAliasMap(array( - 'aliasToClassNameMapping' => array( + $this->subject->setAliasMap([ + 'aliasToClassNameMapping' => [ strtolower($testAlias1) => $testClassName, strtolower($testAlias2) => $testClassName, - ), - 'classNameToAliasMapping' => array( - $testClassName => array(strtolower($testAlias1), strtolower($testAlias2)) - ), - )); + ], + 'classNameToAliasMapping' => [ + $testClassName => [strtolower($testAlias1), strtolower($testAlias2)], + ], + ]); $this->composerClassLoaderMock->expects($this->once())->method('loadClass'); - $this->assertNull($this->subject->loadClassWithAlias($testClassName)); + self::assertNull($this->subject->loadClassWithAlias($testClassName)); } } diff --git a/tests/Unit/ConfigTest.php b/tests/Unit/ConfigTest.php index d4dc111..5f577da 100644 --- a/tests/Unit/ConfigTest.php +++ b/tests/Unit/ConfigTest.php @@ -1,4 +1,7 @@ ioMock = $this->getMockBuilder('Composer\\IO\\IOInterface')->getMock(); - $this->packageMock = $this->getMockBuilder('Composer\\Package\\PackageInterface')->getMock(); - $this->subject = new Config($this->packageMock, $this->ioMock); + $this->packageMock = $this->getMockBuilder(PackageInterface::class)->getMock(); + $this->subject = new Config($this->packageMock); } - /** - * @test - */ + #[Test] public function throwsExceptionForEmptyKey(): void { - // Use this instead when old PHP versions are dropped and minimum phpunit version can be raised: - /** - $this->expectException('\\InvalidArgumentException'); + $this->expectException(\InvalidArgumentException::class); $this->expectExceptionCode(1444039407); $this->subject->get(null); - */ - try { - $result = false; - $this->subject->get(null); - } catch (\InvalidArgumentException $e) { - if ($e->getCode() === 1444039407) { - $result = true; - } - } - $this->assertTrue($result, 'Expected exception with expected code not received'); } - /** - * @test - */ + #[Test] public function defaultConfigIsAppliedWhenNothingIsConfiguredInPackage(): void { - $this->assertFalse($this->subject->get('always-add-alias-loader')); - $this->assertTrue($this->subject->get('autoload-case-sensitivity')); - $this->assertNull($this->subject->get('class-alias-maps')); + self::assertFalse($this->subject->get('always-add-alias-loader')); + self::assertNull($this->subject->get('class-alias-maps')); } - /** - * @test - */ + #[Test] public function aliasMapConfigIsExtracted(): void { $this->packageMock->expects($this->any())->method('getExtra')->willReturn( - array( - 'typo3/class-alias-loader' => array( - 'class-alias-maps' => array( - 'path/map.php' - ) - ) - ) + [ + 'typo3/class-alias-loader' => [ + 'class-alias-maps' => [ + 'path/map.php', + ], + ], + ] ); - $subject = new Config($this->packageMock, $this->ioMock); + $subject = new Config($this->packageMock); - $this->assertSame(array('path/map.php'), $subject->get('class-alias-maps')); + self::assertSame(['path/map.php'], $subject->get('class-alias-maps')); } - /** - * @test - */ - public function aliasMapConfigIsExtractedFromDeprecatedKey(): void - { - $this->packageMock->expects($this->any())->method('getExtra')->willReturn( - array( - 'helhum/class-alias-loader' => array( - 'class-alias-maps' => array( - 'path/map.php' - ) - ) - ) - ); - $this->ioMock->expects($this->once())->method('writeError'); - - $subject = new Config($this->packageMock, $this->ioMock); - - $this->assertSame(array('path/map.php'), $subject->get('class-alias-maps')); - } - - /** - * @test - */ + #[Test] public function otherConfigIsExtracted(): void { $this->packageMock->expects($this->any())->method('getExtra')->willReturn( - array( - 'typo3/class-alias-loader' => array( - 'always-add-alias-loader' => true, - 'autoload-case-sensitivity' => false, - ) - ) - ); - - $subject = new Config($this->packageMock, $this->ioMock); - - $this->assertTrue($subject->get('always-add-alias-loader')); - $this->assertFalse($subject->get('autoload-case-sensitivity')); - } - - /** - * @test - */ - public function otherConfigIsExtractedFromDeprecatedKey(): void - { - $this->packageMock->expects($this->any())->method('getExtra')->willReturn( - array( - 'helhum/class-alias-loader' => array( + [ + 'typo3/class-alias-loader' => [ 'always-add-alias-loader' => true, - 'autoload-case-sensitivity' => false, - ) - ) - ); - - $subject = new Config($this->packageMock, $this->ioMock); - - $this->assertTrue($subject->get('always-add-alias-loader')); - $this->assertFalse($subject->get('autoload-case-sensitivity')); - } - - /** - * @test - */ - public function caseSensitivityConfigIsExtractedFromVeryDeprecatedKey(): void - { - $this->packageMock->expects($this->any())->method('getExtra')->willReturn( - array( - 'autoload-case-sensitivity' => false, - ) + ], + ] ); - $subject = new Config($this->packageMock, $this->ioMock); + $subject = new Config($this->packageMock); - $this->assertFalse($subject->get('autoload-case-sensitivity')); + self::assertTrue($subject->get('always-add-alias-loader')); } } diff --git a/tests/Unit/IncludeFileTest.php b/tests/Unit/IncludeFileTest.php index c3ae857..de459ee 100644 --- a/tests/Unit/IncludeFileTest.php +++ b/tests/Unit/IncludeFileTest.php @@ -1,4 +1,7 @@ ioMock = $this->getMockBuilder('Composer\\IO\\IOInterface')->getMock(); - $this->packageMock = $this->getMockBuilder('Composer\\Package\\RootPackageInterface')->getMock(); - $this->composerMock = $this->getMockBuilder('Composer\\Composer')->getMock(); - $configMock = $this->getMockBuilder('Composer\\Config')->getMock(); + $this->ioMock = $this->getMockBuilder(IOInterface::class)->getMock(); + $this->packageMock = $this->getMockBuilder(RootPackageInterface::class)->getMock(); + $this->composerMock = $this->getMockBuilder(Composer::class)->getMock(); + $configMock = $this->getMockBuilder(ComposerConfig::class)->getMock(); $testDir = $this->testDir; - $configMock->expects(self::any()) + $configMock->expects($this->any()) ->method('get') ->willReturnCallback(function ($key) use ($testDir) { switch ($key) { @@ -65,20 +67,19 @@ public function setUp(): void } }); mkdir($testDir . '/typo3'); - $this->composerMock->expects(self::any()) + $this->composerMock->expects($this->any()) ->method('getPackage') ->willReturn($this->packageMock); - $this->composerMock->expects(self::any()) + $this->composerMock->expects($this->any()) ->method('getConfig') ->willReturn($configMock); $this->subject = new IncludeFile( $this->ioMock, $this->composerMock, - array( - new IncludeFile\PrependToken($this->ioMock, $configMock), - new IncludeFile\CaseSensitiveToken($this->ioMock, new Config($this->packageMock, $this->ioMock)) - ) + [ + new IncludeFile\PrependToken($configMock), + ] ); } @@ -88,8 +89,8 @@ public function tearDown(): void rmdir(dirname($this->testDir . IncludeFile::INCLUDE_FILE)); } - - public function testIncludeFileCanPeWritten(): void + #[Test] + public function includeFileCanBeWritten(): void { $this->subject->register(); self::assertTrue(file_exists($this->testDir . IncludeFile::INCLUDE_FILE));