Skip to content

Commit dc07fd1

Browse files
authored
Fix debugging via Twig (#66)
* Update coverage.yml * Fix indent * Specify coverage file explictly * Upgrade PHPUnit * Add tests * Add test for form failure * Fix debug error line calculation * Fix filename replacement * Add tests * Fix code style
1 parent c6073bf commit dc07fd1

17 files changed

+319
-88
lines changed

.github/workflows/coverage.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ jobs:
4444
continue-on-error: true
4545

4646
- name: Coverage
47-
run: bash <(curl -s https://codecov.io/bash)
48-
env:
49-
PHP_VERSION: ${{ matrix.php }}
47+
uses: codecov/codecov-action@v3
48+
with:
49+
files: ./coverage.xml
50+
token: ${{ secrets.CODECOV_TOKEN }}

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"twig/twig": "^3.5.0"
2121
},
2222
"require-dev": {
23-
"phpunit/phpunit": "^8.5",
23+
"phpunit/phpunit": "^8.5 || ^9.6",
2424
"symfony/symfony": "^6.0",
2525
"monolog/monolog": "^3.2"
2626
},

src/Pug/PugSymfonyEngine.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,10 @@ protected function crawlDirectories(string $srcDir, array &$assetsDirectories, a
9292
continue;
9393
}
9494

95-
if (is_dir($viewDirectory = $srcDir.'/'.$directory.'/Resources/views')) {
96-
if ($baseDir === null) {
97-
$baseDir = $viewDirectory;
98-
}
95+
$viewDirectory = $srcDir.'/'.$directory.'/Resources/views';
96+
97+
if (is_dir($viewDirectory)) {
98+
$baseDir ??= $viewDirectory;
9999

100100
$viewDirectories[] = $srcDir.'/'.$directory.'/Resources/views';
101101
}

src/Pug/Symfony/Traits/HelpersHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ protected function createEngine(array $options): Pug
140140
/** @var Closure|null $transformation */
141141
$transformation = $pug->hasOption('patterns')
142142
? ($pug->getOption('patterns')['transform_expression'] ?? null)
143-
: null;
143+
: null; // @codeCoverageIgnore
144144
$pug->setOptionsRecursive([
145145
'patterns' => [
146146
'transform_expression' => function ($code) use ($transformation) {

src/Pug/Symfony/Traits/Installer.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ protected static function askConfirmation(IOInterface $io, string $message): boo
1818
return !$io->isInteractive() || $io->askConfirmation($message);
1919
}
2020

21+
/**
22+
* @SuppressWarnings(PHPMD.ErrorControlOperator)
23+
*/
2124
protected static function installSymfonyBundle(
2225
IOInterface $io,
2326
string $dir,
@@ -35,7 +38,7 @@ protected static function installSymfonyBundle(
3538
return;
3639
}
3740

38-
if (strpos($contents, $bundleClass) !== false) {
41+
if (str_contains($contents, $bundleClass)) {
3942
$flags |= InstallerInterface::KERNEL_OK;
4043
$io->write('The bundle already exists in config/bundles.php');
4144

@@ -50,7 +53,7 @@ protected static function installSymfonyBundle(
5053
file_put_contents($appFile, $contents),
5154
InstallerInterface::KERNEL_OK,
5255
'Bundle added to config/bundles.php',
53-
'Unable to add the bundle engine in config/bundles.php'
56+
'Unable to add the bundle engine in config/bundles.php',
5457
);
5558
}
5659

src/Pug/Twig/Environment.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ public function setContainer(ContainerInterface $container): void
123123
$this->container = $container;
124124
}
125125

126+
/**
127+
* @SuppressWarnings(PHPMD.DuplicatedArrayKey)
128+
*/
126129
public function compileSource(Source $source): string
127130
{
128131
$path = $source->getPath();
@@ -131,7 +134,7 @@ public function compileSource(Source $source): string
131134
$pug = $this->getRenderer();
132135
$code = $source->getCode();
133136
$php = $pug->compile($code, $path);
134-
$codeFirstLine = $this->isDebug() ? 31 : 25;
137+
$codeFirstLine = $this->isDebug() ? 39 : 28;
135138
$templateLine = 1;
136139
$debugInfo = [$codeFirstLine => $templateLine];
137140
$lines = explode("\n", $php);
@@ -159,28 +162,28 @@ public function compileSource(Source $source): string
159162
$fileName = $this->isDebug() ? 'PugDebugTemplateTemplate' : 'PugTemplateTemplate';
160163
$templateFile = __DIR__."/../../../cache-templates/$fileName.php";
161164
$name = $source->getName();
162-
$className = isset($this->classNames[$name]) ? $this->classNames[$name] : '__Template_'.sha1($path);
165+
$className = $this->classNames[$name] ?? '__Template_'.sha1($path);
166+
$pathExport = var_export($path, true);
163167
$replacements = [
164168
$fileName => $className,
165-
'"{{filename}}"' => var_export($name, true),
169+
"'{{filename}}'" => var_export($name, true),
166170
'{{filename}}' => $name,
167-
'"{{path}}"' => var_export($path, true),
171+
"'{{path}}'" => $pathExport,
168172
'// {{code}}' => "?>$php<?php",
169-
'[/* {{debugInfo}} */]' => var_export($debugInfo, true),
173+
'[/* {{debugInfo}} */]' => var_export(array_reverse($debugInfo, true), true),
170174
];
171175

172176
if ($this->isDebug()) {
173-
$replacements['"{{source}}"'] = var_export($code, true);
177+
$sourceExport = var_export($code, true);
178+
$replacements["'{{source}}'"] = $sourceExport;
174179
$replacements['__internal_1'] = '__internal_'.sha1('1'.$path);
175180
$replacements['__internal_2'] = '__internal_'.sha1('2'.$path);
176181
}
177182

178183
return strtr(file_get_contents($templateFile), $replacements);
179184
}
180185

181-
$html = parent::compileSource($source);
182-
183-
return $html;
186+
return parent::compileSource($source);
184187
}
185188

186189
public function loadTemplate(string $cls, string $name, int $index = null): Template

tests/Pug/AbstractTestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public function setUp(): void
122122
$this->addFormRenderer();
123123
}
124124

125-
protected function addFormRenderer()
125+
protected function addFormRenderer(): void
126126
{
127127
require_once __DIR__.'/TestCsrfTokenManager.php';
128128

tests/Pug/EnvironmentTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class EnvironmentTest extends AbstractTestCase
1111
/**
1212
* @throws RuntimeError
1313
*/
14-
public function testWithoutRoot()
14+
public function testWithoutRoot(): void
1515
{
1616
self::expectException(RuntimeError::class);
1717
self::expectExceptionMessage('Unable to load the "I-surely-does-not-exist" runtime.');
@@ -23,7 +23,7 @@ public function testWithoutRoot()
2323
/**
2424
* @throws RuntimeError
2525
*/
26-
public function testWithRoot()
26+
public function testWithRoot(): void
2727
{
2828
self::expectException(RuntimeError::class);
2929
self::expectExceptionMessage('Unable to load the "I-surely-does-not-exist" runtime.');

tests/Pug/Exceptions/ReservedVariableTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
class ReservedVariableTest extends AbstractTestCase
99
{
10-
public function testConstruct()
10+
public function testConstruct(): void
1111
{
1212
$exception = new ReservedVariable('foobar');
1313

tests/Pug/InstallerTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
class InstallerTest extends AbstractTestCase
1919
{
20-
public function testTestInstallQuickExit()
20+
public function testTestInstallQuickExit(): void
2121
{
2222
$io = new CaptureIO();
2323
$io->setInteractive(false);
@@ -35,7 +35,7 @@ public function testTestInstallQuickExit()
3535
self::assertSame(['Not inside a composer vendor directory, setup skipped.'], $io->getLastOutput());
3636
}
3737

38-
public function testTestInstall()
38+
public function testTestInstall(): void
3939
{
4040
$projectDir = sys_get_temp_dir().'/pug-symfony-'.mt_rand(0, 9999999);
4141
$fs = new Filesystem();
@@ -72,7 +72,7 @@ public function testTestInstall()
7272

7373
self::assertTrue(PugSymfonyEngine::install(new Event('update', new Composer(), $io), $projectDir));
7474
self::assertSame(['Sorry, config/bundles.php has a format we can\'t handle automatically.'], $io->getLastOutput());
75-
self::assertFileNotExists(__DIR__.'/../../installed');
75+
self::assertFileDoesNotExist(__DIR__.'/../../installed');
7676

7777
file_put_contents("$projectDir/config/bundles.php", file_get_contents(__DIR__.'/../project-s5/config/bundles-before.php'));
7878
$io->reset();

tests/Pug/LogoutUrlHelper.php

Lines changed: 0 additions & 9 deletions
This file was deleted.

tests/Pug/PugSymfonyBundle/Command/AssetsPublishCommandTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ public function testCommand()
4444
file_put_contents($customHelperFile, $customHelper);
4545
file_put_contents($stylePhpFile, $stylePhp);
4646

47-
$this->assertStringContainsString('16 templates cached', $output, 'All templates can be cached except filter.pug as the upper filter does not exists.');
47+
$this->assertStringContainsString('17 templates cached', $output, 'All templates can be cached except filter.pug as the upper filter does not exists.');
4848
$this->assertStringContainsString('1 templates failed to be cached', $output, 'filter.pug fails as the upper filter does not exists.');
49-
$this->assertRegExp('/(Unknown\sfilter\supper|upper:\sFilter\sdoes\s?n[\'o]t\sexists)/', $output, 'filter.pug fails as the upper filter does not exists.');
49+
$this->assertMatchesRegularExpression('/(Unknown\sfilter\supper|upper:\sFilter\sdoes\s?n[\'o]t\sexists)/', $output, 'filter.pug fails as the upper filter does not exists.');
5050
$this->assertStringContainsString('filter.pug', $output, 'filter.pug fails as the upper filter does not exists.');
5151
}
5252
}

0 commit comments

Comments
 (0)