Skip to content

Commit ffdaee3

Browse files
authored
Refactor tests to Pest (#57)
1 parent d44169d commit ffdaee3

File tree

5 files changed

+123
-228
lines changed

5 files changed

+123
-228
lines changed

.github/workflows/run-tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,4 @@ jobs:
5454
composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction --no-suggest
5555
5656
- name: Execute tests
57-
run: vendor/bin/phpunit
57+
run: vendor/bin/pest

composer.json

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
},
3333
"require-dev": {
3434
"phpunit/phpunit": "^8.5.22|^9.4",
35-
"orchestra/testbench": "^4.0|^5.0|^6.0|^7.0"
35+
"orchestra/testbench": "^4.0|^5.0|^6.0|^7.0",
36+
"pestphp/pest-plugin-laravel": "^1.3"
3637
},
3738
"autoload": {
3839
"psr-4": {
@@ -41,11 +42,11 @@
4142
},
4243
"autoload-dev": {
4344
"psr-4": {
44-
"Spatie\\BladeJavaScript\\Test\\": "tests"
45+
"Spatie\\BladeJavaScript\\Tests\\": "tests"
4546
}
4647
},
4748
"scripts": {
48-
"test": "vendor/bin/phpunit"
49+
"test": "vendor/bin/pest"
4950
},
5051
"extra": {
5152
"laravel": {
@@ -55,5 +56,10 @@
5556
}
5657
},
5758
"minimum-stability": "dev",
58-
"prefer-stable": true
59-
}
59+
"prefer-stable": true,
60+
"config": {
61+
"allow-plugins": {
62+
"pestphp/pest-plugin": true
63+
}
64+
}
65+
}

tests/BladeTest.php

Lines changed: 98 additions & 207 deletions
Original file line numberDiff line numberDiff line change
@@ -1,211 +1,102 @@
11
<?php
22

3-
namespace Spatie\Permission\Test;
4-
5-
use ErrorException;
63
use Illuminate\Contracts\Support\Arrayable;
7-
use JsonSerializable;
8-
use Spatie\BladeJavaScript\Test\TestCase;
9-
10-
class BladeTest extends TestCase
11-
{
12-
/** @test */
13-
public function it_can_render_a_key_value_pair()
14-
{
15-
$this->assertEquals(
16-
'<script>window[\'js\'] = window[\'js\'] || {};window[\'js\'][\'key\'] = \'value\';</script>',
17-
$this->renderView('keyValue')
18-
);
19-
}
20-
21-
/** @test */
22-
public function it_can_render_an_array()
23-
{
24-
$parameter = ['key' => 'value'];
25-
26-
$this->assertEquals(
27-
'<script>window[\'js\'] = window[\'js\'] || {};window[\'js\'][\'key\'] = \'value\';</script>',
28-
$this->renderView('variable', compact('parameter'))
29-
);
30-
}
31-
32-
/** @test */
33-
public function it_can_render_a_boolean()
34-
{
35-
$parameter = ['boolean' => true];
36-
37-
$this->assertEquals(
38-
'<script>window[\'js\'] = window[\'js\'] || {};window[\'js\'][\'boolean\'] = true;</script>',
39-
$this->renderView('variable', compact('parameter'))
40-
);
41-
42-
$parameter = ['boolean' => false];
43-
44-
$this->assertEquals(
45-
'<script>window[\'js\'] = window[\'js\'] || {};window[\'js\'][\'boolean\'] = false;</script>',
46-
$this->renderView('variable', compact('parameter'))
47-
);
48-
}
49-
50-
/** @test */
51-
public function it_can_render_an_integer()
52-
{
53-
$parameter = ['number' => 5];
54-
55-
$this->assertEquals(
56-
'<script>window[\'js\'] = window[\'js\'] || {};window[\'js\'][\'number\'] = 5;</script>',
57-
$this->renderView('variable', compact('parameter'))
58-
);
59-
}
60-
61-
/** @test */
62-
public function it_can_render_a_float()
63-
{
64-
$parameter = ['number' => 5.5];
65-
66-
$this->assertEquals(
67-
'<script>window[\'js\'] = window[\'js\'] || {};window[\'js\'][\'number\'] = 5.5;</script>',
68-
$this->renderView('variable', compact('parameter'))
69-
);
70-
}
71-
72-
/** @test */
73-
public function it_can_render_null()
74-
{
75-
$parameter = ['nothing' => null];
76-
77-
$this->assertEquals(
78-
'<script>window[\'js\'] = window[\'js\'] || {};window[\'js\'][\'nothing\'] = null;</script>',
79-
$this->renderView('variable', compact('parameter'))
80-
);
81-
}
82-
83-
/** @test */
84-
public function it_can_render_a_string_with_line_breaks()
85-
{
86-
$parameter = ['string' => "This is\r\n a test"];
87-
88-
$this->assertEquals(
89-
'<script>window[\'js\'] = window[\'js\'] || {};window[\'js\'][\'string\'] = \'This is\\r\\n a test\';</script>',
90-
$this->renderView('variable', compact('parameter'))
91-
);
92-
}
93-
94-
/** @test */
95-
public function it_can_render_a_numeric_string_as_a_string()
96-
{
97-
$parameter = ['socialSecurity' => '123456789'];
98-
99-
$this->assertEquals(
100-
'<script>window[\'js\'] = window[\'js\'] || {};window[\'js\'][\'socialSecurity\'] = \'123456789\';</script>',
101-
$this->renderView('variable', compact('parameter'))
102-
);
103-
}
104-
105-
/** @test */
106-
public function it_escapes_tags_in_a_string()
107-
{
108-
$parameter = ['string' => "This is a <tag>"];
109-
110-
$this->assertEquals(
111-
'<script>window[\'js\'] = window[\'js\'] || {};window[\'js\'][\'string\'] = \'This is a \<tag\>\';</script>',
112-
$this->renderView('variable', compact('parameter'))
113-
);
114-
}
115-
116-
/** @test */
117-
public function it_can_render_arrayable_objects()
118-
{
119-
$parameter = new class () implements Arrayable {
120-
public function toArray()
121-
{
122-
return ['arrayableKey' => 'arrayableValue'];
123-
}
124-
};
125-
126-
$this->assertEquals(
127-
'<script>window[\'js\'] = window[\'js\'] || {};window[\'js\'][\'arrayableKey\'] = \'arrayableValue\';</script>',
128-
$this->renderView('variable', compact('parameter'))
129-
);
130-
}
131-
132-
/** @test */
133-
public function it_can_render_json_serializable_objects()
134-
{
135-
$parameter = new class () implements JsonSerializable {
136-
public function jsonSerialize()
137-
{
138-
return ['jsonKey' => 'jsonValue'];
139-
}
140-
};
141-
142-
$this->assertEquals(
143-
'<script>window[\'js\'] = window[\'js\'] || {};window[\'js\'][\'0\'] = {"jsonKey":"jsonValue"};</script>',
144-
$this->renderView('variable', compact('parameter'))
145-
);
146-
}
147-
148-
/** @test */
149-
public function it_can_render_an_object_that_implements_toJson()
150-
{
151-
$parameter = new class () {
152-
public function toJson()
153-
{
154-
return json_encode(['jsonKey' => 'jsonValue']);
155-
}
156-
};
157-
158-
$this->assertEquals(
159-
'<script>window[\'js\'] = window[\'js\'] || {};window[\'js\'][\'0\'] = {"jsonKey":"jsonValue"};</script>',
160-
$this->renderView('variable', compact('parameter'))
161-
);
162-
}
163-
164-
/** @test */
165-
public function it_can_render_an_object_that_implements_to_string()
166-
{
167-
$parameter = new class () {
168-
public function __toString()
169-
{
170-
return 'string';
171-
}
172-
};
173-
174-
$this->assertEquals(
175-
'<script>window[\'js\'] = window[\'js\'] || {};window[\'js\'][\'0\'] = \'string\';</script>',
176-
$this->renderView('variable', compact('parameter'))
177-
);
178-
}
179-
180-
/** @test */
181-
public function it_can_render_data_without_a_namespace()
182-
{
183-
$this->app['config']->set('blade-javascript.namespace', '');
184-
185-
$this->assertEquals(
186-
'<script>window[\'key\'] = \'value\';</script>',
187-
$this->renderView('keyValue')
188-
);
189-
}
190-
191-
/** @test */
192-
public function it_cannot_translate_resources_to_javascript()
193-
{
194-
$resource = fopen(__FILE__, 'r');
195-
196-
$this->expectException(ErrorException::class);
197-
198-
$this->renderView('variable', ['parameter' => $resource]);
199-
}
200-
201-
/** @test */
202-
public function it_can_render_a_customized_view()
203-
{
204-
$this->app['view']->replaceNamespace('bladeJavaScript', [__DIR__.'/resources/views/override']);
2054

206-
$this->assertEquals(
207-
'<script type="application/javascript">window[\'js\'] = window[\'js\'] || {};window[\'js\'][\'key\'] = \'value\';</script>',
208-
$this->renderView('keyValue')
209-
);
210-
}
211-
}
5+
it('can render a key-value pair')
6+
->expect(fn () => renderView('keyValue'))
7+
->toEqual('<script>window[\'js\'] = window[\'js\'] || {};window[\'js\'][\'key\'] = \'value\';</script>');
8+
9+
it('can render', function (mixed $parameter, string $expected) {
10+
expect(
11+
renderView('variable', compact('parameter'))
12+
)->toEqual($expected);
13+
})
14+
->with([
15+
'an array' => [
16+
'parameter' => fn () => ['key' => 'value'],
17+
'expected' => '<script>window[\'js\'] = window[\'js\'] || {};window[\'js\'][\'key\'] = \'value\';</script>',
18+
],
19+
'a boolean with value of `true`' => [
20+
'parameter' => fn () => ['boolean' => true],
21+
'expected' => '<script>window[\'js\'] = window[\'js\'] || {};window[\'js\'][\'boolean\'] = true;</script>',
22+
],
23+
'a boolean with value of `false`' => [
24+
'parameter' => fn () => ['boolean' => false],
25+
'expected' => '<script>window[\'js\'] = window[\'js\'] || {};window[\'js\'][\'boolean\'] = false;</script>',
26+
],
27+
'an integer' => [
28+
'parameter' => fn () => ['number' => 5],
29+
'expected' => '<script>window[\'js\'] = window[\'js\'] || {};window[\'js\'][\'number\'] = 5;</script>',
30+
],
31+
'an float' => [
32+
'parameter' => fn () => ['number' => 5.5],
33+
'expected' => '<script>window[\'js\'] = window[\'js\'] || {};window[\'js\'][\'number\'] = 5.5;</script>',
34+
],
35+
'null' => [
36+
'parameter' => fn () => ['nothing' => null],
37+
'expected' => '<script>window[\'js\'] = window[\'js\'] || {};window[\'js\'][\'nothing\'] = null;</script>',
38+
],
39+
'a string with line breaks' => [
40+
'parameter' => fn () => ['string' => "This is\r\n a test"],
41+
'expected' => '<script>window[\'js\'] = window[\'js\'] || {};window[\'js\'][\'string\'] = \'This is\\r\\n a test\';</script>',
42+
],
43+
'a numeric string as a string' => [
44+
'parameter' => fn () => ['socialSecurity' => '123456789'],
45+
'expected' => '<script>window[\'js\'] = window[\'js\'] || {};window[\'js\'][\'socialSecurity\'] = \'123456789\';</script>',
46+
],
47+
'escapes tags in a string' => [
48+
'parameter' => fn () => ['string' => "This is a <tag>"],
49+
'expected' => '<script>window[\'js\'] = window[\'js\'] || {};window[\'js\'][\'string\'] = \'This is a \<tag\>\';</script>',
50+
],
51+
'arrayable objects' => [
52+
'parameter' => fn () => new class () implements Arrayable {
53+
public function toArray()
54+
{
55+
return ['arrayableKey' => 'arrayableValue'];
56+
}
57+
},
58+
'expected' => '<script>window[\'js\'] = window[\'js\'] || {};window[\'js\'][\'arrayableKey\'] = \'arrayableValue\';</script>',
59+
],
60+
'JSON serializable objects' => [
61+
'parameter' => fn () => new class () implements JsonSerializable {
62+
public function jsonSerialize()
63+
{
64+
return ['jsonKey' => 'jsonValue'];
65+
}
66+
},
67+
'expected' => '<script>window[\'js\'] = window[\'js\'] || {};window[\'js\'][\'0\'] = {"jsonKey":"jsonValue"};</script>',
68+
],
69+
'an object that implements `toJson`' => [
70+
'parameter' => fn () => new class () {
71+
public function toJson()
72+
{
73+
return json_encode(['jsonKey' => 'jsonValue']);
74+
}
75+
},
76+
'expected' => '<script>window[\'js\'] = window[\'js\'] || {};window[\'js\'][\'0\'] = {"jsonKey":"jsonValue"};</script>',
77+
],
78+
'an object that implements `toString`' => [
79+
'parameter' => fn () => new class () {
80+
public function __toString()
81+
{
82+
return 'string';
83+
}
84+
},
85+
'expected' => '<script>window[\'js\'] = window[\'js\'] || {};window[\'js\'][\'0\'] = \'string\';</script>',
86+
],
87+
88+
]);
89+
90+
it('can render data without a namespace')
91+
->tap(fn () => config()->set('blade-javascript.namespace', ''))
92+
->expect(fn () => renderView('keyValue'))
93+
->toEqual('<script>window[\'key\'] = \'value\';</script>');
94+
95+
it('cannot translate resources to Javascript')
96+
->tap(fn () => renderView('variable', ['parameter' => fopen(__FILE__, 'r')]))
97+
->throws(ErrorException::class);
98+
99+
it('can render a customized view')
100+
->tap(fn () => view()->replaceNamespace('bladeJavaScript', [__DIR__ . '/resources/views/override']))
101+
->expect(fn () => renderView('keyValue'))
102+
->toEqual('<script type="application/javascript">window[\'js\'] = window[\'js\'] || {};window[\'js\'][\'key\'] = \'value\';</script>');

tests/Pest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
uses(Spatie\BladeJavaScript\Tests\TestCase::class)->in('.');
4+
5+
function renderView(string $viewName, array $withParameters = []): string
6+
{
7+
return view($viewName)->with($withParameters)->render();
8+
}

0 commit comments

Comments
 (0)