Skip to content

Commit 2310630

Browse files
committed
add some stronger tests for @user directive
1 parent 1508cc3 commit 2310630

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

tests/BladeTestCase.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ public function setUp(): void
1414
{
1515
parent::setUp();
1616

17+
config()->set('view.paths', [
18+
__DIR__.'/views',
19+
]);
20+
1721
$this->compiler = app('blade.compiler');
1822
}
1923

tests/DirectivesTest.php

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33

44
use BladeTest\BladeTestCase;
5+
use Illuminate\Support\Facades\Auth;
6+
use Illuminate\Support\Str;
57

68
class DirectivesTest extends BladeTestCase
79
{
@@ -30,13 +32,10 @@ public function testisActiveDirective()
3032

3133
public function testCountDirective()
3234
{
33-
config()->set('view.paths', [
34-
__DIR__.'/views',
35-
]);
3635
$array = collect(range(1, 5));
3736
$count = 5;
3837
$view = view('count', compact('array', 'count'))->render();
39-
$this->assertTrue(\Illuminate\Support\Str::contains($view, 'equal or greater'));
38+
$this->assertTrue(Str::contains($view, 'equal or greater'));
4039
$this->assertEquals('<?php if(count($collection) >= 1): ?>', $this->compiler->compileString('@count($collection, 1)'));
4140
$this->assertEquals('<?php if(count([1, 2, 3, 4]) >= 1): ?>', $this->compiler->compileString('@count([1, 2, 3, 4], 1)'));
4241
}
@@ -51,4 +50,29 @@ public function testUserDirective()
5150
$this->assertEquals("<?php if(\auth()->check()): echo \auth()->user()->name; endif; ?>", $this->compiler->compileString("@user('name')"));
5251
}
5352

53+
public function testUserDirectiveWhenUserLoggedIn()
54+
{
55+
Auth::shouldReceive('check')
56+
->once()
57+
->andReturn(true);
58+
Auth::shouldReceive('user')
59+
->once()
60+
->andReturn((object) [
61+
'name' => 'reza'
62+
]);
63+
$view = view('user')->render();
64+
$this->assertEquals($view, 'reza');
65+
}
66+
67+
public function testUserDirectiveWhenUserNotLoggedIn()
68+
{
69+
Auth::shouldReceive('check')
70+
->once()
71+
->andReturn(false);
72+
Auth::shouldReceive('user')
73+
->never();
74+
$view = view('user')->render();
75+
$this->assertNotEquals($view, 'reza');
76+
}
77+
5478
}

0 commit comments

Comments
 (0)