2022-02-23 15:58:18 +00:00
< ? php declare ( strict_types = 1 );
2011-12-03 22:20:06 +00:00
/*
* This file is part of Composer .
*
* ( c ) Nils Adermann < naderman @ naderman . de >
* Jordi Boggiano < j . boggiano @ seld . be >
*
* For the full copyright and license information , please view the LICENSE
* file that was distributed with this source code .
*/
2012-03-08 13:17:22 +00:00
namespace Composer\Test\Autoload ;
2011-12-03 22:20:06 +00:00
use Composer\Autoload\AutoloadGenerator ;
2021-11-11 11:24:12 +00:00
use Composer\Filter\PlatformRequirementFilter\PlatformRequirementFilterFactory ;
2023-09-13 12:11:00 +00:00
use Composer\IO\BufferIO ;
2022-04-01 19:20:21 +00:00
use Composer\Package\CompletePackage ;
2012-09-01 11:24:17 +00:00
use Composer\Package\Link ;
2020-04-21 06:59:36 +00:00
use Composer\Package\Version\VersionParser ;
2019-01-17 16:11:32 +00:00
use Composer\Semver\Constraint\Constraint ;
2020-06-05 14:41:37 +00:00
use Composer\Semver\Constraint\MatchAllConstraint ;
2012-02-09 17:45:28 +00:00
use Composer\Util\Filesystem ;
2012-05-14 12:01:37 +00:00
use Composer\Package\AliasPackage ;
2012-08-23 13:52:40 +00:00
use Composer\Package\Package ;
2020-11-12 09:35:13 +00:00
use Composer\Package\RootPackage ;
2018-11-12 14:23:32 +00:00
use Composer\Test\TestCase ;
2013-01-06 19:34:52 +00:00
use Composer\Script\ScriptEvents ;
2013-12-22 20:56:58 +00:00
use Composer\Repository\InstalledRepositoryInterface ;
use Composer\Installer\InstallationManager ;
use Composer\Config ;
use Composer\EventDispatcher\EventDispatcher ;
2018-04-11 10:33:27 +00:00
use Composer\Util\Platform ;
2021-12-08 16:03:05 +00:00
use PHPUnit\Framework\MockObject\MockObject ;
2011-12-03 22:20:06 +00:00
2012-02-24 11:28:41 +00:00
class AutoloadGeneratorTest extends TestCase
2011-12-03 22:20:06 +00:00
{
2013-12-22 20:56:58 +00:00
/**
* @ var string
*/
2011-12-03 22:20:06 +00:00
public $vendorDir ;
2013-12-22 20:56:58 +00:00
/**
* @ var Config | MockObject
*/
2012-06-24 19:58:51 +00:00
private $config ;
2013-12-22 20:56:58 +00:00
/**
* @ var string
*/
2011-12-03 22:20:06 +00:00
private $workingDir ;
2013-12-22 20:56:58 +00:00
2015-12-14 14:35:27 +00:00
/**
* @ var string
*/
private $origDir ;
2013-12-22 20:56:58 +00:00
/**
2021-12-08 16:03:05 +00:00
* @ var InstallationManager & MockObject
2013-12-22 20:56:58 +00:00
*/
2011-12-03 22:20:06 +00:00
private $im ;
2013-12-22 20:56:58 +00:00
/**
2021-12-08 16:03:05 +00:00
* @ var InstalledRepositoryInterface & MockObject
2013-12-22 20:56:58 +00:00
*/
2011-12-03 22:20:06 +00:00
private $repository ;
2013-12-22 20:56:58 +00:00
/**
* @ var AutoloadGenerator
*/
2011-12-03 22:20:06 +00:00
private $generator ;
2013-12-22 20:56:58 +00:00
/**
* @ var Filesystem
*/
2012-02-24 09:40:47 +00:00
private $fs ;
2013-12-22 20:56:58 +00:00
2023-09-13 12:11:00 +00:00
/**
* @ var BufferIO
*/
private $io ;
2013-12-22 20:56:58 +00:00
/**
2021-12-08 16:03:05 +00:00
* @ var EventDispatcher & MockObject
2013-12-22 20:56:58 +00:00
*/
2013-01-06 19:34:52 +00:00
private $eventDispatcher ;
2011-12-03 22:20:06 +00:00
2015-01-04 00:35:25 +00:00
/**
2015-01-04 01:01:18 +00:00
* Map of setting name => return value configuration for the stub Config
* object .
*
2021-10-26 19:48:56 +00:00
* @ var array < string , callable | boolean >
2015-01-04 00:35:25 +00:00
*/
2022-02-23 13:09:49 +00:00
private $configValueMap ;
2015-01-04 00:35:25 +00:00
2021-12-08 16:03:05 +00:00
protected function setUp () : void
2011-12-03 22:20:06 +00:00
{
2012-02-24 09:40:47 +00:00
$this -> fs = new Filesystem ;
2011-12-03 22:20:06 +00:00
2022-05-11 14:05:35 +00:00
$this -> workingDir = self :: getUniqueTmpDirectory ();
2013-02-20 15:50:26 +00:00
$this -> vendorDir = $this -> workingDir . DIRECTORY_SEPARATOR . 'composer-test-autoload' ;
2012-02-24 11:28:41 +00:00
$this -> ensureDirectoryExistsAndClear ( $this -> vendorDir );
2011-12-03 22:20:06 +00:00
2018-04-12 08:24:56 +00:00
$this -> config = $this -> getMockBuilder ( 'Composer\Config' ) -> getMock ();
2013-01-14 08:32:26 +00:00
2022-08-17 12:20:07 +00:00
$this -> configValueMap = [
2022-02-21 12:42:28 +00:00
'vendor-dir' => function () : string {
2022-01-03 14:40:32 +00:00
return $this -> vendorDir ;
2015-01-04 00:35:25 +00:00
},
2022-08-17 12:20:07 +00:00
'platform-check' => static function () : bool {
2020-04-21 13:25:35 +00:00
return true ;
},
2022-08-17 12:20:07 +00:00
'use-include-path' => static function () : bool {
2022-04-28 19:20:40 +00:00
return false ;
},
2022-08-17 12:20:07 +00:00
];
2012-06-24 19:58:51 +00:00
2023-09-13 12:11:00 +00:00
$this -> io = new BufferIO ();
2015-01-04 00:35:25 +00:00
$this -> config -> expects ( $this -> atLeastOnce ())
2013-01-14 08:32:26 +00:00
-> method ( 'get' )
2022-01-03 14:40:32 +00:00
-> will ( $this -> returnCallback ( function ( $arg ) {
2015-01-04 00:35:25 +00:00
$ret = null ;
2022-01-03 14:40:32 +00:00
if ( isset ( $this -> configValueMap [ $arg ])) {
$ret = $this -> configValueMap [ $arg ];
2015-01-04 00:35:25 +00:00
if ( is_callable ( $ret )) {
$ret = $ret ();
}
}
2015-02-24 14:22:54 +00:00
2015-01-04 00:35:25 +00:00
return $ret ;
2013-01-14 08:32:26 +00:00
}));
2022-02-22 15:47:09 +00:00
$this -> origDir = Platform :: getCwd ();
2011-12-03 22:20:06 +00:00
chdir ( $this -> workingDir );
$this -> im = $this -> getMockBuilder ( 'Composer\Installer\InstallationManager' )
-> disableOriginalConstructor ()
-> getMock ();
$this -> im -> expects ( $this -> any ())
-> method ( 'getInstallPath' )
2023-05-24 12:58:11 +00:00
-> will ( $this -> returnCallback ( function ( $package ) : ? string {
if ( $package -> getType () === 'metapackage' ) {
return null ;
}
2013-01-31 22:39:57 +00:00
$targetDir = $package -> getTargetDir ();
2013-06-13 11:28:24 +00:00
2022-01-03 14:40:32 +00:00
return $this -> vendorDir . '/' . $package -> getName () . ( $targetDir ? '/' . $targetDir : '' );
2011-12-03 22:20:06 +00:00
}));
2018-04-12 08:24:56 +00:00
$this -> repository = $this -> getMockBuilder ( 'Composer\Repository\InstalledRepositoryInterface' ) -> getMock ();
2020-11-04 22:41:58 +00:00
$this -> repository -> expects ( $this -> any ())
-> method ( 'getDevPackageNames' )
2022-08-17 12:20:07 +00:00
-> willReturn ([]);
2011-12-03 22:20:06 +00:00
2013-08-14 15:42:11 +00:00
$this -> eventDispatcher = $this -> getMockBuilder ( 'Composer\EventDispatcher\EventDispatcher' )
2013-01-06 19:34:52 +00:00
-> disableOriginalConstructor ()
-> getMock ();
2023-09-13 12:11:00 +00:00
$this -> generator = new AutoloadGenerator ( $this -> eventDispatcher , $this -> io );
2011-12-03 22:20:06 +00:00
}
2021-12-08 16:03:05 +00:00
protected function tearDown () : void
2011-12-03 22:20:06 +00:00
{
2021-12-09 16:09:07 +00:00
parent :: tearDown ();
2013-02-20 15:50:26 +00:00
chdir ( $this -> origDir );
2012-06-19 21:15:42 +00:00
if ( is_dir ( $this -> workingDir )) {
$this -> fs -> removeDirectory ( $this -> workingDir );
2012-02-24 09:40:47 +00:00
}
2016-01-21 12:01:55 +00:00
2012-06-19 21:15:42 +00:00
if ( is_dir ( $this -> vendorDir )) {
$this -> fs -> removeDirectory ( $this -> vendorDir );
2012-03-21 12:58:35 +00:00
}
2011-12-03 22:20:06 +00:00
}
2022-02-18 09:38:54 +00:00
public function testRootPackageAutoloading () : void
2011-12-03 22:20:06 +00:00
{
2020-11-12 09:35:13 +00:00
$package = new RootPackage ( 'root/a' , '1.0' , '1.0' );
2022-08-17 12:20:07 +00:00
$package -> setAutoload ([
'psr-0' => [
2013-11-26 01:53:44 +00:00
'Main' => 'src/' ,
2022-08-17 12:20:07 +00:00
'Lala' => [ 'src/' , 'lib/' ],
],
'psr-4' => [
2013-11-26 01:53:44 +00:00
'Acme\Fruit\\' => 'src-fruit/' ,
2022-08-17 12:20:07 +00:00
'Acme\Cake\\' => [ 'src-cake/' , 'lib-cake/' ],
],
'classmap' => [ 'composersrc/' ],
]);
2011-12-03 22:20:06 +00:00
2012-02-24 11:28:41 +00:00
$this -> repository -> expects ( $this -> once ())
2013-04-28 20:32:46 +00:00
-> method ( 'getCanonicalPackages' )
2022-08-17 12:20:07 +00:00
-> will ( $this -> returnValue ([]));
2011-12-03 22:20:06 +00:00
2012-06-19 21:15:42 +00:00
$this -> fs -> ensureDirectoryExists ( $this -> workingDir . '/composer' );
2015-06-12 15:52:55 +00:00
$this -> fs -> ensureDirectoryExists ( $this -> workingDir . '/src/Lala/Test' );
2012-06-19 21:15:42 +00:00
$this -> fs -> ensureDirectoryExists ( $this -> workingDir . '/lib' );
2014-02-03 15:53:56 +00:00
file_put_contents ( $this -> workingDir . '/src/Lala/ClassMapMain.php' , '<?php namespace Lala; class ClassMapMain {}' );
2015-06-12 15:52:55 +00:00
file_put_contents ( $this -> workingDir . '/src/Lala/Test/ClassMapMainTest.php' , '<?php namespace Lala\Test; class ClassMapMainTest {}' );
2012-03-21 12:58:35 +00:00
2013-11-26 01:53:44 +00:00
$this -> fs -> ensureDirectoryExists ( $this -> workingDir . '/src-fruit' );
$this -> fs -> ensureDirectoryExists ( $this -> workingDir . '/src-cake' );
$this -> fs -> ensureDirectoryExists ( $this -> workingDir . '/lib-cake' );
2014-02-03 15:53:56 +00:00
file_put_contents ( $this -> workingDir . '/src-cake/ClassMapBar.php' , '<?php namespace Acme\Cake; class ClassMapBar {}' );
2013-11-26 01:53:44 +00:00
2013-02-19 14:23:43 +00:00
$this -> fs -> ensureDirectoryExists ( $this -> workingDir . '/composersrc' );
file_put_contents ( $this -> workingDir . '/composersrc/foo.php' , '<?php class ClassMapFoo {}' );
2012-03-21 12:58:35 +00:00
2023-10-06 07:34:10 +00:00
$this -> generator -> dump ( $this -> config , $this -> repository , $package , $this -> im , 'composer' , true , '_1' );
2013-11-26 01:53:44 +00:00
// Assert that autoload_namespaces.php was correctly generated.
2024-05-29 21:12:06 +00:00
self :: assertAutoloadFiles ( 'main' , $this -> vendorDir . '/composer' );
2013-11-26 01:53:44 +00:00
// Assert that autoload_psr4.php was correctly generated.
2024-05-29 21:12:06 +00:00
self :: assertAutoloadFiles ( 'psr4' , $this -> vendorDir . '/composer' , 'psr4' );
2013-11-26 01:53:44 +00:00
// Assert that autoload_classmap.php was correctly generated.
2024-05-29 21:12:06 +00:00
self :: assertAutoloadFiles ( 'classmap' , $this -> vendorDir . '/composer' , 'classmap' );
2011-12-03 22:20:06 +00:00
}
2014-03-01 19:39:06 +00:00
2022-02-18 09:38:54 +00:00
public function testRootPackageDevAutoloading () : void
2014-02-27 09:39:33 +00:00
{
2020-11-12 09:35:13 +00:00
$package = new RootPackage ( 'root/a' , '1.0' , '1.0' );
2022-08-17 12:20:07 +00:00
$package -> setAutoload ([
'psr-0' => [
2014-02-27 09:39:33 +00:00
'Main' => 'src/' ,
2022-08-17 12:20:07 +00:00
],
]);
$package -> setDevAutoload ([
'files' => [ 'devfiles/foo.php' ],
'psr-0' => [
2015-09-28 09:51:14 +00:00
'Main' => 'tests/' ,
2022-08-17 12:20:07 +00:00
],
]);
2014-02-27 09:39:33 +00:00
$this -> repository -> expects ( $this -> once ())
-> method ( 'getCanonicalPackages' )
2022-08-17 12:20:07 +00:00
-> will ( $this -> returnValue ([]));
2014-02-27 09:39:33 +00:00
$this -> fs -> ensureDirectoryExists ( $this -> workingDir . '/composer' );
$this -> fs -> ensureDirectoryExists ( $this -> workingDir . '/src/Main' );
file_put_contents ( $this -> workingDir . '/src/Main/ClassMain.php' , '<?php namespace Main; class ClassMain {}' );
$this -> fs -> ensureDirectoryExists ( $this -> workingDir . '/devfiles' );
file_put_contents ( $this -> workingDir . '/devfiles/foo.php' , '<?php function foo() { echo "foo"; }' );
// generate autoload files with the dev mode set to true
$this -> generator -> setDevMode ( true );
2023-10-06 07:34:10 +00:00
$this -> generator -> dump ( $this -> config , $this -> repository , $package , $this -> im , 'composer' , true , '_1' );
2014-03-01 19:39:06 +00:00
2014-02-27 09:39:33 +00:00
// check standard autoload
2024-05-29 21:12:06 +00:00
self :: assertAutoloadFiles ( 'main5' , $this -> vendorDir . '/composer' );
self :: assertAutoloadFiles ( 'classmap7' , $this -> vendorDir . '/composer' , 'classmap' );
2014-03-01 19:39:06 +00:00
2014-02-27 09:39:33 +00:00
// make sure dev autoload is correctly dumped
2024-05-29 21:12:06 +00:00
self :: assertAutoloadFiles ( 'files2' , $this -> vendorDir . '/composer' , 'files' );
2014-02-27 09:39:33 +00:00
}
2022-02-18 09:38:54 +00:00
public function testRootPackageDevAutoloadingDisabledByDefault () : void
2014-02-27 09:39:33 +00:00
{
2020-11-12 09:35:13 +00:00
$package = new RootPackage ( 'root/a' , '1.0' , '1.0' );
2022-08-17 12:20:07 +00:00
$package -> setAutoload ([
'psr-0' => [
2014-02-27 09:39:33 +00:00
'Main' => 'src/' ,
2022-08-17 12:20:07 +00:00
],
]);
$package -> setDevAutoload ([
'files' => [ 'devfiles/foo.php' ],
]);
2011-12-03 22:20:06 +00:00
2014-02-27 09:39:33 +00:00
$this -> repository -> expects ( $this -> once ())
-> method ( 'getCanonicalPackages' )
2022-08-17 12:20:07 +00:00
-> will ( $this -> returnValue ([]));
2014-02-27 09:39:33 +00:00
$this -> fs -> ensureDirectoryExists ( $this -> workingDir . '/composer' );
$this -> fs -> ensureDirectoryExists ( $this -> workingDir . '/src/Main' );
file_put_contents ( $this -> workingDir . '/src/Main/ClassMain.php' , '<?php namespace Main; class ClassMain {}' );
$this -> fs -> ensureDirectoryExists ( $this -> workingDir . '/devfiles' );
file_put_contents ( $this -> workingDir . '/devfiles/foo.php' , '<?php function foo() { echo "foo"; }' );
2023-10-06 07:34:10 +00:00
$this -> generator -> dump ( $this -> config , $this -> repository , $package , $this -> im , 'composer' , true , '_1' );
2014-02-27 09:39:33 +00:00
// check standard autoload
2024-05-29 21:12:06 +00:00
self :: assertAutoloadFiles ( 'main4' , $this -> vendorDir . '/composer' );
self :: assertAutoloadFiles ( 'classmap7' , $this -> vendorDir . '/composer' , 'classmap' );
2014-02-27 09:39:33 +00:00
// make sure dev autoload is disabled when dev mode is set to false
2024-05-29 21:12:06 +00:00
self :: assertFalse ( is_file ( $this -> vendorDir . '/composer/autoload_files.php' ));
2014-02-27 09:39:33 +00:00
}
2014-03-01 19:39:06 +00:00
2022-02-18 09:38:54 +00:00
public function testVendorDirSameAsWorkingDir () : void
2012-01-31 14:04:49 +00:00
{
2012-02-02 15:38:48 +00:00
$this -> vendorDir = $this -> workingDir ;
2020-11-12 09:35:13 +00:00
$package = new RootPackage ( 'root/a' , '1.0' , '1.0' );
2022-08-17 12:20:07 +00:00
$package -> setAutoload ([
'psr-0' => [ 'Main' => 'src/' , 'Lala' => 'src/' ],
'psr-4' => [
2013-11-26 01:53:44 +00:00
'Acme\Fruit\\' => 'src-fruit/' ,
2022-08-17 12:20:07 +00:00
'Acme\Cake\\' => [ 'src-cake/' , 'lib-cake/' ],
],
'classmap' => [ 'composersrc/' ],
]);
2012-02-02 15:38:48 +00:00
2012-02-24 11:28:41 +00:00
$this -> repository -> expects ( $this -> once ())
2013-04-28 20:32:46 +00:00
-> method ( 'getCanonicalPackages' )
2022-08-17 12:20:07 +00:00
-> will ( $this -> returnValue ([]));
2012-02-02 15:38:48 +00:00
2012-06-19 21:15:42 +00:00
$this -> fs -> ensureDirectoryExists ( $this -> vendorDir . '/composer' );
$this -> fs -> ensureDirectoryExists ( $this -> vendorDir . '/src/Main' );
file_put_contents ( $this -> vendorDir . '/src/Main/Foo.php' , '<?php namespace Main; class Foo {}' );
2012-02-24 11:28:41 +00:00
2013-02-19 14:23:43 +00:00
$this -> fs -> ensureDirectoryExists ( $this -> vendorDir . '/composersrc' );
file_put_contents ( $this -> vendorDir . '/composersrc/foo.php' , '<?php class ClassMapFoo {}' );
2012-03-21 12:58:35 +00:00
2023-10-06 07:34:10 +00:00
$this -> generator -> dump ( $this -> config , $this -> repository , $package , $this -> im , 'composer' , true , '_2' );
2024-05-29 21:12:06 +00:00
self :: assertAutoloadFiles ( 'main3' , $this -> vendorDir . '/composer' );
self :: assertAutoloadFiles ( 'psr4_3' , $this -> vendorDir . '/composer' , 'psr4' );
self :: assertAutoloadFiles ( 'classmap3' , $this -> vendorDir . '/composer' , 'classmap' );
2012-01-31 14:04:49 +00:00
}
2022-02-18 09:38:54 +00:00
public function testRootPackageAutoloadingAlternativeVendorDir () : void
2011-12-03 22:20:06 +00:00
{
2020-11-12 09:35:13 +00:00
$package = new RootPackage ( 'root/a' , '1.0' , '1.0' );
2022-08-17 12:20:07 +00:00
$package -> setAutoload ([
'psr-0' => [ 'Main' => 'src/' , 'Lala' => 'src/' ],
'psr-4' => [
2013-11-26 01:53:44 +00:00
'Acme\Fruit\\' => 'src-fruit/' ,
2022-08-17 12:20:07 +00:00
'Acme\Cake\\' => [ 'src-cake/' , 'lib-cake/' ],
],
'classmap' => [ 'composersrc/' ],
]);
2011-12-03 22:20:06 +00:00
2012-02-24 11:28:41 +00:00
$this -> repository -> expects ( $this -> once ())
2013-04-28 20:32:46 +00:00
-> method ( 'getCanonicalPackages' )
2022-08-17 12:20:07 +00:00
-> will ( $this -> returnValue ([]));
2011-12-03 22:20:06 +00:00
$this -> vendorDir .= '/subdir' ;
2012-06-19 21:15:42 +00:00
$this -> fs -> ensureDirectoryExists ( $this -> vendorDir . '/composer' );
$this -> fs -> ensureDirectoryExists ( $this -> workingDir . '/src' );
2013-02-19 14:23:43 +00:00
$this -> fs -> ensureDirectoryExists ( $this -> workingDir . '/composersrc' );
file_put_contents ( $this -> workingDir . '/composersrc/foo.php' , '<?php class ClassMapFoo {}' );
2023-10-06 07:34:10 +00:00
$this -> generator -> dump ( $this -> config , $this -> repository , $package , $this -> im , 'composer' , false , '_3' );
2024-05-29 21:12:06 +00:00
self :: assertAutoloadFiles ( 'main2' , $this -> vendorDir . '/composer' );
self :: assertAutoloadFiles ( 'psr4_2' , $this -> vendorDir . '/composer' , 'psr4' );
self :: assertAutoloadFiles ( 'classmap2' , $this -> vendorDir . '/composer' , 'classmap' );
2011-12-03 22:20:06 +00:00
}
2022-02-18 09:38:54 +00:00
public function testRootPackageAutoloadingWithTargetDir () : void
2012-04-19 19:26:01 +00:00
{
2020-11-12 09:35:13 +00:00
$package = new RootPackage ( 'root/a' , '1.0' , '1.0' );
2022-08-17 12:20:07 +00:00
$package -> setAutoload ([
'psr-0' => [ 'Main\\Foo' => '' , 'Main\\Bar' => '' ],
'classmap' => [ 'Main/Foo/src' , 'lib' ],
'files' => [ 'foo.php' , 'Main/Foo/bar.php' ],
]);
2012-04-19 20:04:49 +00:00
$package -> setTargetDir ( 'Main/Foo/' );
2012-04-19 19:26:01 +00:00
$this -> repository -> expects ( $this -> once ())
2013-04-28 20:32:46 +00:00
-> method ( 'getCanonicalPackages' )
2022-08-17 12:20:07 +00:00
-> will ( $this -> returnValue ([]));
2012-04-19 19:26:01 +00:00
2012-06-19 21:15:42 +00:00
$this -> fs -> ensureDirectoryExists ( $this -> vendorDir . '/a' );
2013-02-19 14:23:43 +00:00
$this -> fs -> ensureDirectoryExists ( $this -> workingDir . '/src' );
$this -> fs -> ensureDirectoryExists ( $this -> workingDir . '/lib' );
file_put_contents ( $this -> workingDir . '/src/rootfoo.php' , '<?php class ClassMapFoo {}' );
file_put_contents ( $this -> workingDir . '/lib/rootbar.php' , '<?php class ClassMapBar {}' );
file_put_contents ( $this -> workingDir . '/foo.php' , '<?php class FilesFoo {}' );
file_put_contents ( $this -> workingDir . '/bar.php' , '<?php class FilesBar {}' );
2012-06-19 21:15:42 +00:00
2023-10-06 07:34:10 +00:00
$this -> generator -> dump ( $this -> config , $this -> repository , $package , $this -> im , 'composer' , false , 'TargetDir' );
2024-05-29 21:12:06 +00:00
self :: assertFileContentEquals ( __DIR__ . '/Fixtures/autoload_target_dir.php' , $this -> vendorDir . '/autoload.php' );
self :: assertFileContentEquals ( __DIR__ . '/Fixtures/autoload_real_target_dir.php' , $this -> vendorDir . '/composer/autoload_real.php' );
self :: assertFileContentEquals ( __DIR__ . '/Fixtures/autoload_static_target_dir.php' , $this -> vendorDir . '/composer/autoload_static.php' );
self :: assertFileContentEquals ( __DIR__ . '/Fixtures/autoload_files_target_dir.php' , $this -> vendorDir . '/composer/autoload_files.php' );
self :: assertAutoloadFiles ( 'classmap6' , $this -> vendorDir . '/composer' , 'classmap' );
2012-08-24 21:11:16 +00:00
}
2023-09-13 12:11:00 +00:00
public function testDuplicateFilesWarning () : void
{
$package = new RootPackage ( 'root/a' , '1.0' , '1.0' );
$package -> setAutoload ([
'files' => [ 'foo.php' , 'bar.php' , './foo.php' , '././foo.php' ],
]);
$this -> repository -> expects ( $this -> once ())
-> method ( 'getCanonicalPackages' )
-> will ( $this -> returnValue ([]));
$this -> fs -> ensureDirectoryExists ( $this -> vendorDir . '/a' );
$this -> fs -> ensureDirectoryExists ( $this -> workingDir . '/src' );
$this -> fs -> ensureDirectoryExists ( $this -> workingDir . '/lib' );
file_put_contents ( $this -> workingDir . '/foo.php' , '<?php class FilesFoo {}' );
file_put_contents ( $this -> workingDir . '/bar.php' , '<?php class FilesBar {}' );
2023-10-06 07:34:10 +00:00
$this -> generator -> dump ( $this -> config , $this -> repository , $package , $this -> im , 'composer' , false , 'FilesWarning' );
2023-09-13 12:11:00 +00:00
self :: assertFileContentEquals ( __DIR__ . '/Fixtures/autoload_files_duplicates.php' , $this -> vendorDir . '/composer/autoload_files.php' );
$expected = '<warning>The following "files" autoload rules are included multiple times, this may cause issues and should be resolved:</warning>' . PHP_EOL .
'<warning> - $baseDir . \'/foo.php\'</warning>' . PHP_EOL ;
self :: assertEquals ( $expected , $this -> io -> getOutput ());;
}
2012-08-24 21:11:16 +00:00
2022-02-18 09:38:54 +00:00
public function testVendorsAutoloading () : void
2011-12-03 22:20:06 +00:00
{
2020-11-12 09:35:13 +00:00
$package = new RootPackage ( 'root/a' , '1.0' , '1.0' );
2022-08-17 12:20:07 +00:00
$package -> setRequires ([
2021-08-21 15:41:52 +00:00
'a/a' => new Link ( 'a' , 'a/a' , new MatchAllConstraint ()),
'b/b' => new Link ( 'a' , 'b/b' , new MatchAllConstraint ()),
2022-08-17 12:20:07 +00:00
]);
2011-12-03 22:20:06 +00:00
2022-08-17 12:20:07 +00:00
$packages = [];
2012-08-23 13:52:40 +00:00
$packages [] = $a = new Package ( 'a/a' , '1.0' , '1.0' );
$packages [] = $b = new Package ( 'b/b' , '1.0' , '1.0' );
2012-05-14 12:01:37 +00:00
$packages [] = $c = new AliasPackage ( $b , '1.2' , '1.2' );
2022-08-17 12:20:07 +00:00
$a -> setAutoload ([ 'psr-0' => [ 'A' => 'src/' , 'A\\B' => 'lib/' ]]);
$b -> setAutoload ([ 'psr-0' => [ 'B\\Sub\\Name' => 'src/' ]]);
2011-12-03 22:20:06 +00:00
2012-02-24 11:28:41 +00:00
$this -> repository -> expects ( $this -> once ())
2013-04-28 20:32:46 +00:00
-> method ( 'getCanonicalPackages' )
2011-12-03 22:20:06 +00:00
-> will ( $this -> returnValue ( $packages ));
2012-06-19 21:15:42 +00:00
$this -> fs -> ensureDirectoryExists ( $this -> vendorDir . '/composer' );
$this -> fs -> ensureDirectoryExists ( $this -> vendorDir . '/a/a/src' );
$this -> fs -> ensureDirectoryExists ( $this -> vendorDir . '/a/a/lib' );
$this -> fs -> ensureDirectoryExists ( $this -> vendorDir . '/b/b/src' );
2023-10-06 07:34:10 +00:00
$this -> generator -> dump ( $this -> config , $this -> repository , $package , $this -> im , 'composer' , false , '_5' );
2024-05-29 21:12:06 +00:00
self :: assertAutoloadFiles ( 'vendors' , $this -> vendorDir . '/composer' );
self :: assertFileExists ( $this -> vendorDir . '/composer/autoload_classmap.php' , " ClassMap file needs to be generated, even if empty. " );
2018-05-31 15:02:04 +00:00
}
2023-05-24 12:58:11 +00:00
public function testVendorsAutoloadingWithMetapackages () : void
{
$package = new RootPackage ( 'root/a' , '1.0' , '1.0' );
$package -> setRequires ([
'a/a' => new Link ( 'a' , 'a/a' , new MatchAllConstraint ()),
]);
$packages = [];
$packages [] = $a = new Package ( 'a/a' , '1.0' , '1.0' );
$packages [] = $b = new Package ( 'b/b' , '1.0' , '1.0' );
$packages [] = $c = new AliasPackage ( $b , '1.2' , '1.2' );
$a -> setAutoload ([ 'psr-0' => [ 'A' => 'src/' , 'A\\B' => 'lib/' ]]);
$b -> setAutoload ([ 'psr-0' => [ 'B\\Sub\\Name' => 'src/' ]]);
$a -> setType ( 'metapackage' );
$a -> setRequires ([
'b/b' => new Link ( 'a/a' , 'b/b' , new MatchAllConstraint ()),
]);
$this -> repository -> expects ( $this -> once ())
-> method ( 'getCanonicalPackages' )
-> will ( $this -> returnValue ( $packages ));
$this -> fs -> ensureDirectoryExists ( $this -> vendorDir . '/composer' );
$this -> fs -> ensureDirectoryExists ( $this -> vendorDir . '/b/b/src' );
// creating a/a files to make sure they would be found by autoloader even tho they are technically not
// needed as the package is a metapackage, but if it fails to be excluded it would find these
$this -> fs -> ensureDirectoryExists ( $this -> vendorDir . '/a/a/src' );
$this -> fs -> ensureDirectoryExists ( $this -> vendorDir . '/a/a/lib' );
2023-10-06 07:34:10 +00:00
$this -> generator -> dump ( $this -> config , $this -> repository , $package , $this -> im , 'composer' , false , '_5' );
2024-05-29 21:12:06 +00:00
self :: assertAutoloadFiles ( 'vendors_meta' , $this -> vendorDir . '/composer' );
self :: assertFileExists ( $this -> vendorDir . '/composer/autoload_classmap.php' , " ClassMap file needs to be generated, even if empty. " );
2023-05-24 12:58:11 +00:00
}
2022-02-18 09:38:54 +00:00
public function testNonDevAutoloadExclusionWithRecursion () : void
2018-05-31 15:02:04 +00:00
{
2020-11-12 09:35:13 +00:00
$package = new RootPackage ( 'root/a' , '1.0' , '1.0' );
2022-08-17 12:20:07 +00:00
$package -> setRequires ([
2021-08-21 15:41:52 +00:00
'a/a' => new Link ( 'a' , 'a/a' , new MatchAllConstraint ()),
2022-08-17 12:20:07 +00:00
]);
2018-05-31 15:02:04 +00:00
2022-08-17 12:20:07 +00:00
$packages = [];
2018-05-31 15:02:04 +00:00
$packages [] = $a = new Package ( 'a/a' , '1.0' , '1.0' );
$packages [] = $b = new Package ( 'b/b' , '1.0' , '1.0' );
2022-08-17 12:20:07 +00:00
$a -> setAutoload ([ 'psr-0' => [ 'A' => 'src/' , 'A\\B' => 'lib/' ]]);
$a -> setRequires ([
2021-08-21 15:41:52 +00:00
'b/b' => new Link ( 'a/a' , 'b/b' , new MatchAllConstraint ()),
2022-08-17 12:20:07 +00:00
]);
$b -> setAutoload ([ 'psr-0' => [ 'B\\Sub\\Name' => 'src/' ]]);
$b -> setRequires ([
2021-08-21 15:41:52 +00:00
'a/a' => new Link ( 'b/b' , 'a/a' , new MatchAllConstraint ()),
2022-08-17 12:20:07 +00:00
]);
2018-05-31 15:02:04 +00:00
$this -> repository -> expects ( $this -> once ())
-> method ( 'getCanonicalPackages' )
-> will ( $this -> returnValue ( $packages ));
$this -> fs -> ensureDirectoryExists ( $this -> vendorDir . '/composer' );
$this -> fs -> ensureDirectoryExists ( $this -> vendorDir . '/a/a/src' );
$this -> fs -> ensureDirectoryExists ( $this -> vendorDir . '/a/a/lib' );
$this -> fs -> ensureDirectoryExists ( $this -> vendorDir . '/b/b/src' );
2023-10-06 07:34:10 +00:00
$this -> generator -> dump ( $this -> config , $this -> repository , $package , $this -> im , 'composer' , false , '_5' );
2024-05-29 21:12:06 +00:00
self :: assertAutoloadFiles ( 'vendors' , $this -> vendorDir . '/composer' );
self :: assertFileExists ( $this -> vendorDir . '/composer/autoload_classmap.php' , " ClassMap file needs to be generated, even if empty. " );
2012-03-05 13:10:01 +00:00
}
2022-02-18 09:38:54 +00:00
public function testNonDevAutoloadShouldIncludeReplacedPackages () : void
2019-01-17 16:11:32 +00:00
{
2020-11-12 09:35:13 +00:00
$package = new RootPackage ( 'root/a' , '1.0' , '1.0' );
2022-08-17 12:20:07 +00:00
$package -> setRequires ([ 'a/a' => new Link ( 'a' , 'a/a' , new MatchAllConstraint ())]);
2019-01-17 16:11:32 +00:00
2022-08-17 12:20:07 +00:00
$packages = [];
2019-01-17 16:11:32 +00:00
$packages [] = $a = new Package ( 'a/a' , '1.0' , '1.0' );
$packages [] = $b = new Package ( 'b/b' , '1.0' , '1.0' );
2022-08-17 12:20:07 +00:00
$a -> setRequires ([ 'b/c' => new Link ( 'a/a' , 'b/c' , new MatchAllConstraint ())]);
2019-01-17 16:11:32 +00:00
2022-08-17 12:20:07 +00:00
$b -> setAutoload ([ 'psr-4' => [ 'B\\' => 'src/' ]]);
2019-01-17 16:11:32 +00:00
$b -> setReplaces (
2022-08-17 12:20:07 +00:00
[ 'b/c' => new Link ( 'b/b' , 'b/c' , new Constraint ( '==' , '1.0' ), Link :: TYPE_REPLACE )]
2019-01-17 16:11:32 +00:00
);
$this -> repository -> expects ( $this -> once ())
-> method ( 'getCanonicalPackages' )
-> will ( $this -> returnValue ( $packages ));
$this -> fs -> ensureDirectoryExists ( $this -> vendorDir . '/b/b/src/C' );
file_put_contents ( $this -> vendorDir . '/b/b/src/C/C.php' , '<?php namespace B\\C; class C {}' );
2023-10-06 07:34:10 +00:00
$this -> generator -> dump ( $this -> config , $this -> repository , $package , $this -> im , 'composer' , true , '_5' );
2019-01-17 16:11:32 +00:00
2024-05-29 21:12:06 +00:00
self :: assertEquals (
2022-08-17 12:20:07 +00:00
[
2019-01-17 16:11:32 +00:00
'B\\C\\C' => $this -> vendorDir . '/b/b/src/C/C.php' ,
2020-04-19 14:00:21 +00:00
'Composer\\InstalledVersions' => $this -> vendorDir . '/composer/InstalledVersions.php' ,
2022-08-17 12:20:07 +00:00
],
2019-01-17 16:11:32 +00:00
include $this -> vendorDir . '/composer/autoload_classmap.php'
);
}
2022-02-18 09:38:54 +00:00
public function testNonDevAutoloadExclusionWithRecursionReplace () : void
2018-08-20 20:06:46 +00:00
{
2020-11-12 09:35:13 +00:00
$package = new RootPackage ( 'root/a' , '1.0' , '1.0' );
2022-08-17 12:20:07 +00:00
$package -> setRequires ([
2021-08-21 15:41:52 +00:00
'a/a' => new Link ( 'a' , 'a/a' , new MatchAllConstraint ()),
2022-08-17 12:20:07 +00:00
]);
2018-08-20 20:06:46 +00:00
2022-08-17 12:20:07 +00:00
$packages = [];
2018-08-20 20:06:46 +00:00
$packages [] = $a = new Package ( 'a/a' , '1.0' , '1.0' );
$packages [] = $b = new Package ( 'b/b' , '1.0' , '1.0' );
2022-08-17 12:20:07 +00:00
$a -> setAutoload ([ 'psr-0' => [ 'A' => 'src/' , 'A\\B' => 'lib/' ]]);
$a -> setRequires ([
2021-08-21 15:41:52 +00:00
'c/c' => new Link ( 'a/a' , 'c/c' , new MatchAllConstraint ()),
2022-08-17 12:20:07 +00:00
]);
$b -> setAutoload ([ 'psr-0' => [ 'B\\Sub\\Name' => 'src/' ]]);
$b -> setReplaces ([
2021-08-21 15:41:52 +00:00
'c/c' => new Link ( 'b/b' , 'c/c' , new MatchAllConstraint ()),
2022-08-17 12:20:07 +00:00
]);
2018-08-20 20:06:46 +00:00
$this -> repository -> expects ( $this -> once ())
-> method ( 'getCanonicalPackages' )
-> will ( $this -> returnValue ( $packages ));
$this -> fs -> ensureDirectoryExists ( $this -> vendorDir . '/composer' );
$this -> fs -> ensureDirectoryExists ( $this -> vendorDir . '/a/a/src' );
$this -> fs -> ensureDirectoryExists ( $this -> vendorDir . '/a/a/lib' );
$this -> fs -> ensureDirectoryExists ( $this -> vendorDir . '/b/b/src' );
2023-10-06 07:34:10 +00:00
$this -> generator -> dump ( $this -> config , $this -> repository , $package , $this -> im , 'composer' , false , '_5' );
2024-05-29 21:12:06 +00:00
self :: assertAutoloadFiles ( 'vendors' , $this -> vendorDir . '/composer' );
self :: assertFileExists ( $this -> vendorDir . '/composer/autoload_classmap.php' , " ClassMap file needs to be generated, even if empty. " );
2018-08-20 20:06:46 +00:00
}
2022-02-18 09:38:54 +00:00
public function testNonDevAutoloadReplacesNestedRequirements () : void
2020-02-24 12:43:00 +00:00
{
2020-11-12 09:35:13 +00:00
$package = new RootPackage ( 'root/a' , '1.0' , '1.0' );
2022-08-17 12:20:07 +00:00
$package -> setRequires ([
2021-08-21 15:41:52 +00:00
'a/a' => new Link ( 'a' , 'a/a' , new MatchAllConstraint ()),
2022-08-17 12:20:07 +00:00
]);
2020-02-24 12:43:00 +00:00
2022-08-17 12:20:07 +00:00
$packages = [];
2020-02-24 12:43:00 +00:00
$packages [] = $a = new Package ( 'a/a' , '1.0' , '1.0' );
$packages [] = $b = new Package ( 'b/b' , '1.0' , '1.0' );
$packages [] = $c = new Package ( 'c/c' , '1.0' , '1.0' );
$packages [] = $d = new Package ( 'd/d' , '1.0' , '1.0' );
$packages [] = $e = new Package ( 'e/e' , '1.0' , '1.0' );
2022-08-17 12:20:07 +00:00
$a -> setAutoload ([ 'classmap' => [ 'src/A.php' ]]);
$a -> setRequires ([
2021-08-21 15:41:52 +00:00
'b/b' => new Link ( 'a/a' , 'b/b' , new MatchAllConstraint ()),
2022-08-17 12:20:07 +00:00
]);
$b -> setAutoload ([ 'classmap' => [ 'src/B.php' ]]);
$b -> setRequires ([
2021-08-21 15:41:52 +00:00
'e/e' => new Link ( 'b/b' , 'e/e' , new MatchAllConstraint ()),
2022-08-17 12:20:07 +00:00
]);
$c -> setAutoload ([ 'classmap' => [ 'src/C.php' ]]);
$c -> setReplaces ([
2021-08-21 15:41:52 +00:00
'b/b' => new Link ( 'c/c' , 'b/b' , new MatchAllConstraint ()),
2022-08-17 12:20:07 +00:00
]);
$c -> setRequires ([
2021-08-21 15:41:52 +00:00
'd/d' => new Link ( 'c/c' , 'd/d' , new MatchAllConstraint ()),
2022-08-17 12:20:07 +00:00
]);
$d -> setAutoload ([ 'classmap' => [ 'src/D.php' ]]);
$e -> setAutoload ([ 'classmap' => [ 'src/E.php' ]]);
2020-02-24 12:43:00 +00:00
$this -> repository -> expects ( $this -> once ())
-> method ( 'getCanonicalPackages' )
-> will ( $this -> returnValue ( $packages ));
$this -> fs -> ensureDirectoryExists ( $this -> vendorDir . '/a/a/src' );
$this -> fs -> ensureDirectoryExists ( $this -> vendorDir . '/b/b/src' );
$this -> fs -> ensureDirectoryExists ( $this -> vendorDir . '/c/c/src' );
$this -> fs -> ensureDirectoryExists ( $this -> vendorDir . '/d/d/src' );
$this -> fs -> ensureDirectoryExists ( $this -> vendorDir . '/e/e/src' );
file_put_contents ( $this -> vendorDir . '/a/a/src/A.php' , '<?php class A {}' );
file_put_contents ( $this -> vendorDir . '/b/b/src/B.php' , '<?php class B {}' );
file_put_contents ( $this -> vendorDir . '/c/c/src/C.php' , '<?php class C {}' );
file_put_contents ( $this -> vendorDir . '/d/d/src/D.php' , '<?php class D {}' );
file_put_contents ( $this -> vendorDir . '/e/e/src/E.php' , '<?php class E {}' );
2023-10-06 07:34:10 +00:00
$this -> generator -> dump ( $this -> config , $this -> repository , $package , $this -> im , 'composer' , false , '_5' );
2020-02-24 12:43:00 +00:00
2024-05-29 21:12:06 +00:00
self :: assertAutoloadFiles ( 'classmap9' , $this -> vendorDir . '/composer' , 'classmap' );
2020-02-24 12:43:00 +00:00
}
2022-02-18 09:38:54 +00:00
public function testPharAutoload () : void
2019-04-11 18:23:31 +00:00
{
2020-11-12 09:35:13 +00:00
$package = new RootPackage ( 'root/a' , '1.0' , '1.0' );
2022-08-17 12:20:07 +00:00
$package -> setRequires ([
2021-08-21 15:41:52 +00:00
'a/a' => new Link ( 'a' , 'a/a' , new MatchAllConstraint ()),
2022-08-17 12:20:07 +00:00
]);
2019-04-11 18:23:31 +00:00
2022-08-17 12:20:07 +00:00
$package -> setAutoload ([
'psr-0' => [
2019-04-11 18:23:31 +00:00
'Foo' => 'foo.phar' ,
'Bar' => 'dir/bar.phar/src' ,
2022-08-17 12:20:07 +00:00
],
'psr-4' => [
2019-04-11 18:23:31 +00:00
'Baz\\' => 'baz.phar' ,
'Qux\\' => 'dir/qux.phar/src' ,
2022-08-17 12:20:07 +00:00
],
]);
2019-04-11 18:23:31 +00:00
$vendorPackage = new Package ( 'a/a' , '1.0' , '1.0' );
2022-08-17 12:20:07 +00:00
$vendorPackage -> setAutoload ([
'psr-0' => [
2019-04-11 18:23:31 +00:00
'Lorem' => 'lorem.phar' ,
'Ipsum' => 'dir/ipsum.phar/src' ,
2022-08-17 12:20:07 +00:00
],
'psr-4' => [
2019-04-11 18:23:31 +00:00
'Dolor\\' => 'dolor.phar' ,
'Sit\\' => 'dir/sit.phar/src' ,
2022-08-17 12:20:07 +00:00
],
]);
2019-04-11 18:23:31 +00:00
$this -> repository -> expects ( $this -> once ())
-> method ( 'getCanonicalPackages' )
2022-08-17 12:20:07 +00:00
-> will ( $this -> returnValue ([ $vendorPackage ]));
2019-04-11 18:23:31 +00:00
2023-10-06 07:34:10 +00:00
$this -> generator -> dump ( $this -> config , $this -> repository , $package , $this -> im , 'composer' , true , 'Phar' );
2019-04-11 18:23:31 +00:00
2024-05-29 21:12:06 +00:00
self :: assertAutoloadFiles ( 'phar' , $this -> vendorDir . '/composer' );
self :: assertAutoloadFiles ( 'phar_psr4' , $this -> vendorDir . '/composer' , 'psr4' );
self :: assertAutoloadFiles ( 'phar_static' , $this -> vendorDir . '/composer' , 'static' );
2019-04-11 18:23:31 +00:00
}
2022-02-18 09:38:54 +00:00
public function testPSRToClassMapIgnoresNonExistingDir () : void
2012-11-27 13:23:10 +00:00
{
2020-11-12 09:35:13 +00:00
$package = new RootPackage ( 'root/a' , '1.0' , '1.0' );
2012-11-27 13:23:10 +00:00
2022-08-17 12:20:07 +00:00
$package -> setAutoload ([
'psr-0' => [ 'Prefix' => 'foo/bar/non/existing/' ],
'psr-4' => [ 'Prefix\\' => 'foo/bar/non/existing2/' ],
]);
2012-11-27 13:23:10 +00:00
$this -> repository -> expects ( $this -> once ())
2013-04-28 20:32:46 +00:00
-> method ( 'getCanonicalPackages' )
2022-08-17 12:20:07 +00:00
-> will ( $this -> returnValue ([]));
2012-11-27 13:23:10 +00:00
2023-10-06 07:34:10 +00:00
$this -> generator -> dump ( $this -> config , $this -> repository , $package , $this -> im , 'composer' , true , '_8' );
2024-05-29 21:12:06 +00:00
self :: assertFileExists ( $this -> vendorDir . '/composer/autoload_classmap.php' , " ClassMap file needs to be generated. " );
self :: assertEquals (
2022-08-17 12:20:07 +00:00
[
2020-04-19 14:00:21 +00:00
'Composer\\InstalledVersions' => $this -> vendorDir . '/composer/InstalledVersions.php' ,
2022-08-17 12:20:07 +00:00
],
2012-11-27 13:23:10 +00:00
include $this -> vendorDir . '/composer/autoload_classmap.php'
);
}
2022-02-18 09:38:54 +00:00
public function testPSRToClassMapIgnoresNonPSRClasses () : void
2019-10-29 18:18:48 +00:00
{
2020-11-12 09:35:13 +00:00
$package = new RootPackage ( 'root/a' , '1.0' , '1.0' );
2019-10-29 18:18:48 +00:00
2022-08-17 12:20:07 +00:00
$package -> setAutoload ([
'psr-0' => [ 'psr0_' => 'psr0/' ],
'psr-4' => [ 'psr4\\' => 'psr4/' ],
]);
2019-10-29 18:18:48 +00:00
$this -> repository -> expects ( $this -> once ())
-> method ( 'getCanonicalPackages' )
2022-08-17 12:20:07 +00:00
-> will ( $this -> returnValue ([]));
2019-10-29 18:18:48 +00:00
$this -> fs -> ensureDirectoryExists ( $this -> workingDir . '/psr0/psr0' );
$this -> fs -> ensureDirectoryExists ( $this -> workingDir . '/psr4' );
file_put_contents ( $this -> workingDir . '/psr0/psr0/match.php' , '<?php class psr0_match {}' );
file_put_contents ( $this -> workingDir . '/psr0/psr0/badfile.php' , '<?php class psr0_badclass {}' );
file_put_contents ( $this -> workingDir . '/psr4/match.php' , '<?php namespace psr4; class match {}' );
file_put_contents ( $this -> workingDir . '/psr4/badfile.php' , '<?php namespace psr4; class badclass {}' );
2023-10-06 07:34:10 +00:00
$this -> generator -> dump ( $this -> config , $this -> repository , $package , $this -> im , 'composer' , true , '_1' );
2024-05-29 21:12:06 +00:00
self :: assertFileExists ( $this -> vendorDir . '/composer/autoload_classmap.php' , " ClassMap file needs to be generated. " );
2019-11-01 14:13:28 +00:00
2019-10-29 18:18:48 +00:00
$expectedClassmap = <<< EOF
< ? php
// autoload_classmap.php @generated by Composer
2020-11-06 23:08:03 +00:00
\ $vendorDir = dirname ( __DIR__ );
2019-10-29 18:18:48 +00:00
\ $baseDir = dirname ( \ $vendorDir );
return array (
2020-04-19 14:00:21 +00:00
'Composer\\\\InstalledVersions' => \ $vendorDir . '/composer/InstalledVersions.php' ,
2019-10-29 18:18:48 +00:00
'psr0_match' => \ $baseDir . '/psr0/psr0/match.php' ,
'psr4\\\\match' => \ $baseDir . '/psr4/match.php' ,
);
EOF ;
2024-05-29 21:12:06 +00:00
self :: assertStringEqualsFile ( $this -> vendorDir . '/composer/autoload_classmap.php' , $expectedClassmap );
2019-10-29 18:18:48 +00:00
}
2022-02-18 09:38:54 +00:00
public function testVendorsClassMapAutoloading () : void
2012-03-05 13:10:01 +00:00
{
2020-11-12 09:35:13 +00:00
$package = new RootPackage ( 'root/a' , '1.0' , '1.0' );
2022-08-17 12:20:07 +00:00
$package -> setRequires ([
2021-08-21 15:41:52 +00:00
'a/a' => new Link ( 'a' , 'a/a' , new MatchAllConstraint ()),
'b/b' => new Link ( 'a' , 'b/b' , new MatchAllConstraint ()),
2022-08-17 12:20:07 +00:00
]);
2012-03-05 13:10:01 +00:00
2022-08-17 12:20:07 +00:00
$packages = [];
2012-08-23 13:52:40 +00:00
$packages [] = $a = new Package ( 'a/a' , '1.0' , '1.0' );
$packages [] = $b = new Package ( 'b/b' , '1.0' , '1.0' );
2022-08-17 12:20:07 +00:00
$a -> setAutoload ([ 'classmap' => [ 'src/' ]]);
$b -> setAutoload ([ 'classmap' => [ 'src/' , 'lib/' ]]);
2012-03-05 13:10:01 +00:00
$this -> repository -> expects ( $this -> once ())
2013-04-28 20:32:46 +00:00
-> method ( 'getCanonicalPackages' )
2012-03-05 13:10:01 +00:00
-> will ( $this -> returnValue ( $packages ));
2012-06-19 21:15:42 +00:00
$this -> fs -> ensureDirectoryExists ( $this -> vendorDir . '/composer' );
$this -> fs -> ensureDirectoryExists ( $this -> vendorDir . '/a/a/src' );
$this -> fs -> ensureDirectoryExists ( $this -> vendorDir . '/b/b/src' );
$this -> fs -> ensureDirectoryExists ( $this -> vendorDir . '/b/b/lib' );
2012-03-05 13:10:01 +00:00
file_put_contents ( $this -> vendorDir . '/a/a/src/a.php' , '<?php class ClassMapFoo {}' );
file_put_contents ( $this -> vendorDir . '/b/b/src/b.php' , '<?php class ClassMapBar {}' );
file_put_contents ( $this -> vendorDir . '/b/b/lib/c.php' , '<?php class ClassMapBaz {}' );
2023-10-06 07:34:10 +00:00
$this -> generator -> dump ( $this -> config , $this -> repository , $package , $this -> im , 'composer' , false , '_6' );
2024-05-29 21:12:06 +00:00
self :: assertFileExists ( $this -> vendorDir . '/composer/autoload_classmap.php' , " ClassMap file needs to be generated. " );
self :: assertEquals (
2022-08-17 12:20:07 +00:00
[
2012-11-10 20:54:23 +00:00
'ClassMapBar' => $this -> vendorDir . '/b/b/src/b.php' ,
'ClassMapBaz' => $this -> vendorDir . '/b/b/lib/c.php' ,
'ClassMapFoo' => $this -> vendorDir . '/a/a/src/a.php' ,
2020-04-19 14:00:21 +00:00
'Composer\\InstalledVersions' => $this -> vendorDir . '/composer/InstalledVersions.php' ,
2022-08-17 12:20:07 +00:00
],
2013-03-14 09:23:35 +00:00
include $this -> vendorDir . '/composer/autoload_classmap.php'
2012-03-05 13:10:01 +00:00
);
2024-05-29 21:12:06 +00:00
self :: assertAutoloadFiles ( 'classmap4' , $this -> vendorDir . '/composer' , 'classmap' );
2011-12-03 22:20:06 +00:00
}
2022-02-18 09:38:54 +00:00
public function testVendorsClassMapAutoloadingWithTargetDir () : void
2013-02-19 14:23:43 +00:00
{
2020-11-12 09:35:13 +00:00
$package = new RootPackage ( 'root/a' , '1.0' , '1.0' );
2022-08-17 12:20:07 +00:00
$package -> setRequires ([
2021-08-21 15:41:52 +00:00
'a/a' => new Link ( 'a' , 'a/a' , new MatchAllConstraint ()),
'b/b' => new Link ( 'a' , 'b/b' , new MatchAllConstraint ()),
2022-08-17 12:20:07 +00:00
]);
2013-02-19 14:23:43 +00:00
2022-08-17 12:20:07 +00:00
$packages = [];
2013-02-19 14:23:43 +00:00
$packages [] = $a = new Package ( 'a/a' , '1.0' , '1.0' );
$packages [] = $b = new Package ( 'b/b' , '1.0' , '1.0' );
2022-08-17 12:20:07 +00:00
$a -> setAutoload ([ 'classmap' => [ 'target/src/' , 'lib/' ]]);
2013-02-19 14:23:43 +00:00
$a -> setTargetDir ( 'target' );
2022-08-17 12:20:07 +00:00
$b -> setAutoload ([ 'classmap' => [ 'src/' ]]);
2013-02-19 14:23:43 +00:00
$this -> repository -> expects ( $this -> once ())
2013-04-28 20:32:46 +00:00
-> method ( 'getCanonicalPackages' )
2013-02-19 14:23:43 +00:00
-> will ( $this -> returnValue ( $packages ));
$this -> fs -> ensureDirectoryExists ( $this -> vendorDir . '/composer' );
$this -> fs -> ensureDirectoryExists ( $this -> vendorDir . '/a/a/target/src' );
$this -> fs -> ensureDirectoryExists ( $this -> vendorDir . '/a/a/target/lib' );
$this -> fs -> ensureDirectoryExists ( $this -> vendorDir . '/b/b/src' );
file_put_contents ( $this -> vendorDir . '/a/a/target/src/a.php' , '<?php class ClassMapFoo {}' );
file_put_contents ( $this -> vendorDir . '/a/a/target/lib/b.php' , '<?php class ClassMapBar {}' );
file_put_contents ( $this -> vendorDir . '/b/b/src/c.php' , '<?php class ClassMapBaz {}' );
2023-10-06 07:34:10 +00:00
$this -> generator -> dump ( $this -> config , $this -> repository , $package , $this -> im , 'composer' , false , '_6' );
2024-05-29 21:12:06 +00:00
self :: assertFileExists ( $this -> vendorDir . '/composer/autoload_classmap.php' , " ClassMap file needs to be generated. " );
self :: assertEquals (
2022-08-17 12:20:07 +00:00
[
2013-02-19 14:23:43 +00:00
'ClassMapBar' => $this -> vendorDir . '/a/a/target/lib/b.php' ,
'ClassMapBaz' => $this -> vendorDir . '/b/b/src/c.php' ,
'ClassMapFoo' => $this -> vendorDir . '/a/a/target/src/a.php' ,
2020-04-19 14:00:21 +00:00
'Composer\\InstalledVersions' => $this -> vendorDir . '/composer/InstalledVersions.php' ,
2022-08-17 12:20:07 +00:00
],
2013-03-14 09:23:35 +00:00
include $this -> vendorDir . '/composer/autoload_classmap.php'
2013-02-19 14:23:43 +00:00
);
}
2022-02-18 09:38:54 +00:00
public function testClassMapAutoloadingEmptyDirAndExactFile () : void
2012-04-01 18:23:47 +00:00
{
2020-11-12 09:35:13 +00:00
$package = new RootPackage ( 'root/a' , '1.0' , '1.0' );
2022-08-17 12:20:07 +00:00
$package -> setRequires ([
2021-08-21 15:41:52 +00:00
'a/a' => new Link ( 'a' , 'a/a' , new MatchAllConstraint ()),
'b/b' => new Link ( 'a' , 'b/b' , new MatchAllConstraint ()),
'c/c' => new Link ( 'a' , 'c/c' , new MatchAllConstraint ()),
2022-08-17 12:20:07 +00:00
]);
2012-04-01 18:23:47 +00:00
2022-08-17 12:20:07 +00:00
$packages = [];
2012-08-23 13:52:40 +00:00
$packages [] = $a = new Package ( 'a/a' , '1.0' , '1.0' );
$packages [] = $b = new Package ( 'b/b' , '1.0' , '1.0' );
$packages [] = $c = new Package ( 'c/c' , '1.0' , '1.0' );
2022-08-17 12:20:07 +00:00
$a -> setAutoload ([ 'classmap' => [ '' ]]);
$b -> setAutoload ([ 'classmap' => [ 'test.php' ]]);
$c -> setAutoload ([ 'classmap' => [ './' ]]);
2012-04-01 18:23:47 +00:00
$this -> repository -> expects ( $this -> once ())
2013-04-28 20:32:46 +00:00
-> method ( 'getCanonicalPackages' )
2012-04-01 18:23:47 +00:00
-> will ( $this -> returnValue ( $packages ));
2012-06-19 21:15:42 +00:00
$this -> fs -> ensureDirectoryExists ( $this -> vendorDir . '/composer' );
$this -> fs -> ensureDirectoryExists ( $this -> vendorDir . '/a/a/src' );
$this -> fs -> ensureDirectoryExists ( $this -> vendorDir . '/b/b' );
$this -> fs -> ensureDirectoryExists ( $this -> vendorDir . '/c/c/foo' );
2012-04-01 18:23:47 +00:00
file_put_contents ( $this -> vendorDir . '/a/a/src/a.php' , '<?php class ClassMapFoo {}' );
file_put_contents ( $this -> vendorDir . '/b/b/test.php' , '<?php class ClassMapBar {}' );
file_put_contents ( $this -> vendorDir . '/c/c/foo/test.php' , '<?php class ClassMapBaz {}' );
2023-10-06 07:34:10 +00:00
$this -> generator -> dump ( $this -> config , $this -> repository , $package , $this -> im , 'composer' , false , '_7' );
2024-05-29 21:12:06 +00:00
self :: assertFileExists ( $this -> vendorDir . '/composer/autoload_classmap.php' , " ClassMap file needs to be generated. " );
self :: assertEquals (
2022-08-17 12:20:07 +00:00
[
2012-11-10 20:54:23 +00:00
'ClassMapBar' => $this -> vendorDir . '/b/b/test.php' ,
'ClassMapBaz' => $this -> vendorDir . '/c/c/foo/test.php' ,
'ClassMapFoo' => $this -> vendorDir . '/a/a/src/a.php' ,
2020-04-19 14:00:21 +00:00
'Composer\\InstalledVersions' => $this -> vendorDir . '/composer/InstalledVersions.php' ,
2022-08-17 12:20:07 +00:00
],
2013-03-14 09:23:35 +00:00
include $this -> vendorDir . '/composer/autoload_classmap.php'
2012-04-01 18:23:47 +00:00
);
2024-05-29 21:12:06 +00:00
self :: assertAutoloadFiles ( 'classmap5' , $this -> vendorDir . '/composer' , 'classmap' );
self :: assertStringNotContainsString ( '$loader->setClassMapAuthoritative(true);' , ( string ) file_get_contents ( $this -> vendorDir . '/composer/autoload_real.php' ));
self :: assertStringNotContainsString ( '$loader->setApcuPrefix(' , ( string ) file_get_contents ( $this -> vendorDir . '/composer/autoload_real.php' ));
2015-01-04 00:35:25 +00:00
}
2022-02-18 09:38:54 +00:00
public function testClassMapAutoloadingAuthoritativeAndApcu () : void
2015-01-04 00:35:25 +00:00
{
2020-11-12 09:35:13 +00:00
$package = new RootPackage ( 'root/a' , '1.0' , '1.0' );
2022-08-17 12:20:07 +00:00
$package -> setRequires ([
2021-08-21 15:41:52 +00:00
'a/a' => new Link ( 'a' , 'a/a' , new MatchAllConstraint ()),
'b/b' => new Link ( 'a' , 'b/b' , new MatchAllConstraint ()),
'c/c' => new Link ( 'a' , 'c/c' , new MatchAllConstraint ()),
2022-08-17 12:20:07 +00:00
]);
2015-01-04 00:35:25 +00:00
2022-08-17 12:20:07 +00:00
$packages = [];
2015-01-04 00:35:25 +00:00
$packages [] = $a = new Package ( 'a/a' , '1.0' , '1.0' );
$packages [] = $b = new Package ( 'b/b' , '1.0' , '1.0' );
$packages [] = $c = new Package ( 'c/c' , '1.0' , '1.0' );
2022-08-17 12:20:07 +00:00
$a -> setAutoload ([ 'psr-4' => [ '' => 'src/' ]]);
$b -> setAutoload ([ 'psr-4' => [ '' => './' ]]);
$c -> setAutoload ([ 'psr-4' => [ '' => 'foo/' ]]);
2015-01-04 00:35:25 +00:00
$this -> repository -> expects ( $this -> once ())
-> method ( 'getCanonicalPackages' )
-> will ( $this -> returnValue ( $packages ));
$this -> fs -> ensureDirectoryExists ( $this -> vendorDir . '/composer' );
$this -> fs -> ensureDirectoryExists ( $this -> vendorDir . '/a/a/src' );
$this -> fs -> ensureDirectoryExists ( $this -> vendorDir . '/b/b' );
$this -> fs -> ensureDirectoryExists ( $this -> vendorDir . '/c/c/foo' );
2015-08-16 19:56:52 +00:00
file_put_contents ( $this -> vendorDir . '/a/a/src/ClassMapFoo.php' , '<?php class ClassMapFoo {}' );
file_put_contents ( $this -> vendorDir . '/b/b/ClassMapBar.php' , '<?php class ClassMapBar {}' );
file_put_contents ( $this -> vendorDir . '/c/c/foo/ClassMapBaz.php' , '<?php class ClassMapBaz {}' );
2015-01-04 00:35:25 +00:00
2015-08-16 19:56:52 +00:00
$this -> generator -> setClassMapAuthoritative ( true );
2016-07-28 08:23:39 +00:00
$this -> generator -> setApcu ( true );
2023-10-06 07:34:10 +00:00
$this -> generator -> dump ( $this -> config , $this -> repository , $package , $this -> im , 'composer' , false , '_7' );
2015-08-16 19:56:52 +00:00
2024-05-29 21:12:06 +00:00
self :: assertFileExists ( $this -> vendorDir . '/composer/autoload_classmap.php' , " ClassMap file needs to be generated. " );
self :: assertEquals (
2022-08-17 12:20:07 +00:00
[
2015-08-16 19:56:52 +00:00
'ClassMapBar' => $this -> vendorDir . '/b/b/ClassMapBar.php' ,
'ClassMapBaz' => $this -> vendorDir . '/c/c/foo/ClassMapBaz.php' ,
'ClassMapFoo' => $this -> vendorDir . '/a/a/src/ClassMapFoo.php' ,
2020-04-19 14:00:21 +00:00
'Composer\\InstalledVersions' => $this -> vendorDir . '/composer/InstalledVersions.php' ,
2022-08-17 12:20:07 +00:00
],
2015-01-04 00:35:25 +00:00
include $this -> vendorDir . '/composer/autoload_classmap.php'
);
2024-05-29 21:12:06 +00:00
self :: assertAutoloadFiles ( 'classmap8' , $this -> vendorDir . '/composer' , 'classmap' );
2015-01-04 00:35:25 +00:00
2024-05-29 21:12:06 +00:00
self :: assertStringContainsString ( '$loader->setClassMapAuthoritative(true);' , ( string ) file_get_contents ( $this -> vendorDir . '/composer/autoload_real.php' ));
self :: assertStringContainsString ( '$loader->setApcuPrefix(' , ( string ) file_get_contents ( $this -> vendorDir . '/composer/autoload_real.php' ));
2012-04-01 18:23:47 +00:00
}
2022-02-18 09:38:54 +00:00
public function testClassMapAutoloadingAuthoritativeAndApcuPrefix () : void
2020-09-14 19:54:44 +00:00
{
2020-11-12 09:35:13 +00:00
$package = new RootPackage ( 'root/a' , '1.0' , '1.0' );
2022-08-17 12:20:07 +00:00
$package -> setRequires ([
2021-08-21 15:41:52 +00:00
'a/a' => new Link ( 'a' , 'a/a' , new MatchAllConstraint ()),
'b/b' => new Link ( 'a' , 'b/b' , new MatchAllConstraint ()),
'c/c' => new Link ( 'a' , 'c/c' , new MatchAllConstraint ()),
2022-08-17 12:20:07 +00:00
]);
2020-09-14 19:54:44 +00:00
2022-08-17 12:20:07 +00:00
$packages = [];
2020-09-14 19:54:44 +00:00
$packages [] = $a = new Package ( 'a/a' , '1.0' , '1.0' );
$packages [] = $b = new Package ( 'b/b' , '1.0' , '1.0' );
$packages [] = $c = new Package ( 'c/c' , '1.0' , '1.0' );
2022-08-17 12:20:07 +00:00
$a -> setAutoload ([ 'psr-4' => [ '' => 'src/' ]]);
$b -> setAutoload ([ 'psr-4' => [ '' => './' ]]);
$c -> setAutoload ([ 'psr-4' => [ '' => 'foo/' ]]);
2020-09-14 19:54:44 +00:00
$this -> repository -> expects ( $this -> once ())
-> method ( 'getCanonicalPackages' )
-> will ( $this -> returnValue ( $packages ));
$this -> fs -> ensureDirectoryExists ( $this -> vendorDir . '/composer' );
$this -> fs -> ensureDirectoryExists ( $this -> vendorDir . '/a/a/src' );
$this -> fs -> ensureDirectoryExists ( $this -> vendorDir . '/b/b' );
$this -> fs -> ensureDirectoryExists ( $this -> vendorDir . '/c/c/foo' );
file_put_contents ( $this -> vendorDir . '/a/a/src/ClassMapFoo.php' , '<?php class ClassMapFoo {}' );
file_put_contents ( $this -> vendorDir . '/b/b/ClassMapBar.php' , '<?php class ClassMapBar {}' );
file_put_contents ( $this -> vendorDir . '/c/c/foo/ClassMapBaz.php' , '<?php class ClassMapBaz {}' );
$this -> generator -> setClassMapAuthoritative ( true );
2020-09-16 08:49:00 +00:00
$this -> generator -> setApcu ( true , 'custom\'Prefix' );
2023-10-06 07:34:10 +00:00
$this -> generator -> dump ( $this -> config , $this -> repository , $package , $this -> im , 'composer' , false , '_7' );
2020-09-14 19:54:44 +00:00
2024-05-29 21:12:06 +00:00
self :: assertFileExists ( $this -> vendorDir . '/composer/autoload_classmap.php' , " ClassMap file needs to be generated. " );
self :: assertEquals (
2022-08-17 12:20:07 +00:00
[
2020-09-14 19:54:44 +00:00
'ClassMapBar' => $this -> vendorDir . '/b/b/ClassMapBar.php' ,
'ClassMapBaz' => $this -> vendorDir . '/c/c/foo/ClassMapBaz.php' ,
'ClassMapFoo' => $this -> vendorDir . '/a/a/src/ClassMapFoo.php' ,
'Composer\\InstalledVersions' => $this -> vendorDir . '/composer/InstalledVersions.php' ,
2022-08-17 12:20:07 +00:00
],
2020-09-14 19:54:44 +00:00
include $this -> vendorDir . '/composer/autoload_classmap.php'
);
2024-05-29 21:12:06 +00:00
self :: assertAutoloadFiles ( 'classmap8' , $this -> vendorDir . '/composer' , 'classmap' );
2020-09-14 19:54:44 +00:00
2024-05-29 21:12:06 +00:00
self :: assertStringContainsString ( '$loader->setClassMapAuthoritative(true);' , ( string ) file_get_contents ( $this -> vendorDir . '/composer/autoload_real.php' ));
self :: assertStringContainsString ( '$loader->setApcuPrefix(\'custom\\\'Prefix\');' , ( string ) file_get_contents ( $this -> vendorDir . '/composer/autoload_real.php' ));
2020-09-14 19:54:44 +00:00
}
2022-02-18 09:38:54 +00:00
public function testFilesAutoloadGeneration () : void
2012-06-02 16:18:33 +00:00
{
2020-11-12 09:35:13 +00:00
$package = new RootPackage ( 'root/a' , '1.0' , '1.0' );
2022-08-17 12:20:07 +00:00
$package -> setAutoload ([ 'files' => [ 'root.php' ]]);
$package -> setRequires ([
2021-08-21 15:41:52 +00:00
'a/a' => new Link ( 'a' , 'a/a' , new MatchAllConstraint ()),
'b/b' => new Link ( 'a' , 'b/b' , new MatchAllConstraint ()),
'c/c' => new Link ( 'a' , 'c/c' , new MatchAllConstraint ()),
2022-08-17 12:20:07 +00:00
]);
2012-06-02 16:18:33 +00:00
2022-08-17 12:20:07 +00:00
$packages = [];
2012-08-23 13:52:40 +00:00
$packages [] = $a = new Package ( 'a/a' , '1.0' , '1.0' );
$packages [] = $b = new Package ( 'b/b' , '1.0' , '1.0' );
2013-01-31 22:39:57 +00:00
$packages [] = $c = new Package ( 'c/c' , '1.0' , '1.0' );
2022-08-17 12:20:07 +00:00
$a -> setAutoload ([ 'files' => [ 'test.php' ]]);
$b -> setAutoload ([ 'files' => [ 'test2.php' ]]);
$c -> setAutoload ([ 'files' => [ 'test3.php' , 'foo/bar/test4.php' ]]);
2013-01-31 22:39:57 +00:00
$c -> setTargetDir ( 'foo/bar' );
2012-06-02 16:18:33 +00:00
$this -> repository -> expects ( $this -> once ())
2013-04-28 20:32:46 +00:00
-> method ( 'getCanonicalPackages' )
2012-06-02 16:18:33 +00:00
-> will ( $this -> returnValue ( $packages ));
2012-06-19 21:15:42 +00:00
$this -> fs -> ensureDirectoryExists ( $this -> vendorDir . '/a/a' );
$this -> fs -> ensureDirectoryExists ( $this -> vendorDir . '/b/b' );
2013-01-31 22:39:57 +00:00
$this -> fs -> ensureDirectoryExists ( $this -> vendorDir . '/c/c/foo/bar' );
2012-06-02 16:18:33 +00:00
file_put_contents ( $this -> vendorDir . '/a/a/test.php' , '<?php function testFilesAutoloadGeneration1() {}' );
file_put_contents ( $this -> vendorDir . '/b/b/test2.php' , '<?php function testFilesAutoloadGeneration2() {}' );
2013-01-31 22:39:57 +00:00
file_put_contents ( $this -> vendorDir . '/c/c/foo/bar/test3.php' , '<?php function testFilesAutoloadGeneration3() {}' );
file_put_contents ( $this -> vendorDir . '/c/c/foo/bar/test4.php' , '<?php function testFilesAutoloadGeneration4() {}' );
2012-08-18 12:58:55 +00:00
file_put_contents ( $this -> workingDir . '/root.php' , '<?php function testFilesAutoloadGenerationRoot() {}' );
2012-06-02 16:18:33 +00:00
2023-10-06 07:34:10 +00:00
$this -> generator -> dump ( $this -> config , $this -> repository , $package , $this -> im , 'composer' , false , 'FilesAutoload' );
2024-05-29 21:12:06 +00:00
self :: assertFileContentEquals ( __DIR__ . '/Fixtures/autoload_functions.php' , $this -> vendorDir . '/autoload.php' );
self :: assertFileContentEquals ( __DIR__ . '/Fixtures/autoload_real_functions.php' , $this -> vendorDir . '/composer/autoload_real.php' );
self :: assertFileContentEquals ( __DIR__ . '/Fixtures/autoload_static_functions.php' , $this -> vendorDir . '/composer/autoload_static.php' );
self :: assertFileContentEquals ( __DIR__ . '/Fixtures/autoload_files_functions.php' , $this -> vendorDir . '/composer/autoload_files.php' );
2012-08-14 17:13:39 +00:00
2020-04-19 14:00:21 +00:00
$loader = require $this -> vendorDir . '/autoload.php' ;
$loader -> unregister ();
2024-05-29 21:12:06 +00:00
self :: assertTrue ( function_exists ( 'testFilesAutoloadGeneration1' ));
self :: assertTrue ( function_exists ( 'testFilesAutoloadGeneration2' ));
self :: assertTrue ( function_exists ( 'testFilesAutoloadGeneration3' ));
self :: assertTrue ( function_exists ( 'testFilesAutoloadGeneration4' ));
self :: assertTrue ( function_exists ( 'testFilesAutoloadGenerationRoot' ));
2012-06-02 16:18:33 +00:00
}
2022-02-18 09:38:54 +00:00
public function testFilesAutoloadGenerationRemoveExtraEntitiesFromAutoloadFiles () : void
2015-08-15 08:12:16 +00:00
{
2020-11-12 09:35:13 +00:00
$autoloadPackage = new RootPackage ( 'root/a' , '1.0' , '1.0' );
2022-08-17 12:20:07 +00:00
$autoloadPackage -> setAutoload ([ 'files' => [ 'root.php' ]]);
$autoloadPackage -> setIncludePaths ([ '/lib' , '/src' ]);
2015-08-15 08:12:16 +00:00
2020-11-12 09:35:13 +00:00
$notAutoloadPackage = new RootPackage ( 'root/a' , '1.0' , '1.0' );
2015-08-15 08:12:16 +00:00
2022-08-17 12:20:07 +00:00
$requires = [
2021-08-21 15:41:52 +00:00
'a/a' => new Link ( 'a' , 'a/a' , new MatchAllConstraint ()),
'b/b' => new Link ( 'a' , 'b/b' , new MatchAllConstraint ()),
'c/c' => new Link ( 'a' , 'c/c' , new MatchAllConstraint ()),
2022-08-17 12:20:07 +00:00
];
2018-05-08 00:24:37 +00:00
$autoloadPackage -> setRequires ( $requires );
$notAutoloadPackage -> setRequires ( $requires );
2022-08-17 12:20:07 +00:00
$autoloadPackages = [];
2015-08-15 08:12:16 +00:00
$autoloadPackages [] = $a = new Package ( 'a/a' , '1.0' , '1.0' );
$autoloadPackages [] = $b = new Package ( 'b/b' , '1.0' , '1.0' );
$autoloadPackages [] = $c = new Package ( 'c/c' , '1.0' , '1.0' );
2022-08-17 12:20:07 +00:00
$a -> setAutoload ([ 'files' => [ 'test.php' ]]);
$a -> setIncludePaths ([ 'lib1' , 'src1' ]);
$b -> setAutoload ([ 'files' => [ 'test2.php' ]]);
$b -> setIncludePaths ([ 'lib2' ]);
$c -> setAutoload ([ 'files' => [ 'test3.php' , 'foo/bar/test4.php' ]]);
$c -> setIncludePaths ([ 'lib3' ]);
2015-08-15 08:12:16 +00:00
$c -> setTargetDir ( 'foo/bar' );
2022-08-17 12:20:07 +00:00
$notAutoloadPackages = [];
2015-08-15 08:12:16 +00:00
$notAutoloadPackages [] = $a = new Package ( 'a/a' , '1.0' , '1.0' );
$notAutoloadPackages [] = $b = new Package ( 'b/b' , '1.0' , '1.0' );
$notAutoloadPackages [] = $c = new Package ( 'c/c' , '1.0' , '1.0' );
2021-12-09 16:09:07 +00:00
$this -> repository -> expects ( $this -> exactly ( 3 ))
2015-08-15 08:12:16 +00:00
-> method ( 'getCanonicalPackages' )
2021-12-09 16:09:07 +00:00
-> willReturnOnConsecutiveCalls (
$autoloadPackages ,
$notAutoloadPackages ,
$notAutoloadPackages
);
2015-08-15 08:12:16 +00:00
$this -> fs -> ensureDirectoryExists ( $this -> vendorDir . '/a/a' );
$this -> fs -> ensureDirectoryExists ( $this -> vendorDir . '/b/b' );
$this -> fs -> ensureDirectoryExists ( $this -> vendorDir . '/c/c/foo/bar' );
file_put_contents ( $this -> vendorDir . '/a/a/test.php' , '<?php function testFilesAutoloadGeneration1() {}' );
file_put_contents ( $this -> vendorDir . '/b/b/test2.php' , '<?php function testFilesAutoloadGeneration2() {}' );
file_put_contents ( $this -> vendorDir . '/c/c/foo/bar/test3.php' , '<?php function testFilesAutoloadGeneration3() {}' );
file_put_contents ( $this -> vendorDir . '/c/c/foo/bar/test4.php' , '<?php function testFilesAutoloadGeneration4() {}' );
file_put_contents ( $this -> workingDir . '/root.php' , '<?php function testFilesAutoloadGenerationRoot() {}' );
2023-10-06 07:34:10 +00:00
$this -> generator -> dump ( $this -> config , $this -> repository , $autoloadPackage , $this -> im , 'composer' , false , 'FilesAutoload' );
2024-05-29 21:12:06 +00:00
self :: assertFileContentEquals ( __DIR__ . '/Fixtures/autoload_functions.php' , $this -> vendorDir . '/autoload.php' );
self :: assertFileContentEquals ( __DIR__ . '/Fixtures/autoload_real_functions_with_include_paths.php' , $this -> vendorDir . '/composer/autoload_real.php' );
self :: assertFileContentEquals ( __DIR__ . '/Fixtures/autoload_static_functions_with_include_paths.php' , $this -> vendorDir . '/composer/autoload_static.php' );
self :: assertFileContentEquals ( __DIR__ . '/Fixtures/autoload_files_functions.php' , $this -> vendorDir . '/composer/autoload_files.php' );
self :: assertFileContentEquals ( __DIR__ . '/Fixtures/include_paths_functions.php' , $this -> vendorDir . '/composer/include_paths.php' );
2015-08-15 08:12:16 +00:00
2023-10-06 07:34:10 +00:00
$this -> generator -> dump ( $this -> config , $this -> repository , $autoloadPackage , $this -> im , 'composer' , false , 'FilesAutoload' );
2024-05-29 21:12:06 +00:00
self :: assertFileContentEquals ( __DIR__ . '/Fixtures/autoload_functions.php' , $this -> vendorDir . '/autoload.php' );
self :: assertFileContentEquals ( __DIR__ . '/Fixtures/autoload_real_functions_with_include_paths.php' , $this -> vendorDir . '/composer/autoload_real.php' );
self :: assertFileContentEquals ( __DIR__ . '/Fixtures/autoload_files_functions_with_removed_extra.php' , $this -> vendorDir . '/composer/autoload_files.php' );
self :: assertFileContentEquals ( __DIR__ . '/Fixtures/include_paths_functions_with_removed_extra.php' , $this -> vendorDir . '/composer/include_paths.php' );
2015-08-15 08:12:16 +00:00
2023-10-06 07:34:10 +00:00
$this -> generator -> dump ( $this -> config , $this -> repository , $notAutoloadPackage , $this -> im , 'composer' , false , 'FilesAutoload' );
2024-05-29 21:12:06 +00:00
self :: assertFileContentEquals ( __DIR__ . '/Fixtures/autoload_functions.php' , $this -> vendorDir . '/autoload.php' );
self :: assertFileContentEquals ( __DIR__ . '/Fixtures/autoload_real_functions_with_removed_include_paths_and_autolad_files.php' , $this -> vendorDir . '/composer/autoload_real.php' );
self :: assertFileContentEquals ( __DIR__ . '/Fixtures/autoload_static_functions_with_removed_include_paths_and_autolad_files.php' , $this -> vendorDir . '/composer/autoload_static.php' );
self :: assertFileDoesNotExist ( $this -> vendorDir . '/composer/autoload_files.php' );
self :: assertFileDoesNotExist ( $this -> vendorDir . '/composer/include_paths.php' );
2015-08-15 08:12:16 +00:00
}
2022-02-18 09:38:54 +00:00
public function testFilesAutoloadOrderByDependencies () : void
2012-09-01 11:24:17 +00:00
{
2020-11-12 09:35:13 +00:00
$package = new RootPackage ( 'root/a' , '1.0' , '1.0' );
2022-08-17 12:20:07 +00:00
$package -> setAutoload ([ 'files' => [ 'root2.php' ]]);
$package -> setRequires ([
2021-08-21 15:41:52 +00:00
'z/foo' => new Link ( 'a' , 'z/foo' , new MatchAllConstraint ()),
'b/bar' => new Link ( 'a' , 'b/bar' , new MatchAllConstraint ()),
'd/d' => new Link ( 'a' , 'd/d' , new MatchAllConstraint ()),
'e/e' => new Link ( 'a' , 'e/e' , new MatchAllConstraint ()),
2022-08-17 12:20:07 +00:00
]);
2012-09-01 11:24:17 +00:00
2022-08-17 12:20:07 +00:00
$packages = [];
2012-10-16 23:00:11 +00:00
$packages [] = $z = new Package ( 'z/foo' , '1.0' , '1.0' );
2012-09-01 11:24:17 +00:00
$packages [] = $b = new Package ( 'b/bar' , '1.0' , '1.0' );
2012-10-17 08:50:53 +00:00
$packages [] = $d = new Package ( 'd/d' , '1.0' , '1.0' );
2012-12-14 14:01:48 +00:00
$packages [] = $c = new Package ( 'c/lorem' , '1.0' , '1.0' );
2012-10-17 08:50:53 +00:00
$packages [] = $e = new Package ( 'e/e' , '1.0' , '1.0' );
2012-09-01 11:24:17 +00:00
2022-03-15 10:04:30 +00:00
// expected order:
// c requires nothing
// d requires c
// b requires c & d
// e requires c
// z requires c
// (b, e, z ordered alphabetically)
2022-08-17 12:20:07 +00:00
$z -> setAutoload ([ 'files' => [ 'testA.php' ]]);
$z -> setRequires ([ 'c/lorem' => new Link ( 'z/foo' , 'c/lorem' , new MatchAllConstraint ())]);
2012-09-01 11:24:17 +00:00
2022-08-17 12:20:07 +00:00
$b -> setAutoload ([ 'files' => [ 'testB.php' ]]);
$b -> setRequires ([ 'c/lorem' => new Link ( 'b/bar' , 'c/lorem' , new MatchAllConstraint ()), 'd/d' => new Link ( 'b/bar' , 'd/d' , new MatchAllConstraint ())]);
2012-09-01 11:24:17 +00:00
2022-08-17 12:20:07 +00:00
$c -> setAutoload ([ 'files' => [ 'testC.php' ]]);
2012-09-01 11:24:17 +00:00
2022-08-17 12:20:07 +00:00
$d -> setAutoload ([ 'files' => [ 'testD.php' ]]);
$d -> setRequires ([ 'c/lorem' => new Link ( 'd/d' , 'c/lorem' , new MatchAllConstraint ())]);
2012-10-17 08:50:53 +00:00
2022-08-17 12:20:07 +00:00
$e -> setAutoload ([ 'files' => [ 'testE.php' ]]);
$e -> setRequires ([ 'c/lorem' => new Link ( 'e/e' , 'c/lorem' , new MatchAllConstraint ())]);
2012-10-17 08:50:53 +00:00
2012-09-01 11:24:17 +00:00
$this -> repository -> expects ( $this -> once ())
2013-04-28 20:32:46 +00:00
-> method ( 'getCanonicalPackages' )
2012-09-01 11:24:17 +00:00
-> will ( $this -> returnValue ( $packages ));
2012-10-16 23:00:11 +00:00
$this -> fs -> ensureDirectoryExists ( $this -> vendorDir . '/z/foo' );
2012-09-01 11:24:17 +00:00
$this -> fs -> ensureDirectoryExists ( $this -> vendorDir . '/b/bar' );
$this -> fs -> ensureDirectoryExists ( $this -> vendorDir . '/c/lorem' );
2012-10-17 08:50:53 +00:00
$this -> fs -> ensureDirectoryExists ( $this -> vendorDir . '/d/d' );
$this -> fs -> ensureDirectoryExists ( $this -> vendorDir . '/e/e' );
2012-10-16 23:00:11 +00:00
file_put_contents ( $this -> vendorDir . '/z/foo/testA.php' , '<?php function testFilesAutoloadOrderByDependency1() {}' );
2012-09-01 11:24:17 +00:00
file_put_contents ( $this -> vendorDir . '/b/bar/testB.php' , '<?php function testFilesAutoloadOrderByDependency2() {}' );
file_put_contents ( $this -> vendorDir . '/c/lorem/testC.php' , '<?php function testFilesAutoloadOrderByDependency3() {}' );
2012-10-17 08:50:53 +00:00
file_put_contents ( $this -> vendorDir . '/d/d/testD.php' , '<?php function testFilesAutoloadOrderByDependency4() {}' );
file_put_contents ( $this -> vendorDir . '/e/e/testE.php' , '<?php function testFilesAutoloadOrderByDependency5() {}' );
2015-11-26 10:38:58 +00:00
file_put_contents ( $this -> workingDir . '/root2.php' , '<?php function testFilesAutoloadOrderByDependencyRoot() {}' );
2012-09-01 11:24:17 +00:00
2023-10-06 07:34:10 +00:00
$this -> generator -> dump ( $this -> config , $this -> repository , $package , $this -> im , 'composer' , false , 'FilesAutoloadOrder' );
2024-05-29 21:12:06 +00:00
self :: assertFileContentEquals ( __DIR__ . '/Fixtures/autoload_functions_by_dependency.php' , $this -> vendorDir . '/autoload.php' );
self :: assertFileContentEquals ( __DIR__ . '/Fixtures/autoload_real_files_by_dependency.php' , $this -> vendorDir . '/composer/autoload_real.php' );
self :: assertFileContentEquals ( __DIR__ . '/Fixtures/autoload_static_files_by_dependency.php' , $this -> vendorDir . '/composer/autoload_static.php' );
2012-09-01 11:24:17 +00:00
2020-04-19 14:00:21 +00:00
$loader = require $this -> vendorDir . '/autoload.php' ;
$loader -> unregister ();
2012-09-01 11:24:17 +00:00
2024-05-29 21:12:06 +00:00
self :: assertTrue ( function_exists ( 'testFilesAutoloadOrderByDependency1' ));
self :: assertTrue ( function_exists ( 'testFilesAutoloadOrderByDependency2' ));
self :: assertTrue ( function_exists ( 'testFilesAutoloadOrderByDependency3' ));
self :: assertTrue ( function_exists ( 'testFilesAutoloadOrderByDependency4' ));
self :: assertTrue ( function_exists ( 'testFilesAutoloadOrderByDependency5' ));
self :: assertTrue ( function_exists ( 'testFilesAutoloadOrderByDependencyRoot' ));
2012-09-01 11:24:17 +00:00
}
2014-01-07 23:05:24 +00:00
/**
* Test that PSR - 0 and PSR - 4 mappings are processed in the correct order for
* autoloading and for classmap generation :
* - The main package has priority over other packages .
* - Longer namespaces have priority over shorter namespaces .
*/
2022-02-18 09:38:54 +00:00
public function testOverrideVendorsAutoloading () : void
2012-01-13 03:14:30 +00:00
{
2020-11-12 09:35:13 +00:00
$rootPackage = new RootPackage ( 'root/z' , '1.0' , '1.0' );
2022-08-17 12:20:07 +00:00
$rootPackage -> setAutoload ([
'psr-0' => [ 'A\\B' => $this -> workingDir . '/lib' ],
'classmap' => [ $this -> workingDir . '/src' ],
]);
$rootPackage -> setRequires ([
2021-08-21 15:41:52 +00:00
'a/a' => new Link ( 'z' , 'a/a' , new MatchAllConstraint ()),
'b/b' => new Link ( 'z' , 'b/b' , new MatchAllConstraint ()),
2022-08-17 12:20:07 +00:00
]);
2012-01-13 03:14:30 +00:00
2022-08-17 12:20:07 +00:00
$packages = [];
2012-08-23 13:52:40 +00:00
$packages [] = $a = new Package ( 'a/a' , '1.0' , '1.0' );
$packages [] = $b = new Package ( 'b/b' , '1.0' , '1.0' );
2022-08-17 12:20:07 +00:00
$a -> setAutoload ([
'psr-0' => [ 'A' => 'src/' , 'A\\B' => 'lib/' ],
'classmap' => [ 'classmap' ],
]);
$b -> setAutoload ([
'psr-0' => [ 'B\\Sub\\Name' => 'src/' ],
]);
2012-01-13 03:14:30 +00:00
2012-02-24 11:28:41 +00:00
$this -> repository -> expects ( $this -> once ())
2013-04-28 20:32:46 +00:00
-> method ( 'getCanonicalPackages' )
2012-01-13 03:14:30 +00:00
-> will ( $this -> returnValue ( $packages ));
2012-06-19 21:15:42 +00:00
$this -> fs -> ensureDirectoryExists ( $this -> workingDir . '/lib/A/B' );
2012-10-16 23:40:15 +00:00
$this -> fs -> ensureDirectoryExists ( $this -> workingDir . '/src/' );
2012-06-19 21:15:42 +00:00
$this -> fs -> ensureDirectoryExists ( $this -> vendorDir . '/composer' );
2012-10-16 23:40:15 +00:00
$this -> fs -> ensureDirectoryExists ( $this -> vendorDir . '/a/a/classmap' );
2012-06-19 21:15:42 +00:00
$this -> fs -> ensureDirectoryExists ( $this -> vendorDir . '/a/a/src' );
$this -> fs -> ensureDirectoryExists ( $this -> vendorDir . '/a/a/lib/A/B' );
$this -> fs -> ensureDirectoryExists ( $this -> vendorDir . '/b/b/src' );
2014-01-07 23:05:24 +00:00
// Define the classes A\B\C and Foo\Bar in the main package.
2012-06-19 21:15:42 +00:00
file_put_contents ( $this -> workingDir . '/lib/A/B/C.php' , '<?php namespace A\\B; class C {}' );
2012-10-16 23:40:15 +00:00
file_put_contents ( $this -> workingDir . '/src/classes.php' , '<?php namespace Foo; class Bar {}' );
2014-01-07 23:05:24 +00:00
// Define the same two classes in the package a/a.
2012-06-19 21:15:42 +00:00
file_put_contents ( $this -> vendorDir . '/a/a/lib/A/B/C.php' , '<?php namespace A\\B; class C {}' );
2012-10-16 23:40:15 +00:00
file_put_contents ( $this -> vendorDir . '/a/a/classmap/classes.php' , '<?php namespace Foo; class Bar {}' );
2012-06-19 21:15:42 +00:00
$expectedNamespace = <<< EOF
< ? php
2013-08-27 23:08:38 +00:00
// autoload_namespaces.php @generated by Composer
2012-06-19 21:15:42 +00:00
2020-11-06 23:08:03 +00:00
\ $vendorDir = dirname ( __DIR__ );
2012-06-19 21:15:42 +00:00
\ $baseDir = dirname ( \ $vendorDir );
return array (
2013-05-05 08:56:46 +00:00
'B\\\\Sub\\\\Name' => array ( \ $vendorDir . '/b/b/src' ),
2013-03-14 09:23:35 +00:00
'A\\\\B' => array ( \ $baseDir . '/lib' , \ $vendorDir . '/a/a/lib' ),
2013-05-05 08:56:46 +00:00
'A' => array ( \ $vendorDir . '/a/a/src' ),
2012-06-19 21:15:42 +00:00
);
2013-12-22 18:38:11 +00:00
EOF ;
// autoload_psr4.php is expected to be empty in this example.
$expectedPsr4 = <<< EOF
< ? php
// autoload_psr4.php @generated by Composer
2020-11-06 23:08:03 +00:00
\ $vendorDir = dirname ( __DIR__ );
2013-12-22 18:38:11 +00:00
\ $baseDir = dirname ( \ $vendorDir );
return array (
);
2012-06-19 21:15:42 +00:00
EOF ;
$expectedClassmap = <<< EOF
< ? php
2013-08-27 23:08:38 +00:00
// autoload_classmap.php @generated by Composer
2012-06-19 21:15:42 +00:00
2020-11-06 23:08:03 +00:00
\ $vendorDir = dirname ( __DIR__ );
2012-06-19 21:15:42 +00:00
\ $baseDir = dirname ( \ $vendorDir );
return array (
'A\\\\B\\\\C' => \ $baseDir . '/lib/A/B/C.php' ,
2020-04-19 14:00:21 +00:00
'Composer\\\\InstalledVersions' => \ $vendorDir . '/composer/InstalledVersions.php' ,
2012-10-16 23:40:15 +00:00
'Foo\\\\Bar' => \ $baseDir . '/src/classes.php' ,
2012-06-19 21:15:42 +00:00
);
EOF ;
2023-10-06 07:34:10 +00:00
$this -> generator -> dump ( $this -> config , $this -> repository , $rootPackage , $this -> im , 'composer' , true , '_9' );
2024-05-29 21:12:06 +00:00
self :: assertStringEqualsFile ( $this -> vendorDir . '/composer/autoload_namespaces.php' , $expectedNamespace );
self :: assertStringEqualsFile ( $this -> vendorDir . '/composer/autoload_psr4.php' , $expectedPsr4 );
self :: assertStringEqualsFile ( $this -> vendorDir . '/composer/autoload_classmap.php' , $expectedClassmap );
2012-01-13 03:14:30 +00:00
}
2022-02-18 09:38:54 +00:00
public function testIncludePathFileGeneration () : void
2012-04-04 13:15:37 +00:00
{
2020-11-12 09:35:13 +00:00
$package = new RootPackage ( 'root/a' , '1.0' , '1.0' );
2022-08-17 12:20:07 +00:00
$packages = [];
2012-04-04 13:15:37 +00:00
2012-08-23 13:52:40 +00:00
$a = new Package ( " a/a " , " 1.0 " , " 1.0 " );
2022-08-17 12:20:07 +00:00
$a -> setIncludePaths ([ " lib/ " ]);
2012-04-04 13:15:37 +00:00
2012-08-23 13:52:40 +00:00
$b = new Package ( " b/b " , " 1.0 " , " 1.0 " );
2022-08-17 12:20:07 +00:00
$b -> setIncludePaths ([ " library " ]);
2012-04-04 13:15:37 +00:00
2012-08-23 13:52:40 +00:00
$c = new Package ( " c " , " 1.0 " , " 1.0 " );
2022-08-17 12:20:07 +00:00
$c -> setIncludePaths ([ " library " ]);
2012-04-19 08:51:57 +00:00
2012-04-04 13:15:37 +00:00
$packages [] = $a ;
$packages [] = $b ;
2012-04-19 08:51:57 +00:00
$packages [] = $c ;
2012-04-04 13:15:37 +00:00
$this -> repository -> expects ( $this -> once ())
2013-04-28 20:32:46 +00:00
-> method ( " getCanonicalPackages " )
2012-04-04 13:15:37 +00:00
-> will ( $this -> returnValue ( $packages ));
2012-06-19 21:15:42 +00:00
$this -> fs -> ensureDirectoryExists ( $this -> vendorDir . '/composer' );
2012-04-04 13:15:37 +00:00
2023-10-06 07:34:10 +00:00
$this -> generator -> dump ( $this -> config , $this -> repository , $package , $this -> im , 'composer' , false , '_10' );
2012-04-04 13:15:37 +00:00
2024-05-29 21:12:06 +00:00
self :: assertFileContentEquals ( __DIR__ . '/Fixtures/include_paths.php' , $this -> vendorDir . '/composer/include_paths.php' );
self :: assertEquals (
2022-08-17 12:20:07 +00:00
[
2012-04-19 08:51:57 +00:00
$this -> vendorDir . " /a/a/lib " ,
$this -> vendorDir . " /b/b/library " ,
$this -> vendorDir . " /c/library " ,
2022-08-17 12:20:07 +00:00
],
2012-11-10 20:54:23 +00:00
require $this -> vendorDir . " /composer/include_paths.php "
2012-04-04 13:15:37 +00:00
);
}
2012-04-08 15:42:57 +00:00
2022-02-18 09:38:54 +00:00
public function testIncludePathsArePrependedInAutoloadFile () : void
2012-04-04 13:15:37 +00:00
{
2020-11-12 09:35:13 +00:00
$package = new RootPackage ( 'root/a' , '1.0' , '1.0' );
2022-08-17 12:20:07 +00:00
$packages = [];
2012-04-04 13:15:37 +00:00
2012-08-23 13:52:40 +00:00
$a = new Package ( " a/a " , " 1.0 " , " 1.0 " );
2022-08-17 12:20:07 +00:00
$a -> setIncludePaths ([ " lib/ " ]);
2012-04-04 13:15:37 +00:00
$packages [] = $a ;
$this -> repository -> expects ( $this -> once ())
2013-04-28 20:32:46 +00:00
-> method ( " getCanonicalPackages " )
2012-04-04 13:15:37 +00:00
-> will ( $this -> returnValue ( $packages ));
2012-04-22 20:11:03 +00:00
mkdir ( $this -> vendorDir . " /composer " , 0777 , true );
2012-04-04 13:15:37 +00:00
2023-10-06 07:34:10 +00:00
$this -> generator -> dump ( $this -> config , $this -> repository , $package , $this -> im , 'composer' , false , '_11' );
2012-04-04 13:15:37 +00:00
$oldIncludePath = get_include_path ();
2020-04-19 14:00:21 +00:00
$loader = require $this -> vendorDir . " /autoload.php " ;
$loader -> unregister ();
2012-04-04 13:15:37 +00:00
2024-05-29 21:12:06 +00:00
self :: assertEquals (
2012-07-06 18:17:27 +00:00
$this -> vendorDir . " /a/a/lib " . PATH_SEPARATOR . $oldIncludePath ,
2012-04-04 13:15:37 +00:00
get_include_path ()
);
set_include_path ( $oldIncludePath );
}
2022-02-18 09:38:54 +00:00
public function testIncludePathsInRootPackage () : void
2012-12-17 09:54:48 +00:00
{
2020-11-12 09:35:13 +00:00
$package = new RootPackage ( 'root/a' , '1.0' , '1.0' );
2022-08-17 12:20:07 +00:00
$package -> setIncludePaths ([ '/lib' , '/src' ]);
2012-12-17 09:54:48 +00:00
2022-08-17 12:20:07 +00:00
$packages = [ $a = new Package ( " a/a " , " 1.0 " , " 1.0 " )];
$a -> setIncludePaths ([ " lib/ " ]);
2012-12-17 09:54:48 +00:00
$this -> repository -> expects ( $this -> once ())
2013-04-28 20:32:46 +00:00
-> method ( " getCanonicalPackages " )
2012-12-17 09:54:48 +00:00
-> will ( $this -> returnValue ( $packages ));
mkdir ( $this -> vendorDir . " /composer " , 0777 , true );
2023-10-06 07:34:10 +00:00
$this -> generator -> dump ( $this -> config , $this -> repository , $package , $this -> im , 'composer' , false , '_12' );
2012-12-17 09:54:48 +00:00
$oldIncludePath = get_include_path ();
2020-04-19 14:00:21 +00:00
$loader = require $this -> vendorDir . " /autoload.php " ;
$loader -> unregister ();
2012-12-17 09:54:48 +00:00
2024-05-29 21:12:06 +00:00
self :: assertEquals (
2012-12-17 09:54:48 +00:00
$this -> workingDir . " /lib " . PATH_SEPARATOR . $this -> workingDir . " /src " . PATH_SEPARATOR . $this -> vendorDir . " /a/a/lib " . PATH_SEPARATOR . $oldIncludePath ,
get_include_path ()
);
set_include_path ( $oldIncludePath );
}
2022-02-18 09:38:54 +00:00
public function testIncludePathFileWithoutPathsIsSkipped () : void
2012-04-04 13:15:37 +00:00
{
2020-11-12 09:35:13 +00:00
$package = new RootPackage ( 'root/a' , '1.0' , '1.0' );
2022-08-17 12:20:07 +00:00
$packages = [];
2012-04-04 13:15:37 +00:00
2012-08-23 13:52:40 +00:00
$a = new Package ( " a/a " , " 1.0 " , " 1.0 " );
2012-04-04 13:15:37 +00:00
$packages [] = $a ;
$this -> repository -> expects ( $this -> once ())
2013-04-28 20:32:46 +00:00
-> method ( " getCanonicalPackages " )
2012-04-04 13:15:37 +00:00
-> will ( $this -> returnValue ( $packages ));
2012-04-22 20:11:03 +00:00
mkdir ( $this -> vendorDir . " /composer " , 0777 , true );
2012-04-04 13:15:37 +00:00
2023-10-06 07:34:10 +00:00
$this -> generator -> dump ( $this -> config , $this -> repository , $package , $this -> im , 'composer' , false , '_12' );
2012-04-04 13:15:37 +00:00
2024-05-29 21:12:06 +00:00
self :: assertFileDoesNotExist ( $this -> vendorDir . " /composer/include_paths.php " );
2012-04-04 13:15:37 +00:00
}
2013-02-19 14:23:43 +00:00
2022-02-18 09:38:54 +00:00
public function testPreAndPostEventsAreDispatchedDuringAutoloadDump () : void
2013-01-06 19:34:52 +00:00
{
$this -> eventDispatcher
2021-12-09 16:09:07 +00:00
-> expects ( $this -> exactly ( 2 ))
2013-08-14 15:42:11 +00:00
-> method ( 'dispatchScript' )
2023-06-07 12:35:16 +00:00
-> willReturnCallback ( function ( $type , $dev ) {
static $series = [
[ ScriptEvents :: PRE_AUTOLOAD_DUMP , false ],
[ ScriptEvents :: POST_AUTOLOAD_DUMP , false ]
];
2024-05-29 21:12:06 +00:00
self :: assertSame ( array_shift ( $series ), [ $type , $dev ]);
2023-06-07 12:35:16 +00:00
return 0 ;
});
2013-01-06 19:34:52 +00:00
2020-11-12 09:35:13 +00:00
$package = new RootPackage ( 'root/a' , '1.0' , '1.0' );
2022-08-17 12:20:07 +00:00
$package -> setAutoload ([ 'psr-0' => [ 'Prefix' => 'foo/bar/non/existing/' ]]);
2013-01-06 19:34:52 +00:00
$this -> repository -> expects ( $this -> once ())
2013-04-28 20:32:46 +00:00
-> method ( 'getCanonicalPackages' )
2022-08-17 12:20:07 +00:00
-> will ( $this -> returnValue ([]));
2013-01-06 19:34:52 +00:00
2014-08-08 07:27:19 +00:00
$this -> generator -> setRunScripts ( true );
2023-10-06 07:34:10 +00:00
$this -> generator -> dump ( $this -> config , $this -> repository , $package , $this -> im , 'composer' , true , '_8' );
2013-01-06 19:34:52 +00:00
}
2013-02-19 14:23:43 +00:00
2022-02-18 09:38:54 +00:00
public function testUseGlobalIncludePath () : void
2013-02-04 08:12:41 +00:00
{
2020-11-12 09:35:13 +00:00
$package = new RootPackage ( 'root/a' , '1.0' , '1.0' );
2022-08-17 12:20:07 +00:00
$package -> setAutoload ([
'psr-0' => [ 'Main\\Foo' => '' , 'Main\\Bar' => '' ],
]);
2013-02-04 08:12:41 +00:00
$package -> setTargetDir ( 'Main/Foo/' );
$this -> repository -> expects ( $this -> once ())
2013-04-28 20:32:46 +00:00
-> method ( 'getCanonicalPackages' )
2022-08-17 12:20:07 +00:00
-> will ( $this -> returnValue ([]));
2013-02-19 14:23:43 +00:00
2015-01-04 00:35:25 +00:00
$this -> configValueMap [ 'use-include-path' ] = true ;
2013-02-04 08:12:41 +00:00
$this -> fs -> ensureDirectoryExists ( $this -> vendorDir . '/a' );
2023-10-06 07:34:10 +00:00
$this -> generator -> dump ( $this -> config , $this -> repository , $package , $this -> im , 'composer' , false , 'IncludePath' );
2024-05-29 21:12:06 +00:00
self :: assertFileContentEquals ( __DIR__ . '/Fixtures/autoload_real_include_path.php' , $this -> vendorDir . '/composer/autoload_real.php' );
self :: assertFileContentEquals ( __DIR__ . '/Fixtures/autoload_static_include_path.php' , $this -> vendorDir . '/composer/autoload_static.php' );
2013-02-04 08:12:41 +00:00
}
2012-04-04 13:15:37 +00:00
2022-02-18 09:38:54 +00:00
public function testVendorDirExcludedFromWorkingDir () : void
2013-04-01 07:27:50 +00:00
{
$workingDir = $this -> vendorDir . '/working-dir' ;
$vendorDir = $workingDir . '/../vendor' ;
$this -> fs -> ensureDirectoryExists ( $workingDir );
chdir ( $workingDir );
2020-11-12 09:35:13 +00:00
$package = new RootPackage ( 'root/a' , '1.0' , '1.0' );
2022-08-17 12:20:07 +00:00
$package -> setAutoload ([
'psr-0' => [ 'Foo' => 'src' ],
'psr-4' => [ 'Acme\Foo\\' => 'src-psr4' ],
'classmap' => [ 'classmap' ],
'files' => [ 'test.php' ],
]);
$package -> setRequires ([
2021-08-21 15:41:52 +00:00
'b/b' => new Link ( 'a' , 'b/b' , new MatchAllConstraint ()),
2022-08-17 12:20:07 +00:00
]);
2013-04-01 07:27:50 +00:00
$vendorPackage = new Package ( 'b/b' , '1.0' , '1.0' );
2022-08-17 12:20:07 +00:00
$vendorPackage -> setAutoload ([
'psr-0' => [ 'Bar' => 'lib' ],
'psr-4' => [ 'Acme\Bar\\' => 'lib-psr4' ],
'classmap' => [ 'classmaps' ],
'files' => [ 'bootstrap.php' ],
]);
2013-04-01 07:27:50 +00:00
$this -> repository -> expects ( $this -> once ())
2013-04-28 20:32:46 +00:00
-> method ( 'getCanonicalPackages' )
2022-08-17 12:20:07 +00:00
-> will ( $this -> returnValue ([ $vendorPackage ]));
2013-04-01 07:27:50 +00:00
$im = $this -> getMockBuilder ( 'Composer\Installer\InstallationManager' )
-> disableOriginalConstructor ()
-> getMock ();
$im -> expects ( $this -> any ())
-> method ( 'getInstallPath' )
2022-08-17 12:20:07 +00:00
-> will ( $this -> returnCallback ( static function ( $package ) use ( $vendorDir ) : string {
2013-04-01 07:27:50 +00:00
$targetDir = $package -> getTargetDir ();
2013-06-13 11:28:24 +00:00
2013-04-01 07:27:50 +00:00
return $vendorDir . '/' . $package -> getName () . ( $targetDir ? '/' . $targetDir : '' );
}));
$this -> fs -> ensureDirectoryExists ( $workingDir . '/src/Foo' );
$this -> fs -> ensureDirectoryExists ( $workingDir . '/classmap' );
$this -> fs -> ensureDirectoryExists ( $vendorDir . '/composer' );
$this -> fs -> ensureDirectoryExists ( $vendorDir . '/b/b/lib/Bar' );
$this -> fs -> ensureDirectoryExists ( $vendorDir . '/b/b/classmaps' );
file_put_contents ( $workingDir . '/src/Foo/Bar.php' , '<?php namespace Foo; class Bar {}' );
file_put_contents ( $workingDir . '/classmap/classes.php' , '<?php namespace Foo; class Foo {}' );
file_put_contents ( $workingDir . '/test.php' , '<?php class Foo {}' );
file_put_contents ( $vendorDir . '/b/b/lib/Bar/Foo.php' , '<?php namespace Bar; class Foo {}' );
file_put_contents ( $vendorDir . '/b/b/classmaps/classes.php' , '<?php namespace Bar; class Bar {}' );
file_put_contents ( $vendorDir . '/b/b/bootstrap.php' , '<?php class Bar {}' );
$oldVendorDir = $this -> vendorDir ;
$this -> vendorDir = $vendorDir ;
2023-10-06 07:34:10 +00:00
$this -> generator -> dump ( $this -> config , $this -> repository , $package , $im , 'composer' , true , '_13' );
2013-04-01 07:27:50 +00:00
$this -> vendorDir = $oldVendorDir ;
$expectedNamespace = <<< 'EOF'
< ? php
2013-08-27 23:08:38 +00:00
// autoload_namespaces.php @generated by Composer
2013-04-01 07:27:50 +00:00
2020-11-06 23:08:03 +00:00
$vendorDir = dirname ( __DIR__ );
2013-04-01 07:27:50 +00:00
$baseDir = dirname ( $vendorDir ) . '/working-dir' ;
return array (
2013-05-05 08:56:46 +00:00
'Foo' => array ( $baseDir . '/src' ),
'Bar' => array ( $vendorDir . '/b/b/lib' ),
2013-04-01 07:27:50 +00:00
);
2013-12-22 18:38:11 +00:00
EOF ;
$expectedPsr4 = <<< 'EOF'
< ? php
// autoload_psr4.php @generated by Composer
2020-11-06 23:08:03 +00:00
$vendorDir = dirname ( __DIR__ );
2013-12-22 18:38:11 +00:00
$baseDir = dirname ( $vendorDir ) . '/working-dir' ;
return array (
2013-12-22 18:58:27 +00:00
'Acme\\Foo\\' => array ( $baseDir . '/src-psr4' ),
'Acme\\Bar\\' => array ( $vendorDir . '/b/b/lib-psr4' ),
2013-12-22 18:38:11 +00:00
);
2013-04-01 07:27:50 +00:00
EOF ;
$expectedClassmap = <<< 'EOF'
< ? php
2013-08-27 23:08:38 +00:00
// autoload_classmap.php @generated by Composer
2013-04-01 07:27:50 +00:00
2020-11-06 23:08:03 +00:00
$vendorDir = dirname ( __DIR__ );
2013-04-01 07:27:50 +00:00
$baseDir = dirname ( $vendorDir ) . '/working-dir' ;
return array (
'Bar\\Bar' => $vendorDir . '/b/b/classmaps/classes.php' ,
'Bar\\Foo' => $vendorDir . '/b/b/lib/Bar/Foo.php' ,
2020-04-19 14:00:21 +00:00
'Composer\\InstalledVersions' => $vendorDir . '/composer/InstalledVersions.php' ,
2013-04-01 07:27:50 +00:00
'Foo\\Bar' => $baseDir . '/src/Foo/Bar.php' ,
'Foo\\Foo' => $baseDir . '/classmap/classes.php' ,
);
EOF ;
2024-05-29 21:12:06 +00:00
self :: assertStringEqualsFile ( $vendorDir . '/composer/autoload_namespaces.php' , $expectedNamespace );
self :: assertStringEqualsFile ( $vendorDir . '/composer/autoload_psr4.php' , $expectedPsr4 );
self :: assertStringEqualsFile ( $vendorDir . '/composer/autoload_classmap.php' , $expectedClassmap );
self :: assertStringContainsString ( " \$ vendorDir . '/b/b/bootstrap.php', \n " , ( string ) file_get_contents ( $vendorDir . '/composer/autoload_files.php' ));
self :: assertStringContainsString ( " \$ baseDir . '/test.php', \n " , ( string ) file_get_contents ( $vendorDir . '/composer/autoload_files.php' ));
2013-04-01 07:27:50 +00:00
}
2022-02-18 09:38:54 +00:00
public function testUpLevelRelativePaths () : void
2013-04-05 12:22:16 +00:00
{
$workingDir = $this -> workingDir . '/working-dir' ;
mkdir ( $workingDir , 0777 , true );
chdir ( $workingDir );
2020-11-12 09:35:13 +00:00
$package = new RootPackage ( 'root/a' , '1.0' , '1.0' );
2022-08-17 12:20:07 +00:00
$package -> setAutoload ([
'psr-0' => [ 'Foo' => '../path/../src' ],
'psr-4' => [ 'Acme\Foo\\' => '../path/../src-psr4' ],
'classmap' => [ '../classmap' , '../classmap2/subdir' , 'classmap3' , 'classmap4' ],
'files' => [ '../test.php' ],
'exclude-from-classmap' => [ './../classmap/excluded' , '../classmap2' , 'classmap3/classes.php' , 'classmap4/*/classes.php' ],
]);
2013-04-05 12:22:16 +00:00
$this -> repository -> expects ( $this -> once ())
2013-04-28 20:32:46 +00:00
-> method ( 'getCanonicalPackages' )
2022-08-17 12:20:07 +00:00
-> will ( $this -> returnValue ([]));
2013-04-05 12:22:16 +00:00
$this -> fs -> ensureDirectoryExists ( $this -> workingDir . '/src/Foo' );
2015-11-12 00:32:02 +00:00
$this -> fs -> ensureDirectoryExists ( $this -> workingDir . '/classmap/excluded' );
2021-01-27 14:02:19 +00:00
$this -> fs -> ensureDirectoryExists ( $this -> workingDir . '/classmap2/subdir' );
$this -> fs -> ensureDirectoryExists ( $this -> workingDir . '/working-dir/classmap3' );
$this -> fs -> ensureDirectoryExists ( $this -> workingDir . '/working-dir/classmap4/foo/' );
2013-04-05 12:22:16 +00:00
file_put_contents ( $this -> workingDir . '/src/Foo/Bar.php' , '<?php namespace Foo; class Bar {}' );
file_put_contents ( $this -> workingDir . '/classmap/classes.php' , '<?php namespace Foo; class Foo {}' );
2015-11-12 00:32:02 +00:00
file_put_contents ( $this -> workingDir . '/classmap/excluded/classes.php' , '<?php namespace Foo; class Boo {}' );
2021-01-27 14:02:19 +00:00
file_put_contents ( $this -> workingDir . '/classmap2/subdir/classes.php' , '<?php namespace Foo; class Boo2 {}' );
file_put_contents ( $this -> workingDir . '/working-dir/classmap3/classes.php' , '<?php namespace Foo; class Boo3 {}' );
file_put_contents ( $this -> workingDir . '/working-dir/classmap4/foo/classes.php' , '<?php namespace Foo; class Boo4 {}' );
2013-04-05 12:22:16 +00:00
file_put_contents ( $this -> workingDir . '/test.php' , '<?php class Foo {}' );
2023-10-06 07:34:10 +00:00
$this -> generator -> dump ( $this -> config , $this -> repository , $package , $this -> im , 'composer' , true , '_14' );
2013-04-05 12:22:16 +00:00
$expectedNamespace = <<< 'EOF'
< ? php
2013-08-27 23:08:38 +00:00
// autoload_namespaces.php @generated by Composer
2013-04-05 12:22:16 +00:00
2020-11-06 23:08:03 +00:00
$vendorDir = dirname ( __DIR__ );
2013-04-05 12:22:16 +00:00
$baseDir = dirname ( $vendorDir ) . '/working-dir' ;
return array (
2013-05-05 08:56:46 +00:00
'Foo' => array ( $baseDir . '/../src' ),
2013-04-05 12:22:16 +00:00
);
2013-12-22 18:38:11 +00:00
EOF ;
$expectedPsr4 = <<< 'EOF'
< ? php
// autoload_psr4.php @generated by Composer
2020-11-06 23:08:03 +00:00
$vendorDir = dirname ( __DIR__ );
2013-12-22 18:38:11 +00:00
$baseDir = dirname ( $vendorDir ) . '/working-dir' ;
return array (
2013-12-22 18:58:27 +00:00
'Acme\\Foo\\' => array ( $baseDir . '/../src-psr4' ),
2013-12-22 18:38:11 +00:00
);
2013-04-05 12:22:16 +00:00
EOF ;
2013-12-22 18:39:09 +00:00
$expectedClassmap = <<< 'EOF'
2013-04-05 12:22:16 +00:00
< ? php
2013-08-27 23:08:38 +00:00
// autoload_classmap.php @generated by Composer
2013-04-05 12:22:16 +00:00
2020-11-06 23:08:03 +00:00
$vendorDir = dirname ( __DIR__ );
2013-04-05 12:22:16 +00:00
$baseDir = dirname ( $vendorDir ) . '/working-dir' ;
return array (
2020-04-19 14:00:21 +00:00
'Composer\\InstalledVersions' => $vendorDir . '/composer/InstalledVersions.php' ,
2013-04-05 12:22:16 +00:00
'Foo\\Bar' => $baseDir . '/../src/Foo/Bar.php' ,
'Foo\\Foo' => $baseDir . '/../classmap/classes.php' ,
);
EOF ;
2024-05-29 21:12:06 +00:00
self :: assertStringEqualsFile ( $this -> vendorDir . '/composer/autoload_namespaces.php' , $expectedNamespace );
self :: assertStringEqualsFile ( $this -> vendorDir . '/composer/autoload_psr4.php' , $expectedPsr4 );
self :: assertStringEqualsFile ( $this -> vendorDir . '/composer/autoload_classmap.php' , $expectedClassmap );
self :: assertStringContainsString ( " \$ baseDir . '/../test.php', \n " , ( string ) file_get_contents ( $this -> vendorDir . '/composer/autoload_files.php' ));
2022-04-01 19:20:21 +00:00
}
2022-04-01 20:10:24 +00:00
public function testAutoloadRulesInPackageThatDoesNotExistOnDisk () : void
2022-04-01 19:20:21 +00:00
{
$package = new RootPackage ( 'root/a' , '1.0' , '1.0' );
2022-08-17 12:20:07 +00:00
$package -> setRequires ([
2022-04-01 19:20:21 +00:00
'dep/a' => new Link ( 'root/a' , 'dep/a' , new MatchAllConstraint (), 'requires' ),
2022-08-17 12:20:07 +00:00
]);
2022-04-01 19:20:21 +00:00
$dep = new CompletePackage ( 'dep/a' , '1.0' , '1.0' );
$this -> repository -> expects ( $this -> any ())
-> method ( 'getCanonicalPackages' )
2022-08-17 12:20:07 +00:00
-> will ( $this -> returnValue ([ $dep ]));
2022-04-01 19:20:21 +00:00
2022-08-17 12:20:07 +00:00
$dep -> setAutoload ([
'psr-0' => [ 'Foo' => './src' ],
]);
2023-10-06 07:34:10 +00:00
$this -> generator -> dump ( $this -> config , $this -> repository , $package , $this -> im , 'composer' , true , '_19' );
2022-04-01 19:20:21 +00:00
$expectedNamespace = <<< 'EOF'
< ? php
// autoload_namespaces.php @generated by Composer
2022-04-01 20:10:24 +00:00
$vendorDir = dirname ( __DIR__ );
2022-04-01 19:20:21 +00:00
$baseDir = dirname ( $vendorDir );
return array (
'Foo' => array ( $vendorDir . '/dep/a/src' ),
);
EOF ;
2024-05-29 21:12:06 +00:00
self :: assertStringEqualsFile ( $this -> vendorDir . '/composer/autoload_namespaces.php' , $expectedNamespace );
2022-04-01 19:20:21 +00:00
2022-08-17 12:20:07 +00:00
$dep -> setAutoload ([
'psr-4' => [ 'Acme\Foo\\' => './src-psr4' ],
]);
2023-10-06 07:34:10 +00:00
$this -> generator -> dump ( $this -> config , $this -> repository , $package , $this -> im , 'composer' , true , '_19' );
2022-04-01 19:20:21 +00:00
$expectedPsr4 = <<< 'EOF'
< ? php
// autoload_psr4.php @generated by Composer
2022-04-01 20:10:24 +00:00
$vendorDir = dirname ( __DIR__ );
2022-04-01 19:20:21 +00:00
$baseDir = dirname ( $vendorDir );
return array (
'Acme\\Foo\\' => array ( $vendorDir . '/dep/a/src-psr4' ),
);
EOF ;
2024-05-29 21:12:06 +00:00
self :: assertStringEqualsFile ( $this -> vendorDir . '/composer/autoload_psr4.php' , $expectedPsr4 );
2022-04-01 19:20:21 +00:00
2022-08-17 12:20:07 +00:00
$dep -> setAutoload ([
'classmap' => [ 'classmap' ],
]);
2022-04-01 19:20:21 +00:00
try {
2023-10-06 07:34:10 +00:00
$this -> generator -> dump ( $this -> config , $this -> repository , $package , $this -> im , 'composer' , true , '_19' );
2022-04-01 19:20:21 +00:00
} catch ( \RuntimeException $e ) {
2024-05-29 21:12:06 +00:00
self :: assertSame ( 'Could not scan for classes inside "' . $this -> vendorDir . '/dep/a/classmap" which does not appear to be a file nor a folder' , $e -> getMessage ());
2022-04-01 19:20:21 +00:00
}
2022-08-17 12:20:07 +00:00
$dep -> setAutoload ([
'files' => [ './test.php' ],
]);
2023-10-06 07:34:10 +00:00
$this -> generator -> dump ( $this -> config , $this -> repository , $package , $this -> im , 'composer' , true , '_19' );
2024-05-29 21:12:06 +00:00
self :: assertStringContainsString ( " \$ vendorDir . '/dep/a/test.php', \n " , ( string ) file_get_contents ( $this -> vendorDir . '/composer/autoload_files.php' ));
2022-04-01 19:20:21 +00:00
2022-08-17 12:20:07 +00:00
$package -> setAutoload ([
'exclude-from-classmap' => [ '../excludedroot' , 'root/excl' ],
]);
$dep -> setAutoload ([
'exclude-from-classmap' => [ '../../excluded' , 'foo/bar' ],
]);
$map = $this -> generator -> buildPackageMap ( $this -> im , $package , [ $dep ]);
2022-04-01 19:20:21 +00:00
$parsed = $this -> generator -> parseAutoloads ( $map , $package );
2024-05-29 21:12:06 +00:00
self :: assertSame ([
2022-04-02 10:58:29 +00:00
preg_quote ( strtr (( string ) realpath ( dirname ( $this -> workingDir )), '\\' , '/' )) . '/excludedroot($|/)' ,
2022-08-17 12:20:07 +00:00
preg_quote ( strtr (( string ) realpath ( $this -> workingDir ), '\\' , '/' )) . '/root/excl($|/)' ,
], $parsed [ 'exclude-from-classmap' ]);
2013-04-05 12:22:16 +00:00
}
2022-02-18 09:38:54 +00:00
public function testEmptyPaths () : void
2013-04-07 09:34:58 +00:00
{
2020-11-12 09:35:13 +00:00
$package = new RootPackage ( 'root/a' , '1.0' , '1.0' );
2022-08-17 12:20:07 +00:00
$package -> setAutoload ([
'psr-0' => [ 'Foo' => '' ],
'psr-4' => [ 'Acme\Foo\\' => '' ],
'classmap' => [ '' ],
]);
2013-04-07 09:34:58 +00:00
$this -> repository -> expects ( $this -> once ())
2013-04-28 20:32:46 +00:00
-> method ( 'getCanonicalPackages' )
2022-08-17 12:20:07 +00:00
-> will ( $this -> returnValue ([]));
2013-04-07 09:34:58 +00:00
$this -> fs -> ensureDirectoryExists ( $this -> workingDir . '/Foo' );
file_put_contents ( $this -> workingDir . '/Foo/Bar.php' , '<?php namespace Foo; class Bar {}' );
file_put_contents ( $this -> workingDir . '/class.php' , '<?php namespace Classmap; class Foo {}' );
2023-10-06 07:34:10 +00:00
$this -> generator -> dump ( $this -> config , $this -> repository , $package , $this -> im , 'composer' , true , '_15' );
2013-04-07 09:34:58 +00:00
$expectedNamespace = <<< 'EOF'
< ? php
2013-08-27 23:08:38 +00:00
// autoload_namespaces.php @generated by Composer
2013-04-07 09:34:58 +00:00
2020-11-06 23:08:03 +00:00
$vendorDir = dirname ( __DIR__ );
2013-04-07 09:34:58 +00:00
$baseDir = dirname ( $vendorDir );
return array (
2013-05-05 08:56:46 +00:00
'Foo' => array ( $baseDir . '/' ),
2013-04-07 09:34:58 +00:00
);
2013-12-22 18:38:11 +00:00
EOF ;
2013-12-22 18:58:27 +00:00
$expectedPsr4 = <<< 'EOF'
2013-12-22 18:38:11 +00:00
< ? php
// autoload_psr4.php @generated by Composer
2020-11-06 23:08:03 +00:00
$vendorDir = dirname ( __DIR__ );
2013-12-22 18:58:27 +00:00
$baseDir = dirname ( $vendorDir );
2013-12-22 18:38:11 +00:00
return array (
2013-12-22 18:58:27 +00:00
'Acme\\Foo\\' => array ( $baseDir . '/' ),
2013-12-22 18:38:11 +00:00
);
2013-04-07 09:34:58 +00:00
EOF ;
2013-12-22 18:39:09 +00:00
$expectedClassmap = <<< 'EOF'
2013-04-07 09:34:58 +00:00
< ? php
2013-08-27 23:08:38 +00:00
// autoload_classmap.php @generated by Composer
2013-04-07 09:34:58 +00:00
2020-11-06 23:08:03 +00:00
$vendorDir = dirname ( __DIR__ );
2013-04-07 09:34:58 +00:00
$baseDir = dirname ( $vendorDir );
return array (
'Classmap\\Foo' => $baseDir . '/class.php' ,
2020-04-19 14:00:21 +00:00
'Composer\\InstalledVersions' => $vendorDir . '/composer/InstalledVersions.php' ,
2013-04-07 09:34:58 +00:00
'Foo\\Bar' => $baseDir . '/Foo/Bar.php' ,
);
EOF ;
2024-05-29 21:12:06 +00:00
self :: assertStringEqualsFile ( $this -> vendorDir . '/composer/autoload_namespaces.php' , $expectedNamespace );
self :: assertStringEqualsFile ( $this -> vendorDir . '/composer/autoload_psr4.php' , $expectedPsr4 );
self :: assertStringEqualsFile ( $this -> vendorDir . '/composer/autoload_classmap.php' , $expectedClassmap );
2013-04-07 09:34:58 +00:00
}
2022-02-18 09:38:54 +00:00
public function testVendorSubstringPath () : void
2013-09-26 12:34:41 +00:00
{
2020-11-12 09:35:13 +00:00
$package = new RootPackage ( 'root/a' , '1.0' , '1.0' );
2022-08-17 12:20:07 +00:00
$package -> setAutoload ([
'psr-0' => [ 'Foo' => 'composer-test-autoload-src/src' ],
'psr-4' => [ 'Acme\Foo\\' => 'composer-test-autoload-src/src-psr4' ],
]);
2013-09-26 12:34:41 +00:00
$this -> repository -> expects ( $this -> once ())
-> method ( 'getCanonicalPackages' )
2022-08-17 12:20:07 +00:00
-> will ( $this -> returnValue ([]));
2013-09-26 12:34:41 +00:00
$this -> fs -> ensureDirectoryExists ( $this -> vendorDir . '/a' );
$expectedNamespace = <<< 'EOF'
< ? php
// autoload_namespaces.php @generated by Composer
2020-11-06 23:08:03 +00:00
$vendorDir = dirname ( __DIR__ );
2013-09-26 12:34:41 +00:00
$baseDir = dirname ( $vendorDir );
return array (
'Foo' => array ( $baseDir . '/composer-test-autoload-src/src' ),
);
2013-12-22 18:38:11 +00:00
EOF ;
2013-12-22 18:58:27 +00:00
$expectedPsr4 = <<< 'EOF'
2013-12-22 18:38:11 +00:00
< ? php
// autoload_psr4.php @generated by Composer
2020-11-06 23:08:03 +00:00
$vendorDir = dirname ( __DIR__ );
2013-12-22 18:58:27 +00:00
$baseDir = dirname ( $vendorDir );
2013-12-22 18:38:11 +00:00
return array (
2013-12-22 18:58:27 +00:00
'Acme\\Foo\\' => array ( $baseDir . '/composer-test-autoload-src/src-psr4' ),
2013-12-22 18:38:11 +00:00
);
2013-09-26 12:34:41 +00:00
EOF ;
2023-10-06 07:34:10 +00:00
$this -> generator -> dump ( $this -> config , $this -> repository , $package , $this -> im , 'composer' , false , 'VendorSubstring' );
2024-05-29 21:12:06 +00:00
self :: assertStringEqualsFile ( $this -> vendorDir . '/composer/autoload_namespaces.php' , $expectedNamespace );
self :: assertStringEqualsFile ( $this -> vendorDir . '/composer/autoload_psr4.php' , $expectedPsr4 );
2013-09-26 12:34:41 +00:00
}
2022-02-18 09:38:54 +00:00
public function testExcludeFromClassmap () : void
2014-01-27 10:42:54 +00:00
{
2020-11-12 09:35:13 +00:00
$package = new RootPackage ( 'root/a' , '1.0' , '1.0' );
2022-08-17 12:20:07 +00:00
$package -> setAutoload ([
'psr-0' => [
2014-01-27 10:42:54 +00:00
'Main' => 'src/' ,
2022-08-17 12:20:07 +00:00
'Lala' => [ 'src/' , 'lib/' ],
],
'psr-4' => [
2015-02-11 17:04:57 +00:00
'Acme\Fruit\\' => 'src-fruit/' ,
2022-08-17 12:20:07 +00:00
'Acme\Cake\\' => [ 'src-cake/' , 'lib-cake/' ],
],
'classmap' => [ 'composersrc/' ],
'exclude-from-classmap' => [
2018-04-11 10:33:27 +00:00
'/composersrc/foo/bar/' ,
2015-10-30 19:12:30 +00:00
'/composersrc/excludedTests/' ,
'/composersrc/ClassToExclude.php' ,
'/composersrc/*/excluded/excsubpath' ,
'**/excsubpath' ,
2020-02-12 09:23:03 +00:00
'composers' , // should _not_ cause exclusion of /composersrc/**, as it is equivalent to /composers/**
'/src-ca/' , // should _not_ cause exclusion of /src-cake/**, as it is equivalent to /src-ca/**
2022-08-17 12:20:07 +00:00
],
]);
2014-01-27 10:42:54 +00:00
$this -> repository -> expects ( $this -> once ())
-> method ( 'getCanonicalPackages' )
2022-08-17 12:20:07 +00:00
-> will ( $this -> returnValue ([]));
2014-01-27 10:42:54 +00:00
$this -> fs -> ensureDirectoryExists ( $this -> workingDir . '/composer' );
2015-10-30 00:06:43 +00:00
$this -> fs -> ensureDirectoryExists ( $this -> workingDir . '/src/Lala/Test' );
2015-02-11 17:04:57 +00:00
$this -> fs -> ensureDirectoryExists ( $this -> workingDir . '/lib' );
file_put_contents ( $this -> workingDir . '/src/Lala/ClassMapMain.php' , '<?php namespace Lala; class ClassMapMain {}' );
2015-10-30 00:06:43 +00:00
file_put_contents ( $this -> workingDir . '/src/Lala/Test/ClassMapMainTest.php' , '<?php namespace Lala\Test; class ClassMapMainTest {}' );
2015-02-11 17:04:57 +00:00
$this -> fs -> ensureDirectoryExists ( $this -> workingDir . '/src-fruit' );
$this -> fs -> ensureDirectoryExists ( $this -> workingDir . '/src-cake' );
$this -> fs -> ensureDirectoryExists ( $this -> workingDir . '/lib-cake' );
file_put_contents ( $this -> workingDir . '/src-cake/ClassMapBar.php' , '<?php namespace Acme\Cake; class ClassMapBar {}' );
2014-01-27 10:42:54 +00:00
$this -> fs -> ensureDirectoryExists ( $this -> workingDir . '/composersrc' );
2015-02-12 08:49:42 +00:00
$this -> fs -> ensureDirectoryExists ( $this -> workingDir . '/composersrc/tests' );
2014-01-27 10:42:54 +00:00
file_put_contents ( $this -> workingDir . '/composersrc/foo.php' , '<?php class ClassMapFoo {}' );
2020-02-04 11:55:57 +00:00
// these classes should not be found in the classmap
2015-10-30 00:06:43 +00:00
$this -> fs -> ensureDirectoryExists ( $this -> workingDir . '/composersrc/excludedTests' );
file_put_contents ( $this -> workingDir . '/composersrc/excludedTests/bar.php' , '<?php class ClassExcludeMapFoo {}' );
2015-02-12 08:49:42 +00:00
file_put_contents ( $this -> workingDir . '/composersrc/ClassToExclude.php' , '<?php class ClassClassToExclude {}' );
2015-10-30 19:12:30 +00:00
$this -> fs -> ensureDirectoryExists ( $this -> workingDir . '/composersrc/long/excluded/excsubpath' );
file_put_contents ( $this -> workingDir . '/composersrc/long/excluded/excsubpath/foo.php' , '<?php class ClassExcludeMapFoo2 {}' );
file_put_contents ( $this -> workingDir . '/composersrc/long/excluded/excsubpath/bar.php' , '<?php class ClassExcludeMapBar {}' );
2014-01-27 10:42:54 +00:00
2018-04-11 10:33:27 +00:00
// symlink directory in project directory in classmap
$this -> fs -> ensureDirectoryExists ( $this -> workingDir . '/forks/bar/src/exclude' );
$this -> fs -> ensureDirectoryExists ( $this -> workingDir . '/composersrc/foo' );
file_put_contents ( $this -> workingDir . '/forks/bar/src/exclude/FooExclClass.php' , '<?php class FooExclClass {};' );
$target = $this -> workingDir . '/forks/bar/' ;
2018-05-04 09:02:02 +00:00
$link = $this -> workingDir . '/composersrc/foo/bar' ;
2018-04-11 10:33:27 +00:00
$command = Platform :: isWindows ()
2018-05-04 09:02:02 +00:00
? 'mklink /j "' . str_replace ( '/' , '\\' , $link ) . '" "' . str_replace ( '/' , '\\' , $target ) . '"'
: 'ln -s "' . $target . '" "' . $link . '"' ;
2018-04-11 10:33:27 +00:00
exec ( $command );
2023-10-06 07:34:10 +00:00
$this -> generator -> dump ( $this -> config , $this -> repository , $package , $this -> im , 'composer' , true , '_1' );
2014-01-27 10:42:54 +00:00
2015-02-11 17:04:57 +00:00
// Assert that autoload_classmap.php was correctly generated.
2024-05-29 21:12:06 +00:00
self :: assertAutoloadFiles ( 'classmap' , $this -> vendorDir . '/composer' , 'classmap' );
2014-01-27 10:42:54 +00:00
}
2020-04-21 06:59:36 +00:00
/**
2021-10-26 19:48:56 +00:00
* @ param array < string , Link > $requires
* @ param array < string , Link > $provides
* @ param array < string , Link > $replaces
2022-02-22 15:47:09 +00:00
* @ param bool | array < string > $ignorePlatformReqs
2021-10-26 19:48:56 +00:00
*
2020-04-21 06:59:36 +00:00
* @ dataProvider platformCheckProvider
*/
2022-08-17 12:20:07 +00:00
public function testGeneratesPlatformCheck ( array $requires , ? string $expectedFixture , array $provides = [], array $replaces = [], $ignorePlatformReqs = false ) : void
2020-04-21 06:59:36 +00:00
{
2020-11-12 09:35:13 +00:00
$package = new RootPackage ( 'root/a' , '1.0' , '1.0' );
2020-04-21 06:59:36 +00:00
$package -> setRequires ( $requires );
2020-04-24 11:38:50 +00:00
if ( $provides ) {
$package -> setProvides ( $provides );
}
if ( $replaces ) {
$package -> setReplaces ( $replaces );
}
2020-04-21 06:59:36 +00:00
$this -> repository -> expects ( $this -> once ())
-> method ( 'getCanonicalPackages' )
2022-08-17 12:20:07 +00:00
-> will ( $this -> returnValue ([]));
2020-04-21 06:59:36 +00:00
2021-11-11 11:24:12 +00:00
$this -> generator -> setPlatformRequirementFilter ( PlatformRequirementFilterFactory :: fromBoolOrList ( $ignorePlatformReqs ));
2023-10-06 07:34:10 +00:00
$this -> generator -> dump ( $this -> config , $this -> repository , $package , $this -> im , 'composer' , true , '_1' );
2020-04-21 06:59:36 +00:00
2020-06-01 07:39:44 +00:00
if ( null === $expectedFixture ) {
2024-05-29 21:12:06 +00:00
self :: assertFileDoesNotExist ( $this -> vendorDir . '/composer/platform_check.php' );
self :: assertStringNotContainsString ( " require __DIR__ . '/platform_check.php'; " , ( string ) file_get_contents ( $this -> vendorDir . '/composer/autoload_real.php' ));
2020-06-01 07:39:44 +00:00
} else {
2024-05-29 21:12:06 +00:00
self :: assertFileContentEquals ( __DIR__ . '/Fixtures/platform/' . $expectedFixture . '.php' , $this -> vendorDir . '/composer/platform_check.php' );
self :: assertStringContainsString ( " require __DIR__ . '/platform_check.php'; " , ( string ) file_get_contents ( $this -> vendorDir . '/composer/autoload_real.php' ));
2020-06-01 07:39:44 +00:00
}
2020-04-21 06:59:36 +00:00
}
2021-10-16 09:13:29 +00:00
/**
* @ return array < string , mixed [] >
*/
2022-11-24 13:39:08 +00:00
public static function platformCheckProvider () : array
2020-04-21 06:59:36 +00:00
{
$versionParser = new VersionParser ();
2022-08-17 12:20:07 +00:00
return [
'Typical project requirements' => [
[
2021-11-12 20:38:08 +00:00
'php' => new Link ( 'a' , 'php' , $versionParser -> parseConstraints ( '^7.2' )),
'ext-xml' => new Link ( 'a' , 'ext-xml' , $versionParser -> parseConstraints ( '*' )),
'ext-json' => new Link ( 'a' , 'ext-json' , $versionParser -> parseConstraints ( '*' )),
2022-08-17 12:20:07 +00:00
],
2020-11-22 13:48:56 +00:00
'typical' ,
2022-08-17 12:20:07 +00:00
],
'No PHP lower bound' => [
[
2021-11-12 20:38:08 +00:00
'php' => new Link ( 'a' , 'php' , $versionParser -> parseConstraints ( '< 8' )),
2022-08-17 12:20:07 +00:00
],
2020-11-22 13:48:56 +00:00
null ,
2022-08-17 12:20:07 +00:00
],
'No PHP upper bound' => [
[
2021-11-12 20:38:08 +00:00
'php' => new Link ( 'a' , 'php' , $versionParser -> parseConstraints ( '>= 7.2' )),
2022-08-17 12:20:07 +00:00
],
2020-11-22 13:48:56 +00:00
'no_php_upper_bound' ,
2022-08-17 12:20:07 +00:00
],
'Specific PHP release version' => [
[
2021-11-12 20:38:08 +00:00
'php' => new Link ( 'a' , 'php' , $versionParser -> parseConstraints ( '^7.2.8' )),
2022-08-17 12:20:07 +00:00
],
2020-11-22 13:48:56 +00:00
'specific_php_release' ,
2022-08-17 12:20:07 +00:00
],
2023-03-20 15:08:12 +00:00
'Specific 64-bit PHP version' => [
[
'php-64bit' => new Link ( 'a' , 'php-64bit' , $versionParser -> parseConstraints ( '^7.2.8' )),
],
'specific_php_64bit_required' ,
],
'64-bit PHP required' => [
[
'php-64bit' => new Link ( 'a' , 'php-64bit' , $versionParser -> parseConstraints ( '*' )),
],
'php_64bit_required' ,
],
2022-08-17 12:20:07 +00:00
'No PHP required' => [
[
2021-11-12 20:38:08 +00:00
'ext-xml' => new Link ( 'a' , 'ext-xml' , $versionParser -> parseConstraints ( '*' )),
'ext-json' => new Link ( 'a' , 'ext-json' , $versionParser -> parseConstraints ( '*' )),
2022-08-17 12:20:07 +00:00
],
2020-11-22 13:48:56 +00:00
'no_php_required' ,
2022-08-17 12:20:07 +00:00
],
'Ignoring all platform requirements skips check completely' => [
[
2021-11-12 20:38:08 +00:00
'php' => new Link ( 'a' , 'php' , $versionParser -> parseConstraints ( '^7.2' )),
'ext-xml' => new Link ( 'a' , 'ext-xml' , $versionParser -> parseConstraints ( '*' )),
'ext-json' => new Link ( 'a' , 'ext-json' , $versionParser -> parseConstraints ( '*' )),
2022-08-17 12:20:07 +00:00
],
2020-06-01 14:01:43 +00:00
null ,
2022-08-17 12:20:07 +00:00
[],
[],
2020-11-22 13:48:56 +00:00
true ,
2022-08-17 12:20:07 +00:00
],
'Ignored platform requirements are not checked for' => [
[
2021-11-12 20:38:08 +00:00
'php' => new Link ( 'a' , 'php' , $versionParser -> parseConstraints ( '^7.2.8' )),
'ext-xml' => new Link ( 'a' , 'ext-xml' , $versionParser -> parseConstraints ( '*' )),
'ext-json' => new Link ( 'a' , 'ext-json' , $versionParser -> parseConstraints ( '*' )),
'ext-pdo' => new Link ( 'a' , 'ext-pdo' , $versionParser -> parseConstraints ( '*' )),
2022-08-17 12:20:07 +00:00
],
2020-06-01 14:01:43 +00:00
'no_php_required' ,
2022-08-17 12:20:07 +00:00
[],
[],
[ 'php' , 'ext-pdo' ],
],
'Via wildcard ignored platform requirements are not checked for' => [
[
2021-11-12 20:38:08 +00:00
'php' => new Link ( 'a' , 'php' , $versionParser -> parseConstraints ( '^7.2.8' )),
'ext-xml' => new Link ( 'a' , 'ext-xml' , $versionParser -> parseConstraints ( '*' )),
'ext-json' => new Link ( 'a' , 'ext-json' , $versionParser -> parseConstraints ( '*' )),
'ext-fileinfo' => new Link ( 'a' , 'ext-fileinfo' , $versionParser -> parseConstraints ( '*' )),
'ext-filesystem' => new Link ( 'a' , 'ext-filesystem' , $versionParser -> parseConstraints ( '*' )),
'ext-filter' => new Link ( 'a' , 'ext-filter' , $versionParser -> parseConstraints ( '*' )),
2022-08-17 12:20:07 +00:00
],
2021-11-11 14:56:38 +00:00
'no_php_required' ,
2022-08-17 12:20:07 +00:00
[],
[],
[ 'php' , 'ext-fil*' ],
],
'No extensions required' => [
[
2021-11-12 20:38:08 +00:00
'php' => new Link ( 'a' , 'php' , $versionParser -> parseConstraints ( '^7.2' )),
2022-08-17 12:20:07 +00:00
],
2020-11-22 13:48:56 +00:00
'no_extensions_required' ,
2022-08-17 12:20:07 +00:00
],
'Replaced/provided extensions are not checked for + checking case insensitivity' => [
[
2021-11-12 20:38:08 +00:00
'ext-xml' => new Link ( 'a' , 'ext-xml' , $versionParser -> parseConstraints ( '^7.2' )),
'ext-pdo' => new Link ( 'a' , 'ext-Pdo' , $versionParser -> parseConstraints ( '^7.2' )),
'ext-bcmath' => new Link ( 'a' , 'ext-bcMath' , $versionParser -> parseConstraints ( '^7.2' )),
2022-08-17 12:20:07 +00:00
],
2020-04-24 11:38:50 +00:00
'replaced_provided_exts' ,
2022-08-17 12:20:07 +00:00
[
2020-04-24 11:38:50 +00:00
// constraint does not satisfy all the ^7.2 requirement so we do not accept it as being replaced
2021-11-12 20:38:08 +00:00
'ext-pdo' => new Link ( 'a' , 'ext-PDO' , $versionParser -> parseConstraints ( '7.1.*' )),
2020-04-24 11:38:50 +00:00
// valid replace of bcmath so no need to check for it
2021-11-12 20:38:08 +00:00
'ext-bcmath' => new Link ( 'a' , 'ext-BCMath' , $versionParser -> parseConstraints ( '^7.1' )),
2022-08-17 12:20:07 +00:00
],
[
2020-04-24 11:38:50 +00:00
// valid provide of ext-xml so no need to check for it
2021-11-12 20:38:08 +00:00
'ext-xml' => new Link ( 'a' , 'ext-XML' , $versionParser -> parseConstraints ( '*' )),
2022-08-17 12:20:07 +00:00
],
],
];
2020-04-21 06:59:36 +00:00
}
2022-02-22 15:47:09 +00:00
private function assertAutoloadFiles ( string $name , string $dir , string $type = 'namespaces' ) : void
2011-12-03 22:20:06 +00:00
{
2012-11-10 20:54:23 +00:00
$a = __DIR__ . '/Fixtures/autoload_' . $name . '.php' ;
$b = $dir . '/autoload_' . $type . '.php' ;
2024-05-29 21:12:06 +00:00
self :: assertFileContentEquals ( $a , $b );
2011-12-03 22:20:06 +00:00
}
2015-05-28 13:42:19 +00:00
2022-02-18 09:38:54 +00:00
public static function assertFileContentEquals ( string $expected , string $actual , ? string $message = null ) : void
2015-05-28 13:42:19 +00:00
{
2021-12-10 12:14:04 +00:00
self :: assertSame (
str_replace ( " \r " , '' , ( string ) file_get_contents ( $expected )),
str_replace ( " \r " , '' , ( string ) file_get_contents ( $actual )),
$message ? ? $expected . ' equals ' . $actual
2015-05-28 13:42:19 +00:00
);
}
2011-12-03 22:20:06 +00:00
}