diff --git a/.gitignore b/.gitignore index a11232f..cb300fb 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,7 @@ .sass-cache node_modules bower_components +composer.lock +package-lock.json +typo3 +vendor diff --git a/.travis.yml b/.travis.yml index 4b49b98..932656a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,3 +12,4 @@ install: script: - composer lint - composer cgl + - composer test diff --git a/Classes/Domain/Finishers/MjmlEmailFinisher.php b/Classes/Domain/Finishers/MjmlEmailFinisher.php index 6fd9876..a2321f9 100644 --- a/Classes/Domain/Finishers/MjmlEmailFinisher.php +++ b/Classes/Domain/Finishers/MjmlEmailFinisher.php @@ -1,20 +1,6 @@ getEscapedCommand($cmd, $args), $result, $returnValue); + + GeneralUtility::unlink_tempfile($temporaryMjmlFileWithPath); + + return implode('', $result); + } + + /** + * @param string $cmd + * @param string $args + * @return string + */ + protected function getEscapedCommand(string $cmd, string $args) + { + $escapedCmd = escapeshellcmd($cmd); + + $argsArray = explode(' ', $args); + $escapedArgsArray = CommandUtility::escapeShellArguments($argsArray); + $escapedArgs = implode(' ', $escapedArgsArray); + + return $escapedCmd . ' ' . $escapedArgs; + } +} diff --git a/Classes/Domain/Renderer/RendererInterface.php b/Classes/Domain/Renderer/RendererInterface.php new file mode 100644 index 0000000..5cef1b2 --- /dev/null +++ b/Classes/Domain/Renderer/RendererInterface.php @@ -0,0 +1,16 @@ +renderer = $renderer; + if ($this->renderer === null) { + $this->renderer = $this->objectManager->get(RendererInterface::class); + } + } + public function render($actionName = null) { - return $this->getHtmlFromMjml(parent::render()); - } - - protected function getHtmlFromMjml($mjml) - { - $configuration = unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf']['mjml']); - - $temporaryMjmlFileWithPath = GeneralUtility::tempnam('mjml_', '.mjml'); - - GeneralUtility::writeFileToTypo3tempDir($temporaryMjmlFileWithPath, $mjml); - - // see https://mjml.io/download and https://www.npmjs.com/package/mjml-cli - $cmd = $configuration['nodeBinaryPath'] . ' ' . $configuration['mjmlBinaryPath'] . $configuration['mjmlBinary']; - $args = $configuration['mjmlParams'] . ' ' . $temporaryMjmlFileWithPath; - - $result = []; - $returnValue = ''; - CommandUtility::exec($this->getEscapedCommand($cmd, $args), $result, $returnValue); - - GeneralUtility::unlink_tempfile($temporaryMjmlFileWithPath); - - return implode('', $result); - } - - /** - * @param string $cmd - * @param string $args - * @return string - */ - private function getEscapedCommand(string $cmd, string $args) - { - $escapedCmd = escapeshellcmd($cmd); - - $argsArray = explode(' ', $args); - $escapedArgsArray = CommandUtility::escapeShellArguments($argsArray); - $escapedArgs = implode(' ', $escapedArgsArray); - - return $escapedCmd . ' ' . $escapedArgs; + return $this->renderer->getHtmlFromMjml(parent::render($actionName)); } } diff --git a/Tests/Unit/AbstractUnitTestCase.php b/Tests/Unit/AbstractUnitTestCase.php new file mode 100644 index 0000000..814e944 --- /dev/null +++ b/Tests/Unit/AbstractUnitTestCase.php @@ -0,0 +1,22 @@ +setCacheConfigurations([ + 'extbase_object' => [ + 'backend' => NullBackend::class, + ], + ]); + } +} diff --git a/Tests/Unit/View/MjmlBasedViewTest.php b/Tests/Unit/View/MjmlBasedViewTest.php new file mode 100644 index 0000000..5648444 --- /dev/null +++ b/Tests/Unit/View/MjmlBasedViewTest.php @@ -0,0 +1,55 @@ + + + + + + + Easy and Quick + + + + Responsive + + + + + Discover + + + + + +'; + + /** + * @test + */ + public function viewCallsRendererAndReturnsRenderedHtml() + { + $expectedHtml = '

Simple HTML

'; + $rendererMock = $this->getMockBuilder(RendererInterface::class)->getMock(); + $rendererMock->expects($this->once()) + ->method('getHtmlFromMjml') + ->with(static::EXAMPLE_MJML_TEMPLATE) + ->willReturn($expectedHtml); + + $subject = new MjmlBasedView(null, $rendererMock); + $subject->setTemplateSource(static::EXAMPLE_MJML_TEMPLATE); + $result = $subject->render(); + + $this->assertSame( + $expectedHtml, + $result, + 'Rendering of view did not return expected HTML.' + ); + } +} diff --git a/composer.json b/composer.json index 725fa2b..1300dd2 100644 --- a/composer.json +++ b/composer.json @@ -10,13 +10,26 @@ "Saccas\\Mjml\\": "Classes/" } }, + "autoload-dev": { + "psr-4": { + "Saccas\\Mjml\\Tests\\": "Tests/" + } + }, "require": { "php": ">=7.0.0 <=7.2.99", "typo3/cms-core": "^8.7.0", "typo3/cms-form": "^8.7.0" }, "require-dev": { - "squizlabs/php_codesniffer": "^3.2.0" + "squizlabs/php_codesniffer": "^3.2.0", + "typo3/cms": "^8.7.0", + "typo3/testing-framework": "^1.2.2" + }, + "extra": { + "typo3/cms": { + "cms-package-dir": "{$vendor-dir}/typo3/cms", + "web-dir": "typo3" + } }, "scripts": { "lint": [ @@ -26,6 +39,9 @@ "cgl": [ "./vendor/bin/phpcs" ], + "test": [ + "TYPO3_PATH_ROOT=typo3 ./vendor/bin/phpunit" + ], "post-install-cmd": [ "npm install" ] diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..0455ed1 --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,28 @@ + + + + + ./Tests/Unit/ + + + + + + Classes + + +