From 58dbc3fa1b37c4ba6249ba4e056f79f73c769c28 Mon Sep 17 00:00:00 2001 From: Kyle Date: Sun, 2 May 2021 19:13:06 +0200 Subject: [PATCH] Switch to GitHub Actions (#4) * Switch to GitHub Actions * Created dedicated method formatAttributeFlag() * Add unit test for attribute name as expressions * Require Phug >= 0.5.2 to get variadic method * Require phug/formatter >= 0.5.6 to get variadic method * Require phug/util >= 0.4.11 to get variadic trait * Require phug/parser >= 0.5.2 to get variadic method * Require phug/lexer >= 0.5.4 to get variadic method * Require phug/util >= 0.4.17 to include array key fix * Require phug/formatter >= 0.5.13 to include setPatterns() * Retores PHPUnit version * Add coverage annotation --- .github/workflows/coverage.yml | 62 ++++++++++++++++++ .github/workflows/tests.yml | 49 +++++++++++++++ .travis.yml | 46 -------------- composer.json | 9 ++- src/Phug/Formatter/Format/TwigXmlFormat.php | 69 +++++++++++++-------- tests/PugToTwigTest.php | 55 ++++++++++++++++ 6 files changed, 217 insertions(+), 73 deletions(-) create mode 100644 .github/workflows/coverage.yml create mode 100644 .github/workflows/tests.yml delete mode 100644 .travis.yml diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml new file mode 100644 index 0000000..523bdd2 --- /dev/null +++ b/.github/workflows/coverage.yml @@ -0,0 +1,62 @@ +name: Coverage + +on: + push: + branches: [ '**' ] + pull_request: + branches: [ '**' ] + +jobs: + test: + runs-on: ubuntu-latest + + strategy: + fail-fast: false + matrix: + php: ['7.4'] + setup: ['stable'] + + name: PHP + + steps: + - uses: actions/checkout@v2 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + tools: composer:v2 + + - name: Cache Composer packages + id: composer-cache + uses: actions/cache@v2 + with: + path: vendor + key: ${{ runner.os }}-${{ matrix.setup }}-v2-php-${{ matrix.php }}-${{ hashFiles('**/composer.lock') }} + restore-keys: | + ${{ runner.os }}-${{ matrix.setup }}-v2-php-${{ matrix.php }}- + + - name: Code Climate Test Reporter Preparation + if: ${{ env.CC_TEST_REPORTER_ID != '' }} + run: | + curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter + chmod +x ./cc-test-reporter + ./cc-test-reporter before-build + env: + CC_TEST_REPORTER_ID: ${{ secrets.CC_TEST_REPORTER_ID }} + + - name: Install dependencies + if: steps.composer-cache.outputs.cache-hit != 'true' + run: composer update --prefer-dist --prefer-${{ matrix.setup }} --no-progress --no-interaction + + - name: Run test suite + run: vendor/bin/phpunit --coverage-text --coverage-clover=coverage.xml + + - name: Code Climate Test Reporter + if: ${{ env.CC_TEST_REPORTER_ID != '' }} + run: | + cp coverage.xml clover.xml + bash <(curl -s https://codecov.io/bash) + ./cc-test-reporter after-build --coverage-input-type clover --exit-code 0 + env: + CC_TEST_REPORTER_ID: ${{ secrets.CC_TEST_REPORTER_ID }} diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml new file mode 100644 index 0000000..9066965 --- /dev/null +++ b/.github/workflows/tests.yml @@ -0,0 +1,49 @@ +name: Tests + +on: + push: + branches: [ '**' ] + pull_request: + branches: [ '**' ] + +jobs: + test: + runs-on: ubuntu-latest + + strategy: + fail-fast: false + matrix: + php: ['5.5', '5.6', '7.0', '7.1', '7.2', '7.3', '7.4', '8.0', '8.1'] + setup: ['lowest', 'stable'] + + name: PHP ${{ matrix.php }} - ${{ matrix.setup }} + + steps: + - uses: actions/checkout@v2 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + tools: composer:v2 + + - name: Cache Composer packages + id: composer-cache + uses: actions/cache@v2 + with: + path: vendor + key: ${{ runner.os }}-${{ matrix.setup }}-v2-php-${{ matrix.php }}-${{ hashFiles('**/composer.lock') }} + restore-keys: | + ${{ runner.os }}-${{ matrix.setup }}-v2-php-${{ matrix.php }}- + + - name: Install dependencies + if: steps.composer-cache.outputs.cache-hit != 'true' + run: | + ${{ matrix.php >= 7.3 && 'composer require --no-update "pug-php/pug:^2.7.5||^3.0" --no-interaction;' || '' }} + ${{ matrix.php >= 7.4 && 'composer require --no-update "pug-php/pug:^2.7.6||^3.0" --no-interaction;' || '' }} + ${{ matrix.php >= 8 && 'composer require --no-update phpunit/phpunit:^8.5.15 --no-interaction;' || '' }} + ${{ matrix.php >= 8 && 'composer require --no-update natxet/cssmin:^3.0.5 --no-interaction;' || '' }} + composer update --prefer-dist --prefer-${{ matrix.setup }} --no-progress --no-interaction ${{ matrix.php >= 8.1 && '--ignore-platform-req=php' || '' }} + + - name: Run test suite + run: vendor/bin/phpunit --no-coverage --verbose diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 27d6394..0000000 --- a/.travis.yml +++ /dev/null @@ -1,46 +0,0 @@ -language: php - -git: - depth: 5 - -env: - global: - - COMPOSER_FLAGS='' - - CC_TEST_REPORTER_ID=a1d38a352ba09d38aa28f376914385fe2434b0e7baee495ab823adb5e670ebb5 - - COVERAGE='' - -matrix: - include: - - php: nightly - - php: 7.4 - env: COVERAGE=on - - php: 7.3 - - php: 7.2 - - php: 7.1 - - php: 7.0 - - php: 5.6 - - php: 5.5 - dist: trusty - -install: - - if [[ $TRAVIS_PHP_VERSION =~ ^nightly ]]; then COMPOSER_FLAGS='--ignore-platform-reqs'; fi - - travis_retry composer self-update - - travis_retry composer install ${COMPOSER_FLAGS} --no-interaction - -before_script: - - if [[ "$COVERAGE" = "on" ]]; then curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter; fi; - - if [[ "$COVERAGE" = "on" ]]; then chmod +x ./cc-test-reporter; fi; - - if [[ "$COVERAGE" = "on" ]]; then ./cc-test-reporter before-build; fi; - -script: - - vendor/bin/phpunit --coverage-text --coverage-clover=coverage.xml - -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; - -after_success: - - if [[ "$COVERAGE" = "on" ]]; then bash <(curl -s https://codecov.io/bash); fi; - -notifications: - slack: phug:nzXFnxhU14RWK2EQSDL0u08z diff --git a/composer.json b/composer.json index 4d91da4..c238122 100644 --- a/composer.json +++ b/composer.json @@ -5,10 +5,15 @@ "keywords": ["phug", "jade", "pug", "html", "php", "twig", "symfony"], "license": "MIT", "require": { - "phug/compiler": "^0.5.0 || ^1.0.0" + "php": ">=5.5", + "phug/compiler": "^0.5.2 || ^1.0.0", + "phug/formatter": "^0.5.13 || ^1.0.0", + "phug/lexer": "^0.5.4 || ^1.0.0", + "phug/parser": "^0.5.2 || ^1.0.0", + "phug/util": "^0.4.17 || ^1.0.0" }, "require-dev": { - "phpunit/phpunit": "^4.8.36 || ^5.7.27" + "phpunit/phpunit": "^4.8.36 || ^5.7.27 || ^8.5.15" }, "autoload": { "psr-4": { diff --git a/src/Phug/Formatter/Format/TwigXmlFormat.php b/src/Phug/Formatter/Format/TwigXmlFormat.php index 21a2d25..5bfb533 100644 --- a/src/Phug/Formatter/Format/TwigXmlFormat.php +++ b/src/Phug/Formatter/Format/TwigXmlFormat.php @@ -51,7 +51,7 @@ public function __construct(Formatter $formatter = null) ->provideAttributeAssignment() ->provideStandAloneAttributeAssignment() ->provideMergeAttributes() - ->provideArrayEscape() + ->provideTwigArrayEscape() ->provideAttributesAssignment() ->provideClassAttributeAssignment() ->provideStandAloneClassAttributeAssignment() @@ -64,6 +64,18 @@ public function __construct(Formatter $formatter = null) } } + /** + * @codeCoverageIgnore + */ + private function provideTwigArrayEscape() + { + if (!method_exists($this, 'provideArrayEscape')) { + return $this; + } + + return $this->provideArrayEscape(); + } + protected function addAttributeAssignment($name, $handler) { $availableAssignments = $this->getHelper('available_attribute_assignments'); @@ -129,30 +141,7 @@ protected function formatAttributeElement(AttributeElement $element) return ''; } if (strtolower($value->getValue()) === 'true') { - $formattedValue = null; - if ($name instanceof ExpressionElement) { - $bufferVariable = $this->pattern('buffer_variable'); - $name = $this->pattern( - 'php_display_code', - $this->pattern( - 'save_value', - $bufferVariable, - $this->formatCode($name->getValue(), $name->isChecked()) - ) - ); - $value = new ExpressionElement($bufferVariable); - $formattedValue = $this->format($value); - } - $formattedName = $this->format($name); - $formattedValue = $formattedValue || $formattedValue === '0' - ? $formattedValue - : $formattedName; - - return $this->pattern( - 'boolean_attribute_pattern', - $formattedName, - $formattedValue - ); + return $this->formatAttributeFlag($name); } if (in_array(strtolower($value->getValue()), ['false', 'null', 'undefined'])) { return ''; @@ -166,6 +155,36 @@ protected function formatAttributeElement(AttributeElement $element) ); } + protected function formatAttributeFlag($name) + { + $formattedValue = null; + + if ($name instanceof ExpressionElement) { + $bufferVariable = $this->pattern('buffer_variable'); + $name = $this->pattern( + 'php_display_code', + $this->pattern( + 'save_value', + $bufferVariable, + $this->formatCode($name->getValue(), $name->isChecked()) + ) + ); + $value = new ExpressionElement($bufferVariable); + $formattedValue = $this->format($value); + } + + $formattedName = $this->format($name); + $formattedValue = $formattedValue || $formattedValue === '0' + ? $formattedValue + : $formattedName; + + return $this->pattern( + 'boolean_attribute_pattern', + $formattedName, + $formattedValue + ); + } + protected function formatPairTagChildren(MarkupElement $element) { $firstChild = $element->getChildAt(0); diff --git a/tests/PugToTwigTest.php b/tests/PugToTwigTest.php index 158c9b0..0183286 100644 --- a/tests/PugToTwigTest.php +++ b/tests/PugToTwigTest.php @@ -4,8 +4,12 @@ use PHPUnit\Framework\TestCase; use Phug\Compiler; +use Phug\Compiler\Event\NodeEvent; use Phug\Formatter\Element\AssignmentElement; use Phug\Formatter\Element\AttributeElement; +use Phug\Formatter\Element\ExpressionElement; +use Phug\Parser\Node\AttributeNode; +use Phug\Parser\Node\ElementNode; use Phug\TwigExtension; use PugToTwig; @@ -43,6 +47,7 @@ protected static function render($pugCode, $options = []) * @covers \Phug\Formatter\AbstractTwigFormat::formatElementChildren * @covers \Phug\Formatter\AbstractTwigFormat::formatTwigChildElement * @covers \Phug\Formatter\Format\TwigXmlFormat::__construct + * @covers \Phug\Formatter\Format\TwigXmlFormat::provideTwigArrayEscape * @covers \Phug\Formatter\Format\TwigXmlFormat::addAttributeAssignment * @covers \Phug\Formatter\Format\TwigXmlFormat::__invoke * @covers \Phug\Formatter\Format\TwigXmlFormat::isSelfClosingTag @@ -186,6 +191,56 @@ public static function testConvert() ); } + /** + * @covers \Phug\Formatter\Format\TwigXmlFormat::formatAttributeElement + * @covers \Phug\Formatter\Format\TwigXmlFormat::formatAttributeFlag + */ + public static function testFlagAttribute() + { + $html = static::render(implode("\n", [ + 'input&attributes(["checked" => true])', + ])); + + self::assertSame( + '', + $html + ); + + $html = static::render(implode("\n", [ + '- attr = "checked"', + 'input({{ attr }}=true)', + ])); + + self::assertSame( + '{% attr = "checked" %}', + $html + ); + + $html = static::render(implode("\n", [ + '- $var = "checked"', + 'input(dynamic=true)', + ]), [ + 'on_node' => function (NodeEvent $nodeEvent) { + $node = $nodeEvent->getNode(); + if ($node instanceof ElementNode && $node->getName() === 'input') { + /** @var AttributeNode $attribute */ + foreach ($node->getAttributes() as $attribute) { + if ($attribute->getName() === 'dynamic') { + $expression = new ExpressionElement(); + $expression->setValue('$var'); + $attribute->setName($expression); + } + } + } + }, + ]); + + self::assertSame( + '{% $var = "checked" %}', + $html + ); + } + /** * @covers \Phug\Formatter\Format\TwigXmlFormat::isSelfClosingTag * @covers \Phug\Formatter\Format\TwigXmlFormat::isBlockTag