Completed
Push — master ( a8b991...dda2b8 )
by Marco
06:05 queued 02:56
created

NotSetStateClass   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 14
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0
Metric Value
wmc 2
lcom 0
cbo 0
dl 0
loc 14
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getValue() 0 4 1
1
<?php
2
3
namespace Doctrine\Tests\Common\Cache;
4
5
use Doctrine\Common\Cache\Cache;
6
use Doctrine\Common\Cache\PhpFileCache;
7
8
/**
9
 * @group DCOM-101
10
 */
11
class PhpFileCacheTest extends BaseFileCacheTest
12
{
13
    public function provideDataToCache()
14
    {
15
        $data = parent::provideDataToCache();
16
17
        unset($data['object'], $data['object_recursive']); // PhpFileCache only allows objects that implement __set_state() and fully support var_export()
18
        unset($data['float_zero']); // var_export exports float(0) as int(0)
19
20
        return $data;
21
    }
22
23
    public function testImplementsSetState()
24
    {
25
        $cache = $this->_getCacheDriver();
26
27
        // Test save
28
        $cache->save('test_set_state', new SetStateClass(array(1,2,3)));
29
30
        //Test __set_state call
31
        $this->assertCount(0, SetStateClass::$values);
32
33
        // Test fetch
34
        $value = $cache->fetch('test_set_state');
35
        $this->assertInstanceOf('Doctrine\Tests\Common\Cache\SetStateClass', $value);
36
        $this->assertEquals(array(1,2,3), $value->getValue());
37
38
        //Test __set_state call
39
        $this->assertCount(1, SetStateClass::$values);
40
41
        // Test contains
42
        $this->assertTrue($cache->contains('test_set_state'));
43
    }
44
45
    public function testNotImplementsSetState()
46
    {
47
        $cache = $this->_getCacheDriver();
48
49
        $this->setExpectedException('InvalidArgumentException');
50
        $cache->save('test_not_set_state', new NotSetStateClass(array(1,2,3)));
51
    }
52
53
    public function testGetStats()
54
    {
55
        $cache = $this->_getCacheDriver();
56
        $stats = $cache->getStats();
57
58
        $this->assertNull($stats[Cache::STATS_HITS]);
59
        $this->assertNull($stats[Cache::STATS_MISSES]);
60
        $this->assertNull($stats[Cache::STATS_UPTIME]);
61
        $this->assertEquals(0, $stats[Cache::STATS_MEMORY_USAGE]);
62
        $this->assertGreaterThan(0, $stats[Cache::STATS_MEMORY_AVAILABLE]);
63
    }
64
65
    protected function _getCacheDriver()
66
    {
67
        return new PhpFileCache($this->directory);
68
    }
69
}
70
71
class NotSetStateClass
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
72
{
73
    private $value;
74
75
    public function __construct($value)
76
    {
77
        $this->value = $value;
78
    }
79
80
    public function getValue()
81
    {
82
        return $this->value;
83
    }
84
}
85
86
class SetStateClass extends NotSetStateClass
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
87
{
88
    public static $values = array();
89
90
    public static function __set_state($data)
91
    {
92
        self::$values = $data;
93
        return new self($data['value']);
94
    }
95
}
96