Skip to content

Fix microsecond error for 2 weeks by using 3. #228

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Module/Functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public function load(\Transphporm\Config $config) {
$functionSet->addFunction('template', $templateFunction);
$functionSet->addFunction('json', new \Transphporm\TSSFunction\Json($baseDir));
$functionSet->addFunction('constant', new \Transphporm\TSSFunction\Constant());
$functionSet->addFunction('file', new \Transphporm\TSSFunction\File($baseDir));

// Register HTML formatter here because it uses the template function
$config->registerFormatter(new \Transphporm\Formatter\HTMLFormatter($templateFunction));
Expand Down
36 changes: 36 additions & 0 deletions src/TSSFunction/File.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
/**
* @file File.php
* Replace with one line description.
*/
namespace Transphporm\TSSFunction;

class File implements \Transphporm\TSSFunction
{
private $filePath;

public function __construct(\Transphporm\FilePath $filePath)
{
$this->filePath = $filePath;
}

/**
* @param array $args
* @param \DomElement|null $element
*
* @return array
* @throws \Exception
*/
public function run(array $args, \DomElement $element = null)
{
$fileContents = $args[0];

$path = $this->filePath->getFilePath($fileContents);
if (!file_exists($path)) {
throw new \Exception('File does not exist at: ' . $path);
}
$fileContents = file_get_contents($path);

return $fileContents;
}
}
2 changes: 1 addition & 1 deletion tests/DateFormatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public function testWeeksAgo() {
}

public function testWeeksin() {
$this->assertEquals('in 2 weeks', $this->relative('+2 weeks'));
$this->assertEquals('in 3 weeks', $this->relative('+3 weeks'));
}

public function testMonthsAgo() {
Expand Down
21 changes: 21 additions & 0 deletions tests/TransphpormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1436,6 +1436,27 @@ public function testJsonFile() {
unlink($file);
}

public function testFile() {
$data = <<<JS
let j = 0;
if (j < 4) {
console.log('j' + " is less than 4");
}
JS;

$file = __DIR__ . 'data.file';
file_put_contents($file, $data);

$xml = "<script></script>";
$tss = 'script { content: file("' . $file . '"); }';

$template = new \Transphporm\Builder($xml, $tss);

$this->assertEquals($this->stripTabs("<script>$data</script>"), $this->stripTabs($template->output($data)->body));

unlink($file);
}

public function testRoot() {
$xml = "
<div></div>
Expand Down