From 28190489a10b9c5d32f9f3be26bbc1fd9aa28dc7 Mon Sep 17 00:00:00 2001 From: rotimi Date: Mon, 11 Dec 2023 19:14:12 -0700 Subject: [PATCH] Testing & refactoring in progress --- src/controllers/BaseController.php | 20 ++++++++++++++++---- tests/BaseControllerDependenciesTrait.php | 16 +++++++++------- tests/BaseControllerTest.php | 2 +- 3 files changed, 26 insertions(+), 12 deletions(-) diff --git a/src/controllers/BaseController.php b/src/controllers/BaseController.php index bceb7e5..c8112e0 100644 --- a/src/controllers/BaseController.php +++ b/src/controllers/BaseController.php @@ -176,6 +176,12 @@ public function getCurrentUriComputed(): string { // Keys for container items needed in this controller //////////////////////////////////////////////////////// public const LAYOUT_RENDERER_CONTAINER_KEY = 'new_layout_renderer'; + public const VIEW_RENDERER_CONTAINER_KEY = 'new_view_renderer'; + + // 'logger' + // 'vespula_auth' + // 'settings' + /** @@ -206,8 +212,11 @@ public function __construct( */ $this->layout_renderer = $this->getContainerItem(static::LAYOUT_RENDERER_CONTAINER_KEY); - /** @psalm-suppress MixedAssignment */ - $this->view_renderer = $this->getContainerItem('new_view_renderer'); + /** + * @psalm-suppress MixedArgument + * @psalm-suppress MixedAssignment + */ + $this->view_renderer = $this->getContainerItem(static::VIEW_RENDERER_CONTAINER_KEY); $uri_path = ($req->getUri() instanceof \Psr\Http\Message\UriInterface) ? $req->getUri()->getPath() : ''; @@ -489,8 +498,11 @@ public function renderView( string $file_name, array $data = [] ): string { $parent_classes = []; $parent_class = get_parent_class($this); - /** @psalm-suppress MixedAssignment */ - $this->view_renderer = $this->getContainerItem('new_view_renderer'); // get new instance for each call to this method renderView + /** + * @psalm-suppress MixedArgument + * @psalm-suppress MixedAssignment + */ + $this->view_renderer = $this->getContainerItem(static::VIEW_RENDERER_CONTAINER_KEY); // get new instance for each call to this method renderView while( $parent_class !== self::class && ($parent_class !== '' && $parent_class !== false) ) { diff --git a/tests/BaseControllerDependenciesTrait.php b/tests/BaseControllerDependenciesTrait.php index 30ca2c1..93bd700 100644 --- a/tests/BaseControllerDependenciesTrait.php +++ b/tests/BaseControllerDependenciesTrait.php @@ -136,15 +136,17 @@ protected function getContainer(array $override_settings=[]): \Psr\Container\Con }); //Object for rendering view files - $psr11Container['new_view_renderer'] = $psr11Container->factory(function () { + $psr11Container[\SlimMvcTools\Controllers\BaseController::VIEW_RENDERER_CONTAINER_KEY] = + $psr11Container->factory(function () { - //return a new instance on each access to $psr11Container['new_view_renderer'] - $ds = DIRECTORY_SEPARATOR; - $path_2_view_files = __DIR__ . DIRECTORY_SEPARATOR . 'fake-smvc-app-root' ."{$ds}src{$ds}views{$ds}base"; - $view_renderer = new \Rotexsoft\FileRenderer\Renderer('', [], [$path_2_view_files]); + // Return a new instance on each access to + // $psr11Container[\SlimMvcTools\Controllers\BaseController::VIEW_RENDERER_CONTAINER_KEY] + $ds = DIRECTORY_SEPARATOR; + $path_2_view_files = __DIR__ . DIRECTORY_SEPARATOR . 'fake-smvc-app-root' ."{$ds}src{$ds}views{$ds}base"; + $view_renderer = new \Rotexsoft\FileRenderer\Renderer('', [], [$path_2_view_files]); - return $view_renderer; - }); + return $view_renderer; + }); } return $psr11Container; diff --git a/tests/BaseControllerTest.php b/tests/BaseControllerTest.php index a95cb24..513fc0a 100644 --- a/tests/BaseControllerTest.php +++ b/tests/BaseControllerTest.php @@ -450,7 +450,7 @@ public function testThat_GetSetViewRenderer_WorksAsExpected() { $psr11Container, '', '', $req, $resp ); - $new_renderer = $this->getContainer()['new_view_renderer']; + $new_renderer = $this->getContainer()[BaseController::VIEW_RENDERER_CONTAINER_KEY]; $controller->setViewRenderer($new_renderer); self::assertSame($new_renderer, $controller->getViewRenderer());