Passed
Pull Request — master (#372)
by Wilmer
03:00
created

AbstractCommandQueryCacheTest   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 32
c 1
b 0
f 0
dl 0
loc 52
rs 10
wmc 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A testQueryCache() 0 50 1
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
10
abstract class AbstractCommandQueryCacheTest extends TestCase
11
{
12
    public function testQueryCache(): void
13
    {
14
        $db = $this->getConnection();
0 ignored issues
show
Bug introduced by
The method getConnection() does not exist on Yiisoft\Db\Tests\AbstractCommandQueryCacheTest. ( Ignorable by Annotation )

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

14
        /** @scrutinizer ignore-call */ 
15
        $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...
15
16
        $command = $db->createCommand('SELECT [[name]] FROM {{customer}} WHERE [[id]] = :id');
17
18
        $this->assertSame('user1', $command->bindValue(':id', 1)->queryScalar());
19
20
        $update = $db->createCommand('UPDATE {{customer}} SET [[name]] = :name WHERE [[id]] = :id');
21
        $update->bindValues([':id' => 1, ':name' => 'user11'])->execute();
22
23
        $this->assertSame('user11', $command->bindValue(':id', 1)->queryScalar());
24
25
        $db->cache(function (ConnectionInterface $db) use ($command, $update) {
26
            $this->assertSame('user2', $command->bindValue(':id', 2)->queryScalar());
27
28
            $update->bindValues([':id' => 2, ':name' => 'user22'])->execute();
29
30
            $this->assertSame('user2', $command->bindValue(':id', 2)->queryScalar());
31
32
            $db->noCache(function () use ($command) {
33
                $this->assertSame('user22', $command->bindValue(':id', 2)->queryScalar());
34
            });
35
36
            $this->assertSame('user2', $command->bindValue(':id', 2)->queryScalar());
37
        }, 10);
38
39
        $db->queryCacheEnable(false);
40
41
        $db->cache(function () use ($command, $update) {
42
            $this->assertSame('user22', $command->bindValue(':id', 2)->queryScalar());
43
            $update->bindValues([':id' => 2, ':name' => 'user2'])->execute();
44
            $this->assertSame('user2', $command->bindValue(':id', 2)->queryScalar());
45
        }, 10);
46
47
        $db->queryCacheEnable(true);
48
        $command = $db->createCommand('SELECT [[name]] FROM {{customer}} WHERE [[id]] = :id')->cache();
49
50
        $this->assertSame('user11', $command->bindValue(':id', 1)->queryScalar());
51
52
        $update->bindValues([':id' => 1, ':name' => 'user1'])->execute();
53
54
        $this->assertSame('user11', $command->bindValue(':id', 1)->queryScalar());
55
        $this->assertSame('user1', $command->noCache()->bindValue(':id', 1)->queryScalar());
56
57
        $command = $db->createCommand('SELECT [[name]] FROM {{customer}} WHERE [[id]] = :id');
58
        $db->cache(function () use ($command) {
59
            $this->assertSame('user11', $command->bindValue(':id', 1)->queryScalar());
60
            $this->assertSame('user1', $command->noCache()->bindValue(':id', 1)->queryScalar());
61
        }, 10);
62
    }
63
}
64