From 6c577bf2c07d7f04ec50575dd41923db38cf1b0b Mon Sep 17 00:00:00 2001 From: Duncan McClean Date: Thu, 22 Feb 2024 15:20:50 +0000 Subject: [PATCH 1/5] Initial AddonTestCase --- src/Extend/AddonTestCase.php | 86 ++++++++++++++++++++++++++++++++++++ src/Extend/Manifest.php | 2 +- 2 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 src/Extend/AddonTestCase.php diff --git a/src/Extend/AddonTestCase.php b/src/Extend/AddonTestCase.php new file mode 100644 index 0000000000..4df6d0ff05 --- /dev/null +++ b/src/Extend/AddonTestCase.php @@ -0,0 +1,86 @@ +withoutMix(); + $this->withoutVite(); + + Version::shouldReceive('get')->andReturn(Composer::create(__DIR__.'/../')->installedVersion(Statamic::PACKAGE)); + $this->addToAssertionCount(-1); + } + + protected function getPackageProviders($app) + { + // TODO: When the GraphQL package is installed, register the service provider here. + return [ + StatamicServiceProvider::class, + $this->addonServiceProvider, + ]; + } + + protected function getPackageAliases($app) + { + return [ + 'Statamic' => Statamic::class, + ]; + } + + protected function getEnvironmentSetUp($app) + { + parent::getEnvironmentSetUp($app); + + $path = dirname((new \ReflectionClass($this->addonServiceProvider))->getFileName()).'/../composer.json'; + $composerJson = $app['files']->get($path); + + $package = json_decode($composerJson, true); + $package['version'] = 'dev-main'; + + // TODO: Ideally, we shouldn't be mocking stuff in the TestCase. + Marketplace::shouldReceive('package') + ->once() + ->with($package['name'], $package['version']) + ->andReturn([ + 'id' => null, + 'slug' => null, + 'url' => null, + 'seller' => null, + 'latest_version' => null, + ]); + + $app->make(Manifest::class)->manifest = [ + $package['name'] => app(Manifest::class)->formatPackage($package), + ]; + } + + protected function resolveApplicationConfiguration($app) + { + parent::resolveApplicationConfiguration($app); + + // TODO: Just grab all the files. + $configs = [ + 'assets', 'cp', 'forms', 'static_caching', + 'sites', 'stache', 'system', 'users', + ]; + + foreach ($configs as $config) { + $app['config']->set("statamic.$config", require (__DIR__."/../../config/{$config}.php")); + } + + $app['config']->set('statamic.users.repository', 'file'); + } +} diff --git a/src/Extend/Manifest.php b/src/Extend/Manifest.php index b038b02637..8fa9b192d5 100644 --- a/src/Extend/Manifest.php +++ b/src/Extend/Manifest.php @@ -31,7 +31,7 @@ public function build() $this->getManifest(); } - protected function formatPackage($package) + public function formatPackage($package) { if (! $provider = $package['extra']['laravel']['providers'][0] ?? null) { return; From d631f073874265e719c25127d950eb7446309d8b Mon Sep 17 00:00:00 2001 From: Duncan McClean Date: Thu, 22 Feb 2024 15:34:59 +0000 Subject: [PATCH 2/5] Avoid faking stuff for the manifest --- src/Extend/AddonTestCase.php | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/src/Extend/AddonTestCase.php b/src/Extend/AddonTestCase.php index 4df6d0ff05..63c5f3ecd8 100644 --- a/src/Extend/AddonTestCase.php +++ b/src/Extend/AddonTestCase.php @@ -2,9 +2,9 @@ namespace Statamic\Extend; -use Facades\Statamic\Marketplace\Marketplace; use Facades\Statamic\Version; use Orchestra\Testbench\TestCase as OrchestraTestCase; +use ReflectionClass; use Statamic\Console\Processes\Composer; use Statamic\Providers\StatamicServiceProvider; use Statamic\Statamic; @@ -44,26 +44,25 @@ protected function getEnvironmentSetUp($app) { parent::getEnvironmentSetUp($app); - $path = dirname((new \ReflectionClass($this->addonServiceProvider))->getFileName()).'/../composer.json'; - $composerJson = $app['files']->get($path); + $reflector = new ReflectionClass($this->addonServiceProvider); + $directory = dirname($reflector->getFileName()); - $package = json_decode($composerJson, true); - $package['version'] = 'dev-main'; + $providerParts = explode('\\', $this->addonServiceProvider, -1); + $namespace = implode('\\', $providerParts); - // TODO: Ideally, we shouldn't be mocking stuff in the TestCase. - Marketplace::shouldReceive('package') - ->once() - ->with($package['name'], $package['version']) - ->andReturn([ - 'id' => null, - 'slug' => null, - 'url' => null, - 'seller' => null, - 'latest_version' => null, - ]); + $json = json_decode($app['files']->get($directory.'/../composer.json'), true); + $statamic = $json['extra']['statamic'] ?? []; + $autoload = $json['autoload']['psr-4'][$namespace.'\\']; $app->make(Manifest::class)->manifest = [ - $package['name'] => app(Manifest::class)->formatPackage($package), + $json['name'] => [ + 'id' => $json['name'], + 'slug' => $statamic['slug'] ?? null, + 'version' => 'dev-main', + 'namespace' => $namespace, + 'autoload' => $autoload, + 'provider' => $this->addonServiceProvider, + ], ]; } From 418d110f85b331eb58ef57b9ab1435c72cbca87c Mon Sep 17 00:00:00 2001 From: Duncan McClean Date: Thu, 22 Feb 2024 17:06:48 +0000 Subject: [PATCH 3/5] Automatically boot GraphQLServiceProvider when GraphQL package is installed --- src/Extend/AddonTestCase.php | 24 +++++++----------------- 1 file changed, 7 insertions(+), 17 deletions(-) diff --git a/src/Extend/AddonTestCase.php b/src/Extend/AddonTestCase.php index 63c5f3ecd8..2bd36f3771 100644 --- a/src/Extend/AddonTestCase.php +++ b/src/Extend/AddonTestCase.php @@ -26,11 +26,16 @@ public function setUp(): void protected function getPackageProviders($app) { - // TODO: When the GraphQL package is installed, register the service provider here. - return [ + $serviceProviders = [ StatamicServiceProvider::class, $this->addonServiceProvider, ]; + + if (class_exists('Rebing\GraphQL\GraphQLServiceProvider')) { + array_unshift($serviceProviders, 'Rebing\GraphQL\GraphQLServiceProvider'); + } + + return $serviceProviders; } protected function getPackageAliases($app) @@ -64,21 +69,6 @@ protected function getEnvironmentSetUp($app) 'provider' => $this->addonServiceProvider, ], ]; - } - - protected function resolveApplicationConfiguration($app) - { - parent::resolveApplicationConfiguration($app); - - // TODO: Just grab all the files. - $configs = [ - 'assets', 'cp', 'forms', 'static_caching', - 'sites', 'stache', 'system', 'users', - ]; - - foreach ($configs as $config) { - $app['config']->set("statamic.$config", require (__DIR__."/../../config/{$config}.php")); - } $app['config']->set('statamic.users.repository', 'file'); } From 4a9e08eac02ffc85c1b8eee73eab8500432bb343 Mon Sep 17 00:00:00 2001 From: Duncan McClean Date: Thu, 22 Feb 2024 17:20:59 +0000 Subject: [PATCH 4/5] this can go back to being protected --- src/Extend/Manifest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Extend/Manifest.php b/src/Extend/Manifest.php index 8fa9b192d5..b038b02637 100644 --- a/src/Extend/Manifest.php +++ b/src/Extend/Manifest.php @@ -31,7 +31,7 @@ public function build() $this->getManifest(); } - public function formatPackage($package) + protected function formatPackage($package) { if (! $provider = $package['extra']['laravel']['providers'][0] ?? null) { return; From de0e5034bf6618a334c8365060a8f428f2cf54a1 Mon Sep 17 00:00:00 2001 From: Duncan McClean Date: Fri, 23 Feb 2024 10:25:34 +0000 Subject: [PATCH 5/5] Make `setUp` protected --- src/Extend/AddonTestCase.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Extend/AddonTestCase.php b/src/Extend/AddonTestCase.php index 2bd36f3771..fa0904f01b 100644 --- a/src/Extend/AddonTestCase.php +++ b/src/Extend/AddonTestCase.php @@ -13,7 +13,7 @@ abstract class AddonTestCase extends OrchestraTestCase { protected string $addonServiceProvider; - public function setUp(): void + protected function setUp(): void { parent::setUp(); @@ -22,6 +22,9 @@ public function setUp(): void Version::shouldReceive('get')->andReturn(Composer::create(__DIR__.'/../')->installedVersion(Statamic::PACKAGE)); $this->addToAssertionCount(-1); + + \Statamic\Facades\CP\Nav::shouldReceive('build')->zeroOrMoreTimes()->andReturn(collect()); + $this->addToAssertionCount(-1); // Dont want to assert this } protected function getPackageProviders($app)