Test Failed
Push — master ( 6a85db...af1a14 )
by Dan
02:16
created

CacheTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 110
Duplicated Lines 27.27 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 8
c 4
b 0
f 0
lcom 1
cbo 7
dl 30
loc 110
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
A testConstructDefaultNullStorage() 0 5 1
A testGetCacheStorage() 0 4 1
A testWithCacheStorage() 0 7 1
A testHas() 10 10 1
A testSet() 0 15 1
A testGet() 10 10 1
A testDelete() 10 10 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
use Ds\Cache\CacheStorageInterface;
7
use Ds\Cache\NullStorage;
8
9
/**
10
 * Class CacheTest
11
 * @package Tests\Ds\Cache
12
 */
13
class CacheTest extends \PHPUnit_Framework_TestCase
14
{
15
    /**
16
     * @var Cache
17
     */
18
    public $cache;
19
20
    /**
21
     * @var \PHPUnit_Framework_MockObject_MockObject
22
     */
23
    public $storageMock;
24
25
    /**
26
     *
27
     */
28
    public function setUp()
29
    {
30
        $this->storageMock = $this->getMockBuilder('\Rs\Cache\CacheStorageInterface')->getMock();
31
        $this->cache = new Cache($this->storageMock);
32
    }
33
34
    /**
35
     * Test that NullStorage Adaptor is called by default.
36
     */
37
    public function testConstructDefaultNullStorage(){
38
        $cache = new Cache();
39
        $storage = $cache->getCacheStorage();
40
        $this->assertInstanceOf('\Rs\Cache\NullStorage',$storage);
41
    }
42
43
    /**
44
     * Test that getCacheStorage returns storageMock.
45
     */
46
    public function testGetCacheStorage(){
47
        $storage = $this->cache->getCacheStorage();
48
        $this->assertSame($this->storageMock, $storage);
49
    }
50
51
    /**
52
     * Test CacheStorage is updated from construct.
53
     */
54
    public function testWithCacheStorage(){
55
        $newStorage = new NullStorage();
56
        $cache = $this->cache->withCacheStorage($newStorage);
57
        $storage = $cache->getCacheStorage();
58
        $this->assertSame($newStorage, $storage);
59
        $this->assertNotSame($this->storageMock,$storage);
60
    }
61
62
    /**
63
     * Test that CacheStorageInterface::has() is called
64
     */
65 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...
66
    {
67
        $key = 'key';
68
        $this->storageMock->expects($this->once())
69
            ->method('has')
70
            ->with(
71
                $this->equalTo($key)
72
            );
73
        $this->cache->has($key);
74
    }
75
76
    /**
77
     * Test that CacheStorageInterface::set() is called
78
     */
79
    public function testSet()
80
    {
81
82
        $key = 'key';
83
        $value = 'value';
84
        $expires = 10;
85
        $this->storageMock->expects($this->once())
86
            ->method('set')
87
            ->with(
88
                $this->equalTo($key),
89
                $this->equalTo($value),
90
                $this->equalTo($expires)
91
            );
92
        $this->cache->set($key, $value, $expires);
93
    }
94
95
    /**
96
     * Test that CacheStorageInterface::get() is called
97
     */
98 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...
99
    {
100
        $key = 'key';
101
        $this->storageMock->expects($this->once())
102
            ->method('get')
103
            ->with(
104
                $this->equalTo($key)
105
            );
106
        $this->cache->get($key);
107
    }
108
109
    /**
110
     * Test that CacheStorageInterface::delete() is called
111
     */
112 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...
113
    {
114
        $key = 'key';
115
        $this->storageMock->expects($this->once())
116
            ->method('delete')
117
            ->with(
118
                $this->equalTo($key)
119
            );
120
        $this->cache->delete($key);
121
    }
122
}
123