Skip to content

Commit 1d99037

Browse files
committed
Blade tests
1 parent 958be2b commit 1d99037

8 files changed

+176
-3
lines changed

src/Blade/CodeComponent.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,25 @@ class CodeComponent extends Component
2525
* @param $language
2626
* @param null $theme
2727
* @param null $contents
28+
* @param null $torchlightId
2829
*/
29-
public function __construct($language, $theme = null, $contents = null)
30+
public function __construct($language, $theme = null, $contents = null, $torchlightId = null)
3031
{
3132
$this->language = $language;
3233
$this->theme = $theme;
3334
$this->contents = $contents;
3435

35-
$this->block = Block::make()->language($this->language)->theme($this->theme);
36+
$this->block = Block::make($torchlightId)->language($this->language)->theme($this->theme);
37+
3638
}
3739

3840
public function capture($contents)
3941
{
4042
$contents = $contents ?: $this->contents;
4143

42-
if (is_file(resource_path($contents))) {
44+
if (is_file($contents)) {
45+
$contents = file_get_contents($contents);
46+
} else if (is_file(resource_path($contents))) {
4347
$contents = file_get_contents(resource_path($contents));
4448
}
4549

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
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 &quot;hello world&quot;;</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+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<x-torchlight-code torchlight-id='tl-id' language='php' :contents='base_path("server.php")'/>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<x-torchlight-code torchlight-id='tl-id' language='javascript'>
2+
console.log('hello world');
3+
</x-torchlight-code>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<x-torchlight-code language='php' theme='a new theme'>
2+
echo "hello world";
3+
</x-torchlight-code>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<x-torchlight-code x-data='{}' torchlight-id='real_response_id' language='php'>
2+
echo "hello world";
3+
</x-torchlight-code>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<x-torchlight-code torchlight-id='real_response_id' language='php' class='mt-4'>
2+
echo "hello world";
3+
</x-torchlight-code>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<x-torchlight-code torchlight-id='real_response_id' language='php'>
2+
echo "hello world";
3+
</x-torchlight-code>

0 commit comments

Comments
 (0)