22
33namespace Symfony \Bundle \MakerBundle \Tests \Util ;
44
5+ use Composer \Autoload \ClassLoader ;
56use PHPUnit \Framework \TestCase ;
67use Symfony \Bundle \MakerBundle \Util \AutoloaderUtil ;
78use Symfony \Component \Filesystem \Filesystem ;
8- use Symfony \Component \Process \Process ;
99
1010class AutoloaderUtilTest extends TestCase
1111{
@@ -28,49 +28,59 @@ public static function setupPaths()
2828
2929 public function testGetPathForFutureClass ()
3030 {
31+ $ classLoader = new ClassLoader ();
3132 $ composerJson = [
3233 'autoload ' => [
3334 'psr-4 ' => [
34- 'Also \\In \\Src \\' => 'src/SubDir ' ,
35- 'App \\' => 'src/ ' ,
36- 'Other \\Namespace \\' => 'lib ' ,
37- '' => 'fallback_dir ' ,
35+ 'Also \\In \\Src \\' => '/ src/SubDir ' ,
36+ 'App \\' => '/src ' ,
37+ 'Other \\Namespace \\' => '/ lib ' ,
38+ '' => '/ fallback_dir ' ,
3839 ],
3940 'psr-0 ' => [
40- 'Psr0 \\Package ' => 'lib/other ' ,
41+ 'Psr0 \\Package ' => '/ lib/other ' ,
4142 ],
4243 ],
4344 ];
4445
45- $ fs = new Filesystem ();
46- if (!file_exists (self ::$ currentRootDir )) {
47- $ fs ->mkdir (self ::$ currentRootDir );
46+ foreach ($ composerJson ['autoload ' ] as $ psr => $ dirs ) {
47+ foreach ($ dirs as $ prefix => $ path ) {
48+ if ($ psr == 'psr-4 ' ) {
49+ $ classLoader ->addPsr4 ($ prefix , self ::$ currentRootDir .$ path );
50+ } else {
51+ $ classLoader ->add ($ prefix , self ::$ currentRootDir .$ path );
52+ }
53+ }
4854 }
4955
50- $ fs ->remove (self ::$ currentRootDir .'/vendor ' );
51- file_put_contents (
52- self ::$ currentRootDir .'/composer.json ' ,
53- json_encode ($ composerJson , JSON_PRETTY_PRINT )
54- );
55- $ process = new Process ('composer dump-autoload ' , self ::$ currentRootDir );
56- $ process ->run ();
57- if (!$ process ->isSuccessful ()) {
58- throw new \Exception ('Error running composer dump-autoload: ' .$ process ->getErrorOutput ());
59- }
56+ $ reflection = new \ReflectionClass (AutoloaderUtil::class);
57+ $ property = $ reflection ->getProperty ('classLoader ' );
58+ $ property ->setAccessible (true );
59+
60+ $ autoloaderUtil = new AutoloaderUtil ();
6061
62+ $ property ->setValue ($ autoloaderUtil , $ classLoader );
6163
62- $ autoloaderUtil = new AutoloaderUtil (self ::$ currentRootDir );
6364 foreach ($ this ->getPathForFutureClassTests () as $ className => $ expectedPath ) {
6465 $ this ->assertSame (
65- // the paths will start in vendor/composer and be relative
66- str_replace ('\\' , '/ ' , self ::$ currentRootDir .'/vendor/composer/../../ ' .$ expectedPath ),
66+ str_replace ('\\' , '/ ' , self ::$ currentRootDir .'/ ' .$ expectedPath ),
6767 // normalize slashes for Windows comparison
6868 str_replace ('\\' , '/ ' , $ autoloaderUtil ->getPathForFutureClass ($ className )),
6969 sprintf ('class "%s" should have been in path "%s" ' , $ className , $ expectedPath )
7070 );
7171 }
7272 }
7373
74+ public function testCanFindClassLoader ()
75+ {
76+ $ reflection = new \ReflectionClass (AutoloaderUtil::class);
77+ $ method = $ reflection ->getMethod ('getClassLoader ' );
78+ $ method ->setAccessible (true );
79+ $ autoloaderUtil = new AutoloaderUtil ();
80+ $ autoloader = $ method ->invoke ($ autoloaderUtil );
81+ $ this ->assertInstanceOf (ClassLoader::class, $ autoloader , 'Wrong ClassLoader found ' );
82+ }
83+
7484 public function getPathForFutureClassTests ()
7585 {
7686 return [
0 commit comments