From 4eb3b2ff736b2a3e1af404fb180ff1642a3784b8 Mon Sep 17 00:00:00 2001 From: Joshua Pease Date: Fri, 7 May 2021 10:47:30 -0700 Subject: [PATCH] [#33] Adds unit tests to GenerateHelpers --- .../controllers/GenerateController.php | 52 ++--------- src/helpers/GenerateHelper.php | 43 +++++++++ tests/unit/helpers/GenerateHelperTest.php | 88 +++++++++++++++++++ 3 files changed, 139 insertions(+), 44 deletions(-) create mode 100644 src/helpers/GenerateHelper.php create mode 100644 tests/unit/helpers/GenerateHelperTest.php diff --git a/src/console/controllers/GenerateController.php b/src/console/controllers/GenerateController.php index 6eaec8c..1bc82c3 100644 --- a/src/console/controllers/GenerateController.php +++ b/src/console/controllers/GenerateController.php @@ -5,8 +5,8 @@ use Craft; use craft\console\Controller; use craft\helpers\FileHelper; -use craft\services\Sections; +use viget\base\helpers\GenerateHelper; use viget\base\Module; use yii\console\ExitCode; use yii\helpers\Console; @@ -59,17 +59,17 @@ public function actionEntrytypes(string $handle) return ExitCode::UNSPECIFIED_ERROR; } - $templateParts = self::_splitAndSanitizeInput($settings->template); + $templateParts = GenerateHelper::parseInput($settings->template); $path = $templateParts['path']; - $templateContent = self::_compileTemplate( + $templateContent = GenerateHelper::compileTemplate( $this->_loadScaffoldTemplate('template.html'), [ 'layout' => self::getConfig('layout'), ] ); - $structureIndex = self::_compileTemplate( + $structureIndex = GenerateHelper::compileTemplate( $this->_loadScaffoldTemplate('structure.html'), [ 'path' => $templateParts['path'], @@ -88,11 +88,11 @@ public function actionEntrytypes(string $handle) public function actionTemplate($templatePath) { - $parts = self::_splitAndSanitizeInput($templatePath); + $parts = GenerateHelper::parseInput($templatePath); $path = $parts['path']; $filename = $parts['filename']; - $templateContent = self::_compileTemplate( + $templateContent = GenerateHelper::compileTemplate( $this->_loadScaffoldTemplate('template.html'), [ 'layout' => self::getConfig('layout'), @@ -108,7 +108,7 @@ public function actionTemplate($templatePath) public function actionPartial($partialPath, ?string $variantsInput = null) { - $parts = self::_splitAndSanitizeInput($partialPath); + $parts = GenerateHelper::parseInput($partialPath); $path = $parts["path"]; $filename = $parts['filename']; @@ -123,7 +123,7 @@ public function actionPartial($partialPath, ?string $variantsInput = null) $partialContent = $this->_loadScaffoldTemplate('partial.html'); $partsKitContent = $this->_loadScaffoldTemplate('partial-parts-kit.html'); - $partsKitContentCompiled = self::_compileTemplate($partsKitContent, [ + $partsKitContentCompiled = GenerateHelper::compileTemplate($partsKitContent, [ 'partialPath' => $partialPath, ]); @@ -154,40 +154,4 @@ private function _writeFile(string $path, string $content) FileHelper::writeToFile($fullPath, $content); } - - // TODO - unit test - private static function _removeFileExtension(string $filename): string - { - $explode = explode('.', $filename); - if(count($explode) === 1) { - return $filename; - } - - return implode('.', array_slice($explode, 0, -1)); - } - - // TODO - unit test - private static function _splitAndSanitizeInput(string $input): array - { - $split = explode(DIRECTORY_SEPARATOR, $input); - $path = implode(DIRECTORY_SEPARATOR, array_slice($split, 0, -1)); - $filename = self::_removeFileExtension(end($split)); - - return [ - 'path' => FileHelper::normalizePath($path), - 'filename' => FileHelper::sanitizeFilename($filename), - ]; - } - - // TODO - unit test - private static function _compileTemplate(string $template, array $vars): string - { - $patterns = array_map(function ($item) { - return "/%%$item%%/"; - }, array_values(array_flip($vars))); - - $replacements = array_values($vars); - - return preg_replace($patterns, $replacements, $template); - } } diff --git a/src/helpers/GenerateHelper.php b/src/helpers/GenerateHelper.php new file mode 100644 index 0000000..da44399 --- /dev/null +++ b/src/helpers/GenerateHelper.php @@ -0,0 +1,43 @@ + FileHelper::normalizePath($path), + 'filename' => FileHelper::sanitizeFilename($filename), + ]; + } + + public static function removeFileExtension(string $filename): string + { + $explode = explode('.', $filename); + if (count($explode) === 1) { + return $filename; + } + + return implode('.', array_slice($explode, 0, -1)); + } +} diff --git a/tests/unit/helpers/GenerateHelperTest.php b/tests/unit/helpers/GenerateHelperTest.php new file mode 100644 index 0000000..3791c53 --- /dev/null +++ b/tests/unit/helpers/GenerateHelperTest.php @@ -0,0 +1,88 @@ + 'foo', + 'var2' => 'bar', + ] + ); + + $this->assertEquals( + "{{ foo }} and {% set bar = \"foo\" %}", + $compiled + ); + } + + public function testParseInputOfFileNames() { + $this->assertEquals( + GenerateHelper::parseInput('file'), + [ + 'path' => '', + 'filename' => 'file', + ] + ); + + // + $this->assertEquals( + GenerateHelper::parseInput('file.html'), + [ + 'path' => '', + 'filename' => 'file', + ] + ); + } + + public function testParseInputOfPath() { + $this->assertEquals( + GenerateHelper::parseInput('/my/directory/file/'), + [ + 'path' => 'my/directory', + 'filename' => 'file', + ] + ); + + $this->assertEquals( + GenerateHelper::parseInput('/my/directory/file.html'), + [ + 'path' => 'my/directory', + 'filename' => 'file', + ] + ); + + $this->assertEquals( + GenerateHelper::parseInput('my\directory//file.html'), + [ + 'path' => 'my/directory', + 'filename' => 'file', + ] + ); + } + + public function testRemoveFileExtension() { + $this->assertEquals( + GenerateHelper::removeFileExtension('file.html'), + 'file' + ); + + $this->assertEquals( + GenerateHelper::removeFileExtension('file'), + 'file' + ); + + $this->assertEquals( + GenerateHelper::removeFileExtension('this.is.file.html'), + 'this.is.file' + ); + } +}