Skip to content

Commit

Permalink
Tests refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik committed Mar 28, 2021
1 parent aa02342 commit 8bc35f7
Show file tree
Hide file tree
Showing 43 changed files with 368 additions and 541 deletions.
221 changes: 97 additions & 124 deletions tests/Integration/ComposerEventHandlerTest.php

Large diffs are not rendered by default.

169 changes: 123 additions & 46 deletions tests/Integration/ComposerTest.php
Expand Up @@ -13,32 +13,37 @@

abstract class ComposerTest extends TestCase
{
protected string $workingDirectory;
protected string $stdoutFile;
protected string $stderrFile;
private const TEST_PACKAGES = [
'a',
'ba',
'c',
'custom-source',
'd-dev-c',
'first-package',
'second-package',
];

private string $workingDirectory;
private string $stdoutFile;
private string $stderrFile;

public function __construct(?string $name = null, array $data = [], $dataName = '')
{
parent::__construct($name, $data, $dataName);

$this->workingDirectory = dirname(__DIR__) . '/Environment';

$tempDirectory = sys_get_temp_dir();
$this->stdoutFile = $tempDirectory . '/yiisoft-hook-stdout';
$this->stderrFile = $tempDirectory . '/yiisoft-hook-stderr';
$tempDirectory = sys_get_temp_dir() . '/yiisoft/config';
$this->ensureDirectoryExists($tempDirectory);
$this->stdoutFile = $tempDirectory . '/hook-stdout';
$this->stderrFile = $tempDirectory . '/hook-stderr';
}

abstract protected function getStartComposerConfig(): array;

protected function setUp(): void
{
parent::setUp();

$this->removeDirectory($this->workingDirectory);
$this->ensureDirectoryExists($this->workingDirectory);

$this->initComposer();
$this->execComposer('install');
}

protected function tearDown(): void
Expand All @@ -47,40 +52,76 @@ protected function tearDown(): void
$this->removeDirectory($this->workingDirectory);
}

protected function assertSameMergePlan(array $expected): void
protected function assertMergePlan(array $expected): void
{
$this->assertSame($expected, require $this->workingDirectory . '/config/packages/merge_plan.php');
}

protected function assertEnvironmentDirectoryExists(string $directory): void
{
$mergePlan = require $this->workingDirectory . '/config/packages/merge_plan.php';
$this->assertDirectoryExists($this->workingDirectory . $directory);
}

$this->assertSameMergePlanKeys($expected, $mergePlan);
protected function assertEnvironmentFileDoesNotExist(string $filename): void
{
$this->assertFileDoesNotExist($this->workingDirectory . $filename);
}

foreach ($expected as $group => $packages) {
$this->assertSameMergePlanKeys($packages, $mergePlan[$group]);
foreach ($packages as $name => $files) {
self::assertSame($files, $mergePlan[$group][$name]);
}
}
protected function assertEnvironmentFileExist(string $filename): void
{
$this->assertFileExists($this->workingDirectory . $filename);
}

protected function assertEnvironmentFileNotEquals(string $expected, string $actual): void
{
$this->assertFileNotEquals($this->workingDirectory . $expected, $this->workingDirectory . $actual);
}

protected function assertEnvironmentFileEquals(string $expected, string $actual): void
{
$this->assertFileEquals($this->workingDirectory . $expected, $this->workingDirectory . $actual);
}

protected function getEnvironmentFileContents(string $filename): string
{
return file_get_contents($this->workingDirectory . $filename);
}

protected function putEnvironmentFileContents(string $filename, string $content, $context = null): void
{
$context === null
? file_put_contents($this->workingDirectory . $filename, $content)
: file_put_contents($this->workingDirectory . $filename, $content, $context);
}

private function assertSameMergePlanKeys(array $expected, array $array): void
protected function removeEnvironmentFile(string $filename): void
{
$expectedKeys = array_keys($expected);
sort($expectedKeys);
$keys = array_keys($array);
sort($keys);
(new Filesystem())->unlink($this->workingDirectory . $filename);
}

self::assertSame($expectedKeys, $keys);
protected function getStdout(): string
{
return file_get_contents($this->stdoutFile);
}

private function initComposer(): void
protected function initComposer(array $config): void
{
$config = $this->getStartComposerConfig();
$root = $this->getRoot();

// Load yiisoft/config locally
$repositories = [
[
'type' => 'path',
'url' => $root,
],
];

// Load yiisoft/config dependencies locally
$packageConfig = $this->getComposerConfigStringAsArray(dirname(__DIR__, 2) . '/composer.lock');
$packageConfig = $this->getComposerJson(dirname(__DIR__, 2) . '/composer.lock');
foreach ($packageConfig['packages'] as $package) {
$config['repositories'][] = [
$repositories[] = [
'type' => 'path',
'url' => '../../vendor/' . $package['name'],
'url' => $root . '/vendor/' . $package['name'],
'options' => [
'versions' => [
$package['name'] => $package['version'],
Expand All @@ -89,7 +130,35 @@ private function initComposer(): void
];
}

file_put_contents($this->workingDirectory . '/composer.json', $this->getArrayAsComposerConfigString($config));
// Load test packages
foreach (self::TEST_PACKAGES as $package) {
$repositories[] = [
'type' => 'path',
'url' => $root . '/tests/Packages/' . $package,
'options' => [
'symlink' => false,
'__name' => $package,
],
];
}

$config = array_merge($config, [
'name' => 'yiisoft/test-package',
'type' => 'library',
'minimum-stability' => 'dev',
'repositories' => $repositories,
]);

$this->setComposerJson($config);
$this->execComposer('install');
}

private function setComposerJson(array $config): void
{
file_put_contents(
$this->workingDirectory . '/composer.json',
json_encode($config, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)
);
}

protected function execComposer(string $command): void
Expand All @@ -115,32 +184,40 @@ private function exec(string $command): void
}
}

protected function changeInstallationPackagePath(string $path, int $index = 1): void
protected function changeTestPackageDir(string $package, string $dir): void
{
$composerConfigPath = $this->workingDirectory . '/composer.json';
$config = $this->getComposerJson();
foreach ($config['repositories'] as $i => $data) {
if (($data['options']['__name'] ?? null) === $package) {
$config['repositories'][$i]['url'] = $this->getRoot() . '/tests/Packages/' . $dir;
break;
}
}

$composerArray = $this->getComposerConfigStringAsArray($composerConfigPath);
$composerArray['repositories'][$index]['url'] = '../Packages/' . $path;
file_put_contents($composerConfigPath, $this->getArrayAsComposerConfigString($composerArray));
$this->setComposerJson($config);
}

protected function getArrayAsComposerConfigString(array $array): string
private function getComposerJson(string $path = null): array
{
return json_encode($array, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
}
if ($path === null) {
$path = $this->workingDirectory . '/composer.json';
}

protected function getComposerConfigStringAsArray(string $composerConfigPath): array
{
return json_decode(file_get_contents($composerConfigPath), true);
return json_decode(file_get_contents($path), true);
}

protected function removeDirectory(string $directory): void
private function removeDirectory(string $directory): void
{
(new Filesystem())->removeDirectory($directory);
}

protected function ensureDirectoryExists(string $directory): void
private function ensureDirectoryExists(string $directory): void
{
(new Filesystem())->ensureDirectoryExists($directory);
}

private function getRoot(): string
{
return dirname(__DIR__, 2);
}
}
37 changes: 8 additions & 29 deletions tests/Integration/ForceCheckTest.php
Expand Up @@ -4,33 +4,15 @@

namespace Yiisoft\Config\Tests\Integration;

use function dirname;

final class ForceCheckTest extends ComposerTest
{
protected function getStartComposerConfig(): array
public function testBase(): void
{
return [
'name' => 'yiisoft/testpackage',
'type' => 'library',
'minimum-stability' => 'dev',
$this->initComposer([
'require' => [
'yiisoft/config' => '*',
'first-vendor/first-package' => '*',
],
'repositories' => [
[
'type' => 'path',
'url' => '../../',
],
[
'type' => 'path',
'url' => '../Packages/first-vendor/first-package',
'options' => [
'symlink' => false,
],
],
],
'extra' => [
'config-plugin-options' => [
'force-check' => true,
Expand All @@ -43,18 +25,15 @@ protected function getStartComposerConfig(): array
'web' => ['config/web.php'],
],
],
];
}
]);

public function testBase(): void
{
$fileDist = $this->workingDirectory . '/config/packages/first-vendor/first-package/config/dist/params.php';
$filePackage = dirname(__DIR__) . '/Packages/first-vendor/first-package/config/params.php';
$fileDist = '/config/packages/first-vendor/first-package/config/dist/params.php';
$filePackage = '/vendor/first-vendor/first-package/config/params.php';

file_put_contents($fileDist, '<?php return [];');
$this->assertFileNotEquals($filePackage, $fileDist);
$this->putEnvironmentFileContents($fileDist, '<?php return [];');
$this->assertEnvironmentFileNotEquals($filePackage, $fileDist);

$this->execComposer('du');
$this->assertFileEquals($filePackage, $fileDist);
$this->assertEnvironmentFileEquals($filePackage, $fileDist);
}
}
27 changes: 4 additions & 23 deletions tests/Integration/OutputDirectoryTest.php
Expand Up @@ -6,29 +6,13 @@

final class OutputDirectoryTest extends ComposerTest
{
protected function getStartComposerConfig(): array
public function testBase(): void
{
return [
'name' => 'yiisoft/testpackage',
'type' => 'library',
'minimum-stability' => 'dev',
$this->initComposer([
'require' => [
'yiisoft/config' => '*',
'first-vendor/first-package' => '*',
],
'repositories' => [
[
'type' => 'path',
'url' => '../../',
],
[
'type' => 'path',
'url' => '../Packages/first-vendor/first-package',
'options' => [
'symlink' => false,
],
],
],
'extra' => [
'config-plugin-options' => [
'output-directory' => 'custom-dir/packages',
Expand All @@ -41,11 +25,8 @@ protected function getStartComposerConfig(): array
'web' => ['config/web.php'],
],
],
];
}
]);

public function testBase(): void
{
$this->assertFileExists($this->workingDirectory . '/custom-dir/packages/merge_plan.php');
$this->assertEnvironmentFileExist('/custom-dir/packages/merge_plan.php');
}
}

0 comments on commit 8bc35f7

Please sign in to comment.