RedisOptionBuilderTTLAndFlushTest   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 37
dl 0
loc 64
rs 10
c 1
b 0
f 0
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A test_flush_after_from_options_triggers_auto_flush() 0 19 1
A tearDown() 0 4 2
A setUp() 0 13 2
A test_expiration_time_from_options_sets_default_ttl() 0 16 1
1
<?php
2
3
use PHPUnit\Framework\TestCase;
4
use Silviooosilva\CacheerPhp\Cacheer;
5
use Silviooosilva\CacheerPhp\Config\Option\Builder\OptionBuilder;
6
use Silviooosilva\CacheerPhp\Helpers\FlushHelper;
7
use Predis\Client as PredisClient;
8
use Predis\Autoloader as PredisAutoloader;
9
10
class RedisOptionBuilderTTLAndFlushTest extends TestCase
11
{
12
  private ?PredisClient $client = null;
13
14
  protected function setUp(): void
15
  {
16
    try {
17
      PredisAutoloader::register();
18
      $this->client = new PredisClient([
19
        'scheme' => 'tcp',
20
        'host'   => REDIS_CONNECTION_CONFIG['REDIS_HOST'] ?? '127.0.0.1',
21
        'port'   => REDIS_CONNECTION_CONFIG['REDIS_PORT'] ?? 6379,
22
      ]);
23
      $this->client->connect();
24
      $this->client->ping();
25
    } catch (\Throwable $e) {
26
      $this->markTestSkipped('Redis not available: ' . $e->getMessage());
27
    }
28
  }
29
30
  protected function tearDown(): void
31
  {
32
    if ($this->client) {
33
      $this->client->disconnect();
34
    }
35
  }
36
37
  public function test_expiration_time_from_options_sets_default_ttl()
38
  {
39
    $options = OptionBuilder::forRedis()
40
      ->setNamespace('app:')
41
      ->expirationTime('1 seconds')
42
      ->build();
43
44
    $cache = new Cacheer($options);
45
    $cache->setDriver()->useRedisDriver();
46
47
    $key = 'redis_opt_ttl_key';
48
    $cache->putCache($key, 'v');
49
    $this->assertTrue($cache->isSuccess());
50
51
    sleep(2);
52
    $this->assertNull($cache->getCache($key));
53
  }
54
55
  public function test_flush_after_from_options_triggers_auto_flush()
56
  {
57
    $options = OptionBuilder::forRedis()
58
      ->setNamespace('app:')
59
      ->flushAfter('1 seconds')
60
      ->build();
61
62
    $flushFile = FlushHelper::pathFor('redis', 'app:');
63
    file_put_contents($flushFile, (string) (time() - 3600));
64
65
    // seed
66
    $seed = new Cacheer(OptionBuilder::forRedis()->setNamespace('app:')->build());
67
    $seed->setDriver()->useRedisDriver();
68
    $seed->putCache('to_be_flushed', '1');
69
70
    // new instance should auto-flush on init
71
    $cache = new Cacheer($options);
72
    $cache->setDriver()->useRedisDriver();
73
    $this->assertFalse((bool)$this->client->exists('app:to_be_flushed'));
0 ignored issues
show
Bug introduced by
The method exists() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

73
    $this->assertFalse((bool)$this->client->/** @scrutinizer ignore-call */ exists('app:to_be_flushed'));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
74
  }
75
}
76