diff --git a/src/Exceptions/ConstantException.php b/src/Exceptions/ConstantException.php index 8c54e4c..06c0e3a 100644 --- a/src/Exceptions/ConstantException.php +++ b/src/Exceptions/ConstantException.php @@ -2,7 +2,6 @@ namespace EverForge\ThinkConstant\Exceptions; -use Psr\SimpleCache\CacheInterface; use ReflectionClass; use think\Exception; use think\helper\Str; @@ -42,15 +41,9 @@ protected static function getConstantMap(): array public function getErrorMessage(int $code): string { - $cache = static::getCache(); - if ($cache->has($cacheKey = static::getCacheKey())) { - return $cache->get($cacheKey)[$code] ?? 'Unknown error code'; - } else { - $constantMap = static::getConstantMap(); - $cache->set($cacheKey, $constantMap); + $constantMap = static::getConstantMap(); + return $constantMap[$code] ?? 'Unknown error code'; - return $constantMap[$code] ?? 'Unknown error code'; - } } private static function parseAnnotationsFromDocComment($docComment): array @@ -64,18 +57,4 @@ private static function parseAnnotationsFromDocComment($docComment): array return $parsed_annotations; } - - /** - * @return CacheInterface - */ - protected static function getCache(): CacheInterface - { - /* @var \think\Cache $cache */ - return \app()->get('cache'); - } - - protected static function getCacheKey(): string - { - return Str::snake(class_basename(static::class)); - } } diff --git a/tests/Pest.php b/tests/Pest.php index bba2a00..caef3d1 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -3,7 +3,3 @@ use EverForge\ThinkConstant\Tests\Support\SimpleCache; require \dirname(__DIR__, 1) . '/vendor/topthink/framework/src/helper.php'; - -\app()->bind('cache', function () { - return new SimpleCache(); -}); diff --git a/tests/Support/SimpleCache.php b/tests/Support/SimpleCache.php deleted file mode 100644 index c8e2a1c..0000000 --- a/tests/Support/SimpleCache.php +++ /dev/null @@ -1,83 +0,0 @@ -storage[$key] ?? $default; - } - - public function set($key, $value, $ttl = null): bool - { - $this->storage[$key] = $value; - - return true; - } - - public function delete($key): bool - { - unset($this->storage[$key]); - - return true; - } - - public function clear(): bool - { - $this->storage = []; - - return true; - } - - /** - * @param $keys - * @param $default - * - * @throws \Psr\SimpleCache\InvalidArgumentException - * - * @return array - */ - public function getMultiple($keys, $default = null): array - { - $values = []; - foreach ($keys as $key) { - $values[$key] = $this->get($key, $default); - } - - return $values; - } - - public function setMultiple($values, $ttl = null): bool - { - foreach ($values as $key => $value) { - $this->set($key, $value, $ttl); - } - - return true; - } - - public function deleteMultiple($keys): bool - { - foreach ($keys as $key) { - $this->delete($key); - } - - return true; - } - - public function has($key): bool - { - return isset($this->storage[$key]); - } -} diff --git a/tests/TestExceptionTest.php b/tests/TestExceptionTest.php index 05533e6..632d0b5 100644 --- a/tests/TestExceptionTest.php +++ b/tests/TestExceptionTest.php @@ -40,15 +40,4 @@ expect($e) ->toBeInstanceOf(ConstantException::class) ->and($e->getMessage())->toBe('Unknown error code'); -}); - -test('cache is useful', function () { - try { - throw new TestException(TestConstant::SERVER_ERROR_FAILED_PARSED); - } catch (TestException $e) { - } - $cache_data = \app()->get('cache')->get('test_exception'); - expect($cache_data)->toBeArray() - ->and($cache_data[TestConstant::SERVER_ERROR])->toBe('失败') - ->and($cache_data[TestConstant::SERVER_ERROR_FAILED_PARSED])->toBe('Unknown error code'); -}); +}); \ No newline at end of file