Passed
Pull Request — master (#372)
by Wilmer
03:25 queued 27s
created

AbstractQueryCacheTest::testQueryCache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 85
Code Lines 54

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 54
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 85
rs 9.0036

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Tests;
6
7
use PHPUnit\Framework\TestCase;
8
use Yiisoft\Db\Connection\ConnectionInterface;
9
use Yiisoft\Db\Query\Query;
10
11
abstract class AbstractQueryCacheTest extends TestCase
12
{
13
    public function testQueryCache()
14
    {
15
        $db = $this->getConnection();
0 ignored issues
show
Bug introduced by
The method getConnection() does not exist on Yiisoft\Db\Tests\AbstractQueryCacheTest. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

15
        /** @scrutinizer ignore-call */ 
16
        $db = $this->getConnection();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
16
17
        $query = (new Query($db))->select(['name'])->from('customer');
18
        $update = $db->createCommand('UPDATE {{customer}} SET [[name]] = :name WHERE [[id]] = :id');
19
20
        $this->assertEquals('user1', $query->where(['id' => 1])->scalar(), 'Asserting initial value');
21
22
        /* No cache */
23
        $update->bindValues([':id' => 1, ':name' => 'user11'])->execute();
24
25
        $this->assertEquals(
26
            'user11',
27
            $query->where(['id' => 1])->scalar(),
28
            'Query reflects DB changes when caching is disabled'
29
        );
30
31
        /* Connection cache */
32
        $db->cache(function (ConnectionInterface $db) use ($query, $update) {
33
            $this->assertEquals(
34
                'user2',
35
                $query->where(['id' => 2])->scalar(),
36
                'Asserting initial value for user #2'
37
            );
38
39
            $update->bindValues([':id' => 2, ':name' => 'user22'])->execute();
40
41
            $this->assertEquals(
42
                'user2',
43
                $query->where(['id' => 2])->scalar(),
44
                'Query does NOT reflect DB changes when wrapped in connection caching'
45
            );
46
47
            $db->noCache(function () use ($query) {
48
                $this->assertEquals(
49
                    'user22',
50
                    $query->where(['id' => 2])->scalar(),
51
                    'Query reflects DB changes when wrapped in connection caching and noCache simultaneously'
52
                );
53
            });
54
55
            $this->assertEquals(
56
                'user2',
57
                $query->where(['id' => 2])->scalar(),
58
                'Cache does not get changes after getting newer data from DB in noCache block.'
59
            );
60
        }, 10);
61
62
        $db->queryCacheEnable(false);
63
64
        $db->cache(function () use ($query, $update) {
65
            $this->assertEquals(
66
                'user22',
67
                $query->where(['id' => 2])->scalar(),
68
                'When cache is disabled for the whole connection, Query inside cache block does not get cached'
69
            );
70
71
            $update->bindValues([':id' => 2, ':name' => 'user2'])->execute();
72
73
            $this->assertEquals('user2', $query->where(['id' => 2])->scalar());
74
        }, 10);
75
76
        $db->queryCacheEnable(true);
77
        $query->cache();
78
79
        $this->assertEquals('user11', $query->where(['id' => 1])->scalar());
80
81
        $update->bindValues([':id' => 1, ':name' => 'user1'])->execute();
82
83
        $this->assertEquals(
84
            'user11',
85
            $query->where(['id' => 1])->scalar(),
86
            'When both Connection and Query have cache enabled, we get cached value'
87
        );
88
        $this->assertEquals(
89
            'user1',
90
            $query->noCache()->where(['id' => 1])->scalar(),
91
            'When Query has disabled cache, we get actual data'
92
        );
93
94
        $db->cache(function () use ($query) {
95
            $this->assertEquals('user1', $query->noCache()->where(['id' => 1])->scalar());
96
            $this->assertEquals('user11', $query->cache()->where(['id' => 1])->scalar());
97
        }, 10);
98
    }
99
}
100