Skip to content

Commit

Permalink
[5.x] Scaffold PHPUnit test suite with make:addon command (#9593)
Browse files Browse the repository at this point in the history
Co-authored-by: Jason Varga <[email protected]>
  • Loading branch information
duncanmcclean and jasonvarga authored Mar 1, 2024
1 parent 180de96 commit 36e9f80
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 1 deletion.
31 changes: 30 additions & 1 deletion src/Console/Commands/MakeAddon.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ public function handle()
$this
->generateAddonFiles()
->installAddon()
->generateOptional();
->generateOptional()
->installComposerDependencies();
} catch (\Exception $e) {
$this->error($e->getMessage());

Expand Down Expand Up @@ -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 = [
Expand All @@ -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;
Expand Down Expand Up @@ -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.
*
Expand Down
14 changes: 14 additions & 0 deletions src/Console/Commands/stubs/addon/ExampleTest.php.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace {{ namespace }}\Tests;

class ExampleTest extends TestCase
{
/**
* A basic test example.
*/
public function test_that_true_is_true(): void
{
$this->assertTrue(true);
}
}
11 changes: 11 additions & 0 deletions src/Console/Commands/stubs/addon/TestCase.php.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace {{ namespace }}\Tests;

use {{ namespace }}\ServiceProvider;
use Statamic\Extend\AddonTestCase;

abstract class TestCase extends AddonTestCase
{
protected string $addonServiceProvider = ServiceProvider::class;
}
17 changes: 17 additions & 0 deletions src/Console/Commands/stubs/addon/composer.json.stub
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,23 @@
"DummyNamespace\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"DummyNamespace\\Tests\\": "tests"
}
},
"require": {
"statamic/cms": "^5.0"
},
"require-dev": {
"nunomaduro/collision": "^6.1",
"orchestra/testbench": "^7.0"
},
"config": {
"allow-plugins": {
"pixelfear/composer-dist-plugin": true
}
},
"extra": {
"statamic": {
"name": "DummyTitle",
Expand Down
21 changes: 21 additions & 0 deletions src/Console/Commands/stubs/addon/phpunit.xml.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" bootstrap="vendor/autoload.php" colors="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" beStrictAboutTestsThatDoNotTestAnything="false">
<coverage/>
<testsuites>
<testsuite name="Test Suite">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<php>
<env name="APP_ENV" value="testing"/>
<env name="APP_KEY" value="base64:ybcI9MKuhLnESRSuWDfnJQuohOXMBaynfbTC5Y5i1FE="/>
<env name="BCRYPT_ROUNDS" value="4"/>
<env name="CACHE_DRIVER" value="array"/>
<!-- <env name="DB_CONNECTION" value="sqlite"/> -->
<!-- <env name="DB_DATABASE" value=":memory:"/> -->
<env name="MAIL_MAILER" value="array"/>
<env name="QUEUE_CONNECTION" value="sync"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="TELESCOPE_ENABLED" value="false"/>
</php>
</phpunit>
10 changes: 10 additions & 0 deletions src/Console/Processes/Composer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
7 changes: 7 additions & 0 deletions tests/Console/Commands/MakeAddonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down

0 comments on commit 36e9f80

Please sign in to comment.