Skip to content

Commit

Permalink
Added tests for JSON files.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexSkrypnyk committed May 28, 2024
1 parent 7adf7ab commit 9cb338b
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,8 @@ passed to the processing callbacks:
- `writeComposerJson()` - Write the contents of the array to the `composer.json`
file.
- `replaceInPath()` - Replace a string in a file or all files in a directory.
- `replaceInPathBetween()` - Replace a string in a file or all files in a directory between two markers.
- `uncommentLine()` - Uncomment a line in a file or all files in a directory.
- `arrayUnsetDeep()` - Unset a fully or partially matched value in a nested
array, removing empty arrays.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
use PHPUnit\Framework\TestStatus\Failure;

/**
* Test the scaffold create-project command with no-install.
* Test for helpers used in operations on files.
*/
#[CoversClass(CustomizeCommand::class)]
#[Group('unit')]
class ReplaceInPathTest extends TestCase {
class FilesTest extends TestCase {

use DirsTrait;
use ReflectionTrait;
Expand Down Expand Up @@ -374,6 +374,60 @@ public static function dataProviderUncommentLine(): array {
];
}

#[DataProvider('dataProviderReadComposerJson')]
public function testReadComposerJson(string $before, string $after, ?string $exception_message = NULL): void {
if (!empty($before)) {
$this->createFileTree($this->dirs->sut, ['composer.json' => $before]);
}

if ($exception_message) {
$this->expectExceptionMessage(\RuntimeException::class);
$this->expectExceptionMessage($exception_message);
}

CustomizeCommand::readComposerJson($this->dirs->sut . '/composer.json');

if (!$exception_message) {
$this->assertFileTree($this->dirs->sut, ['composer.json' => $after]);
}
}

/**
* Data provider for testUncommentLine.
*
* @return array
* The data.
*/
public static function dataProviderReadComposerJson(): array {
return [
[
'{}',
'{}',
],
[
'',
'',
'Failed to read composer.json',
],
[
'{',
'{',
'Failed to decode composer.json',
],
];
}

public function testWriteComposerJson(): void {
CustomizeCommand::writeComposerJson($this->dirs->sut . '/composer.json', [1, 2, 3]);
$this->assertFileTree($this->dirs->sut, [
'composer.json' => "[
1,
2,
3
]",
]);
}

/**
* Creates a file tree from the provided structure.
*
Expand Down

0 comments on commit 9cb338b

Please sign in to comment.