<?php

namespace Tests\Services\CacheService;

use App\Services\CacheService;
use Illuminate\Support\Facades\Cache;
use Tests\TestCase;

class GetOrExecuteTest extends TestCase
{

    /** @var CacheService $cacheService */
    public $cacheService;
    public function setUp(): void
    {
        parent::setUp();
        $this->cacheService = app('CacheService');
    }

    public function testPutAndGet(){
        $key = md5(microtime(true));
        $result=$this->cacheService->getOrExecute($key,function (){
            return 'yes';
        });
        $this->assertEquals('yes',$result);
        $this->assertEquals('yes',Cache::pull($key));//销毁
    }

    public function testNullFunction(){
        $this->expectException(\Exception::class);
        $key = md5(microtime(true));
        $this->cacheService->getOrExecute($key,null);
    }

}
