Skip to content

Commit

Permalink
Merge pull request #188 from lemberg/issue/173-use-post-install-update
Browse files Browse the repository at this point in the history
Add functional tests covering install & update managers
  • Loading branch information
T2L committed Apr 25, 2020
2 parents 92da967 + b93ccce commit 1827b34
Show file tree
Hide file tree
Showing 31 changed files with 1,400 additions and 16 deletions.
8 changes: 8 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ jobs:
before_install:
- phpenv config-rm xdebug.ini
- composer self-update
# Some tests run Composer via command line. It's not possible to require
# a package in the detached state. Checkout to a branch to workaround
# this.
- git checkout -b test

install:
- composer update --no-progress --no-suggest --prefer-dist $PREFER_LOWEST --optimize-autoloader
Expand Down Expand Up @@ -157,6 +161,10 @@ jobs:

before_install:
- composer self-update
# Some tests run Composer via command line. It's not possible to require
# a package in the detached state. Checkout to a branch to workaround
# this.
- git checkout -b test

install:
- composer install --no-interaction --no-progress --no-suggest --prefer-dist --optimize-autoloader
Expand Down
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"phpstan/phpstan-strict-rules": "^0.12",
"phpunit/phpunit": "^8.5 || ~9.0.0",
"slam/phpstan-extensions": "^5.0",
"symfony/process": "^3.4.3 || ^4.4 || ^5",
"thecodingmachine/phpstan-strict-rules": "^0.12"
},
"config": {
Expand All @@ -43,7 +44,9 @@
},
"autoload-dev": {
"psr-4": {
"Lemberg\\Tests\\Draft\\Environment\\": "tests/"
"Lemberg\\Tests\\Extensions\\Draft\\Environment\\": "tests/Extensions",
"Lemberg\\Tests\\Functional\\Draft\\Environment\\": "tests/Functional",
"Lemberg\\Tests\\Unit\\Draft\\Environment\\": "tests/Unit"
}
},
"repositories": [
Expand Down
2 changes: 2 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +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\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
9 changes: 6 additions & 3 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@
<ini name="memory_limit" value="-1"/>
</php>
<testsuites>
<testsuite name="draft-environment">
<directory>./tests/</directory>
<testsuite name="unit">
<directory>./tests/Unit/</directory>
</testsuite>
<testsuite name="functional">
<directory>./tests/Functional/</directory>
</testsuite>
</testsuites>
<!-- Filter for coverage reports. -->
Expand All @@ -22,6 +25,6 @@
</whitelist>
</filter>
<extensions>
<extension class="Lemberg\Tests\Draft\Environment\PHPUnit\Hook\BypassFinalHook"/>
<extension class="Lemberg\Tests\Extensions\Draft\Environment\PHPUnit\Hook\BypassFinalHook"/>
</extensions>
</phpunit>
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Lemberg\Tests\Draft\Environment\PHPUnit\Hook;
namespace Lemberg\Tests\Extensions\Draft\Environment\PHPUnit\Hook;

use DG\BypassFinals;
use PHPUnit\Runner\BeforeTestHook;
Expand Down
71 changes: 71 additions & 0 deletions tests/Functional/Config/Manager/AbstractConfigManagerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

declare(strict_types=1);

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

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

/**
* 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);
}

/**
* Asserts that composer.lock exists and contains correct data in the package
* extra section.
*/
final protected function assertComposerLockContainsPackageExtra(): void {
self::assertFileExists("$this->workingDir/composer.lock");

$composer_lock = new JsonFile("$this->workingDir/composer.lock");
$decoded_composer_lock = $composer_lock->read();

$key = array_search(App::PACKAGE_NAME, array_column($decoded_composer_lock['packages-dev'], 'name'), TRUE);

self::assertTrue($decoded_composer_lock['packages-dev'][$key]['extra']['draft-environment']['already-installed']);
self::assertSame(3, $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();
}

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

declare(strict_types=1);

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

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

/**
* Tests Draft Environment configuration update manager.
*
* @coversNothing
*/
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.
*
* @param string $directory
*
* @testWith ["update-before-update-manager"]
* ["update-after-update-manager"]
*/
public function testComposerUpdate(string $directory): void {

$this->fs->mirror("$this->basePath/$directory", $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();

// Get the current working branch to test against.
$working_branch = (new Process([
'git', 'rev-parse', '--abbrev-ref', 'HEAD',
]))
->mustRun()
->getOutput();

// Update the package by requiring the latest dev version.
(new Process([
'vendor/bin/composer', 'require',
'--dev', App::PACKAGE_NAME . ':' . rtrim($working_branch) . '-dev',
'--no-suggest',
'--working-dir', $this->workingDir,
]))
->mustRun();

$this->assertComposerLockContainsPackageExtra();
}

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

declare(strict_types=1);

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

use Symfony\Component\Process\Process;

/**
* Tests Draft Environment configuration install manager.
*
* @coversNothing
*/
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.
*/
public function testComposerInstall(): 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();

$this->assertComposerLockContainsPackageExtra();
}

}
2 changes: 1 addition & 1 deletion tests/AppTest.php → tests/Unit/AppTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

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

use Composer\Composer;
use Composer\Config as ComposerConfig;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Lemberg\Tests\Draft\Environment\Composer;
namespace Lemberg\Tests\Unit\Draft\Environment\Composer;

use Composer\Composer;
use Composer\Config;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

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

use Lemberg\Draft\Environment\Config\Config;
use Lemberg\Draft\Environment\Utility\Filesystem;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

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

use Composer\Composer;
use Composer\Config as ComposerConfig;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

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

use Composer\Composer;
use Composer\Config as ComposerConfig;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

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

use Composer\Composer;
use Composer\Config as ComposerConfig;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

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

use Composer\Composer;
use Composer\Config as ComposerConfig;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

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

use Composer\Composer;
use Composer\Config as ComposerConfig;
Expand Down Expand Up @@ -80,7 +80,7 @@ protected function setUp(): void {
putenv("COMPOSER=$this->root/wd/composer.json");

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

$configObject = new Config("$this->root/source", "$this->root/target");
$this->configUpdateManager = new UpdateManager($this->composer, $this->io, $configObject);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

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

use Composer\Composer;
use Composer\Config as ComposerConfig;
Expand Down

0 comments on commit 1827b34

Please sign in to comment.