Skip to content

Commit

Permalink
[#33] Adds unit tests to GenerateHelpers
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuapease committed May 7, 2021
1 parent dd16281 commit 4eb3b2f
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 44 deletions.
52 changes: 8 additions & 44 deletions src/console/controllers/GenerateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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'],
Expand All @@ -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'),
Expand All @@ -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'];

Expand All @@ -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,
]);

Expand Down Expand Up @@ -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);
}
}
43 changes: 43 additions & 0 deletions src/helpers/GenerateHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace viget\base\helpers;

use craft\helpers\FileHelper;

class GenerateHelper
{

public 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);
}

public static function parseInput(string $input): array
{
$cleanInput = trim($input, DIRECTORY_SEPARATOR);
$split = explode(DIRECTORY_SEPARATOR, $cleanInput);
$path = implode(DIRECTORY_SEPARATOR, array_slice($split, 0, -1));
$filename = self::removeFileExtension(end($split));

return [
'path' => 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));
}
}
88 changes: 88 additions & 0 deletions tests/unit/helpers/GenerateHelperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

namespace vigetbasetests\unit\helpers;

use Codeception\Test\Unit;
use viget\base\helpers\GenerateHelper;


class GenerateHelperTest extends Unit {

public function testCompileTemplate() {
$template = "{{ %%var1%% }} and {% set %%var2%% = \"foo\" %}";
$compiled = GenerateHelper::compileTemplate(
$template,
[
'var1' => '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'
);
}
}

0 comments on commit 4eb3b2f

Please sign in to comment.