Skip to content

Commit e58bc23

Browse files
committed
add @session tests
1 parent 1c9303f commit e58bc23

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

src/Directives/SessionDirective.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ class SessionDirective implements Directive
99

1010
public static function handle($parameter)
1111
{
12-
return "<?php if(session()->exists({$parameter})): echo session({$parameter}) endif; ?>";
12+
return "<?php if(\session()->exists($parameter)){ echo \session()->get({$parameter}); } ?>";
1313
}
1414
}

tests/DirectivesTest.php

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
use BladeTest\BladeTestCase;
55
use Illuminate\Support\Facades\Auth;
6+
use Illuminate\Support\Facades\Session;
67
use Illuminate\Support\Str;
78

89
class DirectivesTest extends BladeTestCase
@@ -92,20 +93,51 @@ public function testSessionExistsDirectiveStyle()
9293
public function testSessionExistsDirective()
9394
{
9495
// When session exists ...
95-
\Illuminate\Support\Facades\Session::shouldReceive('exists')
96+
Session::shouldReceive('exists')
9697
->once()
9798
->with('foo')
9899
->andReturn(true);
99100
$view = view('sessionExists')->render();
100101
$this->assertTrue(Str::contains($view, 'exists'));
101102

102103
// When session does not exist ...
103-
\Illuminate\Support\Facades\Session::shouldReceive('exists')
104+
Session::shouldReceive('exists')
104105
->once()
105106
->with('foo')
106107
->andReturn(false);
107108
$view = view('sessionExists')->render();
108109
$this->assertFalse(Str::contains($view, 'exists'));
109110
}
110111

112+
public function testSessionDirectiveStyle()
113+
{
114+
$this->assertEquals("<?php if(\session()->exists('foo')){ echo \session()->get('foo'); } ?>", $this->compiler->compileString("@session('foo')"));
115+
}
116+
117+
public function testSessionDirectiveWorks()
118+
{
119+
Session::shouldReceive('exists')
120+
->once()
121+
->with('foo')
122+
->andReturn(true);
123+
Session::shouldReceive('get')
124+
->once()
125+
->with('foo')
126+
->andReturn('hello');
127+
$view = view('session')->render();
128+
$this->assertEquals($view, 'hello');
129+
}
130+
131+
public function testSessionDirectiveNotWork()
132+
{
133+
Session::shouldReceive('exists')
134+
->once()
135+
->with('foo')
136+
->andReturn(false);
137+
Session::shouldReceive('get')
138+
->never();
139+
$view = view('session')->render();
140+
$this->assertEquals($view, '');
141+
}
142+
111143
}

tests/views/session.blade.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@session('foo')

0 commit comments

Comments
 (0)