diff --git a/CHANGELOG.md b/CHANGELOG.md index 440715c9..63c14600 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,14 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). -## v1.1.1 +## v1.1.2 - 2024-06-20 + +### Fixed + +- Fixed issue with the `mantle-framework/testkit` package depending on classes + that do not exist for that package (introduced in v1.1.0). + +## v1.1.1 - 2024-06-20 ### Added diff --git a/src/mantle/testkit/class-application.php b/src/mantle/testkit/class-application.php index c770869c..9601650e 100644 --- a/src/mantle/testkit/class-application.php +++ b/src/mantle/testkit/class-application.php @@ -11,7 +11,6 @@ use Faker\Generator; use Faker\Factory; -use Mantle\Application\Concerns\Loads_Base_Configuration; use Mantle\Container\Container; use Mantle\Contracts\Application as Application_Contract; use Mantle\Contracts\Container as Container_Contract; @@ -28,8 +27,6 @@ * For use of the Mantle testing framework entirely independent of the Mantle framework. */ class Application extends Container implements Application_Contract { - use Loads_Base_Configuration; - /** * Base path of the application. * @@ -286,8 +283,6 @@ protected function register_base_bindings() { $this->instance( Container::class, $this ); $this->instance( Container_Contract::class, $this ); $this->instance( static::class, $this ); - - $this->load_base_configuration(); } /** diff --git a/src/mantle/testkit/composer.json b/src/mantle/testkit/composer.json index ee53cd35..2c27cf75 100644 --- a/src/mantle/testkit/composer.json +++ b/src/mantle/testkit/composer.json @@ -39,5 +39,11 @@ "config": { "sort-packages": true }, - "minimum-stability": "dev" + "conflict": { + "alleyinteractive/mantle-framework": "*" + }, + "minimum-stability": "dev", + "suggest": { + "alleyinteractive/mantle-framework": "For the full Mantle Framework, require the base package instead." + } } diff --git a/src/mantle/testkit/concerns/trait-create-application.php b/src/mantle/testkit/concerns/trait-create-application.php index 1d51191b..e52f7a1f 100644 --- a/src/mantle/testkit/concerns/trait-create-application.php +++ b/src/mantle/testkit/concerns/trait-create-application.php @@ -7,11 +7,12 @@ namespace Mantle\Testkit\Concerns; -use Mantle\Testkit\Application; +use Mantle\Config\Repository; use Mantle\Contracts\Exceptions\Handler as Handler_Contract; -use Mantle\Framework\Bootloader; use Mantle\Http\Request; use Mantle\Http\Routing\Url_Generator; +use Mantle\Support\Collection; +use Mantle\Testkit\Application; use Mantle\Testkit\Exception_Handler; use Symfony\Component\Routing\RouteCollection; @@ -22,6 +23,13 @@ * Application that doesn't have all the bells and whistles of a full Mantle * Application. Notably, the service providers are never registered or booted * here. + * + * One thing to remember is that this package is largely used in isolation. The + * dependent package will not have the files within the "framework" package and + * cannot be used here. In a future version, we should consider loading the + * framework's base configuration (in the config directory) instead of stubbing + * out our defaults here. We should also consider reusing the bootloader here + * and moving that to a standalone package. */ trait Create_Application { /** @@ -30,34 +38,14 @@ trait Create_Application { * @return Application */ public function create_application(): \Mantle\Contracts\Application { - Bootloader::create( $app = new Application() ) - ->with_config( - [ - 'view' => [ - 'compiled' => sys_get_temp_dir(), - ], - ...$this->override_application_config( $app ), - ] - ); + $app = new Application(); - $this->resolve_application_core( $app ); - $this->resolve_application_bindings( $app ); + $this->resolve_application_bindings( $app ); + $this->resolve_application_config( $app ); return $app; } - /** - * Resolve application core configuration. - * - * @todo Review if we can add Register_Providers bootstrap here. - * - * @param Application $app Application instance. - */ - protected function resolve_application_core( $app ): void { - $app->make( \Mantle\Framework\Bootstrap\Load_Configuration::class )->bootstrap( $app ); - $app->make( \Mantle\Framework\Bootstrap\Boot_Providers::class )->bootstrap( $app ); - } - /** * Override application bindings, to be overridden by the child unit test. * @@ -74,7 +62,6 @@ protected function override_application_bindings( $app ) { * @param Application $app Application instance. */ final protected function resolve_application_bindings( $app ): void { - // Register the TestKit exception handler. $app->singleton( Handler_Contract::class, Exception_Handler::class ); foreach ( $this->override_application_bindings( $app ) as $original => $replacement ) { @@ -91,6 +78,35 @@ final protected function resolve_application_bindings( $app ): void { ); } + /** + * Default configuration for the test. + */ + protected function get_application_config(): array { + return [ + 'app' => [ + 'debug' => true, + 'providers' => [], + ], + 'queue' => [ + 'batch_size' => 100, + 'default' => 'wordpress', + ], + 'logging' => [ + 'default' => 'error_log', + 'channels' => [ + 'error_log' => [ + 'driver' => 'error_log', + ], + ], + ], + 'view' => [ + 'compiled' => sys_get_temp_dir(), + ], + 'filesystem' => [], + 'cache' => [], + ]; + } + /** * Configuration for the test. * @@ -99,4 +115,60 @@ final protected function resolve_application_bindings( $app ): void { protected function override_application_config( $app ): array { return []; } + + /** + * Resolve application core configuration. + * + * @param Application $app Application instance. + * @todo Allow for overriding the configuration aliases and providers easily within the unit test. + */ + protected function resolve_application_config( $app ) { + $config = new Repository( + array_merge( + $this->get_application_config(), + $this->override_application_config( $app ) + ) + ); + + $app->instance( 'config', $config ); + $app['config']['app.providers'] = $this->resolve_application_providers( $app ); + } + + /** + * Get application providers. + * + * @param Application $app Application instance. + * @return array + */ + protected function get_application_providers( $app ) { + return $app['config']['app.providers']; + } + + /** + * Override application aliases. + * + * @param Application $app Application instance. + * @return array + */ + protected function override_application_providers( $app ) { + return []; + } + + /** + * Resolve application aliases. + * + * @param Application $app Application instance. + */ + final protected function resolve_application_providers( $app ): array { + $providers = new Collection( $this->get_application_providers( $app ) ); + $overrides = $this->override_application_providers( $app ); + + if ( ! empty( $overrides ) ) { + $providers->transform( + static fn ( $provider) => $overrides[ $provider ] ?? $provider + ); + } + + return $providers->all(); + } }