Skip to content

Commit

Permalink
Add exception for null cache_dir option
Browse files Browse the repository at this point in the history
  • Loading branch information
kylekatarnls committed Sep 20, 2023
1 parent 0d1f76c commit b959b4c
Show file tree
Hide file tree
Showing 7 changed files with 177 additions and 35 deletions.
6 changes: 6 additions & 0 deletions src/Phug/Formatter/Formatter/Format/XmlFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,19 +167,23 @@ protected function formatAttributeElement(AttributeElement $element)
$value = $element->getValue();
$name = $element->getName();
$nonEmptyAttribute = ($name === 'class' || $name === 'id');

if ($nonEmptyAttribute && (
!$value ||
($value instanceof TextElement && ((string) $value->getValue()) === '') ||
(is_string($value) && in_array(trim($value), ['', '""', "''"], true))
)) {
return '';
}

if ($value instanceof ExpressionElement) {
if ($nonEmptyAttribute && in_array(trim($value->getValue()), ['', '""', "''"], true)) {
return '';
}

if (strtolower($value->getValue()) === 'true') {
$formattedValue = null;

if ($name instanceof ExpressionElement) {
$bufferVariable = $this->pattern('buffer_variable');
$name = $this->pattern(
Expand All @@ -193,6 +197,7 @@ protected function formatAttributeElement(AttributeElement $element)
$value = new ExpressionElement($bufferVariable);
$formattedValue = $this->format($value);
}

$formattedName = $this->format($name);
$formattedValue = $formattedValue || $formattedValue === '0'
? $formattedValue
Expand All @@ -204,6 +209,7 @@ protected function formatAttributeElement(AttributeElement $element)
$formattedValue
);
}

if (in_array(strtolower($value->getValue()), ['false', 'null', 'undefined'], true)) {
return '';
}
Expand Down
10 changes: 10 additions & 0 deletions src/Phug/Renderer/Renderer/Adapter/FileAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -394,15 +394,25 @@ private function getCacheDirectory()
$cacheFolder = $this->hasOption('cache_dir')
? $this->getOption('cache_dir')
: null;

if (!$cacheFolder && $cacheFolder !== false) {
$cacheFolder = $this->getRenderer()->hasOption('cache_dir')
? $this->getRenderer()->getOption('cache_dir')
: null;
}

if ($cacheFolder === true) {
$cacheFolder = $this->getOption('tmp_dir');
}

if ($cacheFolder === null) {
throw new RuntimeException(
'You need to set "cache_dir" option to a writable location in order '.
'to use cache feature.',
7
);
}

if (!is_dir($cacheFolder) && !@mkdir($cacheFolder, 0777, true)) {
throw new RuntimeException(
$cacheFolder.': Cache directory doesn\'t exist.'."\n".
Expand Down
53 changes: 53 additions & 0 deletions tests/Phug/Adapter/FileAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,59 @@ function ($path, $input) {
self::assertCount(2, $files);
}

/**
* @covers ::getCacheDirectory
*
* @expectedException \RuntimeException
*
* @expectedExceptionMessage You need to set "cache_dir" option to a writable location in order to use cache feature.
*/
public function testNullCacheDir()
{
ExceptionAnnotationReader::read($this, __METHOD__);

$renderer = new Renderer([
'cache_dir' => null,
'adapter_class_name' => FileAdapter::class,
]);

/** @var FileAdapter $adapter */
$adapter = $renderer->getAdapter();

self::assertInstanceOf(FileAdapter::class, $adapter);

$adapter->cache('foo', 'bar', function () {
return 'abc';
});
}

/**
* @covers ::getCacheDirectory
*
* @expectedException \RuntimeException
*
* @expectedExceptionMessage You need to set "cache_dir" option to a writable location in order to use cache feature.
*/
public function testUnsetCacheDir()
{
ExceptionAnnotationReader::read($this, __METHOD__);

$renderer = new Renderer([
'adapter_class_name' => FileAdapter::class,
]);
/** @var FileAdapter $adapter */
$adapter = $renderer->getAdapter();

$renderer->unsetOption('cache_dir');
$adapter->unsetOption('cache_dir');

self::assertInstanceOf(FileAdapter::class, $adapter);

$adapter->cache('foo', 'bar', function () {
return 'abc';
});
}

/**
* @covers ::<public>
* @covers ::getCachePath
Expand Down
85 changes: 50 additions & 35 deletions tests/Phug/Element/AttributeElementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,47 +61,62 @@ public function testStaticAttributeElement()
'default_format' => XmlFormat::class,
]);

$attribute = new AttributeElement('foo', new ExpressionElement('null'));

ob_start();
$php = $formatter->format($attribute);
eval('?>'.$formatter->formatDependencies().$php);
$actual = ob_get_contents();
ob_end_clean();

self::assertSame('', $actual);

$attribute = new AttributeElement('class', '""');

ob_start();
$php = $formatter->format($attribute);
eval('?>'.$formatter->formatDependencies().$php);
$actual = ob_get_contents();
ob_end_clean();

self::assertSame('', $actual);

$attribute = new AttributeElement('class', new ExpressionElement("''"));

ob_start();
$php = $formatter->format($attribute);
eval('?>'.$formatter->formatDependencies().$php);
$actual = ob_get_contents();
ob_end_clean();

self::assertSame('', $actual);

$attribute = new AttributeElement('width', '12');
$iframe = new MarkupElement('iframe');
$iframe->getAttributes()->attach($attribute);
// $attribute = new AttributeElement('foo', new ExpressionElement('null'));
//
// ob_start();
// $php = $formatter->format($attribute);
/* eval('?>'.$formatter->formatDependencies().$php);*/
// $actual = ob_get_contents();
// ob_end_clean();
//
// self::assertSame('', $actual);
//
// $attribute = new AttributeElement('class', '""');
//
// ob_start();
// $php = $formatter->format($attribute);
/* eval('?>'.$formatter->formatDependencies().$php);*/
// $actual = ob_get_contents();
// ob_end_clean();
//
// self::assertSame('', $actual);
//
// $attribute = new AttributeElement('class', new ExpressionElement("''"));
//
// ob_start();
// $php = $formatter->format($attribute);
/* eval('?>'.$formatter->formatDependencies().$php);*/
// $actual = ob_get_contents();
// ob_end_clean();
//
// self::assertSame('', $actual);
//
// $attribute = new AttributeElement('width', '12');
// $iframe = new MarkupElement('iframe');
// $iframe->getAttributes()->attach($attribute);
//
// ob_start();
// $php = $formatter->format($iframe);
/* eval('?>'.$formatter->formatDependencies().$php);*/
// $actual = ob_get_contents();
// ob_end_clean();
//
// self::assertSame('<iframe width="12"></iframe>', $actual);

$attribute = new AttributeElement(
new ExpressionElement('$mandatoryName'),
new ExpressionElement('true')
);
$input = new MarkupElement('input', true);
$input->getAttributes()->attach($attribute);

ob_start();
$php = $formatter->format($iframe);
$php = $formatter->format($input);
eval('?>'.$formatter->formatDependencies().$php);
$actual = ob_get_contents();
ob_end_clean();

self::assertSame('<iframe width="12"></iframe>', $actual);
self::assertSame('<input required="" />', $actual);
}

/**
Expand Down
12 changes: 12 additions & 0 deletions tests/Phug/Element/ExpressionElementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Phug\Test\Element;

use Phug\Ast\Node;
use Phug\Formatter;
use Phug\Formatter\Element\AttributeElement;
use Phug\Formatter\Element\ExpressionElement;
Expand Down Expand Up @@ -105,4 +106,15 @@ public function testTrueDynamicValue()
$actual
);
}

/**
* @covers \Phug\Formatter\AbstractElement::dump
*/
public function testDump()
{
$expression = new ExpressionElement('$foo');
$expression->appendChild(new Node());

self::assertSame("Expression\n ".Node::class, $expression->dump());
}
}
33 changes: 33 additions & 0 deletions tests/Phug/Element/MixinElementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Phug\Formatter\Element\AttributeElement;
use Phug\Formatter\Element\CodeElement;
use Phug\Formatter\Element\DocumentElement;
use Phug\Formatter\Element\ExpressionElement;
use Phug\Formatter\Element\MarkupElement;
use Phug\Formatter\Element\MixinElement;
use Phug\Formatter\Element\TextElement;
Expand Down Expand Up @@ -127,6 +128,38 @@ public function testRequireAllMixins()
self::assertSame('<div>block</div>', $html);
}
/**
* @covers \Phug\Formatter\AbstractFormat::formatMixinElement
*/
public function testFormatMixinWithDynamicName()
{
$mixin = new MixinElement();
$name = new ExpressionElement();
$name->setValue('$var');
$mixin->setName($name);
$div = new MarkupElement('p');
$div->appendChild(new AnonymousBlockElement());
$mixin->appendChild($div);
$formatter = new Formatter();
$php = $formatter->format($mixin);
$formatter->requireAllMixins();
$php = $formatter->formatDependencies().$php;
$call = '<?php $__pug_mixins["foo"]('.
'true, [], [], [], '.
'function () { echo "foo"; }'.
'); ?>';

ob_start();
$var = 'foo';
eval('?>'.$php.$call);
$html = ob_get_contents();
ob_end_clean();

self::assertSame('<p>foo</p>', $html);
}

/**
* @covers \Phug\Formatter::getMixins
* @covers \Phug\Formatter::requireMixin
Expand Down
13 changes: 13 additions & 0 deletions tests/Phug/Format/FormatExtendTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Phug\Formatter;
use Phug\Formatter\Element\AttributeElement;
use Phug\Formatter\Element\ExpressionElement;
use Phug\Formatter\Element\MarkupElement;
use Phug\Util\TestCase;

/**
Expand All @@ -23,6 +24,18 @@ public function testExtendedFormat()
$formatter = new Formatter();
$format = new FakeXmlFormat($formatter);

$attribute = new AttributeElement(
'required',
new ExpressionElement('true')
);

ob_start();
eval('?>'.$format->callFormatAttributeElement($attribute));
$actual = ob_get_contents();
ob_end_clean();
self::assertSame(' required="required"', $actual);
ob_start();
eval('?>'.$format->callFormatAttributeElement(
new AttributeElement(new ExpressionElement('"foo" . "bar"'), 'abc')
Expand Down

0 comments on commit b959b4c

Please sign in to comment.