Skip to content

Commit

Permalink
tests(cache): add tests for Leevel\Cache\Load::clearCacheLoaded
Browse files Browse the repository at this point in the history
  • Loading branch information
doyouhaobaby committed Sep 29, 2020
1 parent dfb82ea commit 71a090c
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 13 deletions.
75 changes: 62 additions & 13 deletions tests/Cache/LoadTest.php
Expand Up @@ -22,9 +22,11 @@

use Leevel\Cache\Load;
use Leevel\Di\Container;
use Leevel\Filesystem\Helper;
use Tests\Cache\Pieces\Test1;
use Tests\Cache\Pieces\Test2;
use Tests\Cache\Pieces\Test4;
use Tests\Cache\Pieces\Test5;
use Tests\TestCase;

/**
Expand Down Expand Up @@ -67,21 +69,9 @@ class LoadTest extends TestCase
{
protected function tearDown(): void
{
$files = [
'test1.php',
'test4.php',
'test2.php',
];

foreach ($files as $val) {
if (is_file($val = __DIR__.'/Pieces/cacheLoad/'.$val)) {
unlink($val);
}
}

$path = __DIR__.'/Pieces/cacheLoad';
if (is_dir($path)) {
rmdir($path);
Helper::deleteDirectory($path);
}
}

Expand Down Expand Up @@ -230,6 +220,65 @@ public function testWithParams(): void
$this->assertSame(['hello', 'world', 'foo', 'bar'], $result);
}

/**
* @api(
* zh-CN:title="clearCacheLoaded 清理已载入的缓存数据",
* zh-CN:description="
* 如果缓存原始数据可以动态变化,比如在一个常驻脚本中,后台修改了系统配置,然后使用 `refresh` 更新了缓存。
* 此时你需要在每次循环调用业务代码前清理掉已载入的缓存数据,而不是通过 `refresh` 清理原始缓存数据,那么这个方法将会变得非常有用。
*
* **例子 \Tests\Cache\Pieces\Test5**
*
* ``` php
* {[\Leevel\Kernel\Utils\Doc::getClassBody(\Tests\Cache\Pieces\Test5::class)]}
* ```
*
* 例子中的缓存原始数据变化,我们通过 `$GLOBALS['cache_data]` 来模拟实现。
* ",
* zh-CN:note="",
* )
*/
public function testClearCacheLoaded(): void
{
$container = new Container();
$load = $this->createLoad($container);

if (isset($GLOBALS['cache_data'])) {
unset($GLOBALS['cache_data']);
}

$GLOBALS['cache_data'] = 'data1';
$result = $load->data([Test5::class]);
$this->assertSame(['data' => 'data1'], $result);

$GLOBALS['cache_data'] = 'data2';
$result = $load->data([Test5::class]);
$this->assertSame(['data' => 'data1'], $result);

// 清理原始缓存,模拟修改系统配置
$newLoad = $this->createLoad($container);
$newLoad->refresh([Test5::class]);

$result = $load->data([Test5::class]);
$this->assertSame(['data' => 'data1'], $result);

$load->clearCacheLoaded([Test5::class]);
$result = $load->data([Test5::class]);
$this->assertSame(['data' => 'data2'], $result);

// 清理原始缓存,模拟修改系统配置
$newLoad = $this->createLoad($container);
$newLoad->refresh([Test5::class]);
$GLOBALS['cache_data'] = 'data3';
$load->clearCacheLoaded();
$result = $load->data([Test5::class]);
$this->assertSame(['data' => 'data3'], $result);

if (isset($GLOBALS['cache_data'])) {
unset($GLOBALS['cache_data']);
}
}

public function testCacheNotFound(): void
{
$this->expectException(\ReflectionException::class);
Expand Down
45 changes: 45 additions & 0 deletions tests/Cache/Pieces/Test5.php
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

/*
* This file is part of the ************************ package.
* _____________ _______________
* ______/ \__ _____ ____ ______ / /_ _________
* ____/ __ / / / / _ \/ __`\/ / __ \/ __ \/ __ \___
* __/ / / / /_/ / __/ / \ / /_/ / / / / /_/ /__
* \_\ \_/\____/\___/_/ / / .___/_/ /_/ .___/
* \_\ /_/_/ /_/
*
* The PHP Framework For Code Poem As Free As Wind. <Query Yet Simple>
* (c) 2010-2020 http://queryphp.com All rights reserved.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Tests\Cache\Pieces;

use Leevel\Cache\File;
use Leevel\Cache\IBlock;
use Leevel\Cache\ICache;

class Test5 implements IBlock
{
public function handle(array $params = []): array
{
return ['data' => $GLOBALS['cache_data'] ?? 'test5'];
}

public function cache(): ICache
{
return new File([
'path' => __DIR__.'/cacheLoad',
]);
}

public static function key(array $params = []): string
{
return 'test5';
}
}

0 comments on commit 71a090c

Please sign in to comment.