From 7203ebee24782d1f5f91087f6d79738ff3b3753f Mon Sep 17 00:00:00 2001 From: Tom Witkowski Date: Sat, 20 Jun 2020 08:22:07 +0200 Subject: [PATCH] Fix inline doc-block for final models (#955) * fix final model classes doc-block generation * add phpunit tests --- src/Console/ModelsCommand.php | 7 +- .../GenerateBasicPhpdocFinal/Models/Post.php | 9 + .../GenerateBasicPhpdocFinal/Test.php | 224 ++++++++++++++++++ 3 files changed, 236 insertions(+), 4 deletions(-) create mode 100644 tests/Console/ModelsCommand/GenerateBasicPhpdocFinal/Models/Post.php create mode 100644 tests/Console/ModelsCommand/GenerateBasicPhpdocFinal/Test.php diff --git a/src/Console/ModelsCommand.php b/src/Console/ModelsCommand.php index b7323f2..51dc649 100644 --- a/src/Console/ModelsCommand.php +++ b/src/Console/ModelsCommand.php @@ -766,11 +766,10 @@ class ModelsCommand extends Command if ($originalDoc) { $contents = str_replace($originalDoc, $docComment, $contents); } else { - $needle = "class {$classname}"; - $replace = "{$docComment}\nclass {$classname}"; - $pos = strpos($contents, $needle); + $replace = "{$docComment}\n"; + $pos = strpos($contents, "final class {$classname}") ?: strpos($contents, "class {$classname}"); if ($pos !== false) { - $contents = substr_replace($contents, $replace, $pos, strlen($needle)); + $contents = substr_replace($contents, $replace, $pos, 0); } } if ($this->files->put($filename, $contents)) { diff --git a/tests/Console/ModelsCommand/GenerateBasicPhpdocFinal/Models/Post.php b/tests/Console/ModelsCommand/GenerateBasicPhpdocFinal/Models/Post.php new file mode 100644 index 0000000..36b2c33 --- /dev/null +++ b/tests/Console/ModelsCommand/GenerateBasicPhpdocFinal/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/GenerateBasicPhpdocFinal/Models', + ], + ]); + } + + 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); + } +}