Skip to content

Commit

Permalink
Merge pull request #76 from phug-php/multitest-php-8
Browse files Browse the repository at this point in the history
Fix PHP 8 compatibility
  • Loading branch information
kylekatarnls authored Sep 25, 2020
2 parents 163ed88 + 8dea3b4 commit ee81108
Show file tree
Hide file tree
Showing 12 changed files with 140 additions and 44 deletions.
20 changes: 10 additions & 10 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
34 changes: 5 additions & 29 deletions src/Phug/Formatter/Formatter/Util/PhpUnwrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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, '<?=') === false
? mb_substr($code, 0, -3).';'
: $code.'<?php';

$this->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();
}
}
61 changes: 61 additions & 0 deletions src/Phug/Formatter/Formatter/Util/PhpUnwrapString.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace Phug\Formatter\Util;

class PhpUnwrapString
{
/**
* @var string
*/
private $code;

public function __construct($code)
{
$this->code = $code;
}

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)
? mb_substr($this->code, 6)
: '?>'.$this->code;
}
public function unwrapEnd()
{
$this->code = preg_match('/\s\?>$/', $this->code) && strpos($this->code, '<?=') === false
? mb_substr($this->code, 0, -3).';'
: $this->code.'<?php';
}

/**
* @return string
*/
public function getCode()
{
return $this->code;
}

public function __toString()
{
return $this->getCode();
}
}
3 changes: 2 additions & 1 deletion src/Phug/Renderer/Renderer/Adapter/EvalAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Phug\Renderer\Adapter;

use Phug\Formatter\Util\PhpUnwrapString;
use Phug\Renderer\AbstractAdapter;

/**
Expand All @@ -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);
}
}
5 changes: 5 additions & 0 deletions tests/Phug/Element/MixinCallElementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class MixinCallElementTest extends TestCase
* @covers \Phug\Formatter::requireMixin
* @covers \Phug\Formatter::formatDependencies
* @covers \Phug\Formatter\Util\PhpUnwrap::<public>
* @covers \Phug\Formatter\Util\PhpUnwrapString::<public>
* @covers \Phug\Formatter\AbstractFormat::getMixinAttributes
* @covers \Phug\Formatter\AbstractFormat::formatMixinAttributeValue
* @covers \Phug\Formatter\AbstractFormat::formatMixinCallElement
Expand Down Expand Up @@ -83,6 +84,7 @@ public function testMixinCallElement()
* @covers \Phug\Formatter::requireMixin
* @covers \Phug\Formatter::formatDependencies
* @covers \Phug\Formatter\Util\PhpUnwrap::<public>
* @covers \Phug\Formatter\Util\PhpUnwrapString::<public>
* @covers \Phug\Formatter\AbstractFormat::getMixinAttributes
* @covers \Phug\Formatter\AbstractFormat::formatMixinAttributeValue
* @covers \Phug\Formatter\AbstractFormat::formatMixinCallElement
Expand Down Expand Up @@ -170,6 +172,7 @@ public function testMixinBlockVariable()
* @covers \Phug\Formatter::requireMixin
* @covers \Phug\Formatter::formatDependencies
* @covers \Phug\Formatter\Util\PhpUnwrap::<public>
* @covers \Phug\Formatter\Util\PhpUnwrapString::<public>
* @covers \Phug\Formatter\AbstractFormat::formatMixinAttributeValue
* @covers \Phug\Formatter\AbstractFormat::getMixinAttributes
* @covers \Phug\Formatter\AbstractFormat::formatMixinCallElement
Expand Down Expand Up @@ -220,6 +223,7 @@ public function testDefaultValue()
* @covers \Phug\Formatter::requireMixin
* @covers \Phug\Formatter::formatDependencies
* @covers \Phug\Formatter\Util\PhpUnwrap::<public>
* @covers \Phug\Formatter\Util\PhpUnwrapString::<public>
* @covers \Phug\Formatter\AbstractFormat::formatMixinAttributeValue
* @covers \Phug\Formatter\AbstractFormat::getMixinAttributes
* @covers \Phug\Formatter\AbstractFormat::formatMixinCallElement
Expand Down Expand Up @@ -330,6 +334,7 @@ public function testUnknownMixinDebugOff()
* @covers \Phug\Formatter::getDestructors
* @covers \Phug\Formatter\AbstractFormat::getChildrenIterator
* @covers \Phug\Formatter\Util\PhpUnwrap::<public>
* @covers \Phug\Formatter\Util\PhpUnwrapString::<public>
*/
public function testPhpUnwrap()
{
Expand Down
5 changes: 5 additions & 0 deletions tests/Phug/Element/MixinElementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class MixinElementTest extends TestCase
* @covers \Phug\Formatter::requireMixin
* @covers \Phug\Formatter::formatDependencies
* @covers \Phug\Formatter\Util\PhpUnwrap::<public>
* @covers \Phug\Formatter\Util\PhpUnwrapString::<public>
* @covers \Phug\Formatter\AbstractFormat::formatMixinAttributeValue
* @covers \Phug\Formatter\AbstractFormat::getMixinAttributes
* @covers \Phug\Formatter\AbstractFormat::formatMixinElement
Expand Down Expand Up @@ -61,6 +62,7 @@ public function testMixinElement()
* @covers \Phug\Formatter::requireMixin
* @covers \Phug\Formatter::formatDependencies
* @covers \Phug\Formatter\Util\PhpUnwrap::<public>
* @covers \Phug\Formatter\Util\PhpUnwrapString::<public>
* @covers \Phug\Formatter\AbstractFormat::formatMixinAttributeValue
* @covers \Phug\Formatter\AbstractFormat::getMixinAttributes
* @covers \Phug\Formatter\AbstractFormat::formatMixinElement
Expand Down Expand Up @@ -89,6 +91,7 @@ public function testLazyLoad()
* @covers \Phug\Formatter::requireAllMixins
* @covers \Phug\Formatter::formatDependencies
* @covers \Phug\Formatter\Util\PhpUnwrap::<public>
* @covers \Phug\Formatter\Util\PhpUnwrapString::<public>
* @covers \Phug\Formatter\AbstractFormat::formatMixinAttributeValue
* @covers \Phug\Formatter\AbstractFormat::getMixinAttributes
* @covers \Phug\Formatter\AbstractFormat::formatMixinElement
Expand Down Expand Up @@ -129,6 +132,7 @@ public function testRequireAllMixins()
* @covers \Phug\Formatter::requireMixin
* @covers \Phug\Formatter::formatDependencies
* @covers \Phug\Formatter\Util\PhpUnwrap::<public>
* @covers \Phug\Formatter\Util\PhpUnwrapString::<public>
* @covers \Phug\Formatter\AbstractFormat::formatMixinAttributeValue
* @covers \Phug\Formatter\AbstractFormat::getMixinAttributes
* @covers \Phug\Formatter\AbstractFormat::formatMixinElement
Expand Down Expand Up @@ -172,6 +176,7 @@ public function testMixinElementReplace()
* @covers \Phug\Formatter::requireMixin
* @covers \Phug\Formatter::formatDependencies
* @covers \Phug\Formatter\Util\PhpUnwrap::<public>
* @covers \Phug\Formatter\Util\PhpUnwrapString::<public>
* @covers \Phug\Formatter\AbstractFormat::formatMixinAttributeValue
* @covers \Phug\Formatter\AbstractFormat::getMixinAttributes
* @covers \Phug\Formatter\AbstractFormat::formatMixinElement
Expand Down
5 changes: 5 additions & 0 deletions tests/Phug/FormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -1355,6 +1358,8 @@ public function testDebugErrorOnRemovedFile()
include $file;
} catch (\Exception $exception) {
$error = $exception;
} catch (\Throwable $exception) {
$error = $exception;
}
ob_end_clean();

Expand Down
6 changes: 2 additions & 4 deletions tests/Phug/ProfilerModuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 2 additions & 0 deletions tests/Phug/RendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
23 changes: 23 additions & 0 deletions tests/Phug/Util/PhpUnwrapStringTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Phug\Test\Util;

use PHPUnit\Framework\TestCase;
use Phug\Formatter\Util\PhpUnwrapString;

/**
* @coversDefaultClass \Phug\Formatter\Util\PhpUnwrapString
*/
class PhpUnwrapStringTest extends TestCase
{
/**
* @covers \Phug\Formatter\Util\PhpUnwrapString::<public>
* @covers ::<public>
*/
public function testPhpUnwrapString()
{
self::assertSame('echo "Foo";', (string) PhpUnwrapString::withoutOpenTag('<?php echo "Foo";'));
self::assertSame('echo "Foo"; ?>', (string) PhpUnwrapString::withoutOpenTag('<?php echo "Foo"; ?>'));
self::assertSame('?><div><?php echo "Foo"; ?></div>', (string) PhpUnwrapString::withoutOpenTag('<div><?php echo "Foo"; ?></div>'));
}
}
2 changes: 2 additions & 0 deletions tests/cases/scope.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
test
test2
18 changes: 18 additions & 0 deletions tests/cases/scope.pug
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit ee81108

Please sign in to comment.