Skip to content

Commit

Permalink
Revert changes in 1.1 to the testkit package (#562)
Browse files Browse the repository at this point in the history
* Revert the bootloader changes to testkit to allow for proper isolation

* CHANGELOG

* Sort classes
  • Loading branch information
srtfisher authored Jun 20, 2024
1 parent 0aa37b7 commit 408a5ca
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 33 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 0 additions & 5 deletions src/mantle/testkit/class-application.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
*
Expand Down Expand Up @@ -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();
}

/**
Expand Down
8 changes: 7 additions & 1 deletion src/mantle/testkit/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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."
}
}
124 changes: 98 additions & 26 deletions src/mantle/testkit/concerns/trait-create-application.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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 {
/**
Expand All @@ -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.
*
Expand All @@ -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 ) {
Expand All @@ -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.
*
Expand All @@ -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();
}
}

0 comments on commit 408a5ca

Please sign in to comment.