Skip to content

Commit

Permalink
Merge pull request #210 from lemberg/issue/205-remove-package-php-fatal
Browse files Browse the repository at this point in the history
[WIP] Add functional test covering package installation & removal
  • Loading branch information
T2L committed Aug 12, 2020
2 parents ed1e837 + c2fc510 commit 05e6c36
Show file tree
Hide file tree
Showing 10 changed files with 280 additions and 64 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

Fixes:

- [GH-205](https://github.com/lemberg/draft-environment/issues/205) - Fix PHP fatal error upon package removal (i.e. when running `composer remove lemberg/draft-environment`
- [GH-208](https://github.com/lemberg/draft-environment/issues/208) - Fix broken provisioning by adding `/vagrant` mount

## Draft Environment 3.1.0, 2020-08-06
Expand Down
2 changes: 1 addition & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ parameters:
- Lemberg\Draft\Environment\Config\Manager\AbstractConfigManager
- 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
- Symfony\Component\Filesystem\Filesystem
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
- tests/Functional/Config/Manager/AbstractConfigManagerTest.php
10 changes: 6 additions & 4 deletions src/Config/Manager/AbstractConfigManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,19 +89,21 @@ final protected function sortSteps(): void {
*/
final protected function getPackageExtra(): array {
$localRepository = $this->composer->getRepositoryManager()->getLocalRepository();
/** @var \Composer\Package\Package $localPackage */
/** @var \Composer\Package\Package|NULL $localPackage */
$localPackage = $localRepository->findPackage(App::PACKAGE_NAME, '*');
return $localPackage->getExtra();
return !is_null($localPackage) ? $localPackage->getExtra() : [];
}

/**
* @param array<mixed> $extra
*/
final protected function setPackageExtra(array $extra): void {
$localRepository = $this->composer->getRepositoryManager()->getLocalRepository();
/** @var \Composer\Package\Package $localPackage */
/** @var \Composer\Package\Package|NULL $localPackage */
$localPackage = $localRepository->findPackage(App::PACKAGE_NAME, '*');
$localPackage->setExtra($extra);
if (!is_null($localPackage)) {
$localPackage->setExtra($extra);
}

// This code might run after Composer has written the lock file.
$composerFile = Factory::getComposerFile();
Expand Down
57 changes: 57 additions & 0 deletions tests/Functional/AbstractFunctionalTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);

namespace Lemberg\Tests\Functional\Draft\Environment;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Filesystem\Filesystem;

/**
* Base functional test.
*
* @coversNothing
*/
abstract class AbstractFunctionalTest extends TestCase {

/**
* @var string
*/
protected $workingDir;

/**
* @var \Symfony\Component\Filesystem\Filesystem
*/
protected $fs;

/**
* @var string
*/
protected $basePath;

/**
* {@inheritdoc}
*/
final protected function setUp(): void {

$this->workingDir = sys_get_temp_dir() . '/draft-environment';

$this->fs = new Filesystem();
$this->fs->remove($this->workingDir);
$this->fs->mkdir($this->workingDir);

// Build path to the test composer.json file based on the current class
// name.
$this->basePath = './tests/fixtures/Functional' . str_replace('\\', DIRECTORY_SEPARATOR, substr(static::class, strlen('Lemberg\Tests\Functional\Draft\Environment')));
}

/**
* {@inheritdoc}
*/
final protected function tearDown(): void {
$this->fs->remove($this->workingDir);

parent::tearDown();
}

}
60 changes: 60 additions & 0 deletions tests/Functional/AppTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

declare(strict_types=1);

namespace Lemberg\Tests\Functional\Draft\Environment;

use Lemberg\Draft\Environment\App;
use Symfony\Component\Process\Process;

/**
* Tests Draft Environment installation and removal.
*
* @coversNothing
*/
final class AppTest extends AbstractFunctionalTest {

/**
* Tests that package installation and removal works as expected.
*
* @doesNotPerformAssertions
*/
public function testComposerInstallAndRemove(): void {

$this->fs->mirror($this->basePath, $this->workingDir, NULL, ['override' => TRUE]);

// Link package working directory, so tests runs against the proper
// source code.
(new Process([
'vendor/bin/composer', 'config',
'repositories.test', json_encode([
'type' => 'path',
'url' => getcwd(),
'options' => [
'symlink' => FALSE,
],
]),
'--working-dir', $this->workingDir,
]))
->mustRun();

// Run composer install.
(new Process([
'vendor/bin/composer', 'install',
'--prefer-dist',
'--no-interaction',
'--no-suggest',
'--working-dir', $this->workingDir,
]))
->mustRun();

// Run composer remove.
(new Process([
'vendor/bin/composer', 'remove',
'--dev', App::PACKAGE_NAME,
'--working-dir', $this->workingDir,
]))
->mustRun();
}

}
41 changes: 2 additions & 39 deletions tests/Functional/Config/Manager/AbstractConfigManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,42 +6,14 @@

use Composer\Json\JsonFile;
use Lemberg\Draft\Environment\App;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Filesystem\Filesystem;
use Lemberg\Tests\Functional\Draft\Environment\AbstractFunctionalTest;

/**
* Base configuration manager test.
*
* @coversNothing
*/
abstract class AbstractConfigManagerTest extends TestCase {

/**
* @var string
*/
protected $workingDir;

/**
* @var \Symfony\Component\Filesystem\Filesystem
*/
protected $fs;

/**
* @var string
*/
protected $basePath;

/**
* {@inheritdoc}
*/
protected function setUp(): void {

$this->workingDir = sys_get_temp_dir() . '/draft-environment';

$this->fs = new Filesystem();
$this->fs->remove($this->workingDir);
$this->fs->mkdir($this->workingDir);
}
abstract class AbstractConfigManagerTest extends AbstractFunctionalTest {

/**
* Asserts that composer.lock exists and contains correct data in the package
Expand All @@ -59,13 +31,4 @@ final protected function assertComposerLockContainsPackageExtra(): void {
self::assertSame(5, $decoded_composer_lock['packages-dev'][$key]['extra']['draft-environment']['last-update-weight']);
}

/**
* {@inheritdoc}
*/
final protected function tearDown(): void {
$this->fs->remove($this->workingDir);

parent::tearDown();
}

}
10 changes: 0 additions & 10 deletions tests/Functional/Config/Manager/ConfigManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,6 @@
*/
final class ConfigManagerTest extends AbstractConfigManagerTest {

/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();

// Build path to the test composer.json file based on this class name.
$this->basePath = './tests/fixtures/Functional' . str_replace('\\', DIRECTORY_SEPARATOR, substr(__CLASS__, strlen('Lemberg\Tests\Functional\Draft\Environment')));
}

/**
* Tests that package update does set up correct data in the package extra.
*
Expand Down
10 changes: 0 additions & 10 deletions tests/Functional/Config/Manager/InstallManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,6 @@
*/
final class InstallManagerTest extends AbstractConfigManagerTest {

/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();

// Build path to the test composer.json file based on this class name.
$this->basePath = './tests/fixtures/Functional' . str_replace('\\', DIRECTORY_SEPARATOR, substr(__CLASS__, strlen('Lemberg\Tests\Functional\Draft\Environment')));
}

/**
* Tests that package installation does set up correct data in the package
* extra.
Expand Down

0 comments on commit 05e6c36

Please sign in to comment.