From 0692626c41553669fd09676e845d9197f300ecbc Mon Sep 17 00:00:00 2001 From: Pataar Date: Tue, 10 Mar 2020 12:18:29 +0100 Subject: [PATCH] Add ignored_models as config option (#890) * Add ignored_models as config option * Add README.md entry * Add tests * Change name of test class * Add Ignored to the return values from the mocked filesystem * Improve test speed by adding prestissimo * Revert "Improve test speed by adding prestissimo" This reverts commit 246a9f2a782a78757064298adc37d9a870b3ae25. --- config/ide-helper.php | 14 +++- readme.md | 8 ++ src/Console/ModelsCommand.php | 5 +- .../ModelsCommand/Ignored/Models/Ignored.php | 10 +++ .../Ignored/Models/NotIgnored.php | 9 +++ tests/Console/ModelsCommand/Ignored/Test.php | 81 +++++++++++++++++++ 6 files changed, 125 insertions(+), 2 deletions(-) create mode 100644 tests/Console/ModelsCommand/Ignored/Models/Ignored.php create mode 100644 tests/Console/ModelsCommand/Ignored/Models/NotIgnored.php create mode 100644 tests/Console/ModelsCommand/Ignored/Test.php diff --git a/config/ide-helper.php b/config/ide-helper.php index 374eacb..7748139 100644 --- a/config/ide-helper.php +++ b/config/ide-helper.php @@ -13,7 +13,7 @@ return array( 'filename' => '_ide_helper', 'format' => 'php', - + 'meta_filename' => '.phpstorm.meta.php', /* @@ -95,6 +95,18 @@ return array( 'app', ), + /* + |-------------------------------------------------------------------------- + | Models to ignore + |-------------------------------------------------------------------------- + | + | Define which models should be ignored. + | + */ + + 'ignored_models' => array( + + ), /* |-------------------------------------------------------------------------- diff --git a/readme.md b/readme.md index 704d10c..5fec144 100644 --- a/readme.md +++ b/readme.md @@ -149,6 +149,14 @@ Models can be ignored using the `--ignore (-I)` option php artisan ide-helper:models --ignore="Post,User" ``` +Or can be ignored by setting the `ignored_models` config +```php +'ignored_models' => array( + Post::class, + Api\User::class +), +``` + Note: With namespaces, wrap your model name in double-quotes (`"`): `php artisan ide-helper:models "API\User"`, or escape the slashes (`Api\\User`) For properly recognition of `Model` methods (i.e. `paginate`, `findOrFail`) you should extend `\Eloquent` or add diff --git a/src/Console/ModelsCommand.php b/src/Console/ModelsCommand.php index dd7a06c..7e3b9ba 100644 --- a/src/Console/ModelsCommand.php +++ b/src/Console/ModelsCommand.php @@ -185,7 +185,10 @@ class ModelsCommand extends Command } } - $ignore = explode(',', $ignore); + $ignore = array_merge( + explode(',', $ignore), + $this->laravel['config']->get('ide-helper.ignored_models', array()) + ); foreach ($models as $name) { if (in_array($name, $ignore)) { diff --git a/tests/Console/ModelsCommand/Ignored/Models/Ignored.php b/tests/Console/ModelsCommand/Ignored/Models/Ignored.php new file mode 100644 index 0000000..df4c7d2 --- /dev/null +++ b/tests/Console/ModelsCommand/Ignored/Models/Ignored.php @@ -0,0 +1,10 @@ +set('ide-helper', [ + 'model_locations' => [ + // This is calculated from the base_path() which points to + // vendor/orchestra/testbench-core/laravel + '/../../../../tests/Console/ModelsCommand/Ignored/Models', + ], + 'ignored_models' => [ + Ignored::class + ] + ]); + } + + public function test(): void + { + $actualContent = null; + $mockFilesystem = Mockery::mock(Filesystem::class); + $mockFilesystem + ->shouldReceive('get') + ->andReturn(file_get_contents(__DIR__ . '/Models/Ignored.php')) + ->andReturn(file_get_contents(__DIR__ . '/Models/NotIgnored.php')) + ->once(); + $mockFilesystem + ->shouldReceive('put') + ->with( + Mockery::any(), + Mockery::capture($actualContent) + ) + ->andReturn(1) // Simulate we wrote _something_ to the file + ->once(); + + $this->instance(Filesystem::class, $mockFilesystem); + + $command = $this->app->make(ModelsCommand::class); + + $tester = $this->runCommand($command, [ + '--write' => true, + ]); + + $this->assertSame(0, $tester->getStatusCode()); + $this->assertEmpty($tester->getDisplay()); + + $expectedContent = <<<'PHP' +assertSame($expectedContent, $actualContent); + } +}