-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathWrapperTest.php
More file actions
117 lines (99 loc) · 5.77 KB
/
WrapperTest.php
File metadata and controls
117 lines (99 loc) · 5.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
use Livewire\Blaze\Support\Utils;
use Livewire\Blaze\Compiler\Wrapper;
use Illuminate\Support\Facades\Blade;
use Livewire\Blaze\BladeService;
test('wraps component templates into function definitions', function () {
$path = fixture_path('views/components/input.blade.php');
$source = file_get_contents($path);
$hash = Utils::hash($path);
$wrapped = app(Wrapper::class)->wrap($source, $path, $source);
expect($wrapped)->toEqualCollapsingWhitespace(join('', [
'<?php if (!function_exists(\'_'. $hash .'\')): function _'. $hash .'($__blaze, $__data = [], $__slots = [], $__bound = [], $__this = null) { ',
'$__env = $__blaze->env; ',
'if (($__data[\'attributes\'] ?? null) instanceof \Illuminate\View\ComponentAttributeBag) { $__data = $__data + $__data[\'attributes\']->all(); unset($__data[\'attributes\']); } ',
'$attributes = \Livewire\Blaze\Runtime\BlazeAttributeBag::sanitized($__data, $__bound); ',
'extract($__slots, EXTR_SKIP); unset($__slots); ',
'extract($__data, EXTR_SKIP); unset($__data, $__bound); ',
'ob_start(); ?> ',
'@blaze ',
'<?php $__defaults = [\'type\' => \'text\', \'disabled\' => false]; ',
'$type ??= $attributes[\'type\'] ?? $__defaults[\'type\']; unset($attributes[\'type\']); ',
'$disabled ??= $attributes[\'disabled\'] ?? $__defaults[\'disabled\']; unset($attributes[\'disabled\']); ',
'unset($__defaults); ?> ',
'<input {{ $attributes }} type="{{ $type }}" @if ($disabled) disabled @endif >',
'<?php echo ltrim(ob_get_clean()); } endif; ?>',
]));
});
test('compiles aware props', function () {
$path = fixture_path('views/components/input-aware.blade.php');
$source = file_get_contents($path);
$hash = Utils::hash($path);
$wrapped = app(Wrapper::class)->wrap($source, $path, $source);
expect($wrapped)->toEqualCollapsingWhitespace(join('', [
'<?php if (!function_exists(\'_'. $hash .'\')): function _'. $hash .'($__blaze, $__data = [], $__slots = [], $__bound = [], $__this = null) { ',
'$__env = $__blaze->env; ',
'if (($__data[\'attributes\'] ?? null) instanceof \Illuminate\View\ComponentAttributeBag) { $__data = $__data + $__data[\'attributes\']->all(); unset($__data[\'attributes\']); } ',
'$attributes = \Livewire\Blaze\Runtime\BlazeAttributeBag::sanitized($__data, $__bound); ',
'extract($__slots, EXTR_SKIP); unset($__slots); ',
'extract($__data, EXTR_SKIP); unset($__data, $__bound); ',
'ob_start(); ?> ',
'@blaze ',
'<?php $__awareDefaults = [\'type\' => \'text\']; ',
'$type = $__blaze->getConsumableData(\'type\', $__awareDefaults[\'type\']); unset($attributes[\'type\']); ',
'unset($__awareDefaults); ?> ',
'<?php $__defaults = [\'type\' => \'text\', \'disabled\' => false]; ',
'$type ??= $attributes[\'type\'] ?? $__defaults[\'type\']; unset($attributes[\'type\']); ',
'$disabled ??= $attributes[\'disabled\'] ?? $__defaults[\'disabled\']; unset($attributes[\'disabled\']); ',
'unset($__defaults); ?> ',
'<input {{ $attributes }} type="{{ $type }}" @if ($disabled) disabled @endif >',
'<?php echo ltrim(ob_get_clean()); } endif; ?>',
]));
});
test('extracts props when props are not defined', function () {
expect(app(Wrapper::class)->wrap('<div></div>', ''))->toContain('extract($__data, EXTR_SKIP);');
});
test('wraps in self invoking closure', function ($source) {
expect(app(Wrapper::class)->wrap($source, ''))->toContain(
'$__blazeFn = function () use ($__blaze, $__data, $__slots, $__bound) {',
'if ($__this !== null) { $__blazeFn->call($__this); } else { $__blazeFn(); }',
);
})->with([
'{{ $this->orders }}',
'@entangle(\'name\')',
'@script',
'@assets',
]);
test('injects variables', function ($source, $expected) {
expect(app(Wrapper::class)->wrap('', '', $source))->toContain($expected);
expect(app(Wrapper::class)->wrap($source, '', ''))->toContain($expected);
})->with([
'errors' => ['{{ $errors->has(\'name\') }}', '$errors = $__blaze->errors;'],
'errors directive' => ['<input @error(\'name\') invalid @enderror >', '$errors = $__blaze->errors;'],
'livewire' => ['{{ $__livewire->id }}', '$__livewire = $__env->shared(\'__livewire\');'],
'entangle' => ['<div x-data="{ name: @entangle(\'name\') }"></div>', '$__livewire = $__env->shared(\'__livewire\');'],
'this directive' => ['<script> console.log(@this) </script>', '$__livewire = $__env->shared(\'__livewire\');' . "\n" . '$_instance = $__livewire;'],
'app' => ['{{ $app->name }}', '$app = $__blaze->app;'],
'slot' => ['{{ $slot }}', '$__slots[\'slot\'] ??= new \Illuminate\View\ComponentSlot(\'\');'],
]);
test('injects echo handler', function () {
Blade::stringable((new class {})::class, fn () => 'dummy');
expect(app(Wrapper::class)->wrap('{{ $a }}', ''))->toContain('$__bladeCompiler = app(\'blade.compiler\');');
});
test('hoists use statements to top of output', function ($statement) {
// Replace raw @php blocks for placeholders. This normally happens in BlazeManager before the template gets to the Wrapper
$source = app(BladeService::class)->preStoreUncompiledBlocks($statement);
expect(app(Wrapper::class)->wrap($source, '', $source))->toStartWith("<?php\nuse \App\Models\User");
})->with([
['@use(\'App\Models\User\')'],
['@php use \App\Models\User; @endphp'],
['<?php use \App\Models\User; ?>'],
]);
test('preserves php directives', function () {
$input = '@php /* uncompiled */ @endphp';
expect(app(Wrapper::class)->wrap($input, ''))->toContain($input);
});
test('preserves verbatim directives', function () {
$input = '@verbatim /* uncompiled */ @endverbatim';
expect(app(Wrapper::class)->wrap($input, ''))->toContain($input);
});