2023-08-30 14:09:05 +00:00
< ? php declare ( strict_types = 1 );
namespace Composer\Test\Command ;
use Composer\Test\TestCase ;
use InvalidArgumentException ;
class DumpAutoloadCommandTest extends TestCase
{
public function testDumpAutoload () : void
{
$appTester = $this -> getApplicationTester ();
2024-05-29 21:12:06 +00:00
self :: assertSame ( 0 , $appTester -> run ([ 'command' => 'dump-autoload' ]));
2023-08-30 14:09:05 +00:00
$output = $appTester -> getDisplay ( true );
2024-05-29 21:12:06 +00:00
self :: assertStringContainsString ( 'Generating autoload files' , $output );
self :: assertStringContainsString ( 'Generated autoload files' , $output );
2023-08-30 14:09:05 +00:00
}
public function testDumpDevAutoload () : void
{
$appTester = $this -> getApplicationTester ();
2024-05-29 21:12:06 +00:00
self :: assertSame ( 0 , $appTester -> run ([ 'command' => 'dump-autoload' , '--dev' => true ]));
2023-08-30 14:09:05 +00:00
$output = $appTester -> getDisplay ( true );
2024-05-29 21:12:06 +00:00
self :: assertStringContainsString ( 'Generating autoload files' , $output );
self :: assertStringContainsString ( 'Generated autoload files' , $output );
2023-08-30 14:09:05 +00:00
}
public function testDumpNoDevAutoload () : void
{
$appTester = $this -> getApplicationTester ();
2024-05-29 21:12:06 +00:00
self :: assertSame ( 0 , $appTester -> run ([ 'command' => 'dump-autoload' , '--dev' => true ]));
2023-08-30 14:09:05 +00:00
$output = $appTester -> getDisplay ( true );
2024-05-29 21:12:06 +00:00
self :: assertStringContainsString ( 'Generating autoload files' , $output );
self :: assertStringContainsString ( 'Generated autoload files' , $output );
2023-08-30 14:09:05 +00:00
}
public function testUsingOptimizeAndStrictPsr () : void
{
$appTester = $this -> getApplicationTester ();
2024-05-29 21:12:06 +00:00
self :: assertSame ( 0 , $appTester -> run ([ 'command' => 'dump-autoload' , '--optimize' => true , '--strict-psr' => true ]));
2023-08-30 14:09:05 +00:00
$output = $appTester -> getDisplay ( true );
2024-05-29 21:12:06 +00:00
self :: assertStringContainsString ( 'Generating optimized autoload files' , $output );
self :: assertMatchesRegularExpression ( '/Generated optimized autoload files containing \d+ classes/' , $output );
2023-08-30 14:09:05 +00:00
}
2023-08-30 19:42:45 +00:00
public function testFailsUsingStrictPsrIfClassMapViolationsAreFound () : void
{
$dir = $this -> initTempComposer ([
'autoload' => [
'psr-4' => [
'Application\\' => 'src' ,
]
]
]);
mkdir ( $dir . '/src/' );
file_put_contents ( $dir . '/src/Foo.php' , '<?php namespace Application\Src; class Foo {}' );
$appTester = $this -> getApplicationTester ();
2024-05-29 21:12:06 +00:00
self :: assertSame ( 1 , $appTester -> run ([ 'command' => 'dump-autoload' , '--optimize' => true , '--strict-psr' => true ]));
2023-08-30 19:42:45 +00:00
$output = $appTester -> getDisplay ( true );
2024-05-31 13:08:44 +00:00
self :: assertMatchesRegularExpression ( '#Class Application\\\\Src\\\\Foo located in .*? does not comply with psr-4 autoloading standard \(rule: Application\\\\ => \./src\)\. Skipping\.#' , $output );
2023-08-30 19:42:45 +00:00
}
2023-08-30 14:09:05 +00:00
public function testUsingClassmapAuthoritative () : void
{
$appTester = $this -> getApplicationTester ();
2024-05-29 21:12:06 +00:00
self :: assertSame ( 0 , $appTester -> run ([ 'command' => 'dump-autoload' , '--classmap-authoritative' => true ]));
2023-08-30 14:09:05 +00:00
$output = $appTester -> getDisplay ( true );
2024-05-29 21:12:06 +00:00
self :: assertStringContainsString ( 'Generating optimized autoload files (authoritative)' , $output );
self :: assertMatchesRegularExpression ( '/Generated optimized autoload files \(authoritative\) containing \d+ classes/' , $output );
2023-08-30 14:09:05 +00:00
}
2023-08-30 19:43:16 +00:00
public function testUsingClassmapAuthoritativeAndStrictPsr () : void
{
$appTester = $this -> getApplicationTester ();
2024-05-29 21:12:06 +00:00
self :: assertSame ( 0 , $appTester -> run ([ 'command' => 'dump-autoload' , '--classmap-authoritative' => true , '--strict-psr' => true ]));
2023-08-30 19:43:16 +00:00
$output = $appTester -> getDisplay ( true );
2024-05-29 21:12:06 +00:00
self :: assertStringContainsString ( 'Generating optimized autoload files' , $output );
self :: assertMatchesRegularExpression ( '/Generated optimized autoload files \(authoritative\) containing \d+ classes/' , $output );
2023-08-30 19:43:16 +00:00
}
2023-08-30 14:09:05 +00:00
public function testStrictPsrDoesNotWorkWithoutOptimizedAutoloader () : void
{
$appTester = $this -> getApplicationTester ();
$this -> expectException ( InvalidArgumentException :: class );
2023-08-30 19:43:16 +00:00
$this -> expectExceptionMessage ( '--strict-psr mode only works with optimized autoloader, use --optimize or --classmap-authoritative if you want a strict return value.' );
2023-08-30 14:09:05 +00:00
$appTester -> run ([ 'command' => 'dump-autoload' , '--strict-psr' => true ]);
}
public function testDevAndNoDevCannotBeCombined () : void
{
$appTester = $this -> getApplicationTester ();
$this -> expectException ( InvalidArgumentException :: class );
$this -> expectExceptionMessage ( 'You can not use both --no-dev and --dev as they conflict with each other.' );
$appTester -> run ([ 'command' => 'dump-autoload' , '--dev' => true , '--no-dev' => true ]);
}
2023-09-28 09:43:52 +00:00
public function testWithCustomAutoloaderSuffix () : void
{
$dir = $this -> initTempComposer ([
'config' => [
'autoloader-suffix' => 'Foobar' ,
],
]);
$appTester = $this -> getApplicationTester ();
2024-05-29 21:12:06 +00:00
self :: assertSame ( 0 , $appTester -> run ([ 'command' => 'dump-autoload' ]));
2023-09-28 09:43:52 +00:00
self :: assertStringContainsString ( 'ComposerAutoloaderInitFoobar' , ( string ) file_get_contents ( $dir . '/vendor/autoload.php' ));
}
public function testWithExistingComposerLockAndAutoloaderSuffix () : void
{
$dir = $this -> initTempComposer (
[
'config' => [
'autoloader-suffix' => 'Foobar' ,
],
],
[],
[
" _readme " => [
" This file locks the dependencies of your project to a known state " ,
" Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies " ,
" This file is @generated automatically "
],
" content-hash " => " d751713988987e9331980363e24189ce " ,
" packages " => [],
" packages-dev " => [],
" aliases " => [],
" minimum-stability " => " stable " ,
" stability-flags " => [],
" prefer-stable " => false ,
" prefer-lowest " => false ,
" platform " => [],
" platform-dev " => [],
" plugin-api-version " => " 2.6.0 "
]
);
$appTester = $this -> getApplicationTester ();
2024-05-29 21:12:06 +00:00
self :: assertSame ( 0 , $appTester -> run ([ 'command' => 'dump-autoload' ]));
2023-09-28 09:43:52 +00:00
self :: assertStringContainsString ( 'ComposerAutoloaderInitFoobar' , ( string ) file_get_contents ( $dir . '/vendor/autoload.php' ));
}
public function testWithExistingComposerLockWithoutAutoloaderSuffix () : void
{
$dir = $this -> initTempComposer (
[
'name' => 'foo/bar' ,
],
[],
[
" _readme " => [
" This file locks the dependencies of your project to a known state " ,
" Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies " ,
" This file is @generated automatically "
],
" content-hash " => " 2d4a6be9a93712c5d6a119b26734a047 " ,
" packages " => [],
" packages-dev " => [],
" aliases " => [],
" minimum-stability " => " stable " ,
" stability-flags " => [],
" prefer-stable " => false ,
" prefer-lowest " => false ,
" platform " => [],
" platform-dev " => [],
" plugin-api-version " => " 2.6.0 "
]
);
$appTester = $this -> getApplicationTester ();
2024-05-29 21:12:06 +00:00
self :: assertSame ( 0 , $appTester -> run ([ 'command' => 'dump-autoload' ]));
2023-09-28 09:43:52 +00:00
self :: assertStringContainsString ( 'ComposerAutoloaderInit2d4a6be9a93712c5d6a119b26734a047' , ( string ) file_get_contents ( $dir . '/vendor/autoload.php' ));
}
2023-08-30 14:09:05 +00:00
}