Skip to content
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

[5.6] Add ElseAuth and ElseGuest Blade directives #23569

Merged
merged 1 commit into from
Mar 16, 2018
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
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');
}
}