Completed
Push — master ( fc5eaa...427062 )
by Dan
03:22
created

CacheTest   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 138
Duplicated Lines 39.13 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 2
Bugs 0 Features 2
Metric Value
wmc 11
c 2
b 0
f 2
lcom 1
cbo 3
dl 54
loc 138
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 8 1
A testConstructNullStorage() 0 5 1
A testConstructWithStorage() 0 5 1
A testHas() 13 13 1
A testSet() 17 17 1
A testSetDefaultExpires() 0 16 1
A testSetFromExpires() 0 18 1
A testGet() 12 12 1
A testDelete() 12 12 1
A testWithCacheStorage() 0 9 1
A testGetCacheStorage() 0 6 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Tests\Ds\Cache;
4
5
use Ds\Cache\Cache;
6
7
class CacheTest extends \PHPUnit_Framework_TestCase
8
{
9
10
    public $cache;
11
    public $storageMock;
12
13
    public function setUp()
14
    {
15
16
        $this->storageMock = $this->getMockBuilder('\Ds\Cache\Interfaces\CacheStorageInterface')
17
            ->getMock();
18
19
        $this->cache = new Cache($this->storageMock,99);
20
    }
21
22
    public function testConstructNullStorage(){
23
        $cache = new Cache();
24
        $storage = $cache->getCacheStorage();
25
        $this->assertInstanceOf('\Ds\Cache\NullStorage',$storage);
26
    }
27
28
    public function testConstructWithStorage(){
29
        $cache = new Cache($this->storageMock);
30
        $storage = $cache->getCacheStorage();
31
        $this->assertSame($this->storageMock,$storage);
32
    }
33
34 View Code Duplication
    public function testHas()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
35
    {
36
37
        $key = 'key';
38
39
        $this->storageMock->expects($this->once())
40
            ->method('has')
41
            ->with(
42
                $this->equalTo($key)
43
            );
44
45
        $this->cache->has($key);
46
    }
47
48 View Code Duplication
    public function testSet()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
49
    {
50
51
        $key = 'key';
52
        $value = 'value';
53
        $expires = 10;
54
55
        $this->storageMock->expects($this->once())
56
            ->method('set')
57
            ->with(
58
                $this->equalTo($key),
59
                $this->equalTo($value),
60
                $this->equalTo($expires)
61
            );
62
63
        $this->cache->set($key, $value, $expires);
64
    }
65
66
    public function testSetDefaultExpires()
67
    {
68
69
        $key = 'key';
70
        $value = 'value';
71
72
        $this->storageMock->expects($this->once())
73
            ->method('set')
74
            ->with(
75
                $this->equalTo($key),
76
                $this->equalTo($value),
77
                $this->equalTo(99)
78
            );
79
80
        $this->cache->set($key, $value);
81
    }
82
83
    public function testSetFromExpires()
84
    {
85
86
        $key = 'key';
87
        $value = 'value';
88
89
        $this->cache = $this->cache->setExpires(60);
90
91
        $this->storageMock->expects($this->once())
92
            ->method('set')
93
            ->with(
94
                $this->equalTo($key),
95
                $this->equalTo($value),
96
                $this->equalTo(60)
97
            );
98
99
        $this->cache->set($key, $value);
100
    }
101
102 View Code Duplication
    public function testGet()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
103
    {
104
        $key = 'key';
105
106
        $this->storageMock->expects($this->once())
107
            ->method('get')
108
            ->with(
109
                $this->equalTo($key)
110
            );
111
112
        $this->cache->get($key);
113
    }
114
115 View Code Duplication
    public function testDelete()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
116
    {
117
        $key = 'key';
118
119
        $this->storageMock->expects($this->once())
120
            ->method('delete')
121
            ->with(
122
                $this->equalTo($key)
123
            );
124
125
        $this->cache->delete($key);
126
    }
127
128
    public function testWithCacheStorage()
129
    {
130
        $newStorageMock = $this->getMockBuilder('\Ds\Cache\Interfaces\CacheStorageInterface')
131
            ->getMock();
132
133
        $newCache = $this->cache->withCacheStorage($newStorageMock);
134
        $this->assertEquals($newStorageMock, $newCache->getCacheStorage());
135
136
    }
137
138
    public function testGetCacheStorage()
139
    {
140
        $expected = $this->storageMock;
141
        $actual = $this->cache->getCacheStorage();
142
        $this->assertEquals($expected, $actual);
143
    }
144
}
145