Skip to content

Commit

Permalink
Fix debugging via Twig (#66)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
kylekatarnls authored Mar 5, 2023
1 parent c6073bf commit dc07fd1
Show file tree
Hide file tree
Showing 17 changed files with 319 additions and 88 deletions.
7 changes: 4 additions & 3 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ jobs:
continue-on-error: true

- name: Coverage
run: bash <(curl -s https://codecov.io/bash)
env:
PHP_VERSION: ${{ matrix.php }}
uses: codecov/codecov-action@v3
with:
files: ./coverage.xml
token: ${{ secrets.CODECOV_TOKEN }}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"twig/twig": "^3.5.0"
},
"require-dev": {
"phpunit/phpunit": "^8.5",
"phpunit/phpunit": "^8.5 || ^9.6",
"symfony/symfony": "^6.0",
"monolog/monolog": "^3.2"
},
Expand Down
8 changes: 4 additions & 4 deletions src/Pug/PugSymfonyEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ protected function crawlDirectories(string $srcDir, array &$assetsDirectories, a
continue;
}

if (is_dir($viewDirectory = $srcDir.'/'.$directory.'/Resources/views')) {
if ($baseDir === null) {
$baseDir = $viewDirectory;
}
$viewDirectory = $srcDir.'/'.$directory.'/Resources/views';

if (is_dir($viewDirectory)) {
$baseDir ??= $viewDirectory;

$viewDirectories[] = $srcDir.'/'.$directory.'/Resources/views';
}
Expand Down
2 changes: 1 addition & 1 deletion src/Pug/Symfony/Traits/HelpersHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ protected function createEngine(array $options): Pug
/** @var Closure|null $transformation */
$transformation = $pug->hasOption('patterns')
? ($pug->getOption('patterns')['transform_expression'] ?? null)
: null;
: null; // @codeCoverageIgnore
$pug->setOptionsRecursive([
'patterns' => [
'transform_expression' => function ($code) use ($transformation) {
Expand Down
7 changes: 5 additions & 2 deletions src/Pug/Symfony/Traits/Installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ protected static function askConfirmation(IOInterface $io, string $message): boo
return !$io->isInteractive() || $io->askConfirmation($message);
}

/**
* @SuppressWarnings(PHPMD.ErrorControlOperator)
*/
protected static function installSymfonyBundle(
IOInterface $io,
string $dir,
Expand All @@ -35,7 +38,7 @@ protected static function installSymfonyBundle(
return;
}

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

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

Expand Down
21 changes: 12 additions & 9 deletions src/Pug/Twig/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ public function setContainer(ContainerInterface $container): void
$this->container = $container;
}

/**
* @SuppressWarnings(PHPMD.DuplicatedArrayKey)
*/
public function compileSource(Source $source): string
{
$path = $source->getPath();
Expand All @@ -131,7 +134,7 @@ public function compileSource(Source $source): string
$pug = $this->getRenderer();
$code = $source->getCode();
$php = $pug->compile($code, $path);
$codeFirstLine = $this->isDebug() ? 31 : 25;
$codeFirstLine = $this->isDebug() ? 39 : 28;
$templateLine = 1;
$debugInfo = [$codeFirstLine => $templateLine];
$lines = explode("\n", $php);
Expand Down Expand Up @@ -159,28 +162,28 @@ public function compileSource(Source $source): string
$fileName = $this->isDebug() ? 'PugDebugTemplateTemplate' : 'PugTemplateTemplate';
$templateFile = __DIR__."/../../../cache-templates/$fileName.php";
$name = $source->getName();
$className = isset($this->classNames[$name]) ? $this->classNames[$name] : '__Template_'.sha1($path);
$className = $this->classNames[$name] ?? '__Template_'.sha1($path);
$pathExport = var_export($path, true);
$replacements = [
$fileName => $className,
'"{{filename}}"' => var_export($name, true),
"'{{filename}}'" => var_export($name, true),
'{{filename}}' => $name,
'"{{path}}"' => var_export($path, true),
"'{{path}}'" => $pathExport,
'// {{code}}' => "?>$php<?php",
'[/* {{debugInfo}} */]' => var_export($debugInfo, true),
'[/* {{debugInfo}} */]' => var_export(array_reverse($debugInfo, true), true),
];

if ($this->isDebug()) {
$replacements['"{{source}}"'] = var_export($code, true);
$sourceExport = var_export($code, true);
$replacements["'{{source}}'"] = $sourceExport;
$replacements['__internal_1'] = '__internal_'.sha1('1'.$path);
$replacements['__internal_2'] = '__internal_'.sha1('2'.$path);
}

return strtr(file_get_contents($templateFile), $replacements);
}

$html = parent::compileSource($source);

return $html;
return parent::compileSource($source);
}

public function loadTemplate(string $cls, string $name, int $index = null): Template
Expand Down
2 changes: 1 addition & 1 deletion tests/Pug/AbstractTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public function setUp(): void
$this->addFormRenderer();
}

protected function addFormRenderer()
protected function addFormRenderer(): void
{
require_once __DIR__.'/TestCsrfTokenManager.php';

Expand Down
4 changes: 2 additions & 2 deletions tests/Pug/EnvironmentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class EnvironmentTest extends AbstractTestCase
/**
* @throws RuntimeError
*/
public function testWithoutRoot()
public function testWithoutRoot(): void
{
self::expectException(RuntimeError::class);
self::expectExceptionMessage('Unable to load the "I-surely-does-not-exist" runtime.');
Expand All @@ -23,7 +23,7 @@ public function testWithoutRoot()
/**
* @throws RuntimeError
*/
public function testWithRoot()
public function testWithRoot(): void
{
self::expectException(RuntimeError::class);
self::expectExceptionMessage('Unable to load the "I-surely-does-not-exist" runtime.');
Expand Down
2 changes: 1 addition & 1 deletion tests/Pug/Exceptions/ReservedVariableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class ReservedVariableTest extends AbstractTestCase
{
public function testConstruct()
public function testConstruct(): void
{
$exception = new ReservedVariable('foobar');

Expand Down
6 changes: 3 additions & 3 deletions tests/Pug/InstallerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

class InstallerTest extends AbstractTestCase
{
public function testTestInstallQuickExit()
public function testTestInstallQuickExit(): void
{
$io = new CaptureIO();
$io->setInteractive(false);
Expand All @@ -35,7 +35,7 @@ public function testTestInstallQuickExit()
self::assertSame(['Not inside a composer vendor directory, setup skipped.'], $io->getLastOutput());
}

public function testTestInstall()
public function testTestInstall(): void
{
$projectDir = sys_get_temp_dir().'/pug-symfony-'.mt_rand(0, 9999999);
$fs = new Filesystem();
Expand Down Expand Up @@ -72,7 +72,7 @@ public function testTestInstall()

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

file_put_contents("$projectDir/config/bundles.php", file_get_contents(__DIR__.'/../project-s5/config/bundles-before.php'));
$io->reset();
Expand Down
9 changes: 0 additions & 9 deletions tests/Pug/LogoutUrlHelper.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ public function testCommand()
file_put_contents($customHelperFile, $customHelper);
file_put_contents($stylePhpFile, $stylePhp);

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

0 comments on commit dc07fd1

Please sign in to comment.