1+ <?php
2+ /**
3+ * @author Aaron Francis <aaron@hammerstone.dev|https://twitter.com/aarondfrancis>
4+ */
5+
6+ namespace Hammerstone \Torchlight \Tests ;
7+
8+ use Hammerstone \Torchlight \Middleware \RenderTorchlight ;
9+ use Illuminate \Support \Facades \Http ;
10+ use Illuminate \Support \Facades \Route ;
11+ use Illuminate \Support \Facades \View ;
12+
13+ class MiddlewareTest extends BaseTest
14+ {
15+ public function getEnvironmentSetUp ($ app )
16+ {
17+ config ()->set ('torchlight.blade_components ' , true );
18+ config ()->set ('torchlight.token ' , 'token ' );
19+
20+ }
21+
22+ protected function getView ($ view )
23+ {
24+ Route::get ('/torchlight ' , function () use ($ view ) {
25+ return View::file (__DIR__ . '/Support/ ' . $ view );
26+ })->middleware (RenderTorchlight::class);
27+
28+ return $ this ->call ('GET ' , 'torchlight ' );
29+ }
30+
31+ protected function nullApiResponse ()
32+ {
33+ Http::fake ([
34+ 'api.torchlight.dev/* ' => Http::response (null , 200 ),
35+ ]);
36+ }
37+
38+ protected function legitApiResponse ()
39+ {
40+ $ response = [
41+ "blocks " => [[
42+ "id " => "real_response_id " ,
43+ "classes " => "torchlight " ,
44+ "styles " => "background-color: #292D3E; " ,
45+ "highlighted " => "this is the highlighted response from the server " ,
46+ ]]
47+ ];
48+
49+ Http::fake ([
50+ 'api.torchlight.dev/* ' => Http::response ($ response , 200 ),
51+ ]);
52+ }
53+
54+
55+ /** @test */
56+ public function it_sends_a_simple_request_with_no_response ()
57+ {
58+ $ this ->nullApiResponse ();
59+
60+ $ response = $ this ->getView ('simple-php-hello-world.blade.php ' );
61+
62+ $ this ->assertEquals (
63+ '<code class="" style="">echo "hello world";</code> ' ,
64+ $ response ->content ()
65+ );
66+
67+ Http::assertSent (function ($ request ) {
68+ return $ request ['blocks ' ][0 ] === [
69+ "id " => "real_response_id " ,
70+ "hash " => "e99681f5450cbaf3774adc5eb74d637f " ,
71+ "language " => "php " ,
72+ "theme " => "material-theme-palenight " ,
73+ "code " => "echo \"hello world \"; " ,
74+ ];
75+ });
76+ }
77+
78+ /** @test */
79+ public function it_sends_a_simple_request_with_highlighted_response ()
80+ {
81+ $ this ->legitApiResponse ();
82+
83+ $ response = $ this ->getView ('simple-php-hello-world.blade.php ' );
84+
85+ $ this ->assertEquals (
86+ '<code class="torchlight" style="background-color: #292D3E;">this is the highlighted response from the server</code> ' ,
87+ $ response ->content ()
88+ );
89+ }
90+
91+ /** @test */
92+ public function classes_get_merged ()
93+ {
94+ $ this ->legitApiResponse ();
95+
96+ $ response = $ this ->getView ('simple-php-hello-world-with-classes.blade.php ' );
97+
98+ $ this ->assertEquals (
99+ '<code class="torchlight mt-4" style="background-color: #292D3E;">this is the highlighted response from the server</code> ' ,
100+ $ response ->content ()
101+ );
102+ }
103+
104+ /** @test */
105+ public function attributes_are_preserved ()
106+ {
107+ $ this ->legitApiResponse ();
108+
109+ $ response = $ this ->getView ('simple-php-hello-world-with-attributes.blade.php ' );
110+
111+ $ this ->assertEquals (
112+ '<code class="torchlight" style="background-color: #292D3E;" x-data="{}">this is the highlighted response from the server</code> ' ,
113+ $ response ->content ()
114+ );
115+ }
116+
117+ /** @test */
118+ public function language_can_be_set_via_component ()
119+ {
120+ $ this ->nullApiResponse ();
121+
122+ $ this ->getView ('simple-js-hello-world.blade.php ' );
123+
124+ Http::assertSent (function ($ request ) {
125+ return $ request ['blocks ' ][0 ]['language ' ] === 'javascript ' ;
126+ });
127+ }
128+
129+ /** @test */
130+ public function theme_can_be_set_via_component ()
131+ {
132+ $ this ->nullApiResponse ();
133+
134+ $ this ->getView ('simple-php-hello-world-new-theme.blade.php ' );
135+
136+ Http::assertSent (function ($ request ) {
137+ return $ request ['blocks ' ][0 ]['theme ' ] === 'a new theme ' ;
138+ });
139+ }
140+
141+ /** @test */
142+ public function code_contents_can_be_a_file ()
143+ {
144+ $ this ->withoutExceptionHandling ();
145+ $ this ->nullApiResponse ();
146+
147+ $ this ->getView ('contents-via-file.blade.php ' );
148+
149+ Http::assertSent (function ($ request ) {
150+ return $ request ['blocks ' ][0 ]['code ' ] === rtrim (file_get_contents (base_path ('server.php ' ), '\n ' ));
151+ });
152+ }
153+ }
0 commit comments