Completed
Push — master ( eeb92f...6713c8 )
by Marco
04:35 queued 02:31
created

SimpleCacheAdapterTest::invalidTTLs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 15
rs 9.4285
cc 1
eloc 12
nc 1
nop 0
1
<?php
2
declare(strict_types = 1);
3
4
namespace RoaveTest\DoctrineSimpleCache;
5
6
use Doctrine\Common\Cache\ArrayCache;
7
use Roave\DoctrineSimpleCache\Exception\CacheException;
8
use Roave\DoctrineSimpleCache\Exception\InvalidArgumentException;
9
use Roave\DoctrineSimpleCache\SimpleCacheAdapter;
10
use RoaveTestAsset\DoctrineSimpleCache\FullyImplementedCache;
11
use RoaveTestAsset\DoctrineSimpleCache\NotClearableCache;
12
use RoaveTestAsset\DoctrineSimpleCache\NotMultiGettableCache;
13
use RoaveTestAsset\DoctrineSimpleCache\NotMultiPuttableCache;
14
15
/**
16
 * @covers \Roave\DoctrineSimpleCache\SimpleCacheAdapter
17
 */
18
final class SimpleCacheAdapterTest extends \PHPUnit_Framework_TestCase
19
{
20
    public function invalidTTLs() : array
21
    {
22
        return [
23
            [''],
24
            [true],
25
            [false],
26
            ['abc'],
27
            [2.5],
28
            [' 1'], // can be casted to a int
29
            ['12foo'], // can be casted to a int
30
            ['025'], // can be interpreted as hex
31
            [new \stdClass()],
32
            [['array']],
33
        ];
34
    }
35
36
    public function validKeys()
37
    {
38
        return [
39
            ['AbC19_.'],
40
            ['1234567890123456789012345678901234567890123456789012345678901234'],
41
        ];
42
    }
43
44
    public function invalidKeys()
45
    {
46
        return [
47
            [''],
48
            [true],
49
            [false],
50
            [null],
51
            [2],
52
            [2.5],
53
            ['{str'],
54
            ['rand{'],
55
            ['rand{str'],
56
            ['rand}str'],
57
            ['rand(str'],
58
            ['rand)str'],
59
            ['rand/str'],
60
            ['rand\\str'],
61
            ['rand@str'],
62
            ['rand:str'],
63
            [new \stdClass()],
64
            [['array']],
65
        ];
66
    }
67
68
    public function testConstructorThrowsExceptionWhenNotMultiPuttableCacheIsUsed()
69
    {
70
        /** @var NotMultiPuttableCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
71
        $doctrineCache = $this->createMock(NotMultiPuttableCache::class);
72
73
        $this->expectException(CacheException::class);
74
        new SimpleCacheAdapter($doctrineCache);
0 ignored issues
show
Bug introduced by
It seems like $doctrineCache defined by $this->createMock(\Roave...tiPuttableCache::class) on line 71 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Roave\DoctrineSimpleCach...eAdapter::__construct() does only seem to accept object<Doctrine\Common\Cache\Cache>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
75
    }
76
77
    public function testConstructorThrowsExceptionWhenNotClearableCacheIsUsed()
78
    {
79
        /** @var NotClearableCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
80
        $doctrineCache = $this->createMock(NotClearableCache::class);
81
82
        $this->expectException(CacheException::class);
83
        new SimpleCacheAdapter($doctrineCache);
0 ignored issues
show
Bug introduced by
It seems like $doctrineCache defined by $this->createMock(\Roave...tClearableCache::class) on line 80 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Roave\DoctrineSimpleCach...eAdapter::__construct() does only seem to accept object<Doctrine\Common\Cache\Cache>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
84
    }
85
86
    public function testConstructorThrowsExceptionWhenNotMultiGettableCacheIsUsed()
87
    {
88
        /** @var NotMultiGettableCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
89
        $doctrineCache = $this->createMock(NotMultiGettableCache::class);
90
91
        $this->expectException(CacheException::class);
92
        new SimpleCacheAdapter($doctrineCache);
0 ignored issues
show
Bug introduced by
It seems like $doctrineCache defined by $this->createMock(\Roave...tiGettableCache::class) on line 89 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Roave\DoctrineSimpleCach...eAdapter::__construct() does only seem to accept object<Doctrine\Common\Cache\Cache>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
93
    }
94
95 View Code Duplication
    public function testGetProxiesToDoctrineFetch()
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...
96
    {
97
        $key = uniqid('key', true);
98
        $value = uniqid('value', true);
99
100
        /** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
101
        $doctrineCache = $this->createMock(FullyImplementedCache::class);
102
        $doctrineCache->expects(self::once())->method('fetch')->with($key)->willReturn($value);
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in RoaveTestAsset\DoctrineS...e\FullyImplementedCache.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
103
104
        $psrCache = new SimpleCacheAdapter($doctrineCache);
0 ignored issues
show
Bug introduced by
It seems like $doctrineCache defined by $this->createMock(\Roave...mplementedCache::class) on line 101 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Roave\DoctrineSimpleCach...eAdapter::__construct() does only seem to accept object<Doctrine\Common\Cache\Cache>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
105
        self::assertSame($value, $psrCache->get($key));
106
    }
107
108
    public function testGetWithNotExistingKey()
109
    {
110
        $key = uniqid('key', true);
111
        $value = uniqid('value', true);
112
113
        $psrCache = new SimpleCacheAdapter(new ArrayCache());
114
        $psrCache->set($key, $value);
115
116
        $default = uniqid('default', true);
117
        self::assertSame($value, $psrCache->get($key, $default));
118
119
        $anotherKey = uniqid('key', true);
120
        self::assertSame($default, $psrCache->get($anotherKey, $default));
121
    }
122
123 View Code Duplication
    public function testGetWithFalseValueStoredInCache()
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...
124
    {
125
        $key = uniqid('key', true);
126
127
        $psrCache = new SimpleCacheAdapter(new ArrayCache());
128
        $psrCache->set($key, false);
129
130
        self::assertFalse($psrCache->get($key, uniqid('default', true)));
131
    }
132
133
    public function testSetProxiesToDoctrineSave()
134
    {
135
        $key = uniqid('key', true);
136
        $value = uniqid('value', true);
137
        $ttl = random_int(1000, 9999);
138
139
        /** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
140
        $doctrineCache = $this->createMock(FullyImplementedCache::class);
141
        $doctrineCache->expects(self::once())->method('save')->with($key, $value, $ttl)->willReturn(true);
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in RoaveTestAsset\DoctrineS...e\FullyImplementedCache.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
142
143
        $psrCache = new SimpleCacheAdapter($doctrineCache);
0 ignored issues
show
Bug introduced by
It seems like $doctrineCache defined by $this->createMock(\Roave...mplementedCache::class) on line 140 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Roave\DoctrineSimpleCach...eAdapter::__construct() does only seem to accept object<Doctrine\Common\Cache\Cache>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
144
        self::assertTrue($psrCache->set($key, $value, $ttl));
145
    }
146
147
    public function testSetWithDateIntervalTTL()
148
    {
149
        $key = uniqid('key', true);
150
        $value = uniqid('value', true);
151
        $ttl_date = \DateInterval::createFromDateString('1 day');
152
153
        $psrCache = new SimpleCacheAdapter(new ArrayCache());
154
155
        // This does not test if ttl is correctly set to 86400 sec.
156
        self::assertTrue($psrCache->set($key, $value, $ttl_date));
157
        self::assertSame($psrCache->get($key), $value);
158
    }
159
160
    public function testSetWithNonPositiveTTL()
161
    {
162
        $key = uniqid('key', true);
163
        $value = uniqid('value', true);
164
        $ttl = random_int(1000, 9999);
165
166
        $psrCache = new SimpleCacheAdapter(new ArrayCache());
167
168
        $psrCache->set($key, $value, $ttl);
169
        self::assertSame($psrCache->get($key), $value);
170
171
        $psrCache->set($key, $value, -1);
172
        self::assertNull($psrCache->get($key), null);
173
    }
174
175
    /**
176
     * @param mixed $ttl
177
     * @dataProvider invalidTTLs
178
     */
179 View Code Duplication
    public function testSetWithInvalidTTL($ttl)
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...
180
    {
181
        $key = uniqid('key', true);
182
        $value = uniqid('value', true);
183
184
        $psrCache = new SimpleCacheAdapter(new ArrayCache());
185
186
        $this->expectException(InvalidArgumentException::class);
187
        $psrCache->set($key, $value, $ttl);
188
    }
189
190 View Code Duplication
    public function testDeleteProxiesToDoctrineDelete()
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...
191
    {
192
        $key = uniqid('key', true);
193
194
        /** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
195
        $doctrineCache = $this->createMock(FullyImplementedCache::class);
196
        $doctrineCache->expects(self::once())->method('delete')->with($key)->willReturn(true);
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in RoaveTestAsset\DoctrineS...e\FullyImplementedCache.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
197
198
        $psrCache = new SimpleCacheAdapter($doctrineCache);
0 ignored issues
show
Bug introduced by
It seems like $doctrineCache defined by $this->createMock(\Roave...mplementedCache::class) on line 195 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Roave\DoctrineSimpleCach...eAdapter::__construct() does only seem to accept object<Doctrine\Common\Cache\Cache>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
199
        self::assertTrue($psrCache->delete($key));
200
    }
201
202
    public function testClearProxiesToDeleteAll()
203
    {
204
        /** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
205
        $doctrineCache = $this->createMock(FullyImplementedCache::class);
206
        $doctrineCache->expects(self::once())->method('deleteAll')->with()->willReturn(true);
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in RoaveTestAsset\DoctrineS...e\FullyImplementedCache.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
207
208
        $psrCache = new SimpleCacheAdapter($doctrineCache);
0 ignored issues
show
Bug introduced by
It seems like $doctrineCache defined by $this->createMock(\Roave...mplementedCache::class) on line 205 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Roave\DoctrineSimpleCach...eAdapter::__construct() does only seem to accept object<Doctrine\Common\Cache\Cache>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
209
        self::assertTrue($psrCache->clear());
210
    }
211
212
    public function testGetMultipleProxiesToFetchMultiple()
213
    {
214
        $values = [
215
            uniqid('key1', true) => uniqid('value1', true),
216
            uniqid('key2', true) => uniqid('value2', true),
217
        ];
218
        $keys = array_keys($values);
219
220
        /** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
221
        $doctrineCache = $this->createMock(FullyImplementedCache::class);
222
        $doctrineCache->expects(self::once())->method('fetchMultiple')->with($keys)->willReturn($values);
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in RoaveTestAsset\DoctrineS...e\FullyImplementedCache.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
223
224
        $psrCache = new SimpleCacheAdapter($doctrineCache);
0 ignored issues
show
Bug introduced by
It seems like $doctrineCache defined by $this->createMock(\Roave...mplementedCache::class) on line 221 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Roave\DoctrineSimpleCach...eAdapter::__construct() does only seem to accept object<Doctrine\Common\Cache\Cache>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
225
        self::assertSame($values, $psrCache->getMultiple($keys));
226
    }
227
228
    public function testGetMultipleWithPartialKeys()
229
    {
230
        $values = [
231
            uniqid('key1', true) => uniqid('value1', true),
232
            uniqid('key2', true) => uniqid('value2', true),
233
        ];
234
        $keys = array_keys($values);
235
236
        $psrCache = new SimpleCacheAdapter(new ArrayCache());
237
        $psrCache->setMultiple($values);
238
239
        $default = uniqid('default', true);
240
        $invalid_key = uniqid('key3', true);
241
        $keys[] = $invalid_key;
242
        $values[$invalid_key] = $default;
243
244
        self::assertSame($values, $psrCache->getMultiple($keys, $default));
245
    }
246
247
    /**
248
     * @param mixed $key
249
     * @dataProvider invalidKeys
250
     */
251
    public function testGetMultipleThrowsExceptionWithInvalidKeys($key)
252
    {
253
        $this->expectException(InvalidArgumentException::class);
254
255
        $psrCache = new SimpleCacheAdapter(new ArrayCache());
256
        $psrCache->getMultiple([$key]);
257
    }
258
259
    /**
260
     * @param mixed $key
261
     * @dataProvider validKeys
262
     */
263 View Code Duplication
    public function testGetMultipleAcceptsTraversable($key)
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...
264
    {
265
        $values = [
266
            $key => uniqid('value', true),
267
        ];
268
269
        /** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
270
        $doctrineCache = $this->createMock(FullyImplementedCache::class);
271
        $doctrineCache->expects(self::once())->method('fetchMultiple')->with(array_keys($values))->willReturn($values);
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in RoaveTestAsset\DoctrineS...e\FullyImplementedCache.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
272
273
        $psrCache = new SimpleCacheAdapter($doctrineCache);
0 ignored issues
show
Bug introduced by
It seems like $doctrineCache defined by $this->createMock(\Roave...mplementedCache::class) on line 270 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Roave\DoctrineSimpleCach...eAdapter::__construct() does only seem to accept object<Doctrine\Common\Cache\Cache>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
274
        $psrCache->getMultiple(new \ArrayObject(array_keys($values)));
275
    }
276
277 View Code Duplication
    public function testGetMultipleThrowsExceptionWhenNotArrayOrTraversable()
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...
278
    {
279
        $this->expectException(InvalidArgumentException::class);
280
281
        $psrCache = new SimpleCacheAdapter(new ArrayCache());
282
        $psrCache->getMultiple(uniqid('string', true));
0 ignored issues
show
Documentation introduced by
uniqid('string', true) is of type string, but the function expects a array|object<Traversable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
283
    }
284
285
    public function testSetMultipleProxiesToSaveMultiple()
286
    {
287
        $values = [
288
            uniqid('key1', true) => uniqid('value1', true),
289
            uniqid('key2', true) => uniqid('value2', true),
290
        ];
291
292
        /** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
293
        $doctrineCache = $this->createMock(FullyImplementedCache::class);
294
        $doctrineCache->expects(self::once())->method('saveMultiple')->with($values)->willReturn(true);
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in RoaveTestAsset\DoctrineS...e\FullyImplementedCache.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
295
296
        $psrCache = new SimpleCacheAdapter($doctrineCache);
0 ignored issues
show
Bug introduced by
It seems like $doctrineCache defined by $this->createMock(\Roave...mplementedCache::class) on line 293 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Roave\DoctrineSimpleCach...eAdapter::__construct() does only seem to accept object<Doctrine\Common\Cache\Cache>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
297
        self::assertTrue($psrCache->setMultiple($values));
298
    }
299
300
    public function testSetMultipleWithDateIntervalTTL()
301
    {
302
        $values = [
303
            uniqid('key1', true) => uniqid('value1', true),
304
            uniqid('key2', true) => uniqid('value2', true),
305
        ];
306
        $keys = array_keys($values);
307
        $ttl_date = \DateInterval::createFromDateString('1 day');
308
309
        $psrCache = new SimpleCacheAdapter(new ArrayCache());
310
311
        // This does not test if ttl is correctly set to 86400 sec.
312
        self::assertTrue($psrCache->setMultiple($values, $ttl_date));
313
        self::assertSame($values, $psrCache->getMultiple($keys));
314
    }
315
316
    public function testSetMultipleWithNonPositiveTTL()
317
    {
318
        $values = [
319
            uniqid('key1', true) => uniqid('value1', true),
320
            uniqid('key2', true) => uniqid('value2', true),
321
        ];
322
        $keys = array_keys($values);
323
324
        $psrCache = new SimpleCacheAdapter(new ArrayCache());
325
        $psrCache->setMultiple($values);
326
327
        $volatile = [$keys[0] => uniqid('value3', true)];
328
        $psrCache->setMultiple($volatile, -1);
329
330
        self::assertNull($psrCache->get($keys[0]));
331
        self::assertNotNull($psrCache->get($keys[1]));
332
    }
333
334
    /**
335
     * @param mixed $ttl
336
     * @dataProvider invalidTTLs
337
     */
338
    public function testSetMultipleWithInvalidTTL($ttl)
339
    {
340
        $values = [
341
            uniqid('key1', true) => uniqid('value1', true),
342
            uniqid('key2', true) => uniqid('value2', true),
343
        ];
344
345
        $psrCache = new SimpleCacheAdapter(new ArrayCache());
346
347
        $this->expectException(InvalidArgumentException::class);
348
        $psrCache->setMultiple($values, $ttl);
349
    }
350
351 View Code Duplication
    public function testSetMultipleThrowsExceptionWhenNotArrayOrTraversable()
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...
352
    {
353
        $this->expectException(InvalidArgumentException::class);
354
355
        $psrCache = new SimpleCacheAdapter(new ArrayCache());
356
        $psrCache->setMultiple(uniqid('string', true));
0 ignored issues
show
Documentation introduced by
uniqid('string', true) is of type string, but the function expects a array|object<Traversable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
357
    }
358
359 View Code Duplication
    public function testDeleteMultipleReturnsTrueWhenAllDeletesSucceed()
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...
360
    {
361
        $keys = [
362
            uniqid('key1', true),
363
            uniqid('key2', true),
364
        ];
365
366
        /** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
367
        $doctrineCache = $this->createMock(FullyImplementedCache::class);
368
        $doctrineCache->expects(self::at(0))->method('delete')->with($keys[0])->willReturn(true);
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in RoaveTestAsset\DoctrineS...e\FullyImplementedCache.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
369
        $doctrineCache->expects(self::at(1))->method('delete')->with($keys[1])->willReturn(true);
370
371
        $psrCache = new SimpleCacheAdapter($doctrineCache);
0 ignored issues
show
Bug introduced by
It seems like $doctrineCache defined by $this->createMock(\Roave...mplementedCache::class) on line 367 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Roave\DoctrineSimpleCach...eAdapter::__construct() does only seem to accept object<Doctrine\Common\Cache\Cache>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
372
        self::assertTrue($psrCache->deleteMultiple($keys));
373
    }
374
375 View Code Duplication
    public function testDeleteMultipleReturnsFalseWhenOneDeleteFails()
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...
376
    {
377
        $keys = [
378
            uniqid('key1', true),
379
            uniqid('key2', true),
380
        ];
381
382
        /** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
383
        $doctrineCache = $this->createMock(FullyImplementedCache::class);
384
        $doctrineCache->expects(self::at(0))->method('delete')->with($keys[0])->willReturn(false);
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in RoaveTestAsset\DoctrineS...e\FullyImplementedCache.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
385
        $doctrineCache->expects(self::at(1))->method('delete')->with($keys[1])->willReturn(true);
386
387
        $psrCache = new SimpleCacheAdapter($doctrineCache);
0 ignored issues
show
Bug introduced by
It seems like $doctrineCache defined by $this->createMock(\Roave...mplementedCache::class) on line 383 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Roave\DoctrineSimpleCach...eAdapter::__construct() does only seem to accept object<Doctrine\Common\Cache\Cache>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
388
        self::assertFalse($psrCache->deleteMultiple($keys));
389
    }
390
391 View Code Duplication
    public function testHasProxiesToDoctrineContains()
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...
392
    {
393
        $key = uniqid('key', true);
394
395
        /** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
396
        $doctrineCache = $this->createMock(FullyImplementedCache::class);
397
        $doctrineCache->expects(self::once())->method('contains')->with($key)->willReturn(true);
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in RoaveTestAsset\DoctrineS...e\FullyImplementedCache.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
398
399
        $psrCache = new SimpleCacheAdapter($doctrineCache);
0 ignored issues
show
Bug introduced by
It seems like $doctrineCache defined by $this->createMock(\Roave...mplementedCache::class) on line 396 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, Roave\DoctrineSimpleCach...eAdapter::__construct() does only seem to accept object<Doctrine\Common\Cache\Cache>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
400
        self::assertTrue($psrCache->has($key));
401
    }
402
}
403