Skip to content

Commit

Permalink
Merge pull request #250 from /issues/232-missing-php-classes
Browse files Browse the repository at this point in the history
Add test coverage for missing classes autoloading
  • Loading branch information
T2L committed Oct 21, 2021
2 parents 28bde6e + fe026f4 commit 6d79969
Show file tree
Hide file tree
Showing 23 changed files with 196 additions and 80 deletions.
43 changes: 22 additions & 21 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,32 @@
"require": {
"php": ">=7.3",
"composer-plugin-api": "^1.1 || ^2.0",
"nette/robot-loader": "^3.2",
"symfony/filesystem": "^3.4 || ^4.4 || ^5",
"symfony/yaml": "^3.4 || ^4.4 || ^5",
"nette/finder": "^2.5.2",
"nette/robot-loader": "^3.4.1",
"symfony/filesystem": "^3.4.47 || ^4.4.27 || ^5.3.4",
"symfony/yaml": "^3.4.41 || ^4.4.9 || ^5.3",
"t2l/comments": "^1.1.0"
},
"require-dev": {
"composer/composer": "^1.10 || ^2.0.13",
"dealerdirect/phpcodesniffer-composer-installer": "^0.7",
"dg/bypass-finals": "^1.2.2",
"drupal/coder": "^8.3.8",
"ergebnis/composer-normalize": "^2.3",
"ergebnis/phpstan-rules": "^0.15",
"mikey179/vfsstream": "^1.6",
"composer/composer": "^1.10.23 || ^2.1.9",
"dealerdirect/phpcodesniffer-composer-installer": "^0.7.1",
"dg/bypass-finals": "^1.3.1",
"drupal/coder": "^8.3.13",
"ergebnis/composer-normalize": "^2.15.0",
"ergebnis/phpstan-rules": "^0.15.3",
"mikey179/vfsstream": "^1.6.10",
"php-mock/php-mock-phpunit": "^2.5",
"php-parallel-lint/php-parallel-lint": "^1.2",
"phpro/grumphp": "^1.2",
"phpstan/extension-installer": "^1.0.4",
"phpstan/phpstan": "^0.12",
"phpstan/phpstan-deprecation-rules": "^0.12",
"phpstan/phpstan-phpunit": "^0.12",
"phpstan/phpstan-strict-rules": "^0.12",
"phpunit/phpunit": "^9.3",
"slam/phpstan-extensions": "^5.0",
"symfony/process": "^3.4.3 || ^4.4 || ^5",
"thecodingmachine/phpstan-strict-rules": "^0.12"
"php-parallel-lint/php-parallel-lint": "^1.3.1",
"phpro/grumphp": "^1.5.0",
"phpstan/extension-installer": "^1.1.0",
"phpstan/phpstan": "^0.12.99",
"phpstan/phpstan-deprecation-rules": "^0.12.6",
"phpstan/phpstan-phpunit": "^0.12.22",
"phpstan/phpstan-strict-rules": "^0.12.11",
"phpunit/phpunit": "^9.5.10",
"slam/phpstan-extensions": "^5.0.2",
"symfony/process": "^3.4.47 || ^4.4.30 || ^5.3.7",
"thecodingmachine/phpstan-strict-rules": "^0.12.1"
},
"config": {
"sort-packages": true
Expand Down
12 changes: 8 additions & 4 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,27 @@ parameters:
paths:
- src
- tests
excludes_analyse:
excludePaths:
- tests/Traits/ComposerPackageEventFactoryTrait.php
dynamicConstantNames:
- Composer\Plugin\PluginInterface::PLUGIN_API_VERSION
ergebnis:
classesAllowedToBeExtended:
- Lemberg\Draft\Environment\Config\Manager\AbstractConfigManager
- Lemberg\Draft\Environment\Config\Manager\UpdateManager
- Lemberg\Draft\Environment\Config\Install\Step\AbstractInstallStep
- Lemberg\Draft\Environment\Config\Update\Step\AbstractUpdateStep
- Lemberg\Tests\Functional\Draft\Environment\AbstractFunctionalTest
- Lemberg\Tests\Functional\Draft\Environment\Config\Manager\AbstractConfigManagerTest
- Lemberg\Tests\Unit\Draft\Environment\Config\Update\Step\Cleanup30400Test
- Symfony\Component\Filesystem\Filesystem
classesNotRequiredToBeAbstractOrFinal:
- Lemberg\Draft\Environment\Config\Manager\UpdateManager
- Lemberg\Tests\Unit\Draft\Environment\Config\Update\Step\Cleanup30400Test
ignoreErrors:
-
message: '#Method [A-Za-z0-9\\]+::[A-Za-z0-9\(\)]+ is not final, but since the containing class is abstract, it should be.#'
paths:
- src/Config/Install/Step/AbstractInstallStep.php
message: '#Constructor in Lemberg\\Draft\\Environment\\Config\\Manager\\AbstractConfigManager has parameter \$classLoader with default value.#'
path: src/Config/Manager/AbstractConfigManager.php
-
message: '#Method Lemberg\\Draft\\Environment\\Config\\Manager\\AbstractConfigManager::__construct\(\) has parameter \$classLoader with null as default value.#'
path: src/Config/Manager/AbstractConfigManager.php
6 changes: 4 additions & 2 deletions src/Composer/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Lemberg\Draft\Environment\Composer;

use Composer\Autoload\ClassLoader;
use Composer\Composer;
use Composer\EventDispatcher\Event;
use Composer\EventDispatcher\EventSubscriberInterface;
Expand Down Expand Up @@ -41,8 +42,9 @@ public function activate(Composer $composer, IOInterface $io): void {
}

$config = new Config($sourceDirectory, $targetDirectory);
$configInstallManager = new InstallManager($composer, $io, $config);
$configUpdateManager = new UpdateManager($composer, $io, $config);
$classLoader = new ClassLoader();
$configInstallManager = new InstallManager($composer, $io, $config, $classLoader);
$configUpdateManager = new UpdateManager($composer, $io, $config, $classLoader);
$this->setApp(new App($composer, $io, $configInstallManager, $configUpdateManager));
}

Expand Down
7 changes: 0 additions & 7 deletions src/Config/Install/Step/AbstractInstallStep.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,4 @@ final public function __construct(Composer $composer, IOInterface $io, ManagerIn
$this->setFilesystem(new Filesystem());
}

/**
* {@inheritdoc}
*/
public function getWeight(): int {
return 0;
}

}
7 changes: 7 additions & 0 deletions src/Config/Install/Step/PhpVersion.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,11 @@ public function install(array &$config): void {
$config['php_version'] = $this->io->select($question, $choices, $default, FALSE, "\nSpecified value '%s' is not a valid PHP version");
}

/**
* {@inheritdoc}
*/
public function getWeight(): int {
return 0;
}

}
61 changes: 39 additions & 22 deletions src/Config/Manager/AbstractConfigManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

namespace Lemberg\Draft\Environment\Config\Manager;

use Nette\IOException;
use Nette\Utils\Finder;
use Composer\Autoload\ClassLoader;
use Composer\Autoload\ClassMapGenerator;
use Composer\Composer;
Expand All @@ -17,7 +15,9 @@
use Lemberg\Draft\Environment\Config\ConfigAwareTrait;
use Lemberg\Draft\Environment\Utility\Filesystem;
use Lemberg\Draft\Environment\Utility\FilesystemAwareTrait;
use Nette\IOException;
use Nette\Loaders\RobotLoader;
use Nette\Utils\Finder;

/**
* Base configuration manager class.
Expand All @@ -27,6 +27,24 @@ abstract class AbstractConfigManager implements ManagerInterface {
use ConfigAwareTrait;
use FilesystemAwareTrait;

/**
* Classes that may not exist during the update.
*/
protected const NEWLY_INTRODUCED_CLASSES = [
'classmap' => [
RobotLoader::class => 'nette/robot-loader',
Finder::class => 'nette/finder',
IOException::class => 'nette/utils',
],
'psr4' => [
Comments::class => [
'package' => 't2l/comments',
'prefix' => 'Consolidation\\Comments\\',
'path' => 'src',
],
],
];

/**
* @var \Composer\Composer
*/
Expand All @@ -37,6 +55,11 @@ abstract class AbstractConfigManager implements ManagerInterface {
*/
protected $io;

/**
* @var \Composer\Autoload\ClassLoader
*/
protected $classLoader;

/**
* @var \Lemberg\Draft\Environment\Config\AbstractStepInterface[]
*/
Expand All @@ -45,11 +68,12 @@ abstract class AbstractConfigManager implements ManagerInterface {
/**
* {@inheritdoc}
*/
final public function __construct(Composer $composer, IOInterface $io, Config $config) {
final public function __construct(Composer $composer, IOInterface $io, Config $config, ClassLoader $classLoader = NULL) {
$this->composer = $composer;
$this->io = $io;
$this->setConfig($config);
$this->setFilesystem(new Filesystem());
$this->classLoader = $classLoader ?? new ClassLoader();

// This code is running in Composer context, newly added packages might
// not be autoloaded.
Expand Down Expand Up @@ -146,34 +170,27 @@ final protected function getLastAvailableUpdateWeight(): int {
* @link https://github.com/lemberg/draft-environment/issues/232
*/
private function autoloadDependencies(): void {
$loader = new ClassLoader();
$vendorDir = $this->composer->getConfig()->get('vendor-dir');
$classes = [
'classmap' => [
RobotLoader::class => 'nette/robot-loader',
Finder::class => 'nette/finder',
IOException::class => 'nette/utils',
],
'psr4' => [
Comments::class => [
'package' => 't2l/comments',
'prefix' => 'Consolidation\\Comments\\',
'path' => 'src',
],
],
];

foreach ($classes['classmap'] as $class_name => $package_name) {
$shouldRegister = FALSE;

foreach (static::NEWLY_INTRODUCED_CLASSES['classmap'] as $class_name => $package_name) {
if (!class_exists($class_name)) {
$loader->addClassMap(ClassMapGenerator::createMap("$vendorDir/$package_name"));
$this->classLoader->addClassMap(ClassMapGenerator::createMap("$vendorDir/$package_name"));
$shouldRegister = TRUE;
}
}

foreach ($classes['psr4'] as $class_name => $autoload_data) {
foreach (static::NEWLY_INTRODUCED_CLASSES['psr4'] as $class_name => $autoload_data) {
if (!class_exists($class_name)) {
$loader->addPsr4($autoload_data['prefix'], $vendorDir . '/' . $autoload_data['package'] . '/' . $autoload_data['path']);
$this->classLoader->addPsr4($autoload_data['prefix'], $vendorDir . '/' . $autoload_data['package'] . '/' . $autoload_data['path']);
$shouldRegister = TRUE;
}
}

if ($shouldRegister) {
$this->classLoader->register();
}
}

}
4 changes: 3 additions & 1 deletion src/Config/Manager/ManagerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Lemberg\Draft\Environment\Config\Manager;

use Composer\Autoload\ClassLoader;
use Composer\Composer;
use Composer\IO\IOInterface;
use Lemberg\Draft\Environment\Config\Config;
Expand All @@ -18,7 +19,8 @@ interface ManagerInterface extends ConfigAwareInterface {
* @param \Composer\Composer $composer
* @param \Composer\IO\IOInterface $io
* @param \Lemberg\Draft\Environment\Config\Config $config
* @param \Composer\Autoload\ClassLoader $classLoader
*/
public function __construct(Composer $composer, IOInterface $io, Config $config);
public function __construct(Composer $composer, IOInterface $io, Config $config, ClassLoader $classLoader);

}
2 changes: 1 addition & 1 deletion src/Config/Manager/UpdateManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
/**
* Draft Environment configuration update manager.
*/
final class UpdateManager extends AbstractConfigManager implements UpdateManagerInterface {
class UpdateManager extends AbstractConfigManager implements UpdateManagerInterface {

/**
* {@inheritdoc}
Expand Down
1 change: 1 addition & 0 deletions tests/Functional/Config/Manager/ConfigManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public function testComposerUpdate(string $directory): void {
(new Process([
'vendor/bin/composer', 'require',
'--dev', App::PACKAGE_NAME . ':' . rtrim($working_branch) . '-dev',
'--update-with-all-dependencies',
'--working-dir', $this->workingDir,
]))
->mustRun();
Expand Down
6 changes: 4 additions & 2 deletions tests/Unit/Config/Install/Step/InitConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@

namespace Lemberg\Tests\Unit\Draft\Environment\Config\Install\Step;

use Composer\Autoload\ClassLoader;
use Composer\Composer;
use Composer\Config as ComposerConfig;
use Composer\IO\IOInterface;
use Lemberg\Draft\Environment\Config\Config;
use Lemberg\Draft\Environment\Config\Manager\InstallManager;
use Lemberg\Draft\Environment\Config\Install\Step\InitConfig;
use Lemberg\Draft\Environment\Config\Manager\InstallManager;
use Lemberg\Draft\Environment\Utility\Filesystem;
use org\bovigo\vfs\vfsStream;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -65,7 +66,8 @@ protected function setUp(): void {
$this->fs->mkdir(["$this->root/target"]);

$configObject = new Config('.', "$this->root/target");
$this->configInstallManager = new InstallManager($this->composer, $this->io, $configObject);
$classLoader = new ClassLoader();
$this->configInstallManager = new InstallManager($this->composer, $this->io, $configObject, $classLoader);
}

/**
Expand Down
6 changes: 4 additions & 2 deletions tests/Unit/Config/Manager/InstallManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Lemberg\Tests\Unit\Draft\Environment\Config\Manager;

use Composer\Autoload\ClassLoader;
use Composer\Composer;
use Composer\Config as ComposerConfig;
use Composer\Factory;
Expand Down Expand Up @@ -111,8 +112,9 @@ protected function setUp(): void {
];
$json->write($lockData);

$config = new Config("$this->root/source", "$this->root/target");
$this->configInstallManager = new InstallManager($this->composer, $this->io, $config);
$configObject = new Config("$this->root/source", "$this->root/target");
$classLoader = new ClassLoader();
$this->configInstallManager = new InstallManager($this->composer, $this->io, $configObject, $classLoader);
}

/**
Expand Down

0 comments on commit 6d79969

Please sign in to comment.