GetOrExecuteTest.php 856 B

1234567891011121314151617181920212223242526272829303132333435
  1. <?php
  2. namespace Tests\Services\CacheService;
  3. use App\Services\CacheService;
  4. use Illuminate\Support\Facades\Cache;
  5. use Tests\TestCase;
  6. class GetOrExecuteTest extends TestCase
  7. {
  8. /** @var CacheService $cacheService */
  9. public $cacheService;
  10. public function setUp(): void
  11. {
  12. parent::setUp();
  13. $this->cacheService = app('CacheService');
  14. }
  15. public function testPutAndGet(){
  16. $key = md5(microtime(true));
  17. $result=$this->cacheService->getOrExecute($key,function (){
  18. return 'yes';
  19. });
  20. $this->assertEquals('yes',$result);
  21. $this->assertEquals('yes',Cache::pull($key));//销毁
  22. }
  23. public function testNullFunction(){
  24. $this->expectException(\Exception::class);
  25. $key = md5(microtime(true));
  26. $this->cacheService->getOrExecute($key,null);
  27. }
  28. }