From a5714380f73f5cec015be053047866128dad9c21 Mon Sep 17 00:00:00 2001 From: KyleKatarn Date: Fri, 25 Sep 2020 13:12:08 +0200 Subject: [PATCH 1/6] Test dependencies with PHP 8 --- .travis.yml | 2 ++ tests/Phug/FormatterTest.php | 5 +++++ tests/Phug/RendererTest.php | 2 ++ 3 files changed, 9 insertions(+) diff --git a/.travis.yml b/.travis.yml index 3c0820a4..af5f1aa6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -30,6 +30,8 @@ matrix: env: STYLECHECK=on - php: 7.3 env: MULTITEST=on + - php: nightly + env: MULTITEST=on - php: 7.3 env: SPLIT=on diff --git a/tests/Phug/FormatterTest.php b/tests/Phug/FormatterTest.php index f6d3123b..76de1815 100644 --- a/tests/Phug/FormatterTest.php +++ b/tests/Phug/FormatterTest.php @@ -1217,6 +1217,9 @@ public function testDebugError() } catch (\Exception $exception) { /** @var LocatedException $error */ $error = $formatter->getDebugError($exception, $code); + } catch (\Throwable $exception) { + /** @var LocatedException $error */ + $error = $formatter->getDebugError($exception, $code); } }, '?>'.$php); ob_end_clean(); @@ -1355,6 +1358,8 @@ public function testDebugErrorOnRemovedFile() include $file; } catch (\Exception $exception) { $error = $exception; + } catch (\Throwable $exception) { + $error = $exception; } ob_end_clean(); diff --git a/tests/Phug/RendererTest.php b/tests/Phug/RendererTest.php index 03748bc6..53bf071b 100644 --- a/tests/Phug/RendererTest.php +++ b/tests/Phug/RendererTest.php @@ -471,6 +471,8 @@ public function testHandleErrorInString() $renderer->render('div: p=trigger_error("Division by zero")'); } catch (\Exception $error) { $message = $error->getMessage(); + } catch (\Throwable $error) { + $message = $error->getMessage(); } self::assertContains( From 6e11ad1638b78b2fcd102e58512fecda56f1a652 Mon Sep 17 00:00:00 2001 From: KyleKatarn Date: Fri, 25 Sep 2020 13:50:11 +0200 Subject: [PATCH 2/6] Add test for namespace in template --- .travis.yml | 2 -- tests/Phug/CasesTest.php | 2 +- tests/cases/scope.html | 2 ++ tests/cases/scope.pug | 18 ++++++++++++++++++ 4 files changed, 21 insertions(+), 3 deletions(-) create mode 100644 tests/cases/scope.html create mode 100644 tests/cases/scope.pug diff --git a/.travis.yml b/.travis.yml index af5f1aa6..3c0820a4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -30,8 +30,6 @@ matrix: env: STYLECHECK=on - php: 7.3 env: MULTITEST=on - - php: nightly - env: MULTITEST=on - php: 7.3 env: SPLIT=on diff --git a/tests/Phug/CasesTest.php b/tests/Phug/CasesTest.php index c5fa5911..91b47397 100644 --- a/tests/Phug/CasesTest.php +++ b/tests/Phug/CasesTest.php @@ -28,7 +28,7 @@ public function caseProvider() public function testRender($expected, $actual, $message) { $debug = $this->renderer->getOption('debug'); - $this->renderer->setOption('debug', true); + $this->renderer->setOption('debug', pathinfo($actual)['filename'] !== 'scope'); $render = $this->renderer->renderFile($actual); $this->renderer->setOption('debug', $debug); diff --git a/tests/cases/scope.html b/tests/cases/scope.html new file mode 100644 index 00000000..b02def2d --- /dev/null +++ b/tests/cases/scope.html @@ -0,0 +1,2 @@ +test +test2 diff --git a/tests/cases/scope.pug b/tests/cases/scope.pug new file mode 100644 index 00000000..8e644e47 --- /dev/null +++ b/tests/cases/scope.pug @@ -0,0 +1,18 @@ +- + namespace Test; + class Req { + public $text; + public function __construct($n = '') { + $this->text = "test$n\n"; + } + public function suffix($n) { + return new Req($n); + } + } + class Test { + static public function app() { + return new Req(); + } + } += \Test\Test::app()->text += \Test\Test::app()->suffix(2)->text From 1c9c27bbd33e145e9d1c2cf2298e00beb3d4d4eb Mon Sep 17 00:00:00 2001 From: KyleKatarn Date: Fri, 25 Sep 2020 14:00:23 +0200 Subject: [PATCH 3/6] Debug unit tests --- .travis.yml | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/.travis.yml b/.travis.yml index 3c0820a4..ce0edb42 100644 --- a/.travis.yml +++ b/.travis.yml @@ -52,20 +52,20 @@ before_script: - if [[ "$COVERAGE" = "on" ]]; then ./cc-test-reporter before-build; fi; script: - - if [[ "$STYLECHECK" != "on" && "$COVERAGE" != "on" && "$MULTITEST" != "on" && "$SPLIT" != "on" ]]; then vendor/bin/phpunit --no-coverage; fi; - - if [[ "$COVERAGE" = "on" ]]; then vendor/bin/phpunit --coverage-text --coverage-clover=coverage.xml; fi; + - if [[ "$STYLECHECK" != "on" && "$COVERAGE" != "on" && "$MULTITEST" != "on" && "$SPLIT" != "on" ]]; then vendor/bin/phpunit --no-coverage --debug --verbose; fi; + - if [[ "$COVERAGE" = "on" ]]; then vendor/bin/phpunit --no-coverage --debug --verbose; fi; - if [[ "$MULTITEST" = "on" ]]; then vendor/bin/multi-tester --verbose --quiet-install; fi; - if [[ "$STYLECHECK" = "on" ]]; then vendor/bin/phpcs --ignore=*.js src; fi; - if [[ "$SPLIT" = "on" ]]; then composer require phug/split && vendor/bin/split update --git-credentials=$GH_CREDENTIALS; fi; -after_success: - - if [[ "$COVERAGE" = "on" ]]; then bash <(curl -s https://codecov.io/bash); fi; - -after_script: - - if [[ "$COVERAGE" = "on" ]]; then cp coverage.xml clover.xml; fi; - - if [[ "$COVERAGE" = "on" ]]; then ./cc-test-reporter after-build --coverage-input-type clover --exit-code $TRAVIS_TEST_RESULT; fi; - - if [[ "$COVERAGE" = "on" ]]; then composer require codacy/coverage; fi; - - if [[ "$COVERAGE" = "on" ]]; then vendor/bin/codacycoverage clover coverage.xml; fi; +#after_success: +# - if [[ "$COVERAGE" = "on" ]]; then bash <(curl -s https://codecov.io/bash); fi; +# +#after_script: +# - if [[ "$COVERAGE" = "on" ]]; then cp coverage.xml clover.xml; fi; +# - if [[ "$COVERAGE" = "on" ]]; then ./cc-test-reporter after-build --coverage-input-type clover --exit-code $TRAVIS_TEST_RESULT; fi; +# - if [[ "$COVERAGE" = "on" ]]; then composer require codacy/coverage; fi; +# - if [[ "$COVERAGE" = "on" ]]; then vendor/bin/codacycoverage clover coverage.xml; fi; notifications: slack: phug:nzXFnxhU14RWK2EQSDL0u08z From afd50e121b298f516d082a5681cb9525b105572e Mon Sep 17 00:00:00 2001 From: KyleKatarn Date: Fri, 25 Sep 2020 14:58:38 +0200 Subject: [PATCH 4/6] Add PhpUnwrapString class --- .../Formatter/Formatter/Util/PhpUnwrap.php | 34 ++----------- .../Formatter/Util/PhpUnwrapString.php | 51 +++++++++++++++++++ .../Renderer/Renderer/Adapter/EvalAdapter.php | 3 +- tests/Phug/Element/MixinCallElementTest.php | 5 ++ tests/Phug/Element/MixinElementTest.php | 5 ++ tests/Phug/ProfilerModuleTest.php | 6 +-- tests/Phug/Util/PhpUnwrapStringTest.php | 38 ++++++++++++++ 7 files changed, 108 insertions(+), 34 deletions(-) create mode 100644 src/Phug/Formatter/Formatter/Util/PhpUnwrapString.php create mode 100644 tests/Phug/Util/PhpUnwrapStringTest.php diff --git a/src/Phug/Formatter/Formatter/Util/PhpUnwrap.php b/src/Phug/Formatter/Formatter/Util/PhpUnwrap.php index ea285c85..b4568ed3 100644 --- a/src/Phug/Formatter/Formatter/Util/PhpUnwrap.php +++ b/src/Phug/Formatter/Formatter/Util/PhpUnwrap.php @@ -4,39 +4,15 @@ use Phug\Formatter; -class PhpUnwrap +class PhpUnwrap extends PhpUnwrapString { - /** - * @var string - */ - private $code; - public function __construct($element, Formatter $formatter) { - $elements = is_array($element) ? $element : [$element]; - $code = implode('', array_map(function ($child) use ($formatter) { + parent::__construct(implode('', array_map(function ($child) use ($formatter) { return is_string($child) ? $child : $formatter->format($child); - }, $elements)); - $code = preg_match('/^<\?php\s/', $code) - ? mb_substr($code, 6) - : '?>'.$code; - $code = preg_match('/\s\?>$/', $code) && strpos($code, 'code = $code; - } + }, is_array($element) ? $element : [$element]))); - /** - * @return string - */ - public function getCode() - { - return $this->code; - } - - public function __toString() - { - return $this->getCode(); + $this->unwrapStart(); + $this->unwrapEnd(); } } diff --git a/src/Phug/Formatter/Formatter/Util/PhpUnwrapString.php b/src/Phug/Formatter/Formatter/Util/PhpUnwrapString.php new file mode 100644 index 00000000..169c1cb3 --- /dev/null +++ b/src/Phug/Formatter/Formatter/Util/PhpUnwrapString.php @@ -0,0 +1,51 @@ +code = $code; + } + + public static function withoutOpenTag($code) + { + $unwrappedCode = new self($code); + $unwrappedCode->unwrapStart(); + + return $unwrappedCode; + } + + public function unwrapStart() + { + $this->code = preg_match('/^<\?php\s/', $this->code) + ? mb_substr($this->code, 6) + : '?>'.$this->code; + } + + public function unwrapEnd() + { + $this->code = preg_match('/\s\?>$/', $this->code) && strpos($this->code, 'code, 0, -3).';' + : $this->code.'code; + } + + public function __toString() + { + return $this->getCode(); + } +} diff --git a/src/Phug/Renderer/Renderer/Adapter/EvalAdapter.php b/src/Phug/Renderer/Renderer/Adapter/EvalAdapter.php index 40e8d498..633218e5 100644 --- a/src/Phug/Renderer/Renderer/Adapter/EvalAdapter.php +++ b/src/Phug/Renderer/Renderer/Adapter/EvalAdapter.php @@ -2,6 +2,7 @@ namespace Phug\Renderer\Adapter; +use Phug\Formatter\Util\PhpUnwrapString; use Phug\Renderer\AbstractAdapter; /** @@ -17,7 +18,7 @@ public function display($__pug_php, array $__pug_parameters) { $this->execute(function () use ($__pug_php, &$__pug_parameters) { extract($__pug_parameters); - eval('?>'.$__pug_php); + eval(PhpUnwrapString::withoutOpenTag($__pug_php)); }, $__pug_parameters); } } diff --git a/tests/Phug/Element/MixinCallElementTest.php b/tests/Phug/Element/MixinCallElementTest.php index c7eb5b25..bc36de6b 100644 --- a/tests/Phug/Element/MixinCallElementTest.php +++ b/tests/Phug/Element/MixinCallElementTest.php @@ -29,6 +29,7 @@ class MixinCallElementTest extends TestCase * @covers \Phug\Formatter::requireMixin * @covers \Phug\Formatter::formatDependencies * @covers \Phug\Formatter\Util\PhpUnwrap:: + * @covers \Phug\Formatter\Util\PhpUnwrapString:: * @covers \Phug\Formatter\AbstractFormat::getMixinAttributes * @covers \Phug\Formatter\AbstractFormat::formatMixinAttributeValue * @covers \Phug\Formatter\AbstractFormat::formatMixinCallElement @@ -83,6 +84,7 @@ public function testMixinCallElement() * @covers \Phug\Formatter::requireMixin * @covers \Phug\Formatter::formatDependencies * @covers \Phug\Formatter\Util\PhpUnwrap:: + * @covers \Phug\Formatter\Util\PhpUnwrapString:: * @covers \Phug\Formatter\AbstractFormat::getMixinAttributes * @covers \Phug\Formatter\AbstractFormat::formatMixinAttributeValue * @covers \Phug\Formatter\AbstractFormat::formatMixinCallElement @@ -170,6 +172,7 @@ public function testMixinBlockVariable() * @covers \Phug\Formatter::requireMixin * @covers \Phug\Formatter::formatDependencies * @covers \Phug\Formatter\Util\PhpUnwrap:: + * @covers \Phug\Formatter\Util\PhpUnwrapString:: * @covers \Phug\Formatter\AbstractFormat::formatMixinAttributeValue * @covers \Phug\Formatter\AbstractFormat::getMixinAttributes * @covers \Phug\Formatter\AbstractFormat::formatMixinCallElement @@ -220,6 +223,7 @@ public function testDefaultValue() * @covers \Phug\Formatter::requireMixin * @covers \Phug\Formatter::formatDependencies * @covers \Phug\Formatter\Util\PhpUnwrap:: + * @covers \Phug\Formatter\Util\PhpUnwrapString:: * @covers \Phug\Formatter\AbstractFormat::formatMixinAttributeValue * @covers \Phug\Formatter\AbstractFormat::getMixinAttributes * @covers \Phug\Formatter\AbstractFormat::formatMixinCallElement @@ -330,6 +334,7 @@ public function testUnknownMixinDebugOff() * @covers \Phug\Formatter::getDestructors * @covers \Phug\Formatter\AbstractFormat::getChildrenIterator * @covers \Phug\Formatter\Util\PhpUnwrap:: + * @covers \Phug\Formatter\Util\PhpUnwrapString:: */ public function testPhpUnwrap() { diff --git a/tests/Phug/Element/MixinElementTest.php b/tests/Phug/Element/MixinElementTest.php index f779bd11..b46d672d 100644 --- a/tests/Phug/Element/MixinElementTest.php +++ b/tests/Phug/Element/MixinElementTest.php @@ -22,6 +22,7 @@ class MixinElementTest extends TestCase * @covers \Phug\Formatter::requireMixin * @covers \Phug\Formatter::formatDependencies * @covers \Phug\Formatter\Util\PhpUnwrap:: + * @covers \Phug\Formatter\Util\PhpUnwrapString:: * @covers \Phug\Formatter\AbstractFormat::formatMixinAttributeValue * @covers \Phug\Formatter\AbstractFormat::getMixinAttributes * @covers \Phug\Formatter\AbstractFormat::formatMixinElement @@ -61,6 +62,7 @@ public function testMixinElement() * @covers \Phug\Formatter::requireMixin * @covers \Phug\Formatter::formatDependencies * @covers \Phug\Formatter\Util\PhpUnwrap:: + * @covers \Phug\Formatter\Util\PhpUnwrapString:: * @covers \Phug\Formatter\AbstractFormat::formatMixinAttributeValue * @covers \Phug\Formatter\AbstractFormat::getMixinAttributes * @covers \Phug\Formatter\AbstractFormat::formatMixinElement @@ -89,6 +91,7 @@ public function testLazyLoad() * @covers \Phug\Formatter::requireAllMixins * @covers \Phug\Formatter::formatDependencies * @covers \Phug\Formatter\Util\PhpUnwrap:: + * @covers \Phug\Formatter\Util\PhpUnwrapString:: * @covers \Phug\Formatter\AbstractFormat::formatMixinAttributeValue * @covers \Phug\Formatter\AbstractFormat::getMixinAttributes * @covers \Phug\Formatter\AbstractFormat::formatMixinElement @@ -129,6 +132,7 @@ public function testRequireAllMixins() * @covers \Phug\Formatter::requireMixin * @covers \Phug\Formatter::formatDependencies * @covers \Phug\Formatter\Util\PhpUnwrap:: + * @covers \Phug\Formatter\Util\PhpUnwrapString:: * @covers \Phug\Formatter\AbstractFormat::formatMixinAttributeValue * @covers \Phug\Formatter\AbstractFormat::getMixinAttributes * @covers \Phug\Formatter\AbstractFormat::formatMixinElement @@ -172,6 +176,7 @@ public function testMixinElementReplace() * @covers \Phug\Formatter::requireMixin * @covers \Phug\Formatter::formatDependencies * @covers \Phug\Formatter\Util\PhpUnwrap:: + * @covers \Phug\Formatter\Util\PhpUnwrapString:: * @covers \Phug\Formatter\AbstractFormat::formatMixinAttributeValue * @covers \Phug\Formatter\AbstractFormat::getMixinAttributes * @covers \Phug\Formatter\AbstractFormat::formatMixinElement diff --git a/tests/Phug/ProfilerModuleTest.php b/tests/Phug/ProfilerModuleTest.php index 0b92aa0f..aff70dbb 100644 --- a/tests/Phug/ProfilerModuleTest.php +++ b/tests/Phug/ProfilerModuleTest.php @@ -155,10 +155,8 @@ public function testDebugDefaultOptions() */ public function testMemoryLimitOptions() { - if (version_compare(PHP_VERSION, '5.6.0-dev', '>=') && - version_compare(PHP_VERSION, '7.0.0-dev', '<') - ) { - self::markTestSkipped('Skipped due to Travis bug with PHP 5.6 only.'); + if (version_compare(PHP_VERSION, '7.0.0-dev', '<')) { + self::markTestSkipped('Skipped due to Travis bug with PHP 5 only.'); return; } diff --git a/tests/Phug/Util/PhpUnwrapStringTest.php b/tests/Phug/Util/PhpUnwrapStringTest.php new file mode 100644 index 00000000..b86b4a7e --- /dev/null +++ b/tests/Phug/Util/PhpUnwrapStringTest.php @@ -0,0 +1,38 @@ + + * @covers :: + */ + public function testPhpUnwrapString() + { + self::assertSame('echo "Foo";', (string) PhpUnwrapString::withoutOpenTag('', (string) PhpUnwrapString::withoutOpenTag('')); + self::assertSame('?>
', (string) PhpUnwrapString::withoutOpenTag('
')); + } +} From feaec94c85100a323ee99bf823dc155f8ade47fd Mon Sep 17 00:00:00 2001 From: KyleKatarn Date: Fri, 25 Sep 2020 15:21:43 +0200 Subject: [PATCH 5/6] Cleanup imports --- tests/Phug/Util/PhpUnwrapStringTest.php | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/tests/Phug/Util/PhpUnwrapStringTest.php b/tests/Phug/Util/PhpUnwrapStringTest.php index b86b4a7e..9db329db 100644 --- a/tests/Phug/Util/PhpUnwrapStringTest.php +++ b/tests/Phug/Util/PhpUnwrapStringTest.php @@ -3,22 +3,7 @@ namespace Phug\Test\Util; use PHPUnit\Framework\TestCase; -use Phug\Formatter; -use Phug\Formatter\Element\AnonymousBlockElement; -use Phug\Formatter\Element\AssignmentElement; -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\MixinCallElement; -use Phug\Formatter\Element\MixinElement; -use Phug\Formatter\Element\TextElement; use Phug\Formatter\Util\PhpUnwrapString; -use Phug\Lexer\Token\MixinCallToken; -use Phug\Parser\Node\MixinCallNode; -use Phug\Util\SourceLocation; -use SplObjectStorage; /** * @coversDefaultClass \Phug\Formatter\Util\PhpUnwrapString From 8dea3b49412371a327dcdf461b601aa1d6ea6cfa Mon Sep 17 00:00:00 2001 From: KyleKatarn Date: Fri, 25 Sep 2020 16:07:38 +0200 Subject: [PATCH 6/6] Support namespace with debug mode on --- src/Phug/Formatter/Formatter/Util/PhpUnwrapString.php | 10 ++++++++++ tests/Phug/CasesTest.php | 2 +- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/Phug/Formatter/Formatter/Util/PhpUnwrapString.php b/src/Phug/Formatter/Formatter/Util/PhpUnwrapString.php index 169c1cb3..5b0929f5 100644 --- a/src/Phug/Formatter/Formatter/Util/PhpUnwrapString.php +++ b/src/Phug/Formatter/Formatter/Util/PhpUnwrapString.php @@ -18,10 +18,20 @@ public static function withoutOpenTag($code) { $unwrappedCode = new self($code); $unwrappedCode->unwrapStart(); + $unwrappedCode->movePhugCodeAfterNamespace(); return $unwrappedCode; } + public function movePhugCodeAfterNamespace() + { + $this->code = preg_replace( + '/^((?:[ \t]*(?:\\\\Phug\\\\.*\n|\\/\\/.*\n|(?:\?><\?php)?[ \t\n]+)?)*)(namespace (?:.*)\n)/', + '$2$1', + $this->code + ); + } + public function unwrapStart() { $this->code = preg_match('/^<\?php\s/', $this->code) diff --git a/tests/Phug/CasesTest.php b/tests/Phug/CasesTest.php index 91b47397..c5fa5911 100644 --- a/tests/Phug/CasesTest.php +++ b/tests/Phug/CasesTest.php @@ -28,7 +28,7 @@ public function caseProvider() public function testRender($expected, $actual, $message) { $debug = $this->renderer->getOption('debug'); - $this->renderer->setOption('debug', pathinfo($actual)['filename'] !== 'scope'); + $this->renderer->setOption('debug', true); $render = $this->renderer->renderFile($actual); $this->renderer->setOption('debug', $debug);