8
8
use Scaffold \Customizer ;
9
9
use PHPUnit \Framework \Attributes \DataProvider ;
10
10
use PHPUnit \Framework \Attributes \CoversClass ;
11
+ use Symfony \Component \Filesystem \Filesystem ;
11
12
12
13
/**
13
14
* Customizer unit test.
14
15
*/
15
16
#[CoversClass(Customizer::class)]
16
17
class CustomizerTest extends TestCase {
17
18
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
+
18
30
/**
19
31
* Test conver string.
20
32
*
@@ -41,7 +53,7 @@ public function testConvertString(string $string_input, string $convert_type, st
41
53
/**
42
54
* Data provider for convert string test.
43
55
*/
44
- public static function convertStringProvider () {
56
+ public static function convertStringProvider (): array {
45
57
return [
46
58
'test convert file_name ' => ['This is-File_name TEST ' , 'file_name ' , 'this_is-file_name_test ' , TRUE ],
47
59
'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() {
64
76
* Expected string after searching & replacment.
65
77
*/
66
78
#[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 {
68
80
$ 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 );
75
83
}
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 );
76
91
}
77
92
78
93
/**
79
94
* Data provider for test replace string in a file.
80
95
*/
81
- public static function replaceStringInFileProvider () {
96
+ public static function replaceStringInFileProvider (): array {
82
97
return [
83
98
['this text contains your-namespace-package ' , 'your-namespace-package ' , 'foo-package ' , 'this text contains foo-package ' ],
84
99
['this text contains your-namespace-package ' , ['your-namespace-package ' ], ['foo-package ' ], 'this text contains foo-package ' ],
85
100
['this text contains your-namespace-package ' , ['your-namespace ' ], ['foo-package ' ], 'this text contains foo-package-package ' ],
86
101
['this text contains your-namespace-package ' , ['foo-your-namespace ' ], ['foo-package ' ], 'this text contains your-namespace-package ' ]
87
102
];
88
103
}
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
+
89
169
}
0 commit comments