LaminasStorageCacheTest   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 0
loc 77
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getCacheDriver() 0 4 1
A testBasics() 0 22 1
A testDeleteAll() 0 10 1
A testFlushAll() 0 10 1
A testNamespace() 0 12 1
A testGetStats() 0 11 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace DoctrineModuleTest\Cache;
6
7
use ArrayObject;
8
use Doctrine\Common\Cache\Cache;
9
use DoctrineModule\Cache\LaminasStorageCache;
10
use Laminas\Cache\Storage\Adapter\Memory;
11
use PHPUnit\Framework\TestCase;
12
13
/**
14
 * Tests for the cache bridge
15
 *
16
 * @link    http://www.doctrine-project.org/
17
 */
18
class LaminasStorageCacheTest extends TestCase
19
{
20
    protected function getCacheDriver() : LaminasStorageCache
21
    {
22
        return new LaminasStorageCache(new Memory());
23
    }
24
25
    public function testBasics() : void
26
    {
27
        $cache = $this->getCacheDriver();
28
29
        // Test save
30
        $cache->save('test_key', 'testing this out');
31
32
        // Test contains to test that save() worked
33
        $this->assertTrue($cache->contains('test_key'));
34
35
        // Test fetch
36
        $this->assertEquals('testing this out', $cache->fetch('test_key'));
37
38
        // Test delete
39
        $cache->save('test_key2', 'test2');
40
        $cache->delete('test_key2');
41
        $this->assertFalse($cache->contains('test_key2'));
42
43
        // Fetch/save test with objects (Is cache driver serializes/unserializes objects correctly ?)
44
        $cache->save('test_object_key', new ArrayObject());
45
        $this->assertInstanceOf('ArrayObject', $cache->fetch('test_object_key'));
46
    }
47
48
    public function testDeleteAll() : void
49
    {
50
        $cache = $this->getCacheDriver();
51
        $cache->save('test_key1', '1');
52
        $cache->save('test_key2', '2');
53
        $cache->deleteAll();
54
55
        $this->assertFalse($cache->contains('test_key1'));
56
        $this->assertFalse($cache->contains('test_key2'));
57
    }
58
59
    public function testFlushAll() : void
60
    {
61
        $cache = $this->getCacheDriver();
62
        $cache->save('test_key1', '1');
63
        $cache->save('test_key2', '2');
64
        $cache->flushAll();
65
66
        $this->assertFalse($cache->contains('test_key1'));
67
        $this->assertFalse($cache->contains('test_key2'));
68
    }
69
70
    public function testNamespace() : void
71
    {
72
        $cache = $this->getCacheDriver();
73
        $cache->setNamespace('test_');
74
        $cache->save('key1', 'test');
75
76
        $this->assertTrue($cache->contains('key1'));
77
78
        $cache->setNamespace('test2_');
79
80
        $this->assertFalse($cache->contains('key1'));
81
    }
82
83
    public function testGetStats() : void
84
    {
85
        $cache = $this->getCacheDriver();
86
        $stats = $cache->getStats();
87
88
        $this->assertArrayHasKey(Cache::STATS_HITS, $stats);
0 ignored issues
show
Bug introduced by
It seems like $stats defined by $cache->getStats() on line 86 can also be of type null; however, PHPUnit\Framework\Assert::assertArrayHasKey() does only seem to accept array|object<ArrayAccess>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
89
        $this->assertArrayHasKey(Cache::STATS_MISSES, $stats);
0 ignored issues
show
Bug introduced by
It seems like $stats defined by $cache->getStats() on line 86 can also be of type null; however, PHPUnit\Framework\Assert::assertArrayHasKey() does only seem to accept array|object<ArrayAccess>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
90
        $this->assertArrayHasKey(Cache::STATS_UPTIME, $stats);
0 ignored issues
show
Bug introduced by
It seems like $stats defined by $cache->getStats() on line 86 can also be of type null; however, PHPUnit\Framework\Assert::assertArrayHasKey() does only seem to accept array|object<ArrayAccess>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
91
        $this->assertArrayHasKey(Cache::STATS_MEMORY_USAGE, $stats);
0 ignored issues
show
Bug introduced by
It seems like $stats defined by $cache->getStats() on line 86 can also be of type null; however, PHPUnit\Framework\Assert::assertArrayHasKey() does only seem to accept array|object<ArrayAccess>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
92
        $this->assertArrayHasKey(Cache::STATS_MEMORY_AVAILIABLE, $stats);
0 ignored issues
show
Deprecated Code introduced by
The constant Doctrine\Common\Cache\Ca...STATS_MEMORY_AVAILIABLE has been deprecated.

This class constant has been deprecated.

Loading history...
Bug introduced by
It seems like $stats defined by $cache->getStats() on line 86 can also be of type null; however, PHPUnit\Framework\Assert::assertArrayHasKey() does only seem to accept array|object<ArrayAccess>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
93
    }
94
}
95