From da82d003b9835129a212083b7115131a4a8f3976 Mon Sep 17 00:00:00 2001 From: Tom Witkowski Date: Fri, 19 Jun 2020 19:25:16 +0200 Subject: [PATCH 1/2] fix final model classes doc-block generation --- src/Console/ModelsCommand.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/Console/ModelsCommand.php b/src/Console/ModelsCommand.php index b7323f2b7..51dc64965 100644 --- a/src/Console/ModelsCommand.php +++ b/src/Console/ModelsCommand.php @@ -766,11 +766,10 @@ protected function createPhpDocs($class) 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)) { From 5fe2c28b6dfae869a68002e5b1db25a544906399 Mon Sep 17 00:00:00 2001 From: Tom Witkowski Date: Fri, 19 Jun 2020 19:25:35 +0200 Subject: [PATCH 2/2] add phpunit tests --- .../GenerateBasicPhpdocFinal/Models/Post.php | 9 + .../GenerateBasicPhpdocFinal/Test.php | 224 ++++++++++++++++++ 2 files changed, 233 insertions(+) create mode 100644 tests/Console/ModelsCommand/GenerateBasicPhpdocFinal/Models/Post.php create mode 100644 tests/Console/ModelsCommand/GenerateBasicPhpdocFinal/Test.php diff --git a/tests/Console/ModelsCommand/GenerateBasicPhpdocFinal/Models/Post.php b/tests/Console/ModelsCommand/GenerateBasicPhpdocFinal/Models/Post.php new file mode 100644 index 000000000..36b2c3300 --- /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); + } +}