-
Notifications
You must be signed in to change notification settings - Fork 11k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add directive @bool to Blade #53179
Merged
taylorotwell
merged 7 commits into
laravel:11.x
from
david-valdivia:nueva-funcionalidad-blade
Oct 16, 2024
+82
−4
Merged
Add directive @bool to Blade #53179
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a07a477
Add directive @bool to Blade
david-valdivia 012ec53
Fix @bool suggestion
david-valdivia f1d4a86
continuous-integration
david-valdivia d722388
Update BladeBoolTest.php
david-valdivia f874756
Update BladeBoolTest.php
david-valdivia f4800d6
Update CompilesConditionals.php
taylorotwell 8937669
Update CompilesConditionals.php
taylorotwell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
namespace Illuminate\Tests\View\Blade; | ||
|
||
class BladeBoolTest extends AbstractBladeTestCase | ||
{ | ||
public function testBool() | ||
{ | ||
// For Javascript object{'isBool' : true} | ||
$string = "{'isBool' : @bool(true)}"; | ||
$expected = "{'isBool' : <?php echo ((true) ? 'true' : 'false'); ?>}"; | ||
$this->assertEquals($expected, $this->compiler->compileString($string)); | ||
|
||
// For Javascript object{'isBool' : false} | ||
$string = "{'isBool' : @bool(false)}"; | ||
$expected = "{'isBool' : <?php echo ((false) ? 'true' : 'false'); ?>}"; | ||
$this->assertEquals($expected, $this->compiler->compileString($string)); | ||
|
||
// For Alpine.js x-show attribute | ||
$string = "<input type='text' x-show='@bool(true)' />"; | ||
$expected = "<input type='text' x-show='<?php echo ((true) ? 'true' : 'false'); ?>' />"; | ||
$this->assertEquals($expected, $this->compiler->compileString($string)); | ||
|
||
// For Alpine.js x-show attribute | ||
$string = "<input type='text' x-show='@bool(false)' />"; | ||
$expected = "<input type='text' x-show='<?php echo ((false) ? 'true' : 'false'); ?>' />"; | ||
$this->assertEquals($expected, $this->compiler->compileString($string)); | ||
} | ||
|
||
public function testCompileBool(): void | ||
{ | ||
$someViewVarTruthy = 123; | ||
$compiled = $this->compiler->compileString('@bool($someViewVarTruthy)'); | ||
|
||
ob_start(); | ||
eval(substr($compiled, 6, -3)); | ||
$this->assertEquals('true', ob_get_clean()); | ||
|
||
$someViewVarFalsey = '0'; | ||
$compiled = $this->compiler->compileString('@bool($someViewVarFalsey)'); | ||
|
||
ob_start(); | ||
eval(substr($compiled, 6, -3)); | ||
$this->assertEquals('false', ob_get_clean()); | ||
|
||
$anotherSomeViewVarTruthy = new SomeClass(); | ||
$compiled = $this->compiler->compileString('@bool($anotherSomeViewVarTruthy)'); | ||
|
||
ob_start(); | ||
eval(substr($compiled, 6, -3)); | ||
$this->assertEquals('true', ob_get_clean()); | ||
|
||
$anotherSomeViewVarFalsey = null; | ||
$compiled = $this->compiler->compileString('@bool($anotherSomeViewVarFalsey)'); | ||
|
||
ob_start(); | ||
eval(substr($compiled, 6, -3)); | ||
$this->assertEquals('false', ob_get_clean()); | ||
} | ||
} | ||
|
||
class SomeClass | ||
{ | ||
public function someMethod() | ||
{ | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is probably one of the few cases where an
eval()
test would have found the error above, like so:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right, it didn't consider a Falsey values, i added a Falsey support and a couple of test for NULL and Class instances.