Skip to content

Commit

Permalink
Fixed tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexSkrypnyk committed Apr 11, 2024
1 parent a3fab52 commit b85fa69
Show file tree
Hide file tree
Showing 15 changed files with 72 additions and 309 deletions.
7 changes: 3 additions & 4 deletions scaffold/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,13 @@
"DrevOps\\composer\\ScaffoldGeneralizer::generalizeAndRemoveItselfAfterProjectCreate"
],
"lint": [
"cp php-script php-script.php && phpcs && rm php-script.php",
"phpmd --exclude vendor,vendor-bin,node_modules . text phpmd.xml",
"phpcs",
"phpstan",
"rector --clear-cache --dry-run"
],
"lint-fix": [
"rector --clear-cache",
"cp php-script php-script.php && phpcbf && rm php-script.php"
"phpcbf",
"rector --clear-cache"
],
"test": "if [ \"${XDEBUG_MODE}\" = 'coverage' ]; then phpunit; else phpunit --no-coverage; fi"
}
Expand Down
5 changes: 4 additions & 1 deletion scaffold/phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
<description>Custom PHPCS standard.</description>

<!-- Coding standard. -->
<rule ref="Drupal"/>
<rule ref="Drupal">
<exclude name="Drupal.Commenting.DocComment.Empty"/>
<exclude name="Drupal.Commenting.DocComment.MissingShort"/>
</rule>

<!-- Show sniff codes in all reports -->
<arg value="s"/>
Expand Down
8 changes: 4 additions & 4 deletions scaffold/scripts/composer/ScaffoldGeneralizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,12 @@ public static function generalizeAndRemoveItselfAfterProjectCreate(Event $event)
// Remove references to this script.
// Remove event script used to invoke this script during the project
// creation.
unset($json['scripts']['post-root-package-install'][array_search(__METHOD__, $json['scripts']['post-root-package-install'])]);
unset($json['scripts']['post-root-package-install'][array_search(__METHOD__, $json['scripts']['post-root-package-install'], true)]);
if (empty($json['scripts']['post-root-package-install'])) {
unset($json['scripts']['post-root-package-install']);
}
// Remove the classmap for this script from the autoload section.
unset($json['autoload']['classmap'][array_search('scripts/composer/ScaffoldGeneralizer.php', $json['autoload']['classmap'])]);
unset($json['autoload']['classmap'][array_search('scripts/composer/ScaffoldGeneralizer.php', $json['autoload']['classmap'], true)]);
$json['autoload']['classmap'] = array_values($json['autoload']['classmap']);
// Remove the script file.
if (!$is_install) {
Expand Down Expand Up @@ -195,7 +195,7 @@ public static function postCreateProjectCmd(Event $event): void {
$fs->unlink(getcwd() . '/scripts/composer/ScaffoldGeneralizer.php');
}

protected static function isInstall(Event $event) {
protected static function isInstall(Event $event): bool {
$io = $event->getIO();

$reflectionIo = new \ReflectionObject($io);
Expand Down Expand Up @@ -225,7 +225,7 @@ protected static function getVersion(): string {
*/
protected static function arrayUpsert(&$array, $after, $key, $value): void {
if (array_key_exists($after, $array)) {
$position = array_search($after, array_keys($array)) + 1;
$position = array_search($after, array_keys($array), true) + 1;
$array = array_slice($array, 0, $position, TRUE)
+ [$key => $value]
+ array_slice($array, $position, NULL, TRUE);
Expand Down
6 changes: 3 additions & 3 deletions scaffold/scripts/composer/ScaffoldScriptHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ public static function preAutoloadDump(Event $event): void {
foreach ($packages as $package) {
if ($package->getName() === 'drevops/scaffold') {
$autoload = $package->getAutoload();
unset($autoload['classmap'][array_search('scripts/composer/ScaffoldScriptHandler.php', $autoload['classmap'])]);
unset($autoload['classmap'][array_search('scripts/composer/ScaffoldGeneralizer.php', $autoload['classmap'])]);
unset($autoload['classmap'][array_search('scripts/composer/ScriptHandler.php', $autoload['classmap'])]);
unset($autoload['classmap'][array_search('scripts/composer/ScaffoldScriptHandler.php', $autoload['classmap'], true)]);
unset($autoload['classmap'][array_search('scripts/composer/ScaffoldGeneralizer.php', $autoload['classmap'], true)]);
unset($autoload['classmap'][array_search('scripts/composer/ScriptHandler.php', $autoload['classmap'], true)]);
$package->setAutoload($autoload);
$event->getIO()->debug('Removed classmap for ' . $package->getName());
break;
Expand Down
6 changes: 3 additions & 3 deletions scaffold/scripts/composer/ScriptHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

class ScriptHandler {

public static function createRequiredFiles(Event $event) {
public static function createRequiredFiles(Event $event): void {
// Noop
}

Expand All @@ -38,15 +38,15 @@ public static function createRequiredFiles(Event $event) {
*
* @see https://github.com/composer/composer/pull/5035
*/
public static function checkComposerVersion(Event $event) {
public static function checkComposerVersion(Event $event): void {
$composer = $event->getComposer();
$io = $event->getIO();

$version = $composer::VERSION;

// The dev-channel of composer uses the git revision as version number,
// try to the branch alias instead.
if (preg_match('/^[0-9a-f]{40}$/i', $version)) {
if (preg_match('/^[0-9a-f]{40}$/i', (string) $version)) {
$version = $composer::BRANCH_ALIAS_VERSION;
}

Expand Down
25 changes: 13 additions & 12 deletions scaffold/tests/phpunit/Dirs.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
use DrevOps\Scaffold\Tests\Traits\FileTrait;
use Symfony\Component\Filesystem\Filesystem;

/**
*
*/
class Dirs {

use FileTrait;

/**
* Directory where a copy of the DrevOps Scaffold (this) repository is located.
* Directory where a copy of the DrevOps Scaffold repository is located.
*
* This allows to isolate the test from this repository files and prevent
* their accidental removal.
Expand Down Expand Up @@ -38,34 +41,32 @@ class Dirs {

/**
* The file system.
*
* @var \Symfony\Component\Filesystem\Filesystem
*/
protected $fs;
protected Filesystem $fs;

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

public function initLocations() {
public function initLocations(): void {
$this->build = rtrim(sys_get_temp_dir(), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . 'drevops-scaffold-' . microtime(TRUE);
$this->sut = "$this->build/sut";
$this->repo = "$this->build/local_repo";
$this->sut = $this->build . '/sut';
$this->repo = $this->build . '/local_repo';

$this->fs->mkdir($this->build);

$this->prepareLocalRepo();
}

public function deleteLocations() {
public function deleteLocations(): void {
$this->fs->remove($this->build);
}

public function printInfo() {
public function printInfo(): void {
$lines[] = '-- LOCATIONS --';
$lines[] = "Build : {$this->build}";
$lines[] = "SUT : {$this->sut}";
$lines[] = "Local repo : {$this->repo}";
$lines[] = 'Build : ' . $this->build;
$lines[] = 'SUT : ' . $this->sut;
$lines[] = 'Local repo : ' . $this->repo;

fwrite(STDERR, PHP_EOL . implode(PHP_EOL, $lines) . PHP_EOL);
}
Expand Down
151 changes: 0 additions & 151 deletions scaffold/tests/phpunit/Functional/ComposerHookTest.php

This file was deleted.

14 changes: 9 additions & 5 deletions scaffold/tests/phpunit/Functional/ScaffoldCreateProjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,37 @@
namespace DrevOps\Scaffold\Tests\Functional;

use DrevOps\composer\ScaffoldGeneralizer;
use DrevOps\composer\ScaffoldScriptHandler;

class ScaffoldCreateProjectTest extends ScaffoldTest {
/**
*
*/
class ScaffoldCreateProjectTest extends ScaffoldTestCase {

public function testCreateProjectNoInstall() {
public function testCreateProjectNoInstall(): void {
$output = $this->composerRun($this->composerCreateProject('--no-install'));

$this->assertStringContainsString('Initialised project from DrevOps Scaffold', $output);
$this->assertStringContainsString('Run `composer install` to further customise the project', $output);

$this->assertJsonValueEquals($this->composerReadJson(), '$.name', ScaffoldGeneralizer::PROJECT_NAME);
$this->assertJsonValueEquals($this->composerReadJson(), '$.require-dev."drevops/scaffold"', "@dev");
$this->assertJsonValueEquals($this->composerReadJson(), '$.scripts."pre-update-cmd"[1]', 'DrevOps\\composer\\ScaffoldScriptHandler::preUpdateCmd');
$this->assertJsonValueEquals($this->composerReadJson(), '$.scripts."pre-update-cmd"[1]', ScaffoldScriptHandler::class . '::preUpdateCmd');
$this->assertJsonHasNoKey($this->composerReadJson(), '$.scripts."post-root-package-install"[1]');
$this->assertJsonValueEquals($this->composerReadJson(), '$.extra."drupal-scaffold"."allowed-packages"[0]', ScaffoldGeneralizer::DREVOPS_SCAFFOLD_NAME);
$this->assertJsonValueEquals($this->composerReadJson(), '$.autoload.classmap[0]', 'scripts/composer/ScaffoldScriptHandler.php');
$this->assertJsonValueEquals($this->composerReadJson(), '$.autoload.classmap[1]', 'scripts/composer/ScriptHandler.php');
}

public function testCreateProjectInstall() {
public function testCreateProjectInstall(): void {
$output = $this->composerRun($this->composerCreateProject());

$this->assertStringContainsString('Initialised project from DrevOps Scaffold', $output);
$this->assertStringNotContainsString('Run `composer install` to further customise the project', $output);

$this->assertJsonValueEquals($this->composerReadJson(), '$.name', ScaffoldGeneralizer::PROJECT_NAME);
$this->assertJsonValueEquals($this->composerReadJson(), '$.require-dev."drevops/scaffold"', "@dev");
$this->assertJsonValueEquals($this->composerReadJson(), '$.scripts."pre-update-cmd"[1]', 'DrevOps\\composer\\ScaffoldScriptHandler::preUpdateCmd');
$this->assertJsonValueEquals($this->composerReadJson(), '$.scripts."pre-update-cmd"[1]', ScaffoldScriptHandler::class . '::preUpdateCmd');
$this->assertJsonHasNoKey($this->composerReadJson(), '$.scripts."post-root-package-install"[1]');
$this->assertJsonValueEquals($this->composerReadJson(), '$.extra."drupal-scaffold"."allowed-packages"[0]', ScaffoldGeneralizer::DREVOPS_SCAFFOLD_NAME);
$this->assertJsonValueEquals($this->composerReadJson(), '$.autoload.classmap[0]', 'scripts/composer/ScaffoldScriptHandler.php');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
use PHPUnit\Framework\TestStatus\Failure;
use Symfony\Component\Filesystem\Filesystem;

class ScaffoldTest extends TestCase {
/**
*
*/
class ScaffoldTestCase extends TestCase {

use CmdTrait;
use ComposerTrait;
Expand Down Expand Up @@ -59,7 +62,8 @@ protected function tearDown(): void {
protected function onNotSuccessfulTest(\Throwable $t): never {
$this->dirs->printInfo();

parent::onNotSuccessfulTest($t); // Rethrow the exception to allow the test to fail normally.
// Rethrow the exception to allow the test to fail normally.
parent::onNotSuccessfulTest($t);
}

public function hasFailed(): bool {
Expand Down
5 changes: 4 additions & 1 deletion scaffold/tests/phpunit/Traits/CmdTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

use Symfony\Component\Process\Process;

/**
*
*/
trait CmdTrait {

/**
Expand All @@ -21,7 +24,7 @@ trait CmdTrait {
* @return string
* Standard output from the command
*/
protected function cmdRun($cmd, $cwd, array $env = []) {
protected function cmdRun($cmd, $cwd, array $env = []): string {
$env += $env + ['PATH' => getenv('PATH'), 'HOME' => getenv('HOME')];

$process = Process::fromShellCommandline($cmd, $cwd, $env);
Expand Down
Loading

0 comments on commit b85fa69

Please sign in to comment.