@@ -321,6 +321,80 @@ public function testLineProcessedIfRequiredPackageIsPresent()
321321 $ actualContents );
322322 }
323323
324+ public function testLineSkippedIfRequiredPackageVersionIsWrong ()
325+ {
326+ $ this ->saveFile ('phpunit.dist.xml ' , <<<EOF
327+ <?xml version="1.0" encoding="UTF-8"?>
328+ <phpunit>
329+ <extensions>
330+ </extensions>
331+ </phpunit>
332+ EOF
333+ );
334+
335+ $ composer = $ this ->createComposerMockWithPackagesInstalled ([
336+ 'phpunit/phpunit:9 ' ,
337+ ]);
338+
339+ $ this ->runConfigure ([
340+ [
341+ 'file ' => 'phpunit.dist.xml ' ,
342+ 'position ' => 'after_target ' ,
343+ 'target ' => '<extensions> ' ,
344+ 'content ' => ' <bootstrap class="Symfony\Component\Panther\ServerExtension" /> ' ,
345+ 'requires ' => 'phpunit/phpunit:12 ' ,
346+ ],
347+ ], $ composer );
348+ $ actualContents = $ this ->readFile ('phpunit.dist.xml ' );
349+ $ this ->assertSame (<<<EOF
350+ <?xml version="1.0" encoding="UTF-8"?>
351+ <phpunit>
352+ <extensions>
353+ </extensions>
354+ </phpunit>
355+ EOF
356+ ,
357+ $ actualContents );
358+ }
359+
360+ public function testLineProcessedIfRequiredPackageVersionIsRight ()
361+ {
362+ $ this ->saveFile ('phpunit.dist.xml ' , <<<EOF
363+ <?xml version="1.0" encoding="UTF-8"?>
364+ <phpunit>
365+ <extensions>
366+ </extensions>
367+ </phpunit>
368+ EOF
369+ );
370+
371+ $ composer = $ this ->createComposerMockWithPackagesInstalled ([
372+ 'phpunit/phpunit:12 ' ,
373+ ]);
374+
375+ $ this ->runConfigure ([
376+ [
377+ 'file ' => 'phpunit.dist.xml ' ,
378+ 'position ' => 'after_target ' ,
379+ 'target ' => '<extensions> ' ,
380+ 'content ' => ' <bootstrap class="Symfony\Component\Panther\ServerExtension" /> ' ,
381+ 'requires ' => 'phpunit/phpunit:12 ' ,
382+ ],
383+ ], $ composer );
384+
385+ $ actualContents = $ this ->readFile ('phpunit.dist.xml ' );
386+ $ this ->assertSame (<<<EOF
387+ <?xml version="1.0" encoding="UTF-8"?>
388+ <phpunit>
389+ <extensions>
390+ <bootstrap class="Symfony\Component\Panther\ServerExtension" />
391+ </extensions>
392+ </phpunit>
393+ EOF
394+ ,
395+ $ actualContents );
396+ }
397+
324398 /**
325399 * @dataProvider getUnconfigureTests
326400 */
@@ -611,11 +685,16 @@ private function readFile(string $filename): string
611685
612686 private function createComposerMockWithPackagesInstalled (array $ packages )
613687 {
688+ $ packages = array_map (fn ($ package ) => explode (': ' , $ package ), $ packages );
689+
690+ $ packageNames = array_column ($ packages , 0 );
691+ $ constraints = array_column ($ packages , 1 );
692+
614693 $ repository = $ this ->getMockBuilder (InstalledRepositoryInterface::class)->getMock ();
615694 $ repository ->expects ($ this ->any ())
616695 ->method ('findPackage ' )
617- ->willReturnCallback (function ($ name ) use ($ packages ) {
618- if (\in_array ($ name , $ packages )) {
696+ ->willReturnCallback (function ($ name, $ constraint ) use ($ packageNames , $ constraints ) {
697+ if (\in_array ($ name , $ packageNames ) && ( ' * ' === $ constraint || \in_array ( $ constraint , $ constraints ) )) {
619698 return new Package ($ name , '1.0.0 ' , '1.0.0 ' );
620699 }
621700
0 commit comments