From 36e9f8020ec777254101d925b84217437aa85d5d Mon Sep 17 00:00:00 2001 From: Duncan McClean Date: Fri, 1 Mar 2024 18:33:48 +0000 Subject: [PATCH] [5.x] Scaffold PHPUnit test suite with `make:addon` command (#9593) Co-authored-by: Jason Varga --- src/Console/Commands/MakeAddon.php | 31 ++++++++++++++++++- .../Commands/stubs/addon/ExampleTest.php.stub | 14 +++++++++ .../Commands/stubs/addon/TestCase.php.stub | 11 +++++++ .../Commands/stubs/addon/composer.json.stub | 17 ++++++++++ .../Commands/stubs/addon/phpunit.xml.stub | 21 +++++++++++++ src/Console/Processes/Composer.php | 10 ++++++ tests/Console/Commands/MakeAddonTest.php | 7 +++++ 7 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 src/Console/Commands/stubs/addon/ExampleTest.php.stub create mode 100644 src/Console/Commands/stubs/addon/TestCase.php.stub create mode 100644 src/Console/Commands/stubs/addon/phpunit.xml.stub diff --git a/src/Console/Commands/MakeAddon.php b/src/Console/Commands/MakeAddon.php index cf7133d15f..bada10e715 100644 --- a/src/Console/Commands/MakeAddon.php +++ b/src/Console/Commands/MakeAddon.php @@ -80,7 +80,8 @@ public function handle() $this ->generateAddonFiles() ->installAddon() - ->generateOptional(); + ->generateOptional() + ->installComposerDependencies(); } catch (\Exception $e) { $this->error($e->getMessage()); @@ -137,8 +138,11 @@ protected function generateAddonFiles() $files = [ 'addon/provider.php.stub' => 'src/ServiceProvider.php', + 'addon/TestCase.php.stub' => 'tests/TestCase.php', + 'addon/ExampleTest.php.stub' => 'tests/ExampleTest.php', 'addon/.gitignore.stub' => '.gitignore', 'addon/README.md.stub' => 'README.md', + 'addon/phpunit.xml.stub' => 'phpunit.xml', ]; $data = [ @@ -150,6 +154,7 @@ protected function generateAddonFiles() foreach ($files as $stub => $file) { $this->createFromStub($stub, $this->addonPath($file), $data); } + $this->checkInfo('Addon boilerplate created successfully.'); return $this; @@ -183,6 +188,30 @@ protected function generateOptional() return $this; } + /** + * Installs the addon's composer dependencies. + * + * @return $this + */ + protected function installComposerDependencies() + { + $this->output->newLine(); + + $this->line("Installing your addon's Composer dependencies. This may take a moment..."); + + try { + Composer::withoutQueue()->throwOnFailure()->install($this->addonPath()); + } catch (ProcessException $exception) { + $this->line($exception->getMessage()); + $this->output->newLine(); + throw new \Exception("An error was encountered while installing your addon's Composer dependencies!"); + } + + $this->checkInfo('Composer dependencies installed successfully.'); + + return $this; + } + /** * Add repository path to app's composer.json file. * diff --git a/src/Console/Commands/stubs/addon/ExampleTest.php.stub b/src/Console/Commands/stubs/addon/ExampleTest.php.stub new file mode 100644 index 0000000000..2304feff96 --- /dev/null +++ b/src/Console/Commands/stubs/addon/ExampleTest.php.stub @@ -0,0 +1,14 @@ +assertTrue(true); + } +} diff --git a/src/Console/Commands/stubs/addon/TestCase.php.stub b/src/Console/Commands/stubs/addon/TestCase.php.stub new file mode 100644 index 0000000000..14d8bbb568 --- /dev/null +++ b/src/Console/Commands/stubs/addon/TestCase.php.stub @@ -0,0 +1,11 @@ + + + + + + ./tests + + + + + + + + + + + + + + + diff --git a/src/Console/Processes/Composer.php b/src/Console/Processes/Composer.php index 0157feb377..0418d8f001 100644 --- a/src/Console/Processes/Composer.php +++ b/src/Console/Processes/Composer.php @@ -204,6 +204,16 @@ public function update(string $package) $this->queueComposerCommand('update', $package, '--with-dependencies'); } + /** + * Install Composer dependencies. + * + * @return void + */ + public function install(string $workingDirectory) + { + $this->runComposerCommand('install', '--working-dir='.$workingDirectory); + } + /** * Get cached output for package process. * diff --git a/tests/Console/Commands/MakeAddonTest.php b/tests/Console/Commands/MakeAddonTest.php index 6f281f0bab..6b02194c9f 100644 --- a/tests/Console/Commands/MakeAddonTest.php +++ b/tests/Console/Commands/MakeAddonTest.php @@ -37,12 +37,19 @@ public function it_can_generate_an_addon() $this->assertFileExists(base_path('addons/hasselhoff/knight-rider/README.md')); $this->assertFileExists(base_path('addons/hasselhoff/knight-rider/.gitignore')); + $this->assertFileExists(base_path('addons/hasselhoff/knight-rider/phpunit.xml')); $this->assertFileExists($composerJson = base_path('addons/hasselhoff/knight-rider/composer.json')); $this->assertStringContainsString('"Hasselhoff\\\KnightRider\\\": "src"', $this->files->get($composerJson)); $this->assertFileExists($provider = base_path('addons/hasselhoff/knight-rider/src/ServiceProvider.php')); $this->assertStringContainsString('namespace Hasselhoff\KnightRider;', $this->files->get($provider)); + + $this->assertFileExists($testCase = base_path('addons/hasselhoff/knight-rider/tests/TestCase.php')); + $this->assertStringContainsString('namespace Hasselhoff\KnightRider\Tests;', $this->files->get($testCase)); + + $this->assertFileExists($exampleTest = base_path('addons/hasselhoff/knight-rider/tests/ExampleTest.php')); + $this->assertStringContainsString('namespace Hasselhoff\KnightRider\Tests;', $this->files->get($exampleTest)); } /** @test */