diff --git a/CHANGELOG-5.0.md b/CHANGELOG-5.0.md index 710a1698194..f6dccba7bce 100644 --- a/CHANGELOG-5.0.md +++ b/CHANGELOG-5.0.md @@ -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) diff --git a/phalcon/Mvc/View/Engine/Volt/Compiler.zep b/phalcon/Mvc/View/Engine/Volt/Compiler.zep index ce94d2b577e..46eef1be290 100644 --- a/phalcon/Mvc/View/Engine/Volt/Compiler.zep +++ b/phalcon/Mvc/View/Engine/Volt/Compiler.zep @@ -2249,7 +2249,6 @@ class Compiler implements InjectionAwareInterface return this; } - /** * Compiles a Volt source code returning a PHP plain version */ @@ -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" { /** diff --git a/tests/_data/fixtures/views/blocks/base.volt b/tests/_data/fixtures/views/blocks/base.volt new file mode 100644 index 00000000000..6e1e3e8eb21 --- /dev/null +++ b/tests/_data/fixtures/views/blocks/base.volt @@ -0,0 +1,15 @@ + + + + + + + +{% block header %} +{{ partial('blocks/partials/header') }} +{% endblock %} + +{% block mainContent %}{% endblock %} + + + diff --git a/tests/_data/fixtures/views/blocks/index/login.volt b/tests/_data/fixtures/views/blocks/index/login.volt new file mode 100644 index 00000000000..79c00a44c27 --- /dev/null +++ b/tests/_data/fixtures/views/blocks/index/login.volt @@ -0,0 +1,7 @@ +{% extends 'base.volt' %} + +{% block header %}{% endblock %} +{% block mainContent %} +

This is the login page

+{% endblock %} +{% block footer %}{% endblock %} diff --git a/tests/_data/fixtures/views/blocks/index/main.volt b/tests/_data/fixtures/views/blocks/index/main.volt new file mode 100644 index 00000000000..c17d7b09b3a --- /dev/null +++ b/tests/_data/fixtures/views/blocks/index/main.volt @@ -0,0 +1,5 @@ +{% extends 'base.volt' %} + +{% block mainContent %} +

This is the main page

+{% endblock %} diff --git a/tests/_data/fixtures/views/blocks/partials/header.volt b/tests/_data/fixtures/views/blocks/partials/header.volt new file mode 100644 index 00000000000..b0b8722266f --- /dev/null +++ b/tests/_data/fixtures/views/blocks/partials/header.volt @@ -0,0 +1 @@ +

This is the header

diff --git a/tests/integration/Mvc/View/Engine/Volt/CompilerFilesCest.php b/tests/integration/Mvc/View/Engine/Volt/CompilerFilesCest.php index e8f1a7fcb10..9c3e625935b 100644 --- a/tests/integration/Mvc/View/Engine/Volt/CompilerFilesCest.php +++ b/tests/integration/Mvc/View/Engine/Volt/CompilerFilesCest.php @@ -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 @@ -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 * @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); @@ -80,32 +112,15 @@ public function shouldCompileExtendsFile(IntegrationTester $I) /** * Tests Compiler::compileFile test case to compile imported files * - * @test - * @issue - * @author Sergii Svyrydenko * @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); @@ -128,47 +143,100 @@ public function shouldCompileImportFile(IntegrationTester $I) * Tests Compiler::compileFile test case to compile imported files * recursively * - * @test - * @issue - * @author Sergii Svyrydenko * @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( + '

This is the title

' + ); + } + /** + * Tests Compiler::compileFile to compile files with blocks and partials + * + * @author Phalcon Team + * @since 2021-06-25 + */ + public function mvcViewEngineVoltCompileBlocks(IntegrationTester $I) + { + $I->wantToTest('Mvc\Vew\Engine\Volt :: compile() blocks and partials'); + + $template = '' . PHP_EOL + . '' . PHP_EOL + . '' . PHP_EOL + . ' ' . PHP_EOL + . ' ' . PHP_EOL + . '' . PHP_EOL + . '' . PHP_EOL + . PHP_EOL + . PHP_EOL + . PHP_EOL + . '%s' . PHP_EOL + . PHP_EOL + . PHP_EOL + . '' . PHP_EOL + . '' . 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( - '

This is the title

' + $expected = sprintf($template, '

This is the login page

'); + $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, '

This is the main page

'); + $I->seeFileContentsEqual($expected); } } diff --git a/tests/integration/Mvc/View/RenderCest.php b/tests/integration/Mvc/View/RenderCest.php index 8e7d334f4da..fbaeb58f305 100644 --- a/tests/integration/Mvc/View/RenderCest.php +++ b/tests/integration/Mvc/View/RenderCest.php @@ -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', @@ -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'), @@ -67,7 +64,6 @@ public function doesNotRenderMultiple(IntegrationTester $I) ); $view->start(); - $view->render( 'simple', 'params', @@ -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); } }