Skip to content

Commit

Permalink
Added more helpers.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexSkrypnyk committed May 28, 2024
1 parent adf6875 commit 154398e
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 23 deletions.
68 changes: 56 additions & 12 deletions CustomizeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,14 +228,14 @@ protected function cleanup(): void {
$json = $this->readComposerJson();

$is_dependency = (
!empty($json['require'])
&& is_array($json['require'])
&& isset($json['require']['alexskrypnyk/customizer'])
) || (
!empty($json['require-dev'])
&& is_array($json['require-dev'])
&& isset($json['require-dev']['alexskrypnyk/customizer'])
);
!empty($json['require'])
&& is_array($json['require'])
&& isset($json['require']['alexskrypnyk/customizer'])
) || (
!empty($json['require-dev'])
&& is_array($json['require-dev'])
&& isset($json['require-dev']['alexskrypnyk/customizer'])
);

static::arrayUnsetDeep($json, ['autoload', 'classmap'], basename(__FILE__), FALSE);
static::arrayUnsetDeep($json, ['scripts', 'customize']);
Expand Down Expand Up @@ -528,11 +528,16 @@ public function writeComposerJson(array $data): void {
* @param string $replace
* Replace string .
* @param bool $replace_line
* Replace for a whole line or only the occurrence.
* Replace a whole line or only the occurrence.
* @param array<int,string>|null $exclude
* Directories to exclude.
* Defaults to ['.git', '.idea', 'vendor', 'node_modules'].
*/
public static function replaceInPath(string $path, string $search, string $replace, bool $replace_line = FALSE): void {
public static function replaceInPath(string $path, string $search, string $replace, bool $replace_line = FALSE, ?array $exclude = NULL): void {
$dir = dirname($path);
$filename = basename($path);
$is_regex = @preg_match($search, '') !== FALSE;
$exclude = $exclude ?? ['.git', '.idea', 'vendor', 'node_modules'];

if (is_dir($path)) {
$dir = $path;
Expand All @@ -542,7 +547,7 @@ public static function replaceInPath(string $path, string $search, string $repla
$finder = Finder::create()
->ignoreVCS(TRUE)
->ignoreDotFiles(FALSE)
->exclude(['vendor'])
->exclude($exclude)
->files()
->contains($search)
->in($dir);
Expand All @@ -555,7 +560,11 @@ public static function replaceInPath(string $path, string $search, string $repla
$file_path = $file->getRealPath();
$file_content = file_get_contents($file_path);
if ($file_content !== FALSE) {
if ($replace_line) {

if ($is_regex) {
$new_content = preg_replace($search, $replace, $file_content);
}
elseif ($replace_line) {
$new_content = preg_replace(sprintf('/^.*%s.*/m', $search), $replace, $file_content);
}
else {
Expand All @@ -569,6 +578,41 @@ public static function replaceInPath(string $path, string $search, string $repla
}
}

/**
* Replace a string in a file or all files in a directory between two markers.
*
* @param string $path
* Directory or file path.
* @param string $search
* Search string.
* @param string $replace
* Replace string.
* @param string $start
* Start marker.
* @param string $end
* End marker.
*/
public static function replaceInPathBetween(string $path, string $search, string $replace, string $start, string $end): void {
$search = empty($search) ? '.*' : preg_quote($search, '/');
$replace = empty($replace) ? '$1' : preg_quote($replace, '/');

self::replaceInPath($path, '/(\W?)(' . preg_quote($start, '/') . '\W' . $search . '\W' . preg_quote($end, '/') . ')(\W)/s', $replace);
}

/**
* Uncomment a line in a file or all files in a directory.
*
* @param string $path
* Directory or file path.
* @param string $search
* Search string.
* @param string $comment_string
* Comment string.
*/
public static function uncommentLine(string $path, string $search, string $comment_string = '#'): void {
self::replaceInPath($path, '/^\s*#' . preg_quote(ltrim($search, $comment_string), '/') . '/', ltrim($search, $comment_string));

Check warning on line 613 in CustomizeCommand.php

View check run for this annotation

Codecov / codecov/patch

CustomizeCommand.php#L613

Added line #L613 was not covered by tests
}

/**
* Unset a value in a nested array by path, removing empty arrays.
*
Expand Down
4 changes: 4 additions & 0 deletions tests/phpunit/Unit/ArrayUnsetDeepTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@
namespace AlexSkrypnyk\Customizer\Tests\Functional;

use AlexSkrypnyk\Customizer\CustomizeCommand;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\TestCase;

/**
* Tests for the CustomizeCommand::arrayUnsetDeep() method.
*/
#[CoversClass(CustomizeCommand::class)]
#[Group('unit')]
class ArrayUnsetDeepTest extends TestCase {

#[DataProvider('dataProviderArrayUnsetDeep')]
Expand Down
104 changes: 93 additions & 11 deletions tests/phpunit/Unit/ReplaceInPathTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
use AlexSkrypnyk\Customizer\Tests\Traits\ReflectionTrait;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\TestStatus\Failure;

/**
* Test the scaffold create-project command with no-install.
*/
#[CoversClass(CustomizeCommand::class)]
#[Group('unit')]
class ReplaceInPathTest extends TestCase {

use DirsTrait;
Expand Down Expand Up @@ -67,15 +69,11 @@ public function hasFailed(): bool {
public function testReplaceInPath(string $path, array $before, string $search, string $replace, bool $replaceLine, array $after): void {
$this->createFileTree($this->dirs->sut, $before);

$this->callProtectedMethod(
CustomizeCommand::class,
'replaceInPath',
[
$this->dirs->sut . DIRECTORY_SEPARATOR . $path,
$search,
$replace,
$replaceLine,
]
CustomizeCommand::replaceInPath(
$this->dirs->sut . DIRECTORY_SEPARATOR . $path,
$search,
$replace,
$replaceLine,
);

$this->assertFileTree($this->dirs->sut, $after);
Expand Down Expand Up @@ -237,6 +235,90 @@ public static function dataProviderReplaceInPath(): array {
];
}

#[DataProvider('dataProviderReplaceInPathBetween')]
public function testReplaceInPathBetween(string $path, array $before, string $search, string $replace, string $start, string $end, array $after): void {
$this->createFileTree($this->dirs->sut, $before);

CustomizeCommand::replaceInPathBetween(
$this->dirs->sut . DIRECTORY_SEPARATOR . $path,
$search,
$replace,
$start,
$end
);

$this->assertFileTree($this->dirs->sut, $after);
}

/**
* Data provider for testReplaceInPath.
*
* @return array
* The data.
*
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public static function dataProviderReplaceInPathBetween(): array {
return [
// Single file, only word, using file, substring.
[
'file1.txt',
[
'file1.txt' => "First line of the first file\nUnique string\nstart non-unique end string",
],
'non-unique',
'',
'start',
'end',
[
'file1.txt' => "First line of the first file\nUnique string\nstring",
],
],

// Single file, only word, using file, multiline.
[
'.',
[
'dir1' => [
'file1.txt' => "First line of the first file\n#;<\nUnique string inside\nnon-unique\n#;>\nUnique string outside\nnon-unique string",
],
],
'',
'',
'#;<',
'#;>',
[
'dir1' => [
'file1.txt' => "First line of the first file\nUnique string outside\nnon-unique string",
],
],
],

// Single file, only word, using file, multiline, marker.
[
'.',
[
'dir1' => [
'file1.txt' => "First line of the first file\n#;<MARKER\nUnique string inside\nnon-unique\n#;>MARKER\nUnique string outside\nnon-unique string",
'file2.txt' => "#;<MARKER\nUnique string inside\nnon-unique\n#;>MARKER\nUnique string outside\nnon-unique string",
'file3.txt' => "First line of the first file\n#;<MARKER2\nUnique string inside\nnon-unique\n#;>MARKER2\nUnique string outside\nnon-unique string",
],
],
'',
'',
'#;<MARKER',
'#;>MARKER',
[
'dir1' => [
'file1.txt' => "First line of the first file\nUnique string outside\nnon-unique string",
'file2.txt' => "Unique string outside\nnon-unique string",
'file3.txt' => "First line of the first file\n#;<MARKER2\nUnique string inside\nnon-unique\n#;>MARKER2\nUnique string outside\nnon-unique string",
],
],
],
];
}

/**
* Creates a file tree from the provided structure.
*
Expand All @@ -245,7 +327,7 @@ public static function dataProviderReplaceInPath(): array {
* @param array $structure
* The structure of the file tree.
*/
private function createFileTree(string $dir, array $structure): void {
protected function createFileTree(string $dir, array $structure): void {
foreach ($structure as $name => $content) {
$path = $dir . DIRECTORY_SEPARATOR . $name;
if (is_array($content)) {
Expand All @@ -266,7 +348,7 @@ private function createFileTree(string $dir, array $structure): void {
* @param array $structure
* The expected structure of the file tree.
*/
private function assertFileTree(string $dir, array $structure): void {
protected function assertFileTree(string $dir, array $structure): void {
foreach ($structure as $name => $content) {
$path = $dir . DIRECTORY_SEPARATOR . $name;
if (is_array($content)) {
Expand Down

0 comments on commit 154398e

Please sign in to comment.