From e58bc239308e734e722096bed04c43222911ec82 Mon Sep 17 00:00:00 2001 From: Reza Amini Date: Sun, 18 Oct 2020 15:00:27 +0330 Subject: [PATCH] add @session tests --- src/Directives/SessionDirective.php | 2 +- tests/DirectivesTest.php | 36 +++++++++++++++++++++++++++-- tests/views/session.blade.php | 1 + 3 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 tests/views/session.blade.php diff --git a/src/Directives/SessionDirective.php b/src/Directives/SessionDirective.php index b3f8dd9..094f692 100644 --- a/src/Directives/SessionDirective.php +++ b/src/Directives/SessionDirective.php @@ -9,6 +9,6 @@ class SessionDirective implements Directive public static function handle($parameter) { - return "exists({$parameter})): echo session({$parameter}) endif; ?>"; + return "exists($parameter)){ echo \session()->get({$parameter}); } ?>"; } } \ No newline at end of file diff --git a/tests/DirectivesTest.php b/tests/DirectivesTest.php index 15bd7d3..d3d731e 100644 --- a/tests/DirectivesTest.php +++ b/tests/DirectivesTest.php @@ -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 @@ -92,7 +93,7 @@ public function testSessionExistsDirectiveStyle() public function testSessionExistsDirective() { // When session exists ... - \Illuminate\Support\Facades\Session::shouldReceive('exists') + Session::shouldReceive('exists') ->once() ->with('foo') ->andReturn(true); @@ -100,7 +101,7 @@ public function testSessionExistsDirective() $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); @@ -108,4 +109,35 @@ public function testSessionExistsDirective() $this->assertFalse(Str::contains($view, 'exists')); } + public function testSessionDirectiveStyle() + { + $this->assertEquals("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, ''); + } + } \ No newline at end of file diff --git a/tests/views/session.blade.php b/tests/views/session.blade.php new file mode 100644 index 0000000..43615fd --- /dev/null +++ b/tests/views/session.blade.php @@ -0,0 +1 @@ +@session('foo') \ No newline at end of file