-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#33] Adds unit tests to GenerateHelpers
- Loading branch information
1 parent
dd16281
commit 4eb3b2f
Showing
3 changed files
with
139 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
); | ||
} | ||
} |