|
| 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 | +} |
0 commit comments