Skip to content

Commit

Permalink
Merge pull request #362 from xepozz/default-config
Browse files Browse the repository at this point in the history
Add default config path
  • Loading branch information
Ocramius authored Dec 3, 2022
2 parents fb2a69a + 807867b commit 194cf64
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 5 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,16 @@ 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`
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"]
Expand Down
20 changes: 18 additions & 2 deletions src/ComposerRequireChecker/Cli/CheckCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
{
Expand All @@ -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',
Expand Down Expand Up @@ -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);
Expand Down
73 changes: 70 additions & 3 deletions test/ComposerRequireCheckerTest/Cli/CheckCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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'],
Expand All @@ -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',
Expand Down Expand Up @@ -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
*/
Expand Down
12 changes: 12 additions & 0 deletions test/fixtures/defaultConfigPath/bin/SomeClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

use Composer\InstalledVersions;

class SomeClass
{
public function __construct()
{
InstalledVersions::getInstalledPackages();
json_decode();
}
}
3 changes: 3 additions & 0 deletions test/fixtures/defaultConfigPath/composer-require-checker.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"scan-files": ["bin/SomeClass.php"]
}
8 changes: 8 additions & 0 deletions test/fixtures/defaultConfigPath/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "example/library",
"autoload": {
"psr-4": {
"Example\\Library\\": "src/"
}
}
}
13 changes: 13 additions & 0 deletions test/fixtures/defaultConfigPath/src/Great.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Example\Library;

use Composer\InstalledVersions;

class Great
{
public function __construct()
{
InstalledVersions::getInstalledPackages();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]

0 comments on commit 194cf64

Please sign in to comment.