|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Illuminate\Tests\View\Blade; |
| 4 | + |
| 5 | +class BladeBoolTest extends AbstractBladeTestCase |
| 6 | +{ |
| 7 | + public function testBool() |
| 8 | + { |
| 9 | + // For Javascript object{'isBool' : true} |
| 10 | + $string = "{'isBool' : @bool(true)}"; |
| 11 | + $expected = "{'isBool' : <?php echo ((true) ? 'true' : 'false'); ?>}"; |
| 12 | + $this->assertEquals($expected, $this->compiler->compileString($string)); |
| 13 | + |
| 14 | + // For Javascript object{'isBool' : false} |
| 15 | + $string = "{'isBool' : @bool(false)}"; |
| 16 | + $expected = "{'isBool' : <?php echo ((false) ? 'true' : 'false'); ?>}"; |
| 17 | + $this->assertEquals($expected, $this->compiler->compileString($string)); |
| 18 | + |
| 19 | + // For Alpine.js x-show attribute |
| 20 | + $string = "<input type='text' x-show='@bool(true)' />"; |
| 21 | + $expected = "<input type='text' x-show='<?php echo ((true) ? 'true' : 'false'); ?>' />"; |
| 22 | + $this->assertEquals($expected, $this->compiler->compileString($string)); |
| 23 | + |
| 24 | + // For Alpine.js x-show attribute |
| 25 | + $string = "<input type='text' x-show='@bool(false)' />"; |
| 26 | + $expected = "<input type='text' x-show='<?php echo ((false) ? 'true' : 'false'); ?>' />"; |
| 27 | + $this->assertEquals($expected, $this->compiler->compileString($string)); |
| 28 | + } |
| 29 | + |
| 30 | + public function testCompileBool(): void |
| 31 | + { |
| 32 | + $someViewVarTruthy = 123; |
| 33 | + $compiled = $this->compiler->compileString('@bool($someViewVarTruthy)'); |
| 34 | + |
| 35 | + ob_start(); |
| 36 | + eval(substr($compiled, 6, -3)); |
| 37 | + $this->assertEquals('true', ob_get_clean()); |
| 38 | + |
| 39 | + $someViewVarFalsey = '0'; |
| 40 | + $compiled = $this->compiler->compileString('@bool($someViewVarFalsey)'); |
| 41 | + |
| 42 | + ob_start(); |
| 43 | + eval(substr($compiled, 6, -3)); |
| 44 | + $this->assertEquals('false', ob_get_clean()); |
| 45 | + |
| 46 | + $anotherSomeViewVarTruthy = new SomeClass(); |
| 47 | + $compiled = $this->compiler->compileString('@bool($anotherSomeViewVarTruthy)'); |
| 48 | + |
| 49 | + ob_start(); |
| 50 | + eval(substr($compiled, 6, -3)); |
| 51 | + $this->assertEquals('true', ob_get_clean()); |
| 52 | + |
| 53 | + $anotherSomeViewVarFalsey = null; |
| 54 | + $compiled = $this->compiler->compileString('@bool($anotherSomeViewVarFalsey)'); |
| 55 | + |
| 56 | + ob_start(); |
| 57 | + eval(substr($compiled, 6, -3)); |
| 58 | + $this->assertEquals('false', ob_get_clean()); |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +class SomeClass |
| 63 | +{ |
| 64 | + public function someMethod() |
| 65 | + { |
| 66 | + } |
| 67 | +} |
0 commit comments