1+ <?php
2+
3+ namespace Torchlight \Commonmark \Tests ;
4+
5+ use Illuminate \Support \Facades \Http ;
6+ use Torchlight \Block ;
7+ use Torchlight \Client ;
8+ use Torchlight \Commonmark \TorchlightExtension ;
9+ use League \CommonMark \DocParser ;
10+ use League \CommonMark \Environment ;
11+ use League \CommonMark \HtmlRenderer ;
12+ use Orchestra \Testbench \TestCase ;
13+
14+ class CodeRendererTest extends TestCase
15+ {
16+
17+ protected function getEnvironmentSetUp ($ app )
18+ {
19+ config ()->set ('torchlight.token ' , 'token ' );
20+
21+ $ ids = [
22+ 'block_id_1 ' ,
23+ 'block_id_2 ' ,
24+ 'block_id_3 ' ,
25+ ];
26+
27+ Block::$ generateIdsUsing = function () use (&$ ids ) {
28+ return array_shift ($ ids );
29+ };
30+ }
31+
32+ protected function render ($ markdown )
33+ {
34+ $ environment = Environment::createCommonMarkEnvironment ();
35+ $ environment ->addExtension (new TorchlightExtension );
36+
37+ $ parser = new DocParser ($ environment );
38+ $ htmlRenderer = new HtmlRenderer ($ environment );
39+
40+ $ document = $ parser ->parse ($ markdown );
41+
42+ return $ htmlRenderer ->renderBlock ($ document );
43+ }
44+
45+ /** @test */
46+ public function it_highlights_code_blocks ()
47+ {
48+ $ markdown = <<<'EOT'
49+ before
50+
51+ ```html
52+ <div>html</div>
53+ ```
54+ after
55+ EOT;
56+
57+ $ response = [
58+ "blocks " => [[
59+ "id " => "block_id_1 " ,
60+ "wrapped " => "<pre><code>highlighted</code></pre> " ,
61+ ]]
62+ ];
63+
64+ Http::fake ([
65+ 'api.torchlight.dev/* ' => Http::response ($ response , 200 ),
66+ ]);
67+
68+ $ html = $ this ->render ($ markdown );
69+
70+ $ expected = <<<EOT
71+ <p>before</p>
72+ <pre><code>highlighted</code></pre>
73+ <p>after</p>
74+
75+ EOT ;
76+
77+ $ this ->assertEquals ($ expected , $ html );
78+ }
79+
80+ /** @test */
81+ public function gets_language_and_contents ()
82+ {
83+ $ markdown = <<<'EOT'
84+ before
85+
86+ ```foobarlang
87+ <div>test</div>
88+ ```
89+ after
90+ EOT;
91+
92+ Http::fake ();
93+
94+ $ this ->render ($ markdown );
95+
96+ Http::assertSent (function ($ request ) {
97+ return $ request ['blocks ' ][0 ]['language ' ] === "foobarlang "
98+ && $ request ['blocks ' ][0 ]['code ' ] === "<div>test</div> " ;
99+
100+ });
101+ }
102+
103+ /** @test */
104+ public function it_sends_one_request_only_and_matches_by_id ()
105+ {
106+ $ markdown = <<<'EOT'
107+ before
108+
109+ ```php
110+ some php
111+ ```
112+
113+ ```ruby
114+ some ruby
115+ ```
116+
117+ ```js
118+ some js
119+ ```
120+ after
121+ EOT;
122+
123+ $ response = [
124+ "blocks " => [[
125+ "id " => "block_id_3 " ,
126+ "wrapped " => "some js " ,
127+ ], [
128+ "id " => "block_id_1 " ,
129+ "wrapped " => "some php " ,
130+ ], [
131+ "id " => "block_id_2 " ,
132+ "wrapped " => "some ruby " ,
133+ ]]
134+ ];
135+
136+ Http::fake ([
137+ 'api.torchlight.dev/* ' => Http::response ($ response , 200 ),
138+ ]);
139+
140+
141+ $ html = $ this ->render ($ markdown );
142+
143+ Http::assertSentCount (1 );
144+
145+ $ expected = <<<EOT
146+ <p>before</p>
147+ some php
148+ some ruby
149+ some js
150+ <p>after</p>
151+
152+ EOT ;
153+
154+ $ this ->assertEquals ($ expected , $ html );
155+ }
156+
157+
158+ }
0 commit comments