1515use PHPUnit \Framework \TestCase ;
1616use Symfony \Component \Filesystem \Filesystem ;
1717use Symfony \Component \Process \Process ;
18+ use Symfony \Flex \Lock ;
1819use Symfony \Flex \Update \RecipePatch ;
1920use Symfony \Flex \Update \RecipePatcher ;
2021
@@ -52,7 +53,7 @@ public function testGeneratePatch(array $originalFiles, array $newFiles, string
5253 (new Process (['git ' , 'commit ' , '-m ' , '"original files" ' ], FLEX_TEST_DIR ))->mustRun ();
5354 }
5455
55- $ patcher = new RecipePatcher (FLEX_TEST_DIR , $ this ->createMock (IOInterface::class));
56+ $ patcher = new RecipePatcher (FLEX_TEST_DIR , $ this ->createMock (IOInterface::class), $ this -> createMock (Lock::class) );
5657
5758 $ patch = $ patcher ->generatePatch ($ originalFiles , $ newFiles );
5859 $ this ->assertSame ($ expectedPatch , rtrim ($ patch ->getPatch (), "\n" ));
@@ -189,7 +190,7 @@ public function testGeneratePatchOnDeletedFile()
189190 $ this ->getFilesystem ()->remove (FLEX_TEST_DIR );
190191 $ this ->getFilesystem ()->mkdir (FLEX_TEST_DIR );
191192
192- $ patcher = new RecipePatcher (FLEX_TEST_DIR , $ this ->createMock (IOInterface::class));
193+ $ patcher = new RecipePatcher (FLEX_TEST_DIR , $ this ->createMock (IOInterface::class), $ this -> createMock (Lock::class) );
193194
194195 // try to update a file that does not exist in the project
195196 $ patch = $ patcher ->generatePatch (['.env ' => 'original contents ' ], ['.env ' => 'new contents ' ]);
@@ -217,7 +218,7 @@ public function testApplyPatch(array $filesCurrentlyInApp, RecipePatch $recipePa
217218 (new Process (['git ' , 'commit ' , '-m ' , 'Committing original files ' ], FLEX_TEST_DIR ))->mustRun ();
218219 }
219220
220- $ patcher = new RecipePatcher (FLEX_TEST_DIR , $ this ->createMock (IOInterface::class));
221+ $ patcher = new RecipePatcher (FLEX_TEST_DIR , $ this ->createMock (IOInterface::class), $ this -> createMock (Lock::class) );
221222 $ hadConflicts = !$ patcher ->applyPatch ($ recipePatch );
222223
223224 foreach ($ expectedFiles as $ file => $ expectedContents ) {
@@ -233,6 +234,38 @@ public function testApplyPatch(array $filesCurrentlyInApp, RecipePatch $recipePa
233234 $ this ->assertSame ($ expectedConflicts , $ hadConflicts );
234235 }
235236
237+ public function testApplyPatchFileOwnedByMultipleRecipes ()
238+ {
239+ (new Process (['git ' , 'init ' ], FLEX_TEST_DIR ))->mustRun ();
240+ (new Process (['git ' , 'config ' , 'user.name ' , 'Unit test ' ], FLEX_TEST_DIR ))->mustRun ();
241+ (new Process (['git ' , 'config ' , 'user.email ' , '' ], FLEX_TEST_DIR ))->mustRun ();
242+
243+ $ dir = FLEX_TEST_DIR .'/config/packages ' ;
244+ @mkdir ($ dir , 0777 , true );
245+ file_put_contents ($ dir .'/security.yaml ' , '# contents ' );
246+ (new Process (['git ' , 'add ' , '-A ' ], FLEX_TEST_DIR ))->mustRun ();
247+ (new Process (['git ' , 'commit ' , '-m ' , 'Committing original files ' ], FLEX_TEST_DIR ))->mustRun ();
248+
249+
250+ $ lock = $ this ->createMock (Lock::class);
251+ $ lock ->expects ($ this ->any ())->method ('all ' )->willReturn ([
252+ 'symfony/security-bundle ' => ['files ' => ['config/packages/security.yaml ' ]],
253+ 'symfony/security ' => ['files ' => ['config/packages/security.yaml ' ]],
254+ ]);
255+ $ patcher = new RecipePatcher (FLEX_TEST_DIR , $ this ->createMock (IOInterface::class), $ lock );
256+
257+ $ patchData = $ this ->generatePatchData ('config/packages/security.yaml ' , '# contents ' , null );
258+ $ hadConflicts = !$ patcher ->applyPatch (new RecipePatch (
259+ '' ,
260+ [$ patchData ['hash ' ] => $ patchData ['blob ' ]],
261+ ['config/packages/security.yaml ' ]
262+ ), 'symfony/security-bundle ' );
263+
264+ $ this ->assertFileExists ($ dir .'/security.yaml ' );
265+ $ this ->assertSame ('# contents ' , file_get_contents ($ dir .'/security.yaml ' ));
266+ $ this ->assertFalse ($ hadConflicts );
267+ }
268+
236269 /**
237270 * @dataProvider getApplyPatchTests
238271 */
@@ -261,7 +294,7 @@ public function testApplyPatchOnSubfolder(array $filesCurrentlyInApp, RecipePatc
261294 (new Process (['git ' , 'commit ' , '-m ' , 'Committing original files ' ], $ subProjectPath ))->mustRun ();
262295 }
263296
264- $ patcher = new RecipePatcher ($ subProjectPath , $ this ->createMock (IOInterface::class));
297+ $ patcher = new RecipePatcher ($ subProjectPath , $ this ->createMock (IOInterface::class), $ this -> createMock (Lock::class) );
265298 $ hadConflicts = !$ patcher ->applyPatch ($ recipePatch );
266299
267300 foreach ($ expectedFiles as $ file => $ expectedContents ) {
@@ -390,7 +423,7 @@ public function testIntegration(bool $useNullForMissingFiles)
390423 (new Process (['git ' , 'add ' , '-A ' ], FLEX_TEST_DIR ))->mustRun ();
391424 (new Process (['git ' , 'commit ' , '-m ' , 'committing in app start files ' ], FLEX_TEST_DIR ))->mustRun ();
392425
393- $ patcher = new RecipePatcher (FLEX_TEST_DIR , $ this ->createMock (IOInterface::class));
426+ $ patcher = new RecipePatcher (FLEX_TEST_DIR , $ this ->createMock (IOInterface::class), $ this -> createMock (Lock::class) );
394427 $ originalFiles = [
395428 '.env ' => $ files ['dot_env_clean ' ]['original_recipe ' ],
396429 'package.json ' => $ files ['package_json_conflict ' ]['original_recipe ' ],
0 commit comments