@@ -233,9 +233,55 @@ public function testApplyPatch(array $filesCurrentlyInApp, RecipePatch $recipePa
233
233
$ this ->assertSame ($ expectedConflicts , $ hadConflicts );
234
234
}
235
235
236
- public function getApplyPatchTests (): iterable
236
+ /**
237
+ * @dataProvider getApplyPatchTests
238
+ */
239
+ public function testApplyPatchOnSubfolder (array $ filesCurrentlyInApp , RecipePatch $ recipePatch , array $ expectedFiles , bool $ expectedConflicts )
237
240
{
238
- $ files = $ this ->getFilesForPatching ();
241
+ $ mainProjectPath = FLEX_TEST_DIR ;
242
+ $ subProjectPath = FLEX_TEST_DIR .'/ProjectA ' ;
243
+
244
+ ## init main project git repo
245
+ (new Process (['git ' , 'init ' ], $ mainProjectPath ))->mustRun ();
246
+ (new Process (['git ' , 'config ' , 'user.name ' , 'Unit test ' ], $ mainProjectPath ))->mustRun ();
247
+ (new Process (['git ' , 'config ' , 'user.email ' , '' ], $ mainProjectPath ))->mustRun ();
248
+
249
+ if (!file_exists ($ subProjectPath )) {
250
+ mkdir ($ subProjectPath , 0777 , true );
251
+ }
252
+
253
+ foreach ($ filesCurrentlyInApp as $ file => $ contents ) {
254
+ $ path = $ subProjectPath .'/ ' .$ file ;
255
+ if (!file_exists (\dirname ($ path ))) {
256
+ @mkdir (\dirname ($ path ), 0777 , true );
257
+ }
258
+ file_put_contents ($ path , $ contents );
259
+ }
260
+ if (\count ($ filesCurrentlyInApp ) > 0 ) {
261
+ (new Process (['git ' , 'add ' , '-A ' ], $ subProjectPath ))->mustRun ();
262
+ (new Process (['git ' , 'commit ' , '-m ' , 'Committing original files ' ], $ subProjectPath ))->mustRun ();
263
+ }
264
+
265
+ $ patcher = new RecipePatcher ($ subProjectPath , $ this ->createMock (IOInterface::class));
266
+ $ hadConflicts = !$ patcher ->applyPatch ($ recipePatch );
267
+
268
+ foreach ($ expectedFiles as $ file => $ expectedContents ) {
269
+ if (null === $ expectedContents ) {
270
+ $ this ->assertFileDoesNotExist ($ subProjectPath .'/ ' .$ file );
271
+
272
+ continue ;
273
+ }
274
+ $ this ->assertFileExists ($ subProjectPath .'/ ' .$ file );
275
+ $ this ->assertSame ($ expectedContents , file_get_contents ($ subProjectPath .'/ ' .$ file ));
276
+ }
277
+
278
+ $ this ->assertSame ($ expectedConflicts , $ hadConflicts );
279
+ }
280
+
281
+ public function getApplyPatchTests (string $ testMethodName ): iterable
282
+ {
283
+ $ projectRootPath = ($ testMethodName === "testApplyPatchOnSubfolder " ) ? "ProjectA/ " : "" ;
284
+ $ files = $ this ->getFilesForPatching ($ projectRootPath );
239
285
$ dotEnvClean = $ files ['dot_env_clean ' ];
240
286
$ packageJsonConflict = $ files ['package_json_conflict ' ];
241
287
$ webpackEncoreAdded = $ files ['webpack_encore_added ' ];
@@ -393,7 +439,7 @@ public function getIntegrationTests(): iterable
393
439
* * original_recipe
394
440
* * updated_recipe.
395
441
*/
396
- private function getFilesForPatching (): array
442
+ private function getFilesForPatching (string $ projectPath = "" ): array
397
443
{
398
444
$ files = [
399
445
// .env
@@ -538,7 +584,7 @@ private function getFilesForPatching(): array
538
584
foreach ($ files as $ key => $ data ) {
539
585
$ files [$ key ] = array_merge (
540
586
$ data ,
541
- $ this ->generatePatchData ($ data ['filename ' ], $ data ['original_recipe ' ], $ data ['updated_recipe ' ])
587
+ $ this ->generatePatchData ($ projectPath . $ data ['filename ' ], $ data ['original_recipe ' ], $ data ['updated_recipe ' ])
542
588
);
543
589
}
544
590
0 commit comments