Skip to content
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

T15556 volt compiler block #15557

Merged
merged 6 commits into from
Jun 28, 2021
Merged
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 CHANGELOG-5.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
## Fixed
- Fixed `Phalcon\Db\Adapter\AbstractAdapter:delete()` when `bindTypes` argument is passed. [#15363](https://github.com/phalcon/cphalcon/issues/15363)
- Fixed `Phalcon\Storage\Adapter\Redis::getAdapter` to use passed `connectionTimeout`, `retryInterval` and `readTimeout` options for the connection [#15484](https://github.com/phalcon/cphalcon/issues/15484)
- Fixed `Phalcon\Mvc\View\Engine\Volt\Compiler` for a use case when a block will return null vs an array for `statementList` in PHP 8 [#15556](https://github.com/phalcon/cphalcon/issues/15556)

# [5.0.0-alpha.2](https://github.com/phalcon/cphalcon/releases/tag/v5.0.0-alpha.2) (2021-05-05)

Expand Down
15 changes: 11 additions & 4 deletions phalcon/Mvc/View/Engine/Volt/Compiler.zep
Original file line number Diff line number Diff line change
Expand Up @@ -2249,7 +2249,6 @@ class Compiler implements InjectionAwareInterface
return this;
}


/**
* Compiles a Volt source code returning a PHP plain version
*/
Expand Down Expand Up @@ -2317,9 +2316,17 @@ class Compiler implements InjectionAwareInterface
/**
* The block is set in the local template
*/
let localBlock = blocks[name],
this->currentBlock = name,
blockCompilation = this->statementList(localBlock);
let localBlock = blocks[name],
this->currentBlock = name;

/**
* This is to ensure in PHP 8.0 we pass an array to statementList
*/
if null === localBlock {
let localBlock = [];
}

let blockCompilation = this->statementList(localBlock);
} else {
if typeof block == "array" {
/**
Expand Down
15 changes: 15 additions & 0 deletions tests/_data/fixtures/views/blocks/base.volt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
</head>
<body>
{% block header %}
{{ partial('blocks/partials/header') }}
{% endblock %}

{% block mainContent %}{% endblock %}

</body>
</html>
7 changes: 7 additions & 0 deletions tests/_data/fixtures/views/blocks/index/login.volt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{% extends 'base.volt' %}

{% block header %}{% endblock %}
{% block mainContent %}
<p>This is the login page</p>
{% endblock %}
{% block footer %}{% endblock %}
5 changes: 5 additions & 0 deletions tests/_data/fixtures/views/blocks/index/main.volt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{% extends 'base.volt' %}

{% block mainContent %}
<p>This is the main page</p>
{% endblock %}
1 change: 1 addition & 0 deletions tests/_data/fixtures/views/blocks/partials/header.volt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>This is the header</p>
176 changes: 122 additions & 54 deletions tests/integration/Mvc/View/Engine/Volt/CompilerFilesCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
use IntegrationTester;
use Phalcon\Mvc\View;
use Phalcon\Mvc\View\Engine\Volt\Compiler;
use function dataDir;
use function sprintf;
use const PHP_EOL;

/**
* Phalcon\Test\Integration\Mvc\View\Engine\Volt\CompilerFilesCest
Expand All @@ -28,31 +31,60 @@
*/
class CompilerFilesCest
{
/**
* @param IntegrationTester $I
*/
public function _after(IntegrationTester $I)
{
$compiledFiles = [
dataDir('fixtures/views/blocks/base.volt.php'),
dataDir('fixtures/views/blocks/base.volt%%e%%.php'),
dataDir('fixtures/views/blocks/index/login.volt.php'),
dataDir('fixtures/views/blocks/index/main.volt.php'),
dataDir('fixtures/views/blocks/partials/header.volt.php'),
dataDir('fixtures/views/extends/children.extends.volt.php'),
dataDir('fixtures/views/extends/import.volt.php'),
dataDir('fixtures/views/extends/import2.volt.php'),
dataDir('fixtures/views/layouts/extends.volt.php'),
dataDir('fixtures/views/partials/header.volt.php'),
dataDir('fixtures/views/partials/header2.volt.php'),
dataDir('fixtures/views/partials/header3.volt.php'),
dataDir('fixtures/views/partials/footer.volt.php'),
];

foreach ($compiledFiles as $fileName) {
$I->safeDeleteFile($fileName);
}
}

/**
* @param IntegrationTester $I
*/
public function _before(IntegrationTester $I)
{
$compiledFiles = [
dataDir('fixtures/views/blocks/base.volt.php'),
dataDir('fixtures/views/blocks/index/login.volt.php'),
dataDir('fixtures/views/blocks/index/main.volt.php'),
dataDir('fixtures/views/blocks/partials/header.volt.php'),
];
foreach ($compiledFiles as $fileName) {
$I->safeDeleteFile($fileName);
}
}

/**
* Tests Compiler::compileFile test case to compile extended files
*
* @test
* @issue -
* @author Sergii Svyrydenko <[email protected]>
* @since 2017-01-17
*/
public function shouldCompileExtendsFile(IntegrationTester $I)
public function mvcViewEngineVoltCompileExtendsFile(IntegrationTester $I)
{
$I->wantToTest('Compile extended files');

$I->safeDeleteFile(
dataDir('fixtures/views/layouts/extends.volt.php')
);

$I->safeDeleteFile(
dataDir('fixtures/views/extends/children.extends.volt.php')
);
$I->wantToTest('Mvc\Vew\Engine\Volt :: compile() extended files');

$view = new View();

$view->setViewsDir(
dataDir('fixtures/views/')
);
$view->setViewsDir(dataDir('fixtures/views/'));

$volt = new Compiler($view);

Expand Down Expand Up @@ -80,32 +112,15 @@ public function shouldCompileExtendsFile(IntegrationTester $I)
/**
* Tests Compiler::compileFile test case to compile imported files
*
* @test
* @issue -
* @author Sergii Svyrydenko <[email protected]>
* @since 2017-01-17
*/
public function shouldCompileImportFile(IntegrationTester $I)
public function mvcViewEngineVoltCompileImportFile(IntegrationTester $I)
{
$I->wantToTest('Compile imported files');

$I->safeDeleteFile(
dataDir('fixtures/views/partials/header.volt.php')
);

$I->safeDeleteFile(
dataDir('fixtures/views/partials/footer.volt.php')
);

$I->safeDeleteFile(
dataDir('fixtures/views/extends/import.volt.php')
);
$I->wantToTest('Mvc\Vew\Engine\Volt :: compile() imported files');

$view = new View();

$view->setViewsDir(
dataDir('fixtures/views/')
);
$view->setViewsDir(dataDir('fixtures/views/'));

$volt = new Compiler($view);

Expand All @@ -128,47 +143,100 @@ public function shouldCompileImportFile(IntegrationTester $I)
* Tests Compiler::compileFile test case to compile imported files
* recursively
*
* @test
* @issue -
* @author Sergii Svyrydenko <[email protected]>
* @since 2017-01-17
*/
public function shouldCompileImportRecursiveFiles(IntegrationTester $I)
public function mvcViewEngineVoltCompileImportRecursiveFiles(IntegrationTester $I)
{
$I->wantToTest('Compile import recursive files');
$I->wantToTest('Mvc\Vew\Engine\Volt :: compile() import recursive files');

$I->safeDeleteFile(
dataDir('fixtures/views/partials/header3.volt.php')
);
$view = new View();
$view->setViewsDir(dataDir('fixtures/views/'));

$I->safeDeleteFile(
dataDir('fixtures/views/partials/header2.volt.php')
$volt = new Compiler($view);

//extends
$volt->compileFile(
dataDir('fixtures/views/extends/import2.volt'),
dataDir('fixtures/views/extends/import2.volt.php')
);

$I->safeDeleteFile(
$I->openFile(
dataDir('fixtures/views/extends/import2.volt.php')
);

$view = new View();
$I->seeFileContentsEqual(
'<div class="header"><h1>This is the title</h1></div>'
);
}

/**
* Tests Compiler::compileFile to compile files with blocks and partials
*
* @author Phalcon Team <[email protected]>
* @since 2021-06-25
*/
public function mvcViewEngineVoltCompileBlocks(IntegrationTester $I)
{
$I->wantToTest('Mvc\Vew\Engine\Volt :: compile() blocks and partials');

$template = '<!DOCTYPE html>' . PHP_EOL
. '<html lang="en">' . PHP_EOL
. '<head>' . PHP_EOL
. ' <meta charset="utf-8" />' . PHP_EOL
. ' <meta name="viewport" content="width=device-width, initial-scale=1.0" />' . PHP_EOL
. '</head>' . PHP_EOL
. '<body>' . PHP_EOL
. PHP_EOL
. PHP_EOL
. PHP_EOL
. '%s' . PHP_EOL
. PHP_EOL
. PHP_EOL
. '</body>' . PHP_EOL
. '</html>' . PHP_EOL
;

/**
* Set up the view and Volt and compile
*/
$view = new View();
$view->setViewsDir(
dataDir('fixtures/views/')
[
dataDir('fixtures/views/blocks'),
]
);

$volt = new Compiler($view);

//extends
/**
* Login - no header output
*/
$volt->compileFile(
dataDir('fixtures/views/extends/import2.volt'),
dataDir('fixtures/views/extends/import2.volt.php')
dataDir('fixtures/views/blocks/index/login.volt'),
dataDir('fixtures/views/blocks/index/login.volt.php')
);

$I->openFile(
dataDir('fixtures/views/extends/import2.volt.php')
dataDir('fixtures/views/blocks/index/login.volt.php')
);

$I->seeFileContentsEqual(
'<div class="header"><h1>This is the title</h1></div>'
$expected = sprintf($template, '<p>This is the login page</p>');
$I->seeFileContentsEqual($expected);

/**
* Main page = header output
*/
$volt->compileFile(
dataDir('fixtures/views/blocks/index/main.volt'),
dataDir('fixtures/views/blocks/index/main.volt.php')
);

$I->openFile(
dataDir('fixtures/views/blocks/index/main.volt.php')
);

$expected = sprintf($template, '<p>This is the main page</p>');
$I->seeFileContentsEqual($expected);
}
}
32 changes: 13 additions & 19 deletions tests/integration/Mvc/View/RenderCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,14 @@ class RenderCest
* @since 2017-09-24
* @issue https://github.com/phalcon/cphalcon/issues/13046
*/
public function shouldRenderWithParams(IntegrationTester $I)
public function mvcViewRenderWithParams(IntegrationTester $I)
{
$view = new View();
$I->wantToTest('Mvc\View - render() with parameters');

$view->setViewsDir(
dataDir('fixtures/views')
);
$view = new View();
$view->setViewsDir(dataDir('fixtures/views'));

$view->start();

$view->render(
'simple',
'params',
Expand All @@ -46,19 +44,18 @@ public function shouldRenderWithParams(IntegrationTester $I)
'age' => 20,
]
);

$view->finish();

$I->assertEquals(
'My name is Sam and I am 20 years old',
$view->getContent()
);
$expected = 'My name is Sam and I am 20 years old';
$actual = $view->getContent();
$I->assertEquals($expected, $actual);
}

public function doesNotRenderMultiple(IntegrationTester $I)
public function mvcViewRenderMultiple(IntegrationTester $I)
{
$view = new View();
$I->wantToTest('Mvc\View - render() multiple');

$view = new View();
$view->setViewsDir(
[
dataDir('fixtures/views'),
Expand All @@ -67,7 +64,6 @@ public function doesNotRenderMultiple(IntegrationTester $I)
);

$view->start();

$view->render(
'simple',
'params',
Expand All @@ -76,12 +72,10 @@ public function doesNotRenderMultiple(IntegrationTester $I)
'age' => 20,
]
);

$view->finish();

$I->assertEquals(
'My name is Sam and I am 20 years old',
$view->getContent()
);
$expected = 'My name is Sam and I am 20 years old';
$actual = $view->getContent();
$I->assertEquals($expected, $actual);
}
}