From 6a27c5df683bded7db8529b65b611ab90a8937d9 Mon Sep 17 00:00:00 2001 From: Daniel Mason Date: Sun, 19 Apr 2020 06:49:23 +0100 Subject: [PATCH] Add custom collection support for get and all methods (#903) * Add custom collection support for get and all methods * Only add get and all when using custom collection * Use static instead of class name * Add missing custom collection test with relation * Use class reference over string Co-Authored-By: Markus Podar * Fix when using class reference Co-authored-by: Markus Podar --- src/Console/ModelsCommand.php | 16 ++++ .../Collections/SimpleCollection.php | 10 ++ .../CustomCollection/Models/Simple.php | 20 ++++ .../ModelsCommand/CustomCollection/Test.php | 94 +++++++++++++++++++ 4 files changed, 140 insertions(+) create mode 100644 tests/Console/ModelsCommand/CustomCollection/Collections/SimpleCollection.php create mode 100644 tests/Console/ModelsCommand/CustomCollection/Models/Simple.php create mode 100644 tests/Console/ModelsCommand/CustomCollection/Test.php diff --git a/src/Console/ModelsCommand.php b/src/Console/ModelsCommand.php index ed9d2d6..1d56281 100644 --- a/src/Console/ModelsCommand.php +++ b/src/Console/ModelsCommand.php @@ -12,6 +12,7 @@ namespace Barryvdh\LaravelIdeHelper\Console; use Composer\Autoload\ClassMapGenerator; use Illuminate\Console\Command; +use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Relations\Relation; use Illuminate\Support\Str; use Illuminate\Filesystem\Filesystem; @@ -229,6 +230,7 @@ class ModelsCommand extends Command $this->getPropertiesFromMethods($model); $this->getSoftDeleteMethods($model); + $this->getCollectionMethods($model); $output .= $this->createPhpDocs($name); $ignore[] = $name; $this->nullableColumns = []; @@ -850,6 +852,20 @@ class ModelsCommand extends Command } } + /** + * Generates methods that return collections + * @param \Illuminate\Database\Eloquent\Model $model + */ + protected function getCollectionMethods($model) + { + $collectionClass = $this->getCollectionClass(get_class($model)); + + if ($collectionClass !== '\\' . \Illuminate\Database\Eloquent\Collection::class) { + $this->setMethod('get', $collectionClass . '|static[]', ['$columns = [\'*\']']); + $this->setMethod('all', $collectionClass . '|static[]', ['$columns = [\'*\']']); + } + } + /** * @param ReflectionClass $reflection * @return string diff --git a/tests/Console/ModelsCommand/CustomCollection/Collections/SimpleCollection.php b/tests/Console/ModelsCommand/CustomCollection/Collections/SimpleCollection.php new file mode 100644 index 0000000..2bce051 --- /dev/null +++ b/tests/Console/ModelsCommand/CustomCollection/Collections/SimpleCollection.php @@ -0,0 +1,10 @@ +hasMany(Simple::class); + } +} diff --git a/tests/Console/ModelsCommand/CustomCollection/Test.php b/tests/Console/ModelsCommand/CustomCollection/Test.php new file mode 100644 index 0000000..f4999f1 --- /dev/null +++ b/tests/Console/ModelsCommand/CustomCollection/Test.php @@ -0,0 +1,94 @@ +set('ide-helper', [ + 'model_locations' => [ + // This is calculated from the base_path() which points to + // vendor/orchestra/testbench-core/laravel + '/../../../../tests/Console/ModelsCommand/CustomCollection/Models', + ], + ]); + } + + public function test(): void + { + $actualContent = null; + $mockFilesystem = Mockery::mock(Filesystem::class); + $mockFilesystem + ->shouldReceive('get') + ->andReturn(file_get_contents(__DIR__ . '/Models/Simple.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(Simple::class); + } +} + +PHP; + + $this->assertSame($expectedContent, $actualContent); + } +}