-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractTestCase.php
More file actions
93 lines (77 loc) · 2.52 KB
/
AbstractTestCase.php
File metadata and controls
93 lines (77 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
namespace Tests\Bemto;
use PHPUnit\Framework\TestCase;
use Phug\Renderer;
use PhugBemto\PhugBemto;
use XhtmlFormatter\Formatter;
abstract class AbstractTestCase extends TestCase
{
/**
* @var array
*/
protected $modules = [];
/**
* @var Renderer
*/
protected $renderer;
/**
* @throws \Phug\RendererException
*/
protected function getRenderer(): Renderer
{
if (!isset($this->renderer)) {
$this->renderer = new Renderer([
'debug' => true,
'execution_max_time' => 180000,
'modules' => array_merge([
PhugBemto::class,
], $this->modules),
]);
}
return $this->renderer;
}
protected function renderFile($sourceFile)
{
$error = null;
$actualOutput = null;
try {
$actualOutput = $this->getRenderer()->renderFile($sourceFile);
} catch (\Throwable $exception) {
$error = $exception;
try {
$debugFile = __DIR__ . '/../debug.php';
file_put_contents($debugFile, $this->getRenderer()->compileFile($sourceFile));
include $debugFile;
} catch (\Throwable $exception) {
throw new \Exception('Error in ' . $sourceFile . "\n" . $exception->getMessage(), 0, $exception);
}
}
if ($error) {
throw $error;
}
return $actualOutput;
}
protected static function htmlStandardize($html)
{
$formatter = new Formatter();
$html = trim($formatter->format($html));
$html = str_replace("\r", '', preg_replace('/\s+$/m', '', $html));
$html = preg_replace('/\n{2,}$/', "\n", $html);
$html = preg_replace('/<!--\s*(\S(.*\S)?)\s*-->/U', '<!-- $1 -->', $html);
$html = preg_replace('/(<[a-z][^>]*>)\s+<\//', '$1</', $html);
$html = preg_replace('/([a-z"\'])\/>/', '$1 />', $html);
$html = preg_replace('/(<[a-z][^>]*)\s\/>/', '$1>', $html);
if (version_compare(PHP_VERSION, '8.1.0-dev', '>=')) {
$html = str_replace(''', "'", $html);
}
return $html;
}
protected function assertSameHtml($expectedOutput, $actualOutput, $message = null)
{
$this->assertSame(
static::htmlStandardize($expectedOutput),
static::htmlStandardize($actualOutput),
$message ?: 'HTML should render the same string once formatted.'
);
}
}