Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ jobs:
matrix:
os: [ubuntu-latest]
php: [8.4, 8.3, 8.2, 8.1, 8.0]
laravel: [11.*, 10.*, 9.*, 8.*]
laravel: [12.*, 11.*, 10.*, 9.*, 8.*]
dependency-version: [prefer-stable]
include:
- laravel: 12.*
testbench: 10.*
- laravel: 11.*
testbench: 9.*
- laravel: 10.*
Expand All @@ -22,6 +24,10 @@ jobs:
- laravel: 8.*
testbench: 6.*
exclude:
- laravel: 12.*
php: 8.1
- laravel: 12.*
php: 8.0
- laravel: 11.*
php: 8.1
- laravel: 11.*
Expand Down Expand Up @@ -50,7 +56,7 @@ jobs:

- name: Install dependencies
run: |
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" "nesbot/carbon:>=2.62.1" --no-interaction --no-update
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" "nesbot/carbon:>=2.72.6" --no-interaction --no-update
composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction --no-suggest

- name: Execute tests
Expand Down
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@
],
"require": {
"php": "^8.0",
"illuminate/contracts": "^8.0|^9.0|^10.0|^11.0",
"illuminate/support": "^8.0|^9.0|^10.0|^11.0"
"illuminate/contracts": "^8.0|^9.0|^10.0|^11.0|^12.0",
"illuminate/support": "^8.0|^9.0|^10.0|^11.0|^12.0"
},
"require-dev": {
"phpunit/phpunit": "^9.5|^10.5",
"orchestra/testbench": "^6.0|^7.0|^8.0|^9.0",
"pestphp/pest": "^1.22|^2.28"
"phpunit/phpunit": "^9.5|^10.5|^11.5",
"orchestra/testbench": "^6.0|^7.0|^8.0|^9.0|^10.0",
"pestphp/pest": "^1.22|^2.28|^3.0"
},
"autoload": {
"psr-4": {
Expand Down
70 changes: 36 additions & 34 deletions tests/BladeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,90 +13,92 @@
})
->with([
'an array' => [
'parameter' => ['key' => 'value'],
'expected' => '<script>window[\'js\'] = window[\'js\'] || {};window[\'js\'][\'key\'] = \'value\';</script>',
['key' => 'value'],
'<script>window[\'js\'] = window[\'js\'] || {};window[\'js\'][\'key\'] = \'value\';</script>',
],
'a boolean with value of `true`' => [
'parameter' => ['boolean' => true],
'expected' => '<script>window[\'js\'] = window[\'js\'] || {};window[\'js\'][\'boolean\'] = true;</script>',
['boolean' => true],
'<script>window[\'js\'] = window[\'js\'] || {};window[\'js\'][\'boolean\'] = true;</script>',
],
'a boolean with value of `false`' => [
'parameter' => ['boolean' => false],
'expected' => '<script>window[\'js\'] = window[\'js\'] || {};window[\'js\'][\'boolean\'] = false;</script>',
['boolean' => false],
'<script>window[\'js\'] = window[\'js\'] || {};window[\'js\'][\'boolean\'] = false;</script>',
],
'an integer' => [
'parameter' => ['number' => 5],
'expected' => '<script>window[\'js\'] = window[\'js\'] || {};window[\'js\'][\'number\'] = 5;</script>',
['number' => 5],
'<script>window[\'js\'] = window[\'js\'] || {};window[\'js\'][\'number\'] = 5;</script>',
],
'an float' => [
'parameter' => ['number' => 5.5],
'expected' => '<script>window[\'js\'] = window[\'js\'] || {};window[\'js\'][\'number\'] = 5.5;</script>',
['number' => 5.5],
'<script>window[\'js\'] = window[\'js\'] || {};window[\'js\'][\'number\'] = 5.5;</script>',
],
'null' => [
'parameter' => ['nothing' => null],
'expected' => '<script>window[\'js\'] = window[\'js\'] || {};window[\'js\'][\'nothing\'] = null;</script>',
['nothing' => null],
'<script>window[\'js\'] = window[\'js\'] || {};window[\'js\'][\'nothing\'] = null;</script>',
],
'a string with line breaks' => [
'parameter' => ['string' => "This is\r\n a test"],
'expected' => '<script>window[\'js\'] = window[\'js\'] || {};window[\'js\'][\'string\'] = \'This is\\r\\n a test\';</script>',
['string' => "This is\r\n a test"],
'<script>window[\'js\'] = window[\'js\'] || {};window[\'js\'][\'string\'] = \'This is\\r\\n a test\';</script>',
],
'a numeric string as a string' => [
'parameter' => ['socialSecurity' => '123456789'],
'expected' => '<script>window[\'js\'] = window[\'js\'] || {};window[\'js\'][\'socialSecurity\'] = \'123456789\';</script>',
['socialSecurity' => '123456789'],
'<script>window[\'js\'] = window[\'js\'] || {};window[\'js\'][\'socialSecurity\'] = \'123456789\';</script>',
],
'escapes tags in a string' => [
'parameter' => ['string' => "This is a <tag>"],
'expected' => '<script>window[\'js\'] = window[\'js\'] || {};window[\'js\'][\'string\'] = \'This is a \<tag\>\';</script>',
['string' => "This is a <tag>"],
'<script>window[\'js\'] = window[\'js\'] || {};window[\'js\'][\'string\'] = \'This is a \<tag\>\';</script>',
],
'arrayable objects' => [
'parameter' => new class () implements Arrayable {
new class () implements Arrayable {
public function toArray()
{
return ['arrayableKey' => 'arrayableValue'];
}
},
'expected' => '<script>window[\'js\'] = window[\'js\'] || {};window[\'js\'][\'arrayableKey\'] = \'arrayableValue\';</script>',
'<script>window[\'js\'] = window[\'js\'] || {};window[\'js\'][\'arrayableKey\'] = \'arrayableValue\';</script>',
],
'JSON serializable objects' => [
'parameter' => new class () implements JsonSerializable {
new class () implements JsonSerializable {
public function jsonSerialize()
{
return ['jsonKey' => 'jsonValue'];
}
},
'expected' => '<script>window[\'js\'] = window[\'js\'] || {};window[\'js\'][\'0\'] = {"jsonKey":"jsonValue"};</script>',
'<script>window[\'js\'] = window[\'js\'] || {};window[\'js\'][\'0\'] = {"jsonKey":"jsonValue"};</script>',
],
'an object that implements `toJson`' => [
'parameter' => new class () {
new class () {
public function toJson()
{
return json_encode(['jsonKey' => 'jsonValue']);
}
},
'expected' => '<script>window[\'js\'] = window[\'js\'] || {};window[\'js\'][\'0\'] = {"jsonKey":"jsonValue"};</script>',
'<script>window[\'js\'] = window[\'js\'] || {};window[\'js\'][\'0\'] = {"jsonKey":"jsonValue"};</script>',
],
'an object that implements `toString`' => [
'parameter' => new class () {
new class () {
public function __toString()
{
return 'string';
}
},
'expected' => '<script>window[\'js\'] = window[\'js\'] || {};window[\'js\'][\'0\'] = \'string\';</script>',
'<script>window[\'js\'] = window[\'js\'] || {};window[\'js\'][\'0\'] = \'string\';</script>',
],

]);

it('can render data without a namespace')
->tap(fn () => config()->set('blade-javascript.namespace', ''))
->expect(fn () => renderView('keyValue'))
->toEqual('<script>window[\'key\'] = \'value\';</script>');
it('can render data without a namespace', function () {
config()->set('blade-javascript.namespace', '');
expect(renderView('keyValue'))
->toEqual('<script>window[\'key\'] = \'value\';</script>');
});

it('cannot translate resources to Javascript')
->tap(fn () => renderView('variable', ['parameter' => fopen(__FILE__, 'r')]))
->throws(ErrorException::class);

it('can render a customized view')
->tap(fn () => view()->replaceNamespace('bladeJavaScript', [__DIR__ . '/resources/views/override']))
->expect(fn () => renderView('keyValue'))
->toEqual('<script type="application/javascript">window[\'js\'] = window[\'js\'] || {};window[\'js\'][\'key\'] = \'value\';</script>');
it('can render a customized view', function () {
view()->replaceNamespace('bladeJavaScript', [__DIR__ . '/resources/views/override']);
expect(renderView('keyValue'))
->toEqual('<script type="application/javascript">window[\'js\'] = window[\'js\'] || {};window[\'js\'][\'key\'] = \'value\';</script>');
});