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 21, 2023
1 parent 0d1f76c commit dd20ce8
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 2 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
21 changes: 19 additions & 2 deletions src/Phug/Renderer/Renderer/Adapter/FileAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -389,18 +389,35 @@ private function isCacheUpToDate(&$path, $input = null)
return file_exists($path);
}

private function getCacheDirectory()
private function readCacheDirectoryFromOptions()
{
$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');
return $this->getOption('tmp_dir');
}

return $cacheFolder;
}

private function getCacheDirectory()
{
$cacheFolder = $this->readCacheDirectoryFromOptions();

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)) {
Expand Down
62 changes: 62 additions & 0 deletions tests/Phug/Adapter/FileAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public function testRender()
* @covers ::getRawCachePath
* @covers ::isCacheUpToDate
* @covers ::checkPathExpiration
* @covers ::readCacheDirectoryFromOptions
* @covers ::getCacheDirectory
* @covers ::getRegistryPath
* @covers \Phug\Renderer\Partial\RegistryTrait::findCachePathInRegistryFile
Expand Down Expand Up @@ -172,12 +173,71 @@ function ($path, $input) {
self::assertCount(2, $files);
}

/**
* @covers ::readCacheDirectoryFromOptions
* @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 ::readCacheDirectoryFromOptions
* @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();

foreach ([$renderer, $adapter] as $handler) {
if ($handler->hasOption('cache_dir')) {
$handler->unsetOption('cache_dir');
}
}

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

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

/**
* @covers ::<public>
* @covers ::getCachePath
* @covers ::getRawCachePath
* @covers ::isCacheUpToDate
* @covers ::checkPathExpiration
* @covers ::readCacheDirectoryFromOptions
* @covers ::getCacheDirectory
* @covers \Phug\Renderer\AbstractAdapter::<public>
* @covers \Phug\Renderer\Partial\AdapterTrait::getAdapter
Expand Down Expand Up @@ -419,6 +479,7 @@ public function testCacheOnExtendsChange()
* @covers ::getRawCachePath
* @covers ::isCacheUpToDate
* @covers ::checkPathExpiration
* @covers ::readCacheDirectoryFromOptions
* @covers ::getCacheDirectory
* @covers \Phug\Renderer\AbstractAdapter::<public>
* @covers \Phug\Renderer\Partial\FileSystemTrait::fileMatchExtensions
Expand Down Expand Up @@ -574,6 +635,7 @@ public function testCacheRenderString()
* @covers \Phug\Renderer\Partial\CacheTrait::getCacheAdapter
* @covers \Phug\Renderer\Partial\CacheTrait::cacheDirectory
* @covers \Phug\Renderer\Adapter\FileAdapter::cacheDirectory
* @covers \Phug\Renderer\Adapter\FileAdapter::readCacheDirectoryFromOptions
* @covers \Phug\Renderer\Adapter\FileAdapter::getCacheDirectory
* @covers \Phug\Renderer\Partial\Debug\DebuggerTrait::getDebuggedException
*
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
12 changes: 12 additions & 0 deletions tests/Phug/Format/FormatExtendTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,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 dd20ce8

Please sign in to comment.