LoginSignup
0
0

More than 1 year has passed since last update.

LaravelのPHPUnitテストでキャッシュ一覧を取得する(CACHE_DRIVER=array編)

Posted at

PHPUnitでキャッシュドライバを配列するときの設定

phpunit.xml
<env name="CACHE_DRIVER" value="array" force="true"/>

キャッシュ一覧を取得する方法

test.php
    $store = Cache::getStore();
    $reflection = new \ReflectionClass(get_class($store));
    $property = $reflection->getProperty('storage');
    $property->setAccessible(true);
    $cache = $property->getValue($store);

これですべてのキャッシュされたキーと値の配列が取得できる。

Example

// キャッシュ
Cache::add("hoge", "value1");
Cache::add("hoge2", "value2");

// すべてのキャッシュを取得
$store = Cache::getStore();
$reflection = new \ReflectionClass(get_class($store));
$property = $reflection->getProperty('storage');
$property->setAccessible(true);
$cache = $property->getValue($store);

print_r($cache);
//Array
//(
//    [hoge] => Array
//        (
//           [value] => value1
//           [expiresAt] => 0
//        )
//    [hoge2] => Array
//        (
//           [value] => value2
//           [expiresAt] => 0
//        )
//
//)
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0