diff --git a/phpcs.xml b/phpcs.xml index eb75d2c..3812f13 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -9,7 +9,6 @@ \/FileInfoTest\/.*?$ \/TemplateFileTest\/.*?$ \/TemplateEngineTest\/.*?$ - \/TemplatingServiceTest\/.*?$ diff --git a/phpstan.neon b/phpstan.neon index 86125d4..15a2035 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -7,7 +7,6 @@ parameters: - message: '#^Variable \$\w+ might not be defined.$#' paths: - - tests/src/Service/TemplatingServiceTest/* - tests/src/TemplateEngineTest/* - message: '#^Call to an undefined method DanBettles\\Marigold\\OutputHelper\\Html5OutputHelper::create[A-Z]#' diff --git a/src/Service/TemplatingService.php b/src/Service/TemplatingService.php deleted file mode 100644 index c94053a..0000000 --- a/src/Service/TemplatingService.php +++ /dev/null @@ -1,121 +0,0 @@ -setConfig($config) - ->setServiceFactory($serviceFactory) - ; - } - - private function createTemplateFilePathname(string $pathnameOrBasename): string - { - $templatesDir = $this->getConfig()['templates_dir'] - ?? null - ; - - return null === $templatesDir - ? $pathnameOrBasename - : $templatesDir . DIRECTORY_SEPARATOR . $pathnameOrBasename - ; - } - - /** - * @throws RangeException If the helper is not an output helper. - */ - private function createOutputHelperFromTemplateFile(TemplateFile $templateFile): ?OutputHelperInterface - { - $outputFormat = $templateFile->getOutputFormat() - ?: 'html' - ; - - $helperServiceId = "output_helpers.{$outputFormat}"; - - if (!$this->getServiceFactory()->contains($helperServiceId)) { - return null; - } - - $helper = $this->getServiceFactory()->get($helperServiceId); - - if (!$helper instanceof OutputHelperInterface) { - throw new RangeException(sprintf( - 'The helper for `%s` output, `%s`, does not implement `%s`.', - $outputFormat, - get_class($helper), - OutputHelperInterface::class - )); - } - - return $helper; - } - - public function render( - string $pathnameOrBasename, - array $variables = [] - ): string { - $templatePathname = $this->createTemplateFilePathname($pathnameOrBasename); - $templateFile = new TemplateFile($templatePathname); - - $augmentedVars = [ - 'variables' => $variables, - 'service' => $this, - ]; - - $outputHelper = $this->createOutputHelperFromTemplateFile($templateFile); - - if ($outputHelper) { - $augmentedVars['helper'] = $outputHelper; - } - - return (new TemplateEngine())->render( - $templateFile, - $augmentedVars - ); - } - - private function setConfig(array $config): self - { - $this->config = $config; - return $this; - } - - public function getConfig(): array - { - return $this->config; - } - - private function setServiceFactory(ServiceFactory $serviceFactory): self - { - $this->serviceFactory = $serviceFactory; - return $this; - } - - public function getServiceFactory(): ServiceFactory - { - return $this->serviceFactory; - } -} diff --git a/tests/src/Service/TemplatingServiceTest.php b/tests/src/Service/TemplatingServiceTest.php deleted file mode 100644 index 7d0f14e..0000000 --- a/tests/src/Service/TemplatingServiceTest.php +++ /dev/null @@ -1,156 +0,0 @@ -assertEquals($config, $templatingService->getConfig()); - $this->assertSame($serviceFactory, $templatingService->getServiceFactory()); - } - - public function testRenderPassesAllTemplateVarsToTheTemplateFileInAnArray() - { - $output = $this - ->createEmptyTemplatingService() - ->render($this->createFixturePathname('contains_var_variables.php'), [ - 'foo' => 'bar', - 'baz' => 'qux', - ]) - ; - - $this->assertSame(<<<'END' - Array - ( - [foo] => bar - [baz] => qux - ) - - END, $output); - } - - public function testRenderDoesNotRequireVars() - { - $output = $this - ->createEmptyTemplatingService() - ->render($this->createFixturePathname('hello_world.php')) - ; - - $this->assertSame('Hello, World!', $output); - } - - public function testRenderPassesTheTemplatingServiceToTheTemplateFile() - { - $templatingService = $this->createEmptyTemplatingService(); - $output = $templatingService->render($this->createFixturePathname('contains_var_service.php')); - - $this->assertSame((string) spl_object_id($templatingService), $output); - } - - public function providesInjectedOutputHelperClassNames(): array - { - return [ - [ - OutputHelper::class, - [ - 'output_helpers.html' => OutputHelper::class, - ], - ], - [ - OutputHelper::class, - [ - 'output_helpers.html' => function () { - return new OutputHelper(); - }, - ], - ], - ]; - } - - /** - * @dataProvider providesInjectedOutputHelperClassNames - */ - public function testRenderInjectsAnAppropriateOutputHelperIfTheNameOfTheTemplateFileFollowsTheConvention( - string $expectedClassName, - array $serviceFactoryConfig - ) { - $serviceFactory = new ServiceFactory($serviceFactoryConfig); - - $output = (new TemplatingService([], $serviceFactory)) - ->render($this->createFixturePathname('contains_var_helper.html.php')) - ; - - $this->assertSame($expectedClassName, $output); - } - - public function testRenderThrowsAnExceptionIfTheOutputHelperClassIsNotAnOutputHelper() - { - $notAnOutputHelperClassName = NotAnOutputHelper::class; - $outputHelperBaseName = OutputHelperInterface::class; - - $this->expectException(RangeException::class); - $this->expectExceptionMessage("The helper for `html` output, `{$notAnOutputHelperClassName}`, does not implement `{$outputHelperBaseName}`."); - - $serviceFactoryConfig = [ - 'output_helpers.html' => $notAnOutputHelperClassName, - ]; - - (new TemplatingService([], new ServiceFactory($serviceFactoryConfig))) - ->render($this->createFixturePathname('empty_file.php')) - ; - } - - public function testRenderThrowsAnExceptionIfTheOutputHelperClosureDoesNotReturnAnOutputHelper() - { - $outputHelperBaseName = OutputHelperInterface::class; - - $this->expectException(RangeException::class); - $this->expectExceptionMessage("The helper for `html` output, `stdClass`, does not implement `{$outputHelperBaseName}`."); - - $serviceFactoryConfig = [ - 'output_helpers.html' => function () { - return new stdClass(); - }, - ]; - - (new TemplatingService([], new ServiceFactory($serviceFactoryConfig))) - ->render($this->createFixturePathname('empty_file.php')) - ; - } - - public function testUsesTheTemplatesDirConfigIfSet() - { - $templatingServiceConfig = [ - 'templates_dir' => $this->getFixturesDir(), - ]; - - $output = (new TemplatingService($templatingServiceConfig, new ServiceFactory([]))) - ->render('hello_world.php') - ; - - $this->assertSame('Hello, World!', $output); - } - - private function createEmptyTemplatingService(): TemplatingService - { - return new TemplatingService([], new ServiceFactory([])); - } -} diff --git a/tests/src/Service/TemplatingServiceTest/NotAnOutputHelper.php b/tests/src/Service/TemplatingServiceTest/NotAnOutputHelper.php deleted file mode 100644 index eebad39..0000000 --- a/tests/src/Service/TemplatingServiceTest/NotAnOutputHelper.php +++ /dev/null @@ -1,9 +0,0 @@ - \ No newline at end of file diff --git a/tests/src/Service/TemplatingServiceTest/contains_var_service.php b/tests/src/Service/TemplatingServiceTest/contains_var_service.php deleted file mode 100644 index 5be8b10..0000000 --- a/tests/src/Service/TemplatingServiceTest/contains_var_service.php +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/tests/src/Service/TemplatingServiceTest/contains_var_variables.php b/tests/src/Service/TemplatingServiceTest/contains_var_variables.php deleted file mode 100644 index 6d1e80a..0000000 --- a/tests/src/Service/TemplatingServiceTest/contains_var_variables.php +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/tests/src/Service/TemplatingServiceTest/empty_file.php b/tests/src/Service/TemplatingServiceTest/empty_file.php deleted file mode 100644 index e69de29..0000000 diff --git a/tests/src/Service/TemplatingServiceTest/hello_world.php b/tests/src/Service/TemplatingServiceTest/hello_world.php deleted file mode 100644 index b83816f..0000000 --- a/tests/src/Service/TemplatingServiceTest/hello_world.php +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file