Skip to content

Commit

Permalink
Switch to GitHub Actions (#4)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
kylekatarnls authored May 2, 2021
1 parent 685145d commit 58dbc3f
Show file tree
Hide file tree
Showing 6 changed files with 217 additions and 73 deletions.
62 changes: 62 additions & 0 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
@@ -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 }}
49 changes: 49 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -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
46 changes: 0 additions & 46 deletions .travis.yml

This file was deleted.

9 changes: 7 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
69 changes: 44 additions & 25 deletions src/Phug/Formatter/Format/TwigXmlFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function __construct(Formatter $formatter = null)
->provideAttributeAssignment()
->provideStandAloneAttributeAssignment()
->provideMergeAttributes()
->provideArrayEscape()
->provideTwigArrayEscape()
->provideAttributesAssignment()
->provideClassAttributeAssignment()
->provideStandAloneClassAttributeAssignment()
Expand All @@ -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');
Expand Down Expand Up @@ -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 '';
Expand All @@ -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);
Expand Down
55 changes: 55 additions & 0 deletions tests/PugToTwigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(
'<input checked="checked" />',
$html
);
$html = static::render(implode("\n", [
'- attr = "checked"',
'input({{ attr }}=true)',
]));
self::assertSame(
'{% attr = "checked" %}<input {{ attr }}="{{ attr }}" />',
$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" %}<input {{ $__value=$var }}="{{ $__value }}" />',
$html
);
}
/**
* @covers \Phug\Formatter\Format\TwigXmlFormat::isSelfClosingTag
* @covers \Phug\Formatter\Format\TwigXmlFormat::isBlockTag
Expand Down

0 comments on commit 58dbc3f

Please sign in to comment.