diff --git a/README.md b/README.md index 920dd0ec..b16cd086 100644 --- a/README.md +++ b/README.md @@ -84,6 +84,8 @@ extensions and internal symbols like `true` or `false` being undefined. bin/composer-require-checker check --config-file=path/to/config.json /path/to/your/project/composer.json ``` +By default, it uses `composer-require-checker.json` if the file exists. + ### Scan Additional Files To scan files, that are not part of your autoload definition you may add glob patterns to the config file's `scan-files` @@ -91,6 +93,7 @@ section. The following example configuration file would also scan the file `bin/console` and all files with `.php` extension within your `bin/` folder: +`composer-require-checker.json`: ```json { "scan-files" : ["bin/console", "bin/*.php"] diff --git a/src/ComposerRequireChecker/Cli/CheckCommand.php b/src/ComposerRequireChecker/Cli/CheckCommand.php index 96caf6e4..3b4687ec 100644 --- a/src/ComposerRequireChecker/Cli/CheckCommand.php +++ b/src/ComposerRequireChecker/Cli/CheckCommand.php @@ -39,6 +39,7 @@ use function assert; use function count; use function dirname; +use function file_exists; use function gettype; use function in_array; use function is_string; @@ -50,7 +51,8 @@ */ class CheckCommand extends Command { - public const NAME = 'check'; + public const NAME = 'check'; + private const DEFAULT_CONFIG_PATH = 'composer-require-checker.json'; protected function configure(): void { @@ -61,7 +63,8 @@ protected function configure(): void 'config-file', null, InputOption::VALUE_REQUIRED, - 'the config.json file to configure the checking options' + 'the config file to configure the checking options', + self::DEFAULT_CONFIG_PATH ) ->addArgument( 'composer-json', @@ -242,6 +245,19 @@ private function getCheckOptions(InputInterface $input): Options return new Options(); } + if (file_exists($fileName) === false) { + if ($fileName === self::DEFAULT_CONFIG_PATH) { + return new Options(); + } + + throw new InvalidArgumentException( + sprintf( + 'Configuration file [%s] does not exist.', + $fileName + ) + ); + } + $config = JsonLoader::getData($fileName); return new Options($config); diff --git a/test/ComposerRequireCheckerTest/Cli/CheckCommandTest.php b/test/ComposerRequireCheckerTest/Cli/CheckCommandTest.php index 34b55d2c..a9fa266b 100644 --- a/test/ComposerRequireCheckerTest/Cli/CheckCommandTest.php +++ b/test/ComposerRequireCheckerTest/Cli/CheckCommandTest.php @@ -13,6 +13,7 @@ use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Tester\CommandTester; +use function chdir; use function dirname; use function file_put_contents; use function json_decode; @@ -37,7 +38,7 @@ protected function setUp(): void public function testExceptionIfComposerJsonIsNotAString(): void { - self::expectException(InvalidArgumentException::class); + $this->expectException(InvalidArgumentException::class); $this->commandTester->execute([ 'composer-json' => ['this-is-a-array-as-input'], @@ -46,14 +47,14 @@ public function testExceptionIfComposerJsonIsNotAString(): void public function testExceptionIfComposerJsonNotFound(): void { - self::expectException(InvalidArgumentException::class); + $this->expectException(InvalidArgumentException::class); $this->commandTester->execute(['composer-json' => 'this-will-not-be-found.json']); } public function testExceptionIfNoSymbolsFound(): void { - self::expectException(LogicException::class); + $this->expectException(LogicException::class); $this->commandTester->execute([ 'composer-json' => dirname(__DIR__, 2) . '/fixtures/noSymbols/composer.json', @@ -269,6 +270,72 @@ public function testNoUnknownComposerSymbolFound(): void ); } + public function testDefaultConfigPath(): void + { + $baseDirectory = dirname(__DIR__, 2) . '/fixtures/defaultConfigPath/'; + + chdir($baseDirectory); + $exitCode = $this->commandTester->execute(['composer-json' => 'composer.json']); + $output = $this->commandTester->getDisplay(); + + $this->assertNotEquals(0, $exitCode); + $this->assertMatchesRegularExpression( + '/The following 2 unknown symbols were found/s', + $output + ); + $this->assertMatchesRegularExpression( + '/Composer\\\\InstalledVersions/s', + $output + ); + $this->assertMatchesRegularExpression( + '/json_decode/s', + $output + ); + } + + public function testOverrideDefaultConfigPath(): void + { + $baseDirectory = dirname(__DIR__, 2) . '/fixtures/defaultConfigPath/'; + $root = vfsStream::setup(); + vfsStream::create(['config.json' => '{"scan-files": []}']); + + chdir($baseDirectory); + $exitCode = $this->commandTester->execute([ + 'composer-json' => 'composer.json', + '--config-file' => $root->getChild('config.json')->url(), + ]); + + $output = $this->commandTester->getDisplay(); + + $this->assertNotEquals(0, $exitCode); + $this->assertMatchesRegularExpression( + '/The following 1 unknown symbols were found/s', + $output + ); + $this->assertMatchesRegularExpression( + '/Composer\\\\InstalledVersions/s', + $output + ); + $this->assertDoesNotMatchRegularExpression( + '/json_decode/s', + $output + ); + } + + public function testNotExistentConfigPath(): void + { + $baseDirectory = dirname(__DIR__, 2) . '/fixtures/defaultConfigPath/'; + + chdir($baseDirectory); + + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('Configuration file [not-existent-config.json] does not exist.'); + $this->commandTester->execute([ + 'composer-json' => 'composer.json', + '--config-file' => 'not-existent-config.json', + ]); + } + /** * @requires PHP >= 8.1.0 */ diff --git a/test/fixtures/defaultConfigPath/bin/SomeClass.php b/test/fixtures/defaultConfigPath/bin/SomeClass.php new file mode 100644 index 00000000..1a3aebed --- /dev/null +++ b/test/fixtures/defaultConfigPath/bin/SomeClass.php @@ -0,0 +1,12 @@ +