Skip to content

Commit

Permalink
Polyfill assertMatchesRegularExpression
Browse files Browse the repository at this point in the history
  • Loading branch information
kylekatarnls committed Sep 20, 2023
1 parent a06f598 commit 90b2a40
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
ini-values: memory_limit=-1, zend.enable_gc=0, xdebug.mode=coverage
ini-values: memory_limit=-1, zend.enable_gc=0, xdebug.mode=coverage,develop
tools: composer:v2
extensions: pcntl
coverage: xdebug
Expand Down
11 changes: 11 additions & 0 deletions src/Phug/Util/CompatibilityUtil/TestCaseTyped.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,16 @@ protected function tearDown(): void
{
$this->finishTest();
}

public static function assertMatchesRegularExpression(string $pattern, string $string, string $message = ''): void
{
if (!method_exists(parent::class, 'assertMatchesRegularExpression')) {
self::assertRegExp($pattern, $string, $message);

return;
}

parent::assertMatchesRegularExpression($pattern, $string, $message);
}
}
// @codeCoverageIgnoreEnd
11 changes: 11 additions & 0 deletions src/Phug/Util/CompatibilityUtil/TestCaseUntyped.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,16 @@ protected function tearDown()
{
$this->finishTest();
}

public static function assertMatchesRegularExpression($pattern, $string, $message = '')
{
if (!method_exists(parent::class, 'assertMatchesRegularExpression')) {
self::assertRegExp($pattern, $string, $message);

return;
}

parent::assertMatchesRegularExpression($pattern, $string, $message);
}
}
// @codeCoverageIgnoreEnd
2 changes: 1 addition & 1 deletion tests/Phug/CliTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ public function testCacheDirectory()

$this->removeFile($expected);

self::assertRegExp('/Inconsistent indentation./', $text);
self::assertMatchesRegularExpression('/Inconsistent indentation./', $text);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/Phug/Element/MixinCallElementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ public function testPhpUnwrap()
eval('?>'.$php);
ob_get_clean();

self::assertRegExp('/echo\s1;\s+\}/', $php);
self::assertMatchesRegularExpression('/echo\s1;\s+\}/', $php);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/Phug/FormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ public function testFormat()
$exp = new ExpressionElement('foreach ($tabs as $key => $tab)');
$code = $formatter->format($exp);
self::assertRegExp('/as\s\$key\s=>\s\$tab/', $code);
self::assertMatchesRegularExpression('/as\s\$key\s=>\s\$tab/', $code);
}
/**
Expand Down
5 changes: 5 additions & 0 deletions tests/Phug/Lexer/Scanner/AssignmentScannerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ public function testScan()
self::assertSame('test', $tok->getName());
}

/**
* @coversNothing
*/
public function testScanWithAttributes()
{
/** @var AssignmentToken $tok */
Expand All @@ -42,6 +45,8 @@ public function testScanWithAttributes()

/**
* @see https://github.com/phug-php/phug/issues/22
*
* @coversNothing
*/
public function testObjectInTernary()
{
Expand Down
49 changes: 28 additions & 21 deletions tests/Phug/ProfilerModuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ public function testRenderProfiler()
]);
$render = $renderer->render('div');

self::assertRegExp('/div lexing\s*<br>\s*[\.\d]+[µm]?s/', $render);
self::assertMatchesRegularExpression('/div lexing\s*<br>\s*[\.\d]+[µm]?s/', $render);
self::assertStringContains('title="div lexing:', $render);
self::assertRegExp('/div parsing\s*<br>\s*[\.\d]+[µm]?s/', $render);
self::assertMatchesRegularExpression('/div parsing\s*<br>\s*[\.\d]+[µm]?s/', $render);
self::assertStringContains('title="div parsing:', $render);
self::assertRegExp('/div compiling\s*<br>\s*[\.\d]+[µm]?s/', $render);
self::assertMatchesRegularExpression('/div compiling\s*<br>\s*[\.\d]+[µm]?s/', $render);
self::assertStringContains('title="div compiling:', $render);
self::assertRegExp('/div formatting\s*<br>\s*[\.\d]+[µm]?s/', $render);
self::assertMatchesRegularExpression('/div formatting\s*<br>\s*[\.\d]+[µm]?s/', $render);
self::assertStringContains('title="div formatting:', $render);
self::assertRegExp('/div rendering\s*<br>\s*[\.\d]+[µm]?s/', $render);
self::assertMatchesRegularExpression('/div rendering\s*<br>\s*[\.\d]+[µm]?s/', $render);
self::assertStringContains('title="div rendering:', $render);

$renderer = new Renderer([
Expand All @@ -76,9 +76,9 @@ public function testRenderProfiler()
]);
$render = $renderer->render("mixin foo\n p&attributes(\$attributes)\n | Hello\n+foo(a='b')");

self::assertRegExp('/\+foo\s+parsing\s*<br>\s*[\.\d]+µs/', $render);
self::assertRegExp('/text\s+parsing\s*<br>\s*[\.\d]+µs/', $render);
self::assertRegExp('/mixin\s+foo\s+parsing\s*<br>\s*[\.\d]+µs/', $render);
self::assertMatchesRegularExpression('/\+foo\s+parsing\s*<br>\s*[\.\d]+µs/', $render);
self::assertMatchesRegularExpression('/text\s+parsing\s*<br>\s*[\.\d]+µs/', $render);
self::assertMatchesRegularExpression('/mixin\s+foo\s+parsing\s*<br>\s*[\.\d]+µs/', $render);

$renderer->reInitOptions([
'debug' => false,
Expand Down Expand Up @@ -117,15 +117,15 @@ public function testLogProfiler()
$count = count($profiler->getEvents());
$profiler->recordDisplayEvent(1);
self::assertCount($count, $profiler->getEvents());
self::assertRegExp('/div lexing\s*<br>\s*[\.\d]+[µm]?s/', $render);
self::assertMatchesRegularExpression('/div lexing\s*<br>\s*[\.\d]+[µm]?s/', $render);
self::assertStringContains('title="div lexing:', $render);
self::assertRegExp('/div parsing\s*<br>\s*[\.\d]+[µm]?s/', $render);
self::assertMatchesRegularExpression('/div parsing\s*<br>\s*[\.\d]+[µm]?s/', $render);
self::assertStringContains('title="div parsing:', $render);
self::assertRegExp('/div compiling\s*<br>\s*[\.\d]+[µm]?s/', $render);
self::assertMatchesRegularExpression('/div compiling\s*<br>\s*[\.\d]+[µm]?s/', $render);
self::assertStringContains('title="div compiling:', $render);
self::assertRegExp('/div formatting\s*<br>\s*[\.\d]+[µm]?s/', $render);
self::assertMatchesRegularExpression('/div formatting\s*<br>\s*[\.\d]+[µm]?s/', $render);
self::assertStringContains('title="div formatting:', $render);
self::assertRegExp('/div rendering\s*<br>\s*[\.\d]+[µm]?s/', $render);
self::assertMatchesRegularExpression('/div rendering\s*<br>\s*[\.\d]+[µm]?s/', $render);
self::assertStringContains('title="div rendering:', $render);
}

Expand Down Expand Up @@ -341,15 +341,15 @@ public function testDisplayProfiler()
$contents = ob_get_contents();
ob_end_clean();

self::assertRegExp('/div lexing\s*<br>\s*[\.\d]+[µm]?s/', $contents);
self::assertMatchesRegularExpression('/div lexing\s*<br>\s*[\.\d]+[µm]?s/', $contents);
self::assertStringContains('title="div lexing:', $contents);
self::assertRegExp('/div parsing\s*<br>\s*[\.\d]+[µm]?s/', $contents);
self::assertMatchesRegularExpression('/div parsing\s*<br>\s*[\.\d]+[µm]?s/', $contents);
self::assertStringContains('title="div parsing:', $contents);
self::assertRegExp('/div compiling\s*<br>\s*[\.\d]+[µm]?s/', $contents);
self::assertMatchesRegularExpression('/div compiling\s*<br>\s*[\.\d]+[µm]?s/', $contents);
self::assertStringContains('title="div compiling:', $contents);
self::assertRegExp('/div formatting\s*<br>\s*[+-]?[\.\d]+[µm]?s/', $contents);
self::assertMatchesRegularExpression('/div formatting\s*<br>\s*[+-]?[\.\d]+[µm]?s/', $contents);
self::assertStringContains('title="div formatting:', $contents);
self::assertRegExp('/div rendering\s*<br>\s*[\.\d]+[µm]?s/', $contents);
self::assertMatchesRegularExpression('/div rendering\s*<br>\s*[\.\d]+[µm]?s/', $contents);
self::assertStringContains('title="div rendering:', $contents);
self::assertStringContains('-void-dump-', $contents);
}
Expand Down Expand Up @@ -430,7 +430,14 @@ public function testCustomDump()
*/
public function testEventVarDump()
{
if (!function_exists('xdebug_is_enabled') || !xdebug_is_enabled()) {
$xdebugMode = getenv('XDEBUG_MODE') ?: ini_get('xdebug.mode');
$xdebugModes = array_map('trim', explode(',', implode(',', (array) $xdebugMode)));
$xdebugSupported = (
(function_exists('xdebug_is_enabled') && xdebug_is_enabled()) ||
in_array('develop', $xdebugModes, true)
);

if (!$xdebugSupported) {
self::markTestSkipped('var_dump test needs XDebug to be enabled.');

return;
Expand All @@ -457,7 +464,7 @@ public function testEventVarDump()

$render = $renderer->render('div');

self::assertRegExp('/class\\s+Phug\\\\Parser\\\\Node\\\\DocumentNode#\\d+\\s+\\(\\d+\\)\\s+\\{/', $render);
self::assertMatchesRegularExpression('/class\\s+Phug\\\\Parser\\\\Node\\\\DocumentNode#\\d+\\s+\\(\\d+\\)\\s+\\{/', $render);
}

/**
Expand Down Expand Up @@ -516,7 +523,7 @@ public function testGetErrorAsHtml()
$renderer = new Renderer();
$error = $getErrorAsHtml->invoke($renderer, (object) [], [], []);

self::assertRegExp('/<pre>Call to undefined method .+::getFile\(\)[\s\S]+<\/pre>/', $error);
self::assertMatchesRegularExpression('/<pre>Call to undefined method .+::getFile\(\)[\s\S]+<\/pre>/', $error);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/Phug/RendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1067,7 +1067,7 @@ public function testWhiteSpace()
'| me!',
])));

self::assertRegExp('/^Don\'t\s+<button id="self-destruct">touch<\/button>\s+me!$/', $html);
self::assertMatchesRegularExpression('/^Don\'t\s+<button id="self-destruct">touch<\/button>\s+me!$/', $html);
}

/**
Expand Down

0 comments on commit 90b2a40

Please sign in to comment.