GetOrExecuteTest.php 1.0 KB

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