Skip to content

Commit

Permalink
Add ElseAuth and ElseGuest Blade directives
Browse files Browse the repository at this point in the history
  • Loading branch information
j-dexx committed Mar 16, 2018
1 parent ac0c717 commit 68141f6
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/Illuminate/View/Compilers/Concerns/CompilesConditionals.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@ protected function compileAuth($guard = null)
return "<?php if(auth()->guard{$guard}->check()): ?>";
}

/**
* Compile the else-auth statements into valid PHP.
*
* @param string|null $guard
* @return string
*/
protected function compileElseAuth($guard = null)
{
$guard = is_null($guard) ? '()' : $guard;

return "<?php elseif(auth()->guard{$guard}->check()): ?>";
}

/**
* Compile the end-auth statements into valid PHP.
*
Expand All @@ -47,6 +60,19 @@ protected function compileGuest($guard = null)
return "<?php if(auth()->guard{$guard}->guest()): ?>";
}

/**
* Compile the else-guest statements into valid PHP.
*
* @param string|null $guard
* @return string
*/
protected function compileElseGuest($guard = null)
{
$guard = is_null($guard) ? '()' : $guard;

return "<?php elseif(auth()->guard{$guard}->guest()): ?>";
}

/**
* Compile the end-guest statements into valid PHP.
*
Expand Down
52 changes: 52 additions & 0 deletions tests/View/Blade/BladeElseAuthStatementsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Illuminate\Tests\Blade;

use Mockery as m;
use PHPUnit\Framework\TestCase;
use Illuminate\View\Compilers\BladeCompiler;

class BladeElseAuthStatementsTest extends TestCase
{
public function tearDown()
{
m::close();
}

public function testElseAuthStatementsAreCompiled()
{
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
$string = '@auth("api")
breeze
@elseauth("standard")
wheeze
@endauth';
$expected = '<?php if(auth()->guard("api")->check()): ?>
breeze
<?php elseif(auth()->guard("standard")->check()): ?>
wheeze
<?php endif; ?>';
$this->assertEquals($expected, $compiler->compileString($string));
}

public function testPlainElseAuthStatementsAreCompiled()
{
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
$string = '@auth("api")
breeze
@elseauth
wheeze
@endauth';
$expected = '<?php if(auth()->guard("api")->check()): ?>
breeze
<?php elseif(auth()->guard()->check()): ?>
wheeze
<?php endif; ?>';
$this->assertEquals($expected, $compiler->compileString($string));
}

protected function getFiles()
{
return m::mock('Illuminate\Filesystem\Filesystem');
}
}
36 changes: 36 additions & 0 deletions tests/View/Blade/BladeElseGuestStatementsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Illuminate\Tests\Blade;

use Mockery as m;
use PHPUnit\Framework\TestCase;
use Illuminate\View\Compilers\BladeCompiler;

class BladeElseGuestStatementsTest extends TestCase
{
public function tearDown()
{
m::close();
}

public function testIfStatementsAreCompiled()
{
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
$string = '@guest("api")
breeze
@elseguest("standard")
wheeze
@endguest';
$expected = '<?php if(auth()->guard("api")->guest()): ?>
breeze
<?php elseif(auth()->guard("standard")->guest()): ?>
wheeze
<?php endif; ?>';
$this->assertEquals($expected, $compiler->compileString($string));
}

protected function getFiles()
{
return m::mock('Illuminate\Filesystem\Filesystem');
}
}

0 comments on commit 68141f6

Please sign in to comment.