From cca99b6d252b0869a43fe89bcea31c859c68d1c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luka=20Sch=C3=A4fer?= <33277331+AegirLeet@users.noreply.github.com> Date: Tue, 23 Jun 2020 22:27:43 +0200 Subject: [PATCH] Make writing relation count properties optional (#969) * Make writing relation count properties optional * Clean up description * Add test for write_model_magic_where=false * Add test for write_model_relation_count_properties=false --- config/ide-helper.php | 11 + src/Console/ModelsCommand.php | 17 +- .../ModelsCommand/MagicWhere/Models/Post.php | 9 + .../Console/ModelsCommand/MagicWhere/Test.php | 150 ++++++++++++ .../RelationCountProperties/Models/Post.php | 14 ++ .../RelationCountProperties/Test.php | 231 ++++++++++++++++++ 6 files changed, 426 insertions(+), 6 deletions(-) create mode 100644 tests/Console/ModelsCommand/MagicWhere/Models/Post.php create mode 100644 tests/Console/ModelsCommand/MagicWhere/Test.php create mode 100644 tests/Console/ModelsCommand/RelationCountProperties/Models/Post.php create mode 100644 tests/Console/ModelsCommand/RelationCountProperties/Test.php diff --git a/config/ide-helper.php b/config/ide-helper.php index d127b5d..0a04f2e 100644 --- a/config/ide-helper.php +++ b/config/ide-helper.php @@ -50,6 +50,17 @@ return array( 'write_model_magic_where' => true, + /* + |-------------------------------------------------------------------------- + | Write Model relation count properties + |-------------------------------------------------------------------------- + | + | Set to false to disable writing of relation count properties to model DocBlocks. + | + */ + + 'write_model_relation_count_properties' => true, + /* |-------------------------------------------------------------------------- | Write Eloquent Model Mixins diff --git a/src/Console/ModelsCommand.php b/src/Console/ModelsCommand.php index 9dee80e..e82d8b7 100644 --- a/src/Console/ModelsCommand.php +++ b/src/Console/ModelsCommand.php @@ -57,6 +57,7 @@ class ModelsCommand extends Command protected $description = 'Generate autocompletion for models'; protected $write_model_magic_where; + protected $write_model_relation_count_properties; protected $properties = array(); protected $methods = array(); protected $write = false; @@ -107,6 +108,8 @@ class ModelsCommand extends Command $this->keep_text = $this->reset = true; } $this->write_model_magic_where = $this->laravel['config']->get('ide-helper.write_model_magic_where', true); + $this->write_model_relation_count_properties = + $this->laravel['config']->get('ide-helper.write_model_relation_count_properties', true); //If filename is default and Write is not specified, ask what to do if (!$this->write && $filename === $this->filename && !$this->option('nowrite')) { @@ -587,12 +590,14 @@ class ModelsCommand extends Command true, null ); - $this->setProperty( - Str::snake($method) . '_count', - 'int|null', - true, - false - ); + if ($this->write_model_relation_count_properties) { + $this->setProperty( + Str::snake($method) . '_count', + 'int|null', + true, + false + ); + } } elseif ($relation === "morphTo") { // Model isn't specified because relation is polymorphic $this->setProperty( diff --git a/tests/Console/ModelsCommand/MagicWhere/Models/Post.php b/tests/Console/ModelsCommand/MagicWhere/Models/Post.php new file mode 100644 index 0000000..865cf42 --- /dev/null +++ b/tests/Console/ModelsCommand/MagicWhere/Models/Post.php @@ -0,0 +1,9 @@ +set('ide-helper', [ + 'model_locations' => [ + // This is calculated from the base_path() which points to + // vendor/orchestra/testbench-core/laravel + '/../../../../tests/Console/ModelsCommand/MagicWhere/Models', + ], + 'write_model_magic_where' => false + ]); + } + + public function test(): void + { + $actualContent = null; + $mockFilesystem = Mockery::mock(Filesystem::class); + $mockFilesystem + ->shouldReceive('get') + ->andReturn(file_get_contents(__DIR__ . '/Models/Post.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); + } +} diff --git a/tests/Console/ModelsCommand/RelationCountProperties/Models/Post.php b/tests/Console/ModelsCommand/RelationCountProperties/Models/Post.php new file mode 100644 index 0000000..9f92526 --- /dev/null +++ b/tests/Console/ModelsCommand/RelationCountProperties/Models/Post.php @@ -0,0 +1,14 @@ +hasMany(Post::class); + } +} diff --git a/tests/Console/ModelsCommand/RelationCountProperties/Test.php b/tests/Console/ModelsCommand/RelationCountProperties/Test.php new file mode 100644 index 0000000..8f55849 --- /dev/null +++ b/tests/Console/ModelsCommand/RelationCountProperties/Test.php @@ -0,0 +1,231 @@ +set('ide-helper', [ + 'model_locations' => [ + // This is calculated from the base_path() which points to + // vendor/orchestra/testbench-core/laravel + '/../../../../tests/Console/ModelsCommand/RelationCountProperties/Models', + ], + 'write_model_relation_count_properties' => false + ]); + } + + public function test(): void + { + $actualContent = null; + $mockFilesystem = Mockery::mock(Filesystem::class); + $mockFilesystem + ->shouldReceive('get') + ->andReturn(file_get_contents(__DIR__ . '/Models/Post.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' +hasMany(Post::class); + } +} + +PHP; + + $this->assertSame($expectedContent, $actualContent); + } +}