Completed
Push — master ( 6713c8...36f283 )
by Marco
14s
created

testSetMultipleAcceptsGenerator()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
cc 2
eloc 13
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
    public function testGetMultipleAcceptsGenerator()
278
    {
279
        $values = [
280
            uniqid('key0', true) => uniqid('value0', true),
281
            uniqid('key1', true) => uniqid('value1', true),
282
        ];
283
284
        $generator = function () use ($values) {
285
            /** @noinspection ForeachOnArrayComponentsInspection */
286
            foreach (array_keys($values) as $k) {
287
                yield $k;
288
            }
289
        };
290
291
        $psrCache = new SimpleCacheAdapter(new ArrayCache());
292
        $psrCache->setMultiple($values);
293
294
        self::assertSame($values, $psrCache->getMultiple($generator()));
295
    }
296
297 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...
298
    {
299
        $this->expectException(InvalidArgumentException::class);
300
301
        $psrCache = new SimpleCacheAdapter(new ArrayCache());
302
        $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...
303
    }
304
305
    public function testSetMultipleProxiesToSaveMultiple()
306
    {
307
        $values = [
308
            uniqid('key1', true) => uniqid('value1', true),
309
            uniqid('key2', true) => uniqid('value2', true),
310
        ];
311
312
        /** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
313
        $doctrineCache = $this->createMock(FullyImplementedCache::class);
314
        $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...
315
316
        $psrCache = new SimpleCacheAdapter($doctrineCache);
0 ignored issues
show
Bug introduced by
It seems like $doctrineCache defined by $this->createMock(\Roave...mplementedCache::class) on line 313 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...
317
        self::assertTrue($psrCache->setMultiple($values));
318
    }
319
320
    public function testSetMultipleWithDateIntervalTTL()
321
    {
322
        $values = [
323
            uniqid('key1', true) => uniqid('value1', true),
324
            uniqid('key2', true) => uniqid('value2', true),
325
        ];
326
        $keys = array_keys($values);
327
        $ttl_date = \DateInterval::createFromDateString('1 day');
328
329
        $psrCache = new SimpleCacheAdapter(new ArrayCache());
330
331
        // This does not test if ttl is correctly set to 86400 sec.
332
        self::assertTrue($psrCache->setMultiple($values, $ttl_date));
333
        self::assertSame($values, $psrCache->getMultiple($keys));
334
    }
335
336
    public function testSetMultipleWithNonPositiveTTL()
337
    {
338
        $values = [
339
            uniqid('key1', true) => uniqid('value1', true),
340
            uniqid('key2', true) => uniqid('value2', true),
341
        ];
342
        $keys = array_keys($values);
343
344
        $psrCache = new SimpleCacheAdapter(new ArrayCache());
345
        $psrCache->setMultiple($values);
346
347
        $volatile = [$keys[0] => uniqid('value3', true)];
348
        $psrCache->setMultiple($volatile, -1);
349
350
        self::assertNull($psrCache->get($keys[0]));
351
        self::assertNotNull($psrCache->get($keys[1]));
352
    }
353
354
    /**
355
     * @param mixed $ttl
356
     * @dataProvider invalidTTLs
357
     */
358
    public function testSetMultipleWithInvalidTTL($ttl)
359
    {
360
        $values = [
361
            uniqid('key1', true) => uniqid('value1', true),
362
            uniqid('key2', true) => uniqid('value2', true),
363
        ];
364
365
        $psrCache = new SimpleCacheAdapter(new ArrayCache());
366
367
        $this->expectException(InvalidArgumentException::class);
368
        $psrCache->setMultiple($values, $ttl);
369
    }
370
371 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...
372
    {
373
        $this->expectException(InvalidArgumentException::class);
374
375
        $psrCache = new SimpleCacheAdapter(new ArrayCache());
376
        $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...
377
    }
378
379
    public function testSetMultipleAcceptsGenerator()
380
    {
381
        $key0 = uniqid('key0', true);
382
        $key1 = uniqid('key1', true);
383
        $values = [
384
            $key0 => uniqid('value0', true),
385
            $key1 => uniqid('value1', true),
386
        ];
387
388
        $generator = function () use ($values) {
389
            foreach ($values as $k => $v) {
390
                yield $k => $v;
391
            }
392
        };
393
394
        $psrCache = new SimpleCacheAdapter(new ArrayCache());
395
        $psrCache->setMultiple($generator());
396
397
        self::assertSame($values[$key0], $psrCache->get($key0));
398
        self::assertSame($values[$key1], $psrCache->get($key1));
399
    }
400
401 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...
402
    {
403
        $keys = [
404
            uniqid('key1', true),
405
            uniqid('key2', true),
406
        ];
407
408
        /** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
409
        $doctrineCache = $this->createMock(FullyImplementedCache::class);
410
        $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...
411
        $doctrineCache->expects(self::at(1))->method('delete')->with($keys[1])->willReturn(true);
412
413
        $psrCache = new SimpleCacheAdapter($doctrineCache);
0 ignored issues
show
Bug introduced by
It seems like $doctrineCache defined by $this->createMock(\Roave...mplementedCache::class) on line 409 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...
414
        self::assertTrue($psrCache->deleteMultiple($keys));
415
    }
416
417 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...
418
    {
419
        $keys = [
420
            uniqid('key1', true),
421
            uniqid('key2', true),
422
        ];
423
424
        /** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
425
        $doctrineCache = $this->createMock(FullyImplementedCache::class);
426
        $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...
427
        $doctrineCache->expects(self::at(1))->method('delete')->with($keys[1])->willReturn(true);
428
429
        $psrCache = new SimpleCacheAdapter($doctrineCache);
0 ignored issues
show
Bug introduced by
It seems like $doctrineCache defined by $this->createMock(\Roave...mplementedCache::class) on line 425 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...
430
        self::assertFalse($psrCache->deleteMultiple($keys));
431
    }
432
433 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...
434
    {
435
        $key = uniqid('key', true);
436
437
        /** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
438
        $doctrineCache = $this->createMock(FullyImplementedCache::class);
439
        $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...
440
441
        $psrCache = new SimpleCacheAdapter($doctrineCache);
0 ignored issues
show
Bug introduced by
It seems like $doctrineCache defined by $this->createMock(\Roave...mplementedCache::class) on line 438 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...
442
        self::assertTrue($psrCache->has($key));
443
    }
444
}
445