| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace ComposerRequireCheckerTest\FileLocator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use ComposerRequireChecker\Exception\DependenciesNotInstalledException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use ComposerRequireChecker\FileLocator\LocateComposerPackageDirectDependenciesSourceFiles; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use org\bovigo\vfs\vfsStream; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use org\bovigo\vfs\vfsStreamDirectory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use PHPUnit\Framework\TestCase; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * @covers \ComposerRequireChecker\FileLocator\LocateComposerPackageDirectDependenciesSourceFiles | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 13 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  | final class LocateComposerPackageDirectDependenciesSourceFilesTest extends TestCase | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |     /** @var LocateComposerPackageDirectDependenciesSourceFiles */ | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |     private $locator; | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |     /** @var vfsStreamDirectory */ | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |     private $root; | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |     protected function setUp(): void | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |         parent::setUp(); | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |         $this->locator = new LocateComposerPackageDirectDependenciesSourceFiles(); | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |         $this->root = vfsStream::setup(); | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |     public function testNoDependencies(): void | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |         vfsStream::create([ | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |             'composer.json' => '{}', | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |             'vendor' => [ | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |                 'composer' => [ | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |                     'installed.json' => '{"packages":[]}', | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |                 ], | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |             ], | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |         ]); | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |         $files = $this->locate($this->root->getChild('composer.json')->url()); | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |         $this->assertCount(0, $files); | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     public function testNoInstalledJson(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         vfsStream::create([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             'composer.json' => '{}', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             'vendor' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |                 'composer' => [], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         $this->expectException(DependenciesNotInstalledException::class); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 55 |  |  |         $this->locate($this->root->getChild('composer.json')->url()); | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     public function testSingleDependency(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         vfsStream::create([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             'composer.json' => '{"require":{"foo/bar": "^1.0"}}', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             'vendor' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |                 'composer' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |                     'installed.json' => '{"packages":[{"name": "foo/bar", "autoload":{"psr-4":{"":"src"}}}]}', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |                 ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |                 'foo' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |                     'bar' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |                         'src' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |                             'MyClass.php' => '', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |                         ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |                     ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |                 ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         $files = $this->locate($this->root->getChild('composer.json')->url()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         $this->assertCount(1, $files); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         $expectedFile = $this->root->getChild('vendor/foo/bar/src/MyClass.php')->url(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         $actualFile = str_replace('\\', '/', reset($files)); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 82 |  |  |         $this->assertSame($expectedFile, $actualFile); | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |     public function testVendorDirsWithoutComposerFilesAreIgnored(): void | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |         vfsStream::create([ | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |             'composer.json' => '{"require": {"foo/bar": "^1.0"}}', | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |             'vendor' => [ | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |                 'composer' => [ | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |                     'installed.json' => '{"packages":[]}', | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |                 ], | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |                 'foo' => [ | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |                     'bar' => [ | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |                         'src' => [ | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |                             'MyClass.php' => '', | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |                         ], | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |                     ], | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |                 ], | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |             ], | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |         ]); | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |         $files = $this->locate($this->root->getChild('composer.json')->url()); | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |         $this->assertCount(0, $files); | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |     public function testVendorConfigSettingIsBeingUsed(): void | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |         vfsStream::create([ | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  |             'composer.json' => '{"require":{"foo/bar": "^1.0"},"config":{"vendor-dir":"alternate-vendor"}}', | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  |             'alternate-vendor' => [ | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  |                 'composer' => [ | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  |                     'installed.json' => '{"packages":[{"name": "foo/bar", "autoload":{"psr-4":{"":"src"}}}]}', | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  |                 ], | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  |                 'foo' => [ | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |                     'bar' => [ | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  |                         'src' => [ | 
            
                                                                        
                            
            
                                    
            
            
                | 119 |  |  |                             'MyClass.php' => '', | 
            
                                                                        
                            
            
                                    
            
            
                | 120 |  |  |                         ], | 
            
                                                                        
                            
            
                                    
            
            
                | 121 |  |  |                     ], | 
            
                                                                        
                            
            
                                    
            
            
                | 122 |  |  |                 ], | 
            
                                                                        
                            
            
                                    
            
            
                | 123 |  |  |             ], | 
            
                                                                        
                            
            
                                    
            
            
                | 124 |  |  |         ]); | 
            
                                                                        
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 126 |  |  |         $files = $this->locate($this->root->getChild('composer.json')->url()); | 
            
                                                                        
                            
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 128 |  |  |         $this->assertCount(1, $files); | 
            
                                                                        
                            
            
                                    
            
            
                | 129 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 130 |  |  |         $expectedFile = $this->root->getChild('alternate-vendor/foo/bar/src/MyClass.php')->url(); | 
            
                                                                        
                            
            
                                    
            
            
                | 131 |  |  |         $actualFile = str_replace('\\', '/', reset($files)); | 
            
                                                                        
                            
            
                                    
            
            
                | 132 |  |  |         $this->assertSame($expectedFile, $actualFile); | 
            
                                                                        
                            
            
                                    
            
            
                | 133 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 134 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     public function testInstalledJsonUsedAsFallback(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         vfsStream::create([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |             'composer.json' => '{"require":{"foo/bar": "^1.0"}}', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |             'vendor' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |                 'composer' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |                     'installed.json' => '{"packages": [{"name": "foo/bar", "autoload":{"psr-4":{"":"src"}}}]}', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |                 ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |                 'foo' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |                     'bar' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |                         'src' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |                             'MyClass.php' => '', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |                         ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |                     ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |                 ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |         $files = $this->locate($this->root->getChild('composer.json')->url()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |         $this->assertCount(1, $files); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |         $expectedFile = $this->root->getChild('vendor/foo/bar/src/MyClass.php')->url(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |         $actualFile = str_replace('\\', '/', reset($files)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |         $this->assertSame($expectedFile, $actualFile); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |         # Ensure we didn't leave our temporary composer.json lying around | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 162 |  |  |         $this->assertFalse($this->root->hasChild('vendor/foo/bar/composer.json')); | 
            
                                                                        
                            
            
                                    
            
            
                | 163 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 164 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 165 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 166 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 167 |  |  |      * https://github.com/composer/composer/pull/7999 | 
            
                                                                        
                            
            
                                    
            
            
                | 168 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 169 |  |  |     public function testOldInstalledJsonUsedAsFallback(): void | 
            
                                                                        
                            
            
                                    
            
            
                | 170 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 171 |  |  |         vfsStream::create([ | 
            
                                                                        
                            
            
                                    
            
            
                | 172 |  |  |             'composer.json' => '{"require":{"foo/bar": "^1.0"}}', | 
            
                                                                        
                            
            
                                    
            
            
                | 173 |  |  |             'vendor' => [ | 
            
                                                                        
                            
            
                                    
            
            
                | 174 |  |  |                 'composer' => [ | 
            
                                                                        
                            
            
                                    
            
            
                | 175 |  |  |                     'installed.json' => '[{"name": "foo/bar", "autoload":{"psr-4":{"":"src"}}}]', | 
            
                                                                        
                            
            
                                    
            
            
                | 176 |  |  |                 ], | 
            
                                                                        
                            
            
                                    
            
            
                | 177 |  |  |                 'foo' => [ | 
            
                                                                        
                            
            
                                    
            
            
                | 178 |  |  |                     'bar' => [ | 
            
                                                                        
                            
            
                                    
            
            
                | 179 |  |  |                         'src' => [ | 
            
                                                                        
                            
            
                                    
            
            
                | 180 |  |  |                             'MyClass.php' => '', | 
            
                                                                        
                            
            
                                    
            
            
                | 181 |  |  |                         ], | 
            
                                                                        
                            
            
                                    
            
            
                | 182 |  |  |                     ], | 
            
                                                                        
                            
            
                                    
            
            
                | 183 |  |  |                 ], | 
            
                                                                        
                            
            
                                    
            
            
                | 184 |  |  |             ], | 
            
                                                                        
                            
            
                                    
            
            
                | 185 |  |  |         ]); | 
            
                                                                        
                            
            
                                    
            
            
                | 186 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 187 |  |  |         $files = $this->locate($this->root->getChild('composer.json')->url()); | 
            
                                                                        
                            
            
                                    
            
            
                | 188 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 189 |  |  |         $this->assertCount(1, $files); | 
            
                                                                        
                            
            
                                    
            
            
                | 190 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 191 |  |  |         $expectedFile = $this->root->getChild('vendor/foo/bar/src/MyClass.php')->url(); | 
            
                                                                        
                            
            
                                    
            
            
                | 192 |  |  |         $actualFile = str_replace('\\', '/', reset($files)); | 
            
                                                                        
                            
            
                                    
            
            
                | 193 |  |  |         $this->assertSame($expectedFile, $actualFile); | 
            
                                                                        
                            
            
                                    
            
            
                | 194 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 195 |  |  |         # Ensure we didn't leave our temporary composer.json lying around | 
            
                                                                        
                            
            
                                    
            
            
                | 196 |  |  |         $this->assertFalse($this->root->hasChild('vendor/foo/bar/composer.json')); | 
            
                                                                        
                            
            
                                    
            
            
                | 197 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 198 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 199 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 200 |  |  |      * @return string[] | 
            
                                                                        
                            
            
                                    
            
            
                | 201 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  |     private function locate(string $composerJson): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 |  |  |     { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 204 |  |  |         return iterator_to_array(($this->locator)($composerJson)); | 
            
                                                                        
                                                                
            
                                    
            
            
                | 205 |  |  |     } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 206 |  |  | } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 207 |  |  |  |