Passed
Pull Request — main (#34)
by Sílvio
02:57
created

CacheStatsTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 31
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A test_stats_record_hits_and_misses() 0 11 1
A setUp() 0 8 3
A tearDown() 0 3 1
1
<?php
2
3
use PHPUnit\Framework\TestCase;
4
use Silviooosilva\CacheerPhp\Cacheer;
5
6
class CacheStatsTest extends TestCase
7
{
8
    private $cache;
9
    private $cacheDir;
10
11
    protected function setUp(): void
12
    {
13
        $this->cacheDir = __DIR__ . '/cache_stats';
14
        if (!file_exists($this->cacheDir) || !is_dir($this->cacheDir)) {
15
            mkdir($this->cacheDir, 0755, true);
16
        }
17
18
        $this->cache = new Cacheer(['cacheDir' => $this->cacheDir]);
19
    }
20
21
    protected function tearDown(): void
22
    {
23
        $this->cache->flushCache();
24
    }
25
26
    public function test_stats_record_hits_and_misses()
27
    {
28
        $this->cache->getCache('missing_key');
29
        $this->cache->putCache('key', 'data');
30
        $this->cache->getCache('key');
31
32
        $stats = $this->cache->getStats();
33
        $this->assertEquals(1, $stats->getHitCount());
34
        $this->assertEquals(1, $stats->getMissCount());
35
        $this->assertGreaterThan(0, $stats->getAverageReadTime());
36
        $this->assertGreaterThan(0, $stats->getAverageWriteTime());
37
    }
38
}
39