Test Setup Failed
Push — master ( 3189d2...130005 )
by Dan
05:17
created

CacheTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 117
Duplicated Lines 46.15 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

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() 15 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\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\Cache
12
 */
13
class CacheTest extends \PHPUnit_Framework_TestCase
14
{
15
    /**
16
     * @var Cache
17
     */
18
    public $cache;
19
20
    /**
21
     * @var CacheStorageInterface
22
     */
23
    public $storageMock;
24
25
    public function setUp()
26
    {
27
        $this->storageMock = $this->getMockBuilder(CacheStorageInterface::class)->getMock();
28
        $this->cache = new Cache($this->storageMock);
29
    }
30
31
    /**
32
     * Test that NullStorage Adaptor is called by default.
33
     */
34
    public function testConstructNullStorage(){
35
        $cache = new Cache();
36
        $storage = $cache->getCacheStorage();
37
        $this->assertInstanceOf(NullStorage::class,$storage);
38
    }
39
40
    /**
41
     * Test CacheStorage is updated from construct.
42
     */
43
    public function testConstructWithStorage(){
44
        $cache = new Cache($this->storageMock);
45
        $storage = $cache->getCacheStorage();
46
        $this->assertSame($this->storageMock,$storage);
47
    }
48
49
    /**
50
     * Test that CacheStorageInterface::has() is called
51
     */
52 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...
53
    {
54
        $key = 'key';
55
        $this->storageMock->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Ds\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...
56
            ->method('has')
57
            ->with(
58
                $this->equalTo($key)
59
            );
60
        $this->cache->has($key);
61
    }
62
63
    /**
64
     * Test that CacheStorageInterface::set() is called
65
     */
66 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...
67
    {
68
69
        $key = 'key';
70
        $value = 'value';
71
        $expires = 10;
72
        $this->storageMock->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Ds\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...
73
            ->method('set')
74
            ->with(
75
                $this->equalTo($key),
76
                $this->equalTo($value),
77
                $this->equalTo($expires)
78
            );
79
        $this->cache->set($key, $value, $expires);
80
    }
81
82
    /**
83
     * Test that CacheStorageInterface::get() is called
84
     */
85 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...
86
    {
87
        $key = 'key';
88
        $this->storageMock->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Ds\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...
89
            ->method('get')
90
            ->with(
91
                $this->equalTo($key)
92
            );
93
        $this->cache->get($key);
94
    }
95
96
    /**
97
     * Test that CacheStorageInterface::delete() is called
98
     */
99 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...
100
    {
101
        $key = 'key';
102
        $this->storageMock->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Ds\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...
103
            ->method('delete')
104
            ->with(
105
                $this->equalTo($key)
106
            );
107
        $this->cache->delete($key);
108
    }
109
110
    /**
111
     * Test that Cache::withCacheStorage() updates CacheStorageInterface adaptor.
112
     */
113
    public function testWithCacheStorage()
114
    {
115
        $newStorageMock = clone $this->storageMock;
116
        $newCache = $this->cache->withCacheStorage($newStorageMock);
117
        $this->assertEquals($newStorageMock, $newCache->getCacheStorage());
118
    }
119
120
    /**
121
     * Test that CacheStorageInterface is returned from Cache::getCacheStorage
122
     */
123
    public function testGetCacheStorage()
124
    {
125
        $expected = $this->storageMock;
126
        $actual = $this->cache->getCacheStorage();
127
        $this->assertEquals($expected, $actual);
128
    }
129
}
130