Completed
Push — master ( 753c87...0fd538 )
by Tom
01:44 queued 10s
created

LaminasStorageCacheTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 0
loc 80
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
namespace DoctrineModuleTest\Cache;
4
5
use DoctrineModule\Cache\LaminasStorageCache;
6
use Doctrine\Common\Cache\Cache;
7
use Laminas\Cache\Storage\Adapter\Memory;
8
use PHPUnit\Framework\TestCase;
9
10
/**
11
 * Tests for the cache bridge
12
 *
13
 * @license MIT
14
 * @link    http://www.doctrine-project.org/
15
 * @author  Marco Pivetta <[email protected]>
16
 */
17
class LaminasStorageCacheTest extends TestCase
18
{
19
    /**
20
     * @return LaminasStorageCache
21
     */
22
    protected function getCacheDriver()
23
    {
24
        return new LaminasStorageCache(new Memory());
25
    }
26
27
    public function testBasics()
28
    {
29
        $cache = $this->getCacheDriver();
30
31
        // Test save
32
        $cache->save('test_key', 'testing this out');
33
34
        // Test contains to test that save() worked
35
        $this->assertTrue($cache->contains('test_key'));
36
37
        // Test fetch
38
        $this->assertEquals('testing this out', $cache->fetch('test_key'));
39
40
        // Test delete
41
        $cache->save('test_key2', 'test2');
42
        $cache->delete('test_key2');
43
        $this->assertFalse($cache->contains('test_key2'));
44
45
        // Fetch/save test with objects (Is cache driver serializes/unserializes objects correctly ?)
46
        $cache->save('test_object_key', new \ArrayObject());
47
        $this->assertInstanceOf('ArrayObject', $cache->fetch('test_object_key'));
48
    }
49
50
    public function testDeleteAll()
51
    {
52
        $cache = $this->getCacheDriver();
53
        $cache->save('test_key1', '1');
54
        $cache->save('test_key2', '2');
55
        $cache->deleteAll();
56
57
        $this->assertFalse($cache->contains('test_key1'));
58
        $this->assertFalse($cache->contains('test_key2'));
59
    }
60
61
    public function testFlushAll()
62
    {
63
        $cache = $this->getCacheDriver();
64
        $cache->save('test_key1', '1');
65
        $cache->save('test_key2', '2');
66
        $cache->flushAll();
67
68
        $this->assertFalse($cache->contains('test_key1'));
69
        $this->assertFalse($cache->contains('test_key2'));
70
    }
71
72
    public function testNamespace()
73
    {
74
        $cache = $this->getCacheDriver();
75
        $cache->setNamespace('test_');
76
        $cache->save('key1', 'test');
77
78
        $this->assertTrue($cache->contains('key1'));
79
80
        $cache->setNamespace('test2_');
81
82
        $this->assertFalse($cache->contains('key1'));
83
    }
84
85
    public function testGetStats()
86
    {
87
        $cache = $this->getCacheDriver();
88
        $stats = $cache->getStats();
89
90
        $this->assertArrayHasKey(Cache::STATS_HITS, $stats);
0 ignored issues
show
Bug introduced by
It seems like $stats defined by $cache->getStats() on line 88 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_MISSES, $stats);
0 ignored issues
show
Bug introduced by
It seems like $stats defined by $cache->getStats() on line 88 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_UPTIME, $stats);
0 ignored issues
show
Bug introduced by
It seems like $stats defined by $cache->getStats() on line 88 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
        $this->assertArrayHasKey(Cache::STATS_MEMORY_USAGE, $stats);
0 ignored issues
show
Bug introduced by
It seems like $stats defined by $cache->getStats() on line 88 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...
94
        $this->assertArrayHasKey(Cache::STATS_MEMORY_AVAILIABLE, $stats);
0 ignored issues
show
Bug introduced by
It seems like $stats defined by $cache->getStats() on line 88 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...
Deprecated Code introduced by
The constant Doctrine\Common\Cache\Ca...STATS_MEMORY_AVAILIABLE has been deprecated.

This class constant has been deprecated.

Loading history...
95
    }
96
}
97