Skip to content

Commit 51bc501

Browse files
Merge pull request #1 from yvesdaxmaz/testing
Add tests for cache directive
2 parents 87f83ad + 45ac4e8 commit 51bc501

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

tests/CacheTest.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
namespace RyanChandler\BladeCacheDirective\Tests;
4+
5+
use Artisan;
6+
use Illuminate\Support\Facades\Cache;
7+
8+
class CacheTest extends TestCase
9+
{
10+
11+
protected $first_value;
12+
protected $second_value;
13+
protected $third_value;
14+
15+
public function setUp(): void
16+
{
17+
parent::setUp();
18+
19+
$this->first_value = now();
20+
$this->second_value = now()->subDays(20);
21+
$this->third_value = now()->subDays(60);
22+
}
23+
24+
/** @test */
25+
public function the_cache_directive_will_render_the_same_view_before_ttl_expired()
26+
{
27+
$time = $this->first_value;
28+
$this->assertEquals($this->first_value->format('Y-m-d H:i:s'), $this->renderView('cache', compact('time')));
29+
30+
$time = $this->second_value;
31+
$this->assertEquals($this->first_value->format('Y-m-d H:i:s'), $this->renderView('cache', compact('time')));
32+
}
33+
34+
/** @test */
35+
public function the_cache_directive_will_render_other_view_after_ttl_expired()
36+
{
37+
$time = $this->first_value;
38+
$this->assertEquals($this->first_value->format('Y-m-d H:i:s'), $this->renderView('cache', compact('time')));
39+
40+
sleep(2);
41+
$time = $this->second_value;
42+
$this->assertNotEquals($this->first_value, $this->second_value);
43+
$this->assertEquals($this->second_value->format('Y-m-d H:i:s'), $this->renderView('cache', compact('time')));
44+
}
45+
46+
protected function renderView($view, $parameters)
47+
{
48+
Artisan::call('view:clear');
49+
50+
if(is_string($view)) {
51+
$view = view($view)->with($parameters);
52+
}
53+
54+
return trim((string) ($view));
55+
}
56+
}

tests/TestCase.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,7 @@ public function getEnvironmentSetUp($app)
3232
include_once __DIR__.'/../database/migrations/create_blade-cache-directive_table.php.stub';
3333
(new \CreatePackageTable())->up();
3434
*/
35+
36+
$app['config']->set('view.paths', [__DIR__ . '/resources/views']);
3537
}
3638
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@cache('current_time', 1)
2+
{{ $time }}
3+
@endcache

0 commit comments

Comments
 (0)