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
|
<?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()
{
}
}
|