Completed
Push — master ( 23b86d...e09bdb )
by Marco
9s
created

testGetMultipleThrowsExceptionWhenNotArrayOrTraversable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 7
Ratio 100 %

Importance

Changes 0
Metric Value
dl 7
loc 7
rs 9.4285
c 0
b 0
f 0
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\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
    public function testSetProxiesToDoctrineSave()
124
    {
125
        $key = uniqid('key', true);
126
        $value = uniqid('value', true);
127
        $ttl = random_int(1000, 9999);
128
129
        /** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
130
        $doctrineCache = $this->createMock(FullyImplementedCache::class);
131
        $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...
132
133
        $psrCache = new SimpleCacheAdapter($doctrineCache);
0 ignored issues
show
Bug introduced by
It seems like $doctrineCache defined by $this->createMock(\Roave...mplementedCache::class) on line 130 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...
134
        self::assertTrue($psrCache->set($key, $value, $ttl));
135
    }
136
137
    public function testSetWithDateIntervalTTL()
138
    {
139
        $key = uniqid('key', true);
140
        $value = uniqid('value', true);
141
        $ttl_date = \DateInterval::createFromDateString('1 day');
142
143
        $psrCache = new SimpleCacheAdapter(new ArrayCache());
144
145
        // This does not test if ttl is correctly set to 86400 sec.
146
        self::assertTrue($psrCache->set($key, $value, $ttl_date));
147
        self::assertSame($psrCache->get($key), $value);
148
    }
149
150
    public function testSetWithNonPositiveTTL()
151
    {
152
        $key = uniqid('key', true);
153
        $value = uniqid('value', true);
154
        $ttl = random_int(1000, 9999);
155
156
        $psrCache = new SimpleCacheAdapter(new ArrayCache());
157
158
        $psrCache->set($key, $value, $ttl);
159
        self::assertSame($psrCache->get($key), $value);
160
161
        $psrCache->set($key, $value, -1);
162
        self::assertNull($psrCache->get($key), null);
163
    }
164
165
    /**
166
     * @param mixed $ttl
167
     * @dataProvider invalidTTLs
168
     */
169
    public function testSetWithInvalidTTL($ttl)
170
    {
171
        $key = uniqid('key', true);
172
        $value = uniqid('value', true);
173
174
        $psrCache = new SimpleCacheAdapter(new ArrayCache());
175
176
        $this->expectException(InvalidArgumentException::class);
177
        $psrCache->set($key, $value, $ttl);
178
    }
179
180 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...
181
    {
182
        $key = uniqid('key', true);
183
184
        /** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
185
        $doctrineCache = $this->createMock(FullyImplementedCache::class);
186
        $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...
187
188
        $psrCache = new SimpleCacheAdapter($doctrineCache);
0 ignored issues
show
Bug introduced by
It seems like $doctrineCache defined by $this->createMock(\Roave...mplementedCache::class) on line 185 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...
189
        self::assertTrue($psrCache->delete($key));
190
    }
191
192
    public function testClearProxiesToDeleteAll()
193
    {
194
        /** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
195
        $doctrineCache = $this->createMock(FullyImplementedCache::class);
196
        $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...
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->clear());
200
    }
201
202
    public function testGetMultipleProxiesToFetchMultiple()
203
    {
204
        $values = [
205
            uniqid('key1', true) => uniqid('value1', true),
206
            uniqid('key2', true) => uniqid('value2', true),
207
        ];
208
        $keys = array_keys($values);
209
210
        /** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
211
        $doctrineCache = $this->createMock(FullyImplementedCache::class);
212
        $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...
213
214
        $psrCache = new SimpleCacheAdapter($doctrineCache);
0 ignored issues
show
Bug introduced by
It seems like $doctrineCache defined by $this->createMock(\Roave...mplementedCache::class) on line 211 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...
215
        self::assertSame($values, $psrCache->getMultiple($keys));
216
    }
217
218
    public function testGetMultipleWithPartialKeys()
219
    {
220
        $values = [
221
            uniqid('key1', true) => uniqid('value1', true),
222
            uniqid('key2', true) => uniqid('value2', true),
223
        ];
224
        $keys = array_keys($values);
225
226
        $psrCache = new SimpleCacheAdapter(new ArrayCache());
227
        $psrCache->setMultiple($values);
228
229
        $default = uniqid('default', true);
230
        $invalid_key = uniqid('key3', true);
231
        $keys[] = $invalid_key;
232
        $values[$invalid_key] = $default;
233
234
        self::assertSame($values, $psrCache->getMultiple($keys, $default));
235
    }
236
237
    /**
238
     * @param mixed $key
239
     * @dataProvider invalidKeys
240
     */
241
    public function testGetMultipleThrowsExceptionWithInvalidKeys($key)
242
    {
243
        $this->expectException(InvalidArgumentException::class);
244
245
        $psrCache = new SimpleCacheAdapter(new ArrayCache());
246
        $psrCache->getMultiple([$key]);
247
    }
248
249
    /**
250
     * @param mixed $key
251
     * @dataProvider validKeys
252
     */
253 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...
254
    {
255
        $values = [
256
            $key => uniqid('value', true),
257
        ];
258
259
        /** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
260
        $doctrineCache = $this->createMock(FullyImplementedCache::class);
261
        $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...
262
263
        $psrCache = new SimpleCacheAdapter($doctrineCache);
0 ignored issues
show
Bug introduced by
It seems like $doctrineCache defined by $this->createMock(\Roave...mplementedCache::class) on line 260 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...
264
        $psrCache->getMultiple(new \ArrayObject(array_keys($values)));
265
    }
266
267 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...
268
    {
269
        $this->expectException(InvalidArgumentException::class);
270
271
        $psrCache = new SimpleCacheAdapter(new ArrayCache());
272
        $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...
273
    }
274
275
    public function testSetMultipleProxiesToSaveMultiple()
276
    {
277
        $values = [
278
            uniqid('key1', true) => uniqid('value1', true),
279
            uniqid('key2', true) => uniqid('value2', true),
280
        ];
281
282
        /** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
283
        $doctrineCache = $this->createMock(FullyImplementedCache::class);
284
        $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...
285
286
        $psrCache = new SimpleCacheAdapter($doctrineCache);
0 ignored issues
show
Bug introduced by
It seems like $doctrineCache defined by $this->createMock(\Roave...mplementedCache::class) on line 283 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...
287
        self::assertTrue($psrCache->setMultiple($values));
288
    }
289
290
    public function testSetMultipleWithDateIntervalTTL()
291
    {
292
        $values = [
293
            uniqid('key1', true) => uniqid('value1', true),
294
            uniqid('key2', true) => uniqid('value2', true),
295
        ];
296
        $keys = array_keys($values);
297
        $ttl_date = \DateInterval::createFromDateString('1 day');
298
299
        $psrCache = new SimpleCacheAdapter(new ArrayCache());
300
301
        // This does not test if ttl is correctly set to 86400 sec.
302
        self::assertTrue($psrCache->setMultiple($values, $ttl_date));
303
        self::assertSame($values, $psrCache->getMultiple($keys));
304
    }
305
306
    public function testSetMultipleWithNonPositiveTTL()
307
    {
308
        $values = [
309
            uniqid('key1', true) => uniqid('value1', true),
310
            uniqid('key2', true) => uniqid('value2', true),
311
        ];
312
        $keys = array_keys($values);
313
314
        $psrCache = new SimpleCacheAdapter(new ArrayCache());
315
        $psrCache->setMultiple($values);
316
317
        $volatile = [$keys[0] => uniqid('value3', true)];
318
        $psrCache->setMultiple($volatile, -1);
319
320
        self::assertNull($psrCache->get($keys[0]));
321
        self::assertNotNull($psrCache->get($keys[1]));
322
    }
323
324
    /**
325
     * @param mixed $ttl
326
     * @dataProvider invalidTTLs
327
     */
328
    public function testSetMultipleWithInvalidTTL($ttl)
329
    {
330
        $values = [
331
            uniqid('key1', true) => uniqid('value1', true),
332
            uniqid('key2', true) => uniqid('value2', true),
333
        ];
334
335
        $psrCache = new SimpleCacheAdapter(new ArrayCache());
336
337
        $this->expectException(InvalidArgumentException::class);
338
        $psrCache->setMultiple($values, $ttl);
339
    }
340
341 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...
342
    {
343
        $this->expectException(InvalidArgumentException::class);
344
345
        $psrCache = new SimpleCacheAdapter(new ArrayCache());
346
        $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...
347
    }
348
349 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...
350
    {
351
        $keys = [
352
            uniqid('key1', true),
353
            uniqid('key2', true),
354
        ];
355
356
        /** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
357
        $doctrineCache = $this->createMock(FullyImplementedCache::class);
358
        $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...
359
        $doctrineCache->expects(self::at(1))->method('delete')->with($keys[1])->willReturn(true);
360
361
        $psrCache = new SimpleCacheAdapter($doctrineCache);
0 ignored issues
show
Bug introduced by
It seems like $doctrineCache defined by $this->createMock(\Roave...mplementedCache::class) on line 357 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...
362
        self::assertTrue($psrCache->deleteMultiple($keys));
363
    }
364
365 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...
366
    {
367
        $keys = [
368
            uniqid('key1', true),
369
            uniqid('key2', true),
370
        ];
371
372
        /** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
373
        $doctrineCache = $this->createMock(FullyImplementedCache::class);
374
        $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...
375
        $doctrineCache->expects(self::at(1))->method('delete')->with($keys[1])->willReturn(true);
376
377
        $psrCache = new SimpleCacheAdapter($doctrineCache);
0 ignored issues
show
Bug introduced by
It seems like $doctrineCache defined by $this->createMock(\Roave...mplementedCache::class) on line 373 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...
378
        self::assertFalse($psrCache->deleteMultiple($keys));
379
    }
380
381 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...
382
    {
383
        $key = uniqid('key', true);
384
385
        /** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
386
        $doctrineCache = $this->createMock(FullyImplementedCache::class);
387
        $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...
388
389
        $psrCache = new SimpleCacheAdapter($doctrineCache);
0 ignored issues
show
Bug introduced by
It seems like $doctrineCache defined by $this->createMock(\Roave...mplementedCache::class) on line 386 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...
390
        self::assertTrue($psrCache->has($key));
391
    }
392
}
393