|
1 | 1 | <?php |
2 | 2 |
|
3 | | -namespace Spatie\Permission\Test; |
4 | | - |
5 | | -use ErrorException; |
6 | 3 | 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']); |
205 | 4 |
|
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>'); |
0 commit comments