Completed
Push — master ( 6c409f...23b86d )
by James
11s
created

testGetProxiesToDoctrineFetch()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
dl 0
loc 12
rs 9.4285
c 2
b 0
f 1
cc 1
eloc 7
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 testConstructorThrowsExceptionWhenNotMultiPuttableCacheIsUsed()
37
    {
38
        /** @var NotMultiPuttableCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
39
        $doctrineCache = $this->createMock(NotMultiPuttableCache::class);
40
41
        $this->expectException(CacheException::class);
42
        new SimpleCacheAdapter($doctrineCache);
0 ignored issues
show
Bug introduced by
It seems like $doctrineCache defined by $this->createMock(\Roave...tiPuttableCache::class) on line 39 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...
43
    }
44
45
    public function testConstructorThrowsExceptionWhenNotClearableCacheIsUsed()
46
    {
47
        /** @var NotClearableCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
48
        $doctrineCache = $this->createMock(NotClearableCache::class);
49
50
        $this->expectException(CacheException::class);
51
        new SimpleCacheAdapter($doctrineCache);
0 ignored issues
show
Bug introduced by
It seems like $doctrineCache defined by $this->createMock(\Roave...tClearableCache::class) on line 48 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...
52
    }
53
54
    public function testConstructorThrowsExceptionWhenNotMultiGettableCacheIsUsed()
55
    {
56
        /** @var NotMultiGettableCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
57
        $doctrineCache = $this->createMock(NotMultiGettableCache::class);
58
59
        $this->expectException(CacheException::class);
60
        new SimpleCacheAdapter($doctrineCache);
0 ignored issues
show
Bug introduced by
It seems like $doctrineCache defined by $this->createMock(\Roave...tiGettableCache::class) on line 57 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...
61
    }
62
63
    public function testGetProxiesToDoctrineFetch()
64
    {
65
        $key = uniqid('key', true);
66
        $value = uniqid('value', true);
67
68
        /** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
69
        $doctrineCache = $this->createMock(FullyImplementedCache::class);
70
        $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...
71
72
        $psrCache = new SimpleCacheAdapter($doctrineCache);
0 ignored issues
show
Bug introduced by
It seems like $doctrineCache defined by $this->createMock(\Roave...mplementedCache::class) on line 69 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...
73
        self::assertSame($value, $psrCache->get($key));
74
    }
75
76
    public function testGetWithNotExistingKey()
77
    {
78
        $key = uniqid('key', true);
79
        $value = uniqid('value', true);
80
81
        $psrCache = new SimpleCacheAdapter(new ArrayCache());
82
        $psrCache->set($key, $value);
83
84
        $default = uniqid('default', true);
85
        self::assertSame($value, $psrCache->get($key, $default));
86
87
        $anotherKey = uniqid('key', true);
88
        self::assertSame($default, $psrCache->get($anotherKey, $default));
89
    }
90
91
    public function testSetProxiesToDoctrineSave()
92
    {
93
        $key = uniqid('key', true);
94
        $value = uniqid('value', true);
95
        $ttl = random_int(1000, 9999);
96
97
        /** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
98
        $doctrineCache = $this->createMock(FullyImplementedCache::class);
99
        $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...
100
101
        $psrCache = new SimpleCacheAdapter($doctrineCache);
0 ignored issues
show
Bug introduced by
It seems like $doctrineCache defined by $this->createMock(\Roave...mplementedCache::class) on line 98 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...
102
        self::assertTrue($psrCache->set($key, $value, $ttl));
103
    }
104
105
    public function testSetWithDateIntervalTTL()
106
    {
107
        $key = uniqid('key', true);
108
        $value = uniqid('value', true);
109
        $ttl_date = \DateInterval::createFromDateString('1 day');
110
111
        $psrCache = new SimpleCacheAdapter(new ArrayCache());
112
113
        // This does not test if ttl is correctly set to 86400 sec.
114
        self::assertTrue($psrCache->set($key, $value, $ttl_date));
115
        self::assertSame($psrCache->get($key), $value);
116
    }
117
118
    public function testSetWithNonPositiveTTL()
119
    {
120
        $key = uniqid('key', true);
121
        $value = uniqid('value', true);
122
        $ttl = random_int(1000, 9999);
123
124
        $psrCache = new SimpleCacheAdapter(new ArrayCache());
125
126
        $psrCache->set($key, $value, $ttl);
127
        self::assertSame($psrCache->get($key), $value);
128
129
        $psrCache->set($key, $value, -1);
130
        self::assertNull($psrCache->get($key), null);
131
    }
132
133
    /**
134
     * @param mixed $ttl
135
     * @dataProvider invalidTTLs
136
     */
137
    public function testSetWithInvalidTTL($ttl)
138
    {
139
        $key = uniqid('key', true);
140
        $value = uniqid('value', true);
141
142
        $psrCache = new SimpleCacheAdapter(new ArrayCache());
143
144
        $this->expectException(InvalidArgumentException::class);
145
        $psrCache->set($key, $value, $ttl);
146
    }
147
148 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...
149
    {
150
        $key = uniqid('key', true);
151
152
        /** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
153
        $doctrineCache = $this->createMock(FullyImplementedCache::class);
154
        $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...
155
156
        $psrCache = new SimpleCacheAdapter($doctrineCache);
0 ignored issues
show
Bug introduced by
It seems like $doctrineCache defined by $this->createMock(\Roave...mplementedCache::class) on line 153 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...
157
        self::assertTrue($psrCache->delete($key));
158
    }
159
160
    public function testClearProxiesToDeleteAll()
161
    {
162
        /** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
163
        $doctrineCache = $this->createMock(FullyImplementedCache::class);
164
        $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...
165
166
        $psrCache = new SimpleCacheAdapter($doctrineCache);
0 ignored issues
show
Bug introduced by
It seems like $doctrineCache defined by $this->createMock(\Roave...mplementedCache::class) on line 163 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...
167
        self::assertTrue($psrCache->clear());
168
    }
169
170
    public function testGetMultipleProxiesToFetchMultiple()
171
    {
172
        $values = [
173
            uniqid('key1', true) => uniqid('value1', true),
174
            uniqid('key2', true) => uniqid('value2', true),
175
        ];
176
        $keys = array_keys($values);
177
178
        /** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
179
        $doctrineCache = $this->createMock(FullyImplementedCache::class);
180
        $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...
181
182
        $psrCache = new SimpleCacheAdapter($doctrineCache);
0 ignored issues
show
Bug introduced by
It seems like $doctrineCache defined by $this->createMock(\Roave...mplementedCache::class) on line 179 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...
183
        self::assertSame($values, $psrCache->getMultiple($keys));
0 ignored issues
show
Documentation introduced by
$keys is of type array<integer,string>, but the function expects a object<Psr\SimpleCache\iterable>.

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...
184
    }
185
186
    public function testGetMultipleWithPartialKeys()
187
    {
188
        $values = [
189
            uniqid('key1', true) => uniqid('value1', true),
190
            uniqid('key2', true) => uniqid('value2', true),
191
        ];
192
        $keys = array_keys($values);
193
194
        $psrCache = new SimpleCacheAdapter(new ArrayCache());
195
        $psrCache->setMultiple($values);
0 ignored issues
show
Documentation introduced by
$values is of type array<string,string>, but the function expects a object<Psr\SimpleCache\iterable>.

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...
196
197
        $default = uniqid('default', true);
198
        $invalid_key = uniqid('key3', true);
199
        $keys[] = $invalid_key;
200
        $values[$invalid_key] = $default;
201
202
        self::assertSame($values, $psrCache->getMultiple($keys, $default));
0 ignored issues
show
Documentation introduced by
$keys is of type array<integer,string>, but the function expects a object<Psr\SimpleCache\iterable>.

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...
203
    }
204
205
    public function testSetMultipleProxiesToSaveMultiple()
206
    {
207
        $values = [
208
            uniqid('key1', true) => uniqid('value1', true),
209
            uniqid('key2', true) => uniqid('value2', true),
210
        ];
211
212
        /** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
213
        $doctrineCache = $this->createMock(FullyImplementedCache::class);
214
        $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...
215
216
        $psrCache = new SimpleCacheAdapter($doctrineCache);
0 ignored issues
show
Bug introduced by
It seems like $doctrineCache defined by $this->createMock(\Roave...mplementedCache::class) on line 213 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...
217
        self::assertTrue($psrCache->setMultiple($values));
0 ignored issues
show
Documentation introduced by
$values is of type array<string,string>, but the function expects a object<Psr\SimpleCache\iterable>.

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...
218
    }
219
220
    public function testSetMultipleWithDateIntervalTTL()
221
    {
222
        $values = [
223
            uniqid('key1', true) => uniqid('value1', true),
224
            uniqid('key2', true) => uniqid('value2', true),
225
        ];
226
        $keys = array_keys($values);
227
        $ttl_date = \DateInterval::createFromDateString('1 day');
228
229
        $psrCache = new SimpleCacheAdapter(new ArrayCache());
230
231
        // This does not test if ttl is correctly set to 86400 sec.
232
        self::assertTrue($psrCache->setMultiple($values, $ttl_date));
0 ignored issues
show
Documentation introduced by
$values is of type array<string,string>, but the function expects a object<Psr\SimpleCache\iterable>.

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...
233
        self::assertSame($values, $psrCache->getMultiple($keys));
0 ignored issues
show
Documentation introduced by
$keys is of type array<integer,string>, but the function expects a object<Psr\SimpleCache\iterable>.

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...
234
    }
235
236
    public function testSetMultipleWithNonPositiveTTL()
237
    {
238
        $values = [
239
            uniqid('key1', true) => uniqid('value1', true),
240
            uniqid('key2', true) => uniqid('value2', true),
241
        ];
242
        $keys = array_keys($values);
243
244
        $psrCache = new SimpleCacheAdapter(new ArrayCache());
245
        $psrCache->setMultiple($values);
0 ignored issues
show
Documentation introduced by
$values is of type array<string,string>, but the function expects a object<Psr\SimpleCache\iterable>.

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...
246
247
        $volatile = [$keys[0] => uniqid('value3', true)];
248
        $psrCache->setMultiple($volatile, -1);
0 ignored issues
show
Documentation introduced by
$volatile is of type array<string,string>, but the function expects a object<Psr\SimpleCache\iterable>.

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...
249
250
        self::assertNull($psrCache->get($keys[0]));
251
        self::assertNotNull($psrCache->get($keys[1]));
252
    }
253
254
    /**
255
     * @param mixed $ttl
256
     * @dataProvider invalidTTLs
257
     */
258
    public function testSetMultipleWithInvalidTTL($ttl)
259
    {
260
        $values = [
261
            uniqid('key1', true) => uniqid('value1', true),
262
            uniqid('key2', true) => uniqid('value2', true),
263
        ];
264
265
        $psrCache = new SimpleCacheAdapter(new ArrayCache());
266
267
        $this->expectException(InvalidArgumentException::class);
268
        $psrCache->setMultiple($values, $ttl);
0 ignored issues
show
Documentation introduced by
$values is of type array<string,string>, but the function expects a object<Psr\SimpleCache\iterable>.

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...
269
    }
270
271 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...
272
    {
273
        $keys = [
274
            uniqid('key1', true),
275
            uniqid('key2', true),
276
        ];
277
278
        /** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
279
        $doctrineCache = $this->createMock(FullyImplementedCache::class);
280
        $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...
281
        $doctrineCache->expects(self::at(1))->method('delete')->with($keys[1])->willReturn(true);
282
283
        $psrCache = new SimpleCacheAdapter($doctrineCache);
0 ignored issues
show
Bug introduced by
It seems like $doctrineCache defined by $this->createMock(\Roave...mplementedCache::class) on line 279 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...
284
        self::assertTrue($psrCache->deleteMultiple($keys));
0 ignored issues
show
Documentation introduced by
$keys is of type array<integer,string,{"0":"string","1":"string"}>, but the function expects a object<Psr\SimpleCache\iterable>.

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...
285
    }
286
287 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...
288
    {
289
        $keys = [
290
            uniqid('key1', true),
291
            uniqid('key2', true),
292
        ];
293
294
        /** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
295
        $doctrineCache = $this->createMock(FullyImplementedCache::class);
296
        $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...
297
        $doctrineCache->expects(self::at(1))->method('delete')->with($keys[1])->willReturn(true);
298
299
        $psrCache = new SimpleCacheAdapter($doctrineCache);
0 ignored issues
show
Bug introduced by
It seems like $doctrineCache defined by $this->createMock(\Roave...mplementedCache::class) on line 295 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...
300
        self::assertFalse($psrCache->deleteMultiple($keys));
0 ignored issues
show
Documentation introduced by
$keys is of type array<integer,string,{"0":"string","1":"string"}>, but the function expects a object<Psr\SimpleCache\iterable>.

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...
301
    }
302
303 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...
304
    {
305
        $key = uniqid('key', true);
306
307
        /** @var FullyImplementedCache|\PHPUnit_Framework_MockObject_MockObject $doctrineCache */
308
        $doctrineCache = $this->createMock(FullyImplementedCache::class);
309
        $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...
310
311
        $psrCache = new SimpleCacheAdapter($doctrineCache);
0 ignored issues
show
Bug introduced by
It seems like $doctrineCache defined by $this->createMock(\Roave...mplementedCache::class) on line 308 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...
312
        self::assertTrue($psrCache->has($key));
313
    }
314
}
315