Skip to content

Commit

Permalink
test: Introduce common way of creating fake Composer project in `Inst…
Browse files Browse the repository at this point in the history
…allViaComposerTest` (#7265)
  • Loading branch information
Wirone committed Sep 7, 2023
1 parent fe9788b commit e718e08
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 40 deletions.
2 changes: 1 addition & 1 deletion tests/AutoReview/ProjectCodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ public function testThatTestClassesPublicMethodsAreCorrectlyNamed(string $testCl

foreach ($publicMethods as $method) {
self::assertMatchesRegularExpression(
'/^(test|expect|provide|setUpBeforeClass$|tearDownAfterClass$)/',
'/^(test|expect|provide|setUpBeforeClass$|tearDownAfterClass$|__construct$)/',
$method->getName(),
sprintf('Public method "%s::%s" is not properly named.', $reflectionClass->getName(), $method->getName())
);
Expand Down
104 changes: 65 additions & 39 deletions tests/Smoke/InstallViaComposerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

/**
* @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
* @author Greg Korba <greg@codito.dev>
*
* @internal
*
Expand All @@ -31,6 +32,24 @@
*/
final class InstallViaComposerTest extends AbstractSmokeTestCase
{
private Filesystem $fs;

/** @var array<string, mixed> */
private array $currentCodeAsComposerDependency = [
'repositories' => [
[
'type' => 'path',
'url' => __DIR__.'/../..',
'options' => [
'symlink' => false,
],
],
],
'require' => [
'friendsofphp/php-cs-fixer' => '*@dev',
],
];

/**
* @var string[]
*/
Expand All @@ -45,6 +64,13 @@ final class InstallViaComposerTest extends AbstractSmokeTestCase
'vendor/bin/php-cs-fixer fix --help',
];

public function __construct()
{
$this->fs = new Filesystem();

parent::__construct();
}

public static function setUpBeforeClass(): void
{
parent::setUpBeforeClass();
Expand Down Expand Up @@ -74,32 +100,11 @@ public static function setUpBeforeClass(): void

public function testInstallationViaPathIsPossible(): void
{
$fs = new Filesystem();

$tmpPath = tempnam(sys_get_temp_dir(), 'cs_fixer_tmp_');
unlink($tmpPath);
$fs->mkdir($tmpPath);

$initialComposerFileState = [
'repositories' => [
[
'type' => 'path',
'url' => __DIR__.'/../..',
],
],
'require' => [
'friendsofphp/php-cs-fixer' => '*@dev',
],
];

file_put_contents(
$tmpPath.'/composer.json',
json_encode($initialComposerFileState, JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT)
);
$tmpPath = $this->createFakeComposerProject($this->currentCodeAsComposerDependency);

self::assertCommandsWork($this->stepsToVerifyInstallation, $tmpPath);

$fs->remove($tmpPath);
$this->fs->remove($tmpPath);
}

// test that respects `export-ignore` from `.gitattributes` file
Expand All @@ -110,19 +115,13 @@ public function testInstallationViaArtifactIsPossible(): void
self::markTestSkippedOrFail('No zip extension available.');
}

$fs = new Filesystem();

$tmpPath = tempnam(sys_get_temp_dir(), 'cs_fixer_tmp_');
unlink($tmpPath);
$fs->mkdir($tmpPath);

$tmpArtifactPath = tempnam(sys_get_temp_dir(), 'cs_fixer_tmp_');
unlink($tmpArtifactPath);
$fs->mkdir($tmpArtifactPath);
$this->fs->mkdir($tmpArtifactPath);

$fakeVersion = preg_replace('/\\-.+/', '', Application::VERSION, 1).'-alpha987654321';

$initialComposerFileState = [
$tmpPath = $this->createFakeComposerProject([
'repositories' => [
[
'type' => 'artifact',
Expand All @@ -132,12 +131,7 @@ public function testInstallationViaArtifactIsPossible(): void
'require' => [
'friendsofphp/php-cs-fixer' => $fakeVersion,
],
];

file_put_contents(
$tmpPath.'/composer.json',
json_encode($initialComposerFileState, JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT)
);
]);

$cwd = __DIR__.'/../..';

Expand All @@ -164,8 +158,8 @@ public function testInstallationViaArtifactIsPossible(): void
self::assertCommandsWork($stepsToPrepareArtifact, $tmpArtifactPath);
self::assertCommandsWork($this->stepsToVerifyInstallation, $tmpPath);

$fs->remove($tmpPath);
$fs->remove($tmpArtifactPath);
$this->fs->remove($tmpPath);
$this->fs->remove($tmpArtifactPath);
}

/**
Expand All @@ -177,4 +171,36 @@ private static function assertCommandsWork(array $commands, string $cwd): void
self::assertSame(0, CommandExecutor::create($command, $cwd)->getResult()->getCode());
}
}

/**
* @param array<string, mixed> $initialComposerFileState
*
* @return string Path to temporary directory containing Composer project
*/
private function createFakeComposerProject(array $initialComposerFileState): string
{
$tmpPath = tempnam(sys_get_temp_dir(), 'cs_fixer_tmp_');

if (false === $tmpPath) {
throw new \RuntimeException('Creating directory for fake Composer project has failed.');
}

unlink($tmpPath);
$this->fs->mkdir($tmpPath);

try {
file_put_contents(
$tmpPath.'/composer.json',
json_encode($initialComposerFileState, JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT)
);
} catch (\JsonException $e) {
throw new \InvalidArgumentException(
'Initial Composer file state could not be saved as composer.json',
$e->getCode(),
$e
);
}

return $tmpPath;
}
}

0 comments on commit e718e08

Please sign in to comment.