Completed
Pull Request — master (#22)
by James
03:22
created

henNotClearableCacheIsUsed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
dl 0
loc 8
rs 9.4285
c 2
b 0
f 1
cc 1
eloc 4
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\NotMultiOperationCache;
13
14
/**
15
 * @covers \Roave\DoctrineSimpleCache\SimpleCacheAdapter
16
 */
17
final class SimpleCacheAdapterTest extends \PHPUnit_Framework_TestCase
18
{
19
    public function invalidTTLs() : array
20
    {
21
        return [
22
            [''],
23
            [true],
24
            [false],
25
            ['abc'],
26
            [2.5],
27
            [' 1'], // can be casted to a int
28
            ['12foo'], // can be casted to a int
29
            ['025'], // can be interpreted as hex
30
            [new \stdClass()],
31
            [['array']],
32
        ];
33
    }
34
35
    public function validKeys()
36
    {
37
        return [
38
            ['AbC19_.'],
39
            ['1234567890123456789012345678901234567890123456789012345678901234'],
40
        ];
41
    }
42
43
    public function invalidKeys()
44
    {
45
        return [
46
            [''],
47
            [true],
48
            [false],
49
            [null],
50
            [2],
51
            [2.5],
52
            ['{str'],
53
            ['rand{'],
54
            ['rand{str'],
55
            ['rand}str'],
56
            ['rand(str'],
57
            ['rand)str'],
58
            ['rand/str'],
59
            ['rand\\str'],
60
            ['rand@str'],
61
            ['rand:str'],
62
            [new \stdClass()],
63
            [['array']],
64
        ];
65
    }
66
67
    public function testConstructorThrowsExceptionWhenNotMultiPuttableCacheIsUsed()
68
    {
69
        /** @var NotMultiOperationCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
70
        $doctrineCache = $this->createMock(NotMultiOperationCache::class);
71
72
        $this->expectException(CacheException::class);
73
        new SimpleCacheAdapter($doctrineCache);
0 ignored issues
show
Bug introduced by
It seems like $doctrineCache defined by $this->createMock(\Roave...iOperationCache::class) on line 70 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...
74
    }
75
76 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...
77
    {
78
        $key = uniqid('key', true);
79
        $value = uniqid('value', true);
80
81
        /** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
82
        $doctrineCache = $this->createMock(FullyImplementedCache::class);
83
        $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...
84
85
        $psrCache = new SimpleCacheAdapter($doctrineCache);
0 ignored issues
show
Bug introduced by
It seems like $doctrineCache defined by $this->createMock(\Roave...mplementedCache::class) on line 82 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...
86
        self::assertSame($value, $psrCache->get($key));
87
    }
88
89
    public function testGetWithNotExistingKey()
90
    {
91
        $key = uniqid('key', true);
92
        $value = uniqid('value', true);
93
94
        $psrCache = new SimpleCacheAdapter(new ArrayCache());
95
        $psrCache->set($key, $value);
96
97
        $default = uniqid('default', true);
98
        self::assertSame($value, $psrCache->get($key, $default));
99
100
        $anotherKey = uniqid('key', true);
101
        self::assertSame($default, $psrCache->get($anotherKey, $default));
102
    }
103
104 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...
105
    {
106
        $key = uniqid('key', true);
107
108
        $psrCache = new SimpleCacheAdapter(new ArrayCache());
109
        $psrCache->set($key, false);
110
111
        self::assertFalse($psrCache->get($key, uniqid('default', true)));
112
    }
113
114
    public function testSetProxiesToDoctrineSave()
115
    {
116
        $key = uniqid('key', true);
117
        $value = uniqid('value', true);
118
        $ttl = random_int(1000, 9999);
119
120
        /** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
121
        $doctrineCache = $this->createMock(FullyImplementedCache::class);
122
        $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...
123
124
        $psrCache = new SimpleCacheAdapter($doctrineCache);
0 ignored issues
show
Bug introduced by
It seems like $doctrineCache defined by $this->createMock(\Roave...mplementedCache::class) on line 121 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...
125
        self::assertTrue($psrCache->set($key, $value, $ttl));
126
    }
127
128
    public function testSetWithDateIntervalTTL()
129
    {
130
        $key = uniqid('key', true);
131
        $value = uniqid('value', true);
132
        $ttl_date = \DateInterval::createFromDateString('1 day');
133
134
        $psrCache = new SimpleCacheAdapter(new ArrayCache());
135
136
        // This does not test if ttl is correctly set to 86400 sec.
137
        self::assertTrue($psrCache->set($key, $value, $ttl_date));
138
        self::assertSame($psrCache->get($key), $value);
139
    }
140
141
    public function testSetWithNonPositiveTTL()
142
    {
143
        $key = uniqid('key', true);
144
        $value = uniqid('value', true);
145
        $ttl = random_int(1000, 9999);
146
147
        $psrCache = new SimpleCacheAdapter(new ArrayCache());
148
149
        $psrCache->set($key, $value, $ttl);
150
        self::assertSame($psrCache->get($key), $value);
151
152
        $psrCache->set($key, $value, -1);
153
        self::assertNull($psrCache->get($key), null);
154
    }
155
156
    /**
157
     * @param mixed $ttl
158
     * @dataProvider invalidTTLs
159
     */
160 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...
161
    {
162
        $key = uniqid('key', true);
163
        $value = uniqid('value', true);
164
165
        $psrCache = new SimpleCacheAdapter(new ArrayCache());
166
167
        $this->expectException(InvalidArgumentException::class);
168
        $psrCache->set($key, $value, $ttl);
169
    }
170
171 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...
172
    {
173
        $key = uniqid('key', true);
174
175
        /** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
176
        $doctrineCache = $this->createMock(FullyImplementedCache::class);
177
        $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...
178
179
        $psrCache = new SimpleCacheAdapter($doctrineCache);
0 ignored issues
show
Bug introduced by
It seems like $doctrineCache defined by $this->createMock(\Roave...mplementedCache::class) on line 176 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...
180
        self::assertTrue($psrCache->delete($key));
181
    }
182
183
    public function testClearProxiesToDeleteAll()
184
    {
185
        /** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
186
        $doctrineCache = $this->createMock(FullyImplementedCache::class);
187
        $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...
188
189
        $psrCache = new SimpleCacheAdapter($doctrineCache);
0 ignored issues
show
Bug introduced by
It seems like $doctrineCache defined by $this->createMock(\Roave...mplementedCache::class) on line 186 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...
190
        self::assertTrue($psrCache->clear());
191
    }
192
193
    public function testGetMultipleProxiesToFetchMultiple()
194
    {
195
        $values = [
196
            uniqid('key1', true) => uniqid('value1', true),
197
            uniqid('key2', true) => uniqid('value2', true),
198
        ];
199
        $keys = array_keys($values);
200
201
        /** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
202
        $doctrineCache = $this->createMock(FullyImplementedCache::class);
203
        $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...
204
205
        $psrCache = new SimpleCacheAdapter($doctrineCache);
0 ignored issues
show
Bug introduced by
It seems like $doctrineCache defined by $this->createMock(\Roave...mplementedCache::class) on line 202 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...
206
        self::assertSame($values, $psrCache->getMultiple($keys));
207
    }
208
209
    public function testGetMultipleWithPartialKeys()
210
    {
211
        $values = [
212
            uniqid('key1', true) => uniqid('value1', true),
213
            uniqid('key2', true) => uniqid('value2', true),
214
        ];
215
        $keys = array_keys($values);
216
217
        $psrCache = new SimpleCacheAdapter(new ArrayCache());
218
        $psrCache->setMultiple($values);
219
220
        $default = uniqid('default', true);
221
        $invalid_key = uniqid('key3', true);
222
        $keys[] = $invalid_key;
223
        $values[$invalid_key] = $default;
224
225
        self::assertSame($values, $psrCache->getMultiple($keys, $default));
226
    }
227
228
    /**
229
     * @param mixed $key
230
     * @dataProvider invalidKeys
231
     */
232
    public function testGetMultipleThrowsExceptionWithInvalidKeys($key)
233
    {
234
        $this->expectException(InvalidArgumentException::class);
235
236
        $psrCache = new SimpleCacheAdapter(new ArrayCache());
237
        $psrCache->getMultiple([$key]);
238
    }
239
240
    /**
241
     * @param mixed $key
242
     * @dataProvider validKeys
243
     */
244 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...
245
    {
246
        $values = [
247
            $key => uniqid('value', true),
248
        ];
249
250
        /** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
251
        $doctrineCache = $this->createMock(FullyImplementedCache::class);
252
        $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...
253
254
        $psrCache = new SimpleCacheAdapter($doctrineCache);
0 ignored issues
show
Bug introduced by
It seems like $doctrineCache defined by $this->createMock(\Roave...mplementedCache::class) on line 251 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...
255
        $psrCache->getMultiple(new \ArrayObject(array_keys($values)));
256
    }
257
258
    public function testGetMultipleAcceptsGenerator()
259
    {
260
        $values = [
261
            uniqid('key0', true) => uniqid('value0', true),
262
            uniqid('key1', true) => uniqid('value1', true),
263
        ];
264
265
        $generator = function () use ($values) {
266
            /** @noinspection ForeachOnArrayComponentsInspection */
267
            foreach (array_keys($values) as $k) {
268
                yield $k;
269
            }
270
        };
271
272
        $psrCache = new SimpleCacheAdapter(new ArrayCache());
273
        $psrCache->setMultiple($values);
274
275
        self::assertSame($values, $psrCache->getMultiple($generator()));
276
    }
277
278 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...
279
    {
280
        $this->expectException(InvalidArgumentException::class);
281
282
        $psrCache = new SimpleCacheAdapter(new ArrayCache());
283
        $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...
284
    }
285
286
    public function testSetMultipleProxiesToSaveMultiple()
287
    {
288
        $values = [
289
            uniqid('key1', true) => uniqid('value1', true),
290
            uniqid('key2', true) => uniqid('value2', true),
291
        ];
292
293
        /** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
294
        $doctrineCache = $this->createMock(FullyImplementedCache::class);
295
        $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...
296
297
        $psrCache = new SimpleCacheAdapter($doctrineCache);
0 ignored issues
show
Bug introduced by
It seems like $doctrineCache defined by $this->createMock(\Roave...mplementedCache::class) on line 294 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...
298
        self::assertTrue($psrCache->setMultiple($values));
299
    }
300
301
    public function testSetMultipleWithDateIntervalTTL()
302
    {
303
        $values = [
304
            uniqid('key1', true) => uniqid('value1', true),
305
            uniqid('key2', true) => uniqid('value2', true),
306
        ];
307
        $keys = array_keys($values);
308
        $ttl_date = \DateInterval::createFromDateString('1 day');
309
310
        $psrCache = new SimpleCacheAdapter(new ArrayCache());
311
312
        // This does not test if ttl is correctly set to 86400 sec.
313
        self::assertTrue($psrCache->setMultiple($values, $ttl_date));
314
        self::assertSame($values, $psrCache->getMultiple($keys));
315
    }
316
317
    public function testSetMultipleWithNonPositiveTTL()
318
    {
319
        $values = [
320
            uniqid('key1', true) => uniqid('value1', true),
321
            uniqid('key2', true) => uniqid('value2', true),
322
        ];
323
        $keys = array_keys($values);
324
325
        $psrCache = new SimpleCacheAdapter(new ArrayCache());
326
        $psrCache->setMultiple($values);
327
328
        $volatile = [$keys[0] => uniqid('value3', true)];
329
        $psrCache->setMultiple($volatile, -1);
330
331
        self::assertNull($psrCache->get($keys[0]));
332
        self::assertNotNull($psrCache->get($keys[1]));
333
    }
334
335
    /**
336
     * @param mixed $ttl
337
     * @dataProvider invalidTTLs
338
     */
339
    public function testSetMultipleWithInvalidTTL($ttl)
340
    {
341
        $values = [
342
            uniqid('key1', true) => uniqid('value1', true),
343
            uniqid('key2', true) => uniqid('value2', true),
344
        ];
345
346
        $psrCache = new SimpleCacheAdapter(new ArrayCache());
347
348
        $this->expectException(InvalidArgumentException::class);
349
        $psrCache->setMultiple($values, $ttl);
350
    }
351
352 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...
353
    {
354
        $this->expectException(InvalidArgumentException::class);
355
356
        $psrCache = new SimpleCacheAdapter(new ArrayCache());
357
        $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...
358
    }
359
360
    public function testSetMultipleAcceptsGenerator()
361
    {
362
        $key0 = uniqid('key0', true);
363
        $key1 = uniqid('key1', true);
364
        $values = [
365
            $key0 => uniqid('value0', true),
366
            $key1 => uniqid('value1', true),
367
        ];
368
369
        $generator = function () use ($values) {
370
            foreach ($values as $k => $v) {
371
                yield $k => $v;
372
            }
373
        };
374
375
        $psrCache = new SimpleCacheAdapter(new ArrayCache());
376
        $psrCache->setMultiple($generator());
377
378
        self::assertSame($values[$key0], $psrCache->get($key0));
379
        self::assertSame($values[$key1], $psrCache->get($key1));
380
    }
381
382 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...
383
    {
384
        $keys = [
385
            uniqid('key1', true),
386
            uniqid('key2', true),
387
        ];
388
389
        /** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
390
        $doctrineCache = $this->createMock(FullyImplementedCache::class);
391
        $doctrineCache->expects(self::once())->method('deleteMultiple')->with($keys)->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...
392
393
        $psrCache = new SimpleCacheAdapter($doctrineCache);
0 ignored issues
show
Bug introduced by
It seems like $doctrineCache defined by $this->createMock(\Roave...mplementedCache::class) on line 390 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...
394
        self::assertTrue($psrCache->deleteMultiple($keys));
395
    }
396
397 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...
398
    {
399
        $keys = [
400
            uniqid('key1', true),
401
            uniqid('key2', true),
402
        ];
403
404
        /** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
405
        $doctrineCache = $this->createMock(FullyImplementedCache::class);
406
        $doctrineCache->expects(self::once())->method('deleteMultiple')->with($keys)->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...
407
408
        $psrCache = new SimpleCacheAdapter($doctrineCache);
0 ignored issues
show
Bug introduced by
It seems like $doctrineCache defined by $this->createMock(\Roave...mplementedCache::class) on line 405 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...
409
        self::assertFalse($psrCache->deleteMultiple($keys));
410
    }
411
412 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...
413
    {
414
        $key = uniqid('key', true);
415
416
        /** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
417
        $doctrineCache = $this->createMock(FullyImplementedCache::class);
418
        $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...
419
420
        $psrCache = new SimpleCacheAdapter($doctrineCache);
0 ignored issues
show
Bug introduced by
It seems like $doctrineCache defined by $this->createMock(\Roave...mplementedCache::class) on line 417 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...
421
        self::assertTrue($psrCache->has($key));
422
    }
423
}
424