Skip to content

Commit

Permalink
add @session tests
Browse files Browse the repository at this point in the history
  • Loading branch information
reziamini committed Oct 18, 2020
1 parent 1c9303f commit e58bc23
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/Directives/SessionDirective.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ class SessionDirective implements Directive

public static function handle($parameter)
{
return "<?php if(session()->exists({$parameter})): echo session({$parameter}) endif; ?>";
return "<?php if(\session()->exists($parameter)){ echo \session()->get({$parameter}); } ?>";
}
}
36 changes: 34 additions & 2 deletions tests/DirectivesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use BladeTest\BladeTestCase;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Str;

class DirectivesTest extends BladeTestCase
Expand Down Expand Up @@ -92,20 +93,51 @@ public function testSessionExistsDirectiveStyle()
public function testSessionExistsDirective()
{
// When session exists ...
\Illuminate\Support\Facades\Session::shouldReceive('exists')
Session::shouldReceive('exists')
->once()
->with('foo')
->andReturn(true);
$view = view('sessionExists')->render();
$this->assertTrue(Str::contains($view, 'exists'));

// When session does not exist ...
\Illuminate\Support\Facades\Session::shouldReceive('exists')
Session::shouldReceive('exists')
->once()
->with('foo')
->andReturn(false);
$view = view('sessionExists')->render();
$this->assertFalse(Str::contains($view, 'exists'));
}

public function testSessionDirectiveStyle()
{
$this->assertEquals("<?php if(\session()->exists('foo')){ echo \session()->get('foo'); } ?>", $this->compiler->compileString("@session('foo')"));
}

public function testSessionDirectiveWorks()
{
Session::shouldReceive('exists')
->once()
->with('foo')
->andReturn(true);
Session::shouldReceive('get')
->once()
->with('foo')
->andReturn('hello');
$view = view('session')->render();
$this->assertEquals($view, 'hello');
}

public function testSessionDirectiveNotWork()
{
Session::shouldReceive('exists')
->once()
->with('foo')
->andReturn(false);
Session::shouldReceive('get')
->never();
$view = view('session')->render();
$this->assertEquals($view, '');
}

}
1 change: 1 addition & 0 deletions tests/views/session.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@session('foo')

0 comments on commit e58bc23

Please sign in to comment.