Completed
Branch master (89d155)
by Dan
14:32 queued 12:04
created

CacheTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 117
Duplicated Lines 25.64 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 4
dl 30
loc 117
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
A testConstructNullStorage() 0 5 1
A testConstructWithStorage() 0 5 1
A testHas() 10 10 1
A testSet() 0 15 1
A testGet() 10 10 1
A testDelete() 10 10 1
A testWithCacheStorage() 0 6 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\Rs\Cache;
4
5
use Rs\Cache\Cache;
6
use Rs\Cache\CacheStorageInterface;
7
8
/**
9
 * Class CacheTest
10
 * @package Tests\Ds\Cache
11
 */
12
class CacheTest extends \PHPUnit_Framework_TestCase
13
{
14
    /**
15
     * @var Cache
16
     */
17
    public $cache;
18
19
    /**
20
     * @var CacheStorageInterface
21
     */
22
    public $storageMock;
23
24
    public function setUp()
25
    {
26
        $this->storageMock = $this->getMockBuilder('\Rs\Cache\CacheStorageInterface')->getMock();
27
        $this->cache = new Cache($this->storageMock);
28
    }
29
30
    /**
31
     * Test that NullStorage Adaptor is called by default.
32
     */
33
    public function testConstructNullStorage(){
34
        $cache = new Cache();
35
        $storage = $cache->getCacheStorage();
36
        $this->assertInstanceOf('\Rs\Cache\NullStorage',$storage);
37
    }
38
39
    /**
40
     * Test CacheStorage is updated from construct.
41
     */
42
    public function testConstructWithStorage(){
43
        $cache = new Cache($this->storageMock);
44
        $storage = $cache->getCacheStorage();
45
        $this->assertSame($this->storageMock,$storage);
46
    }
47
48
    /**
49
     * Test that CacheStorageInterface::has() is called
50
     */
51 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...
52
    {
53
        $key = 'key';
54
        $this->storageMock->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Rs\Cache\CacheStorageInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
55
            ->method('has')
56
            ->with(
57
                $this->equalTo($key)
58
            );
59
        $this->cache->has($key);
60
    }
61
62
    /**
63
     * Test that CacheStorageInterface::set() is called
64
     */
65
    public function testSet()
66
    {
67
68
        $key = 'key';
69
        $value = 'value';
70
        $expires = 10;
71
        $this->storageMock->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Rs\Cache\CacheStorageInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
72
            ->method('set')
73
            ->with(
74
                $this->equalTo($key),
75
                $this->equalTo($value),
76
                $this->equalTo($expires)
77
            );
78
        $this->cache->set($key, $value, $expires);
79
    }
80
81
    /**
82
     * Test that CacheStorageInterface::get() is called
83
     */
84 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...
85
    {
86
        $key = 'key';
87
        $this->storageMock->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Rs\Cache\CacheStorageInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
88
            ->method('get')
89
            ->with(
90
                $this->equalTo($key)
91
            );
92
        $this->cache->get($key);
93
    }
94
95
    /**
96
     * Test that CacheStorageInterface::delete() is called
97
     */
98 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...
99
    {
100
        $key = 'key';
101
        $this->storageMock->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Rs\Cache\CacheStorageInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
102
            ->method('delete')
103
            ->with(
104
                $this->equalTo($key)
105
            );
106
        $this->cache->delete($key);
107
    }
108
109
    /**
110
     * Test that Cache::withCacheStorage() updates CacheStorageInterface adaptor.
111
     */
112
    public function testWithCacheStorage()
113
    {
114
        $newStorageMock = clone $this->storageMock;
115
        $newCache = $this->cache->withCacheStorage($newStorageMock);
116
        $this->assertEquals($newStorageMock, $newCache->getCacheStorage());
117
    }
118
119
    /**
120
     * Test that CacheStorageInterface is returned from Cache::getCacheStorage
121
     */
122
    public function testGetCacheStorage()
123
    {
124
        $expected = $this->storageMock;
125
        $actual = $this->cache->getCacheStorage();
126
        $this->assertEquals($expected, $actual);
127
    }
128
}
129