-
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathCodeBlockViewModelUnitTest.php
More file actions
97 lines (75 loc) · 3.28 KB
/
Copy pathCodeBlockViewModelUnitTest.php
File metadata and controls
97 lines (75 loc) · 3.28 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
94
95
96
97
<?php
declare(strict_types=1);
namespace Hyde\Framework\Testing\Unit;
use Hyde\Markdown\Extensions\CodeBlockViewModel;
use Hyde\Testing\UnitTestCase;
use Hyde\Testing\UsesRealBladeInUnitTests;
use Illuminate\Support\HtmlString;
/**
* @see \Hyde\Framework\Testing\Feature\CodeBlocksTest
*/
#[\PHPUnit\Framework\Attributes\CoversClass(\Hyde\Markdown\Extensions\CodeBlockViewModel::class)]
class CodeBlockViewModelUnitTest extends UnitTestCase
{
use UsesRealBladeInUnitTests;
protected static bool $needsKernel = true;
protected function setUp(): void
{
$this->createRealBladeCompilerEnvironment();
}
public function testCanConstructWithOnlyContents()
{
$model = new CodeBlockViewModel('<pre><code>Hello</code></pre>');
$this->assertSame('<pre><code>Hello</code></pre>', $model->contents);
$this->assertNull($model->language);
$this->assertNull($model->label);
}
public function testCanConstructWithAllArguments()
{
$model = new CodeBlockViewModel('<pre><code>Hello</code></pre>', 'php', 'foo.php');
$this->assertSame('php', $model->language);
$this->assertSame('foo.php', $model->label);
}
public function testLabelCanBeAnHtmlString()
{
$label = new HtmlString('<a href="#">foo.php</a>');
$this->assertSame($label, (new CodeBlockViewModel('<pre><code>Hello</code></pre>', label: $label))->label);
}
public function testRenderReturnsCodeBlockComponent()
{
$html = (new CodeBlockViewModel('<pre><code>Hello</code></pre>'))->render();
$this->assertStringContainsString('<div class="hyde-code-block ', $html);
$this->assertStringContainsString('<pre><code>Hello</code></pre>', $html);
}
public function testRenderDoesNotEscapeContents()
{
$this->assertStringNotContainsString('<pre>',
(new CodeBlockViewModel('<pre><code>Hello</code></pre>'))->render()
);
}
public function testRenderOmitsTheLabelWhenNoneIsSet()
{
$this->assertStringNotContainsString('hyde-code-block-label',
(new CodeBlockViewModel('<pre><code>Hello</code></pre>'))->render()
);
}
public function testRenderIncludesTheLabelWhenOneIsSet()
{
$html = (new CodeBlockViewModel('<pre><code>Hello</code></pre>', label: 'foo.php'))->render();
$this->assertStringContainsString('hyde-code-block-label', $html);
$this->assertStringContainsString('<figure class="hyde-code-block ', $html);
$this->assertStringContainsString('<figcaption class="hyde-code-block-label ', $html);
$this->assertStringContainsString('>foo.php</figcaption>', $html);
}
public function testRenderEscapesAStringLabel()
{
$html = (new CodeBlockViewModel('<pre><code>Hello</code></pre>', label: '<script>alert(1)</script>'))->render();
$this->assertStringNotContainsString('<script>', $html);
$this->assertStringContainsString('<script>alert(1)</script>', $html);
}
public function testRenderDoesNotEscapeAnHtmlStringLabel()
{
$html = (new CodeBlockViewModel('<pre><code>Hello</code></pre>', label: new HtmlString('<a href="#">foo.php</a>')))->render();
$this->assertStringContainsString('<a href="#">foo.php</a>', $html);
}
}