Skip to content

Commit a51605a

Browse files
committed
Update phpunit test.
1 parent 8d6b62b commit a51605a

File tree

3 files changed

+93
-11
lines changed

3 files changed

+93
-11
lines changed

.scaffold/Customizer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ public static function convertString(string $string, string $type = 'file_name')
420420
* @param string $directory
421421
* Directory.
422422
*/
423-
public static function replaceStringInFilesInDirectory($string_search, $string_replace, string $directory): void {
423+
public static function replaceStringInFilesInDirectory(string|array $string_search, string|array $string_replace, string $directory): void {
424424
$finder = new Finder();
425425
$finder
426426
->files()
@@ -443,7 +443,7 @@ public static function replaceStringInFilesInDirectory($string_search, $string_r
443443
* @param string $file_path
444444
* File path.
445445
*/
446-
public static function replaceStringInFile($string_search, $string_replace, string $file_path): void {
446+
public static function replaceStringInFile(string|array $string_search, string|array $string_replace, string $file_path): void {
447447
$file_content = file_get_contents($file_path);
448448
if (!empty($file_content)) {
449449
$new_file_content = str_replace($string_search, $string_replace, $file_content);

.scaffold/phpcs.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<exclude name="PSR2.Classes.ClassDeclaration.CloseBraceAfterBody"/>
1717
<exclude name="PSR2.Classes.ClassDeclaration.OpenBraceNewLine"/>
1818
<exclude name="PSR12.Classes.OpeningBraceSpace.Found"/>
19+
<exclude name="Generic.PHP.LowerCaseConstant.Found" />
1920
</rule>
2021

2122
<!-- Show sniff codes in all reports -->
@@ -31,4 +32,5 @@
3132
@see https://github.com/squizlabs/PHP_CodeSniffer/issues/2916
3233
-->
3334
<file>Customizer.php</file>
35+
<file>tests/phpunit</file>
3436
</ruleset>

.scaffold/tests/phpunit/Unit/CustomizerTest.php

Lines changed: 89 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,25 @@
88
use Scaffold\Customizer;
99
use PHPUnit\Framework\Attributes\DataProvider;
1010
use PHPUnit\Framework\Attributes\CoversClass;
11+
use Symfony\Component\Filesystem\Filesystem;
1112

1213
/**
1314
* Customizer unit test.
1415
*/
1516
#[CoversClass(Customizer::class)]
1617
class CustomizerTest extends TestCase {
1718

19+
/**
20+
* File system.
21+
*/
22+
protected Filesystem $filesystem;
23+
24+
protected function setUp(): void {
25+
parent::setUp();
26+
27+
$this->filesystem = new Filesystem();
28+
}
29+
1830
/**
1931
* Test conver string.
2032
*
@@ -41,7 +53,7 @@ public function testConvertString(string $string_input, string $convert_type, st
4153
/**
4254
* Data provider for convert string test.
4355
*/
44-
public static function convertStringProvider() {
56+
public static function convertStringProvider(): array {
4557
return [
4658
'test convert file_name' => ['This is-File_name TEST', 'file_name', 'this_is-file_name_test', TRUE],
4759
'test convert package_namespace' => ['This_is-Package_NAMESPACE TEST', 'package_namespace', 'this_is_package_namespace_test', TRUE],
@@ -64,26 +76,94 @@ public static function convertStringProvider() {
6476
* Expected string after searching & replacment.
6577
*/
6678
#[DataProvider('replaceStringInFileProvider')]
67-
public function testReplaceStringInFile(string $string, $string_search, $string_replace, string $string_expected): void {
79+
public function testReplaceStringInFile(string $string, string|array $string_search, string|array $string_replace, string $string_expected): void {
6880
$file_path = tempnam(sys_get_temp_dir(), 'test-replace-string-');
69-
if ($file_path) {
70-
file_put_contents($file_path, $string);
71-
Customizer::replaceStringInFile($string_search, $string_replace, $file_path);
72-
$file_content = file_get_contents($file_path);
73-
$this->assertEquals($string_expected, $file_content);
74-
unlink($file_path);
81+
if (!$file_path) {
82+
throw new \Exception('Could not create test file: ' . $file_path);
7583
}
84+
$this->filesystem->dumpFile($file_path, $string);
85+
$file_content = file_get_contents($file_path);
86+
$this->assertEquals($string, $file_content);
87+
Customizer::replaceStringInFile($string_search, $string_replace, $file_path);
88+
$file_content = file_get_contents($file_path);
89+
$this->assertEquals($string_expected, $file_content);
90+
$this->filesystem->remove($file_path);
7691
}
7792

7893
/**
7994
* Data provider for test replace string in a file.
8095
*/
81-
public static function replaceStringInFileProvider() {
96+
public static function replaceStringInFileProvider(): array {
8297
return [
8398
['this text contains your-namespace-package', 'your-namespace-package', 'foo-package', 'this text contains foo-package'],
8499
['this text contains your-namespace-package', ['your-namespace-package'], ['foo-package'], 'this text contains foo-package'],
85100
['this text contains your-namespace-package', ['your-namespace'], ['foo-package'], 'this text contains foo-package-package'],
86101
['this text contains your-namespace-package', ['foo-your-namespace'], ['foo-package'], 'this text contains your-namespace-package']
87102
];
88103
}
104+
105+
/**
106+
* Test replace string in dir.
107+
*
108+
* @param string|string[] $string_search
109+
* String to search.
110+
* @param string|string[] $string_replace
111+
* String to replace.
112+
* @param string $directory
113+
* Directory to search.
114+
* @param array<mixed> $files
115+
* Files in above dir.
116+
*/
117+
#[DataProvider('replaceStringInFilesInDirectoryProvider')]
118+
public function testReplaceStringInFilesInDirectory(string|array $string_search, string|array $string_replace, string $directory, array $files): void {
119+
$dir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . $directory;
120+
121+
foreach ($files as $file) {
122+
$file_path = $dir . DIRECTORY_SEPARATOR . $file['path'];
123+
$this->filesystem->dumpFile($file_path, $file['content']);
124+
$file_content = file_get_contents($file_path);
125+
$this->assertEquals($file['content'], $file_content);
126+
}
127+
128+
Customizer::replaceStringInFilesInDirectory($string_search, $string_replace, $dir);
129+
130+
foreach ($files as $file) {
131+
$file_path = $dir . DIRECTORY_SEPARATOR . $file['path'];
132+
$file_content = file_get_contents($file_path);
133+
$this->assertEquals($file['expected_content'], $file_content);
134+
}
135+
136+
$this->filesystem->remove($dir);
137+
}
138+
139+
/**
140+
* Data provider for test replace string in dir.
141+
*/
142+
public static function replaceStringInFilesInDirectoryProvider(): array {
143+
return [
144+
[
145+
'search-string',
146+
'replace-string',
147+
'dir-1',
148+
[
149+
[
150+
'path' => 'foo/file-1.txt',
151+
'content' => 'Foo file 1 search-string content',
152+
'expected_content' => 'Foo file 1 replace-string content'
153+
],
154+
[
155+
'path' => 'foo/file-2.txt',
156+
'content' => 'Foo file 2 search-string content',
157+
'expected_content' => 'Foo file 2 replace-string content'
158+
],
159+
[
160+
'path' => 'foo/bar/file-1.txt',
161+
'content' => 'Foo/Bar file 1 content',
162+
'expected_content' => 'Foo/Bar file 1 content',
163+
],
164+
]
165+
],
166+
];
167+
}
168+
89169
}

0 commit comments

Comments
 (0)