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

AbstractQuoterTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 41
c 1
b 0
f 0
dl 0
loc 94
rs 10
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A generateQuoterEscapingValues() 0 20 4
A testQuoteSimpleTableName() 0 12 1
A testQuoterEscapingValue() 0 19 2
A testQuoteSimpleColumnName() 0 15 1
A testQuoteColumnName() 0 8 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Tests;
6
7
use PHPUnit\Framework\TestCase;
8
9
abstract class AbstractQuoterTest extends TestCase
10
{
11
    public function testQuoterEscapingValue()
12
    {
13
        $db = $this->getConnection();
0 ignored issues
show
Bug introduced by
The method getConnection() does not exist on Yiisoft\Db\Tests\AbstractQuoterTest. ( Ignorable by Annotation )

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

13
        /** @scrutinizer ignore-call */ 
14
        $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...
14
15
        $quoter = $db->getQuoter();
16
17
        $db->createCommand('delete from {{quoter}}')->execute();
18
        $data = $this->generateQuoterEscapingValues();
19
20
        foreach ($data as $index => $value) {
21
            $quotedName = $quoter->quoteValue('testValue_' . $index);
22
            $quoteValue = $quoter->quoteValue($value);
23
24
            $db->createCommand(
25
                'insert into {{quoter}}([[name]], [[description]]) values(' . $quotedName . ', ' . $quoteValue . ')'
26
            )->execute();
27
            $result = $db->createCommand('select * from {{quoter}} where [[name]]=' . $quotedName)->queryOne();
28
29
            $this->assertSame($value, $result['description']);
30
        }
31
    }
32
33
    /**
34
     * @dataProvider \Yiisoft\Db\Tests\Provider\QuoterProvider::columnNames
35
     */
36
    public function testQuoteColumnName(string $columnName, string $expectedQuotedColumnName): void
37
    {
38
        $db = $this->getConnection();
39
40
        $quoter = $db->getQuoter();
41
        $quoted = $quoter->quoteColumnName($columnName);
42
43
        $this->assertSame($expectedQuotedColumnName, $quoted);
44
    }
45
46
    /**
47
     * @dataProvider \Yiisoft\Db\Tests\Provider\QuoterProvider::simpleColumnNames
48
     */
49
    public function testQuoteSimpleColumnName(
50
        string $columnName,
51
        string $expectedQuotedColumnName,
52
        string $expectedUnQuotedColunName
53
    ): void {
54
        $db = $this->getConnection();
55
56
        $quoter = $db->getQuoter();
57
        $quoted = $quoter->quoteSimpleColumnName($columnName);
58
59
        $this->assertSame($expectedQuotedColumnName, $quoted);
60
61
        $unQuoted = $quoter->unquoteSimpleColumnName($quoted);
62
63
        $this->assertSame($expectedUnQuotedColunName, $unQuoted);
64
    }
65
66
    /**
67
     * @dataProvider \Yiisoft\Db\Tests\Provider\QuoterProvider::simpleTableNames()
68
     */
69
    public function testQuoteSimpleTableName(string $tableName, string $expectedTableName): void
70
    {
71
        $db = $this->getConnection();
72
73
        $quoter = $db->getQuoter();
74
        $unQuoted = $quoter->unquoteSimpleTableName($quoter->quoteSimpleTableName($tableName));
75
76
        $this->assertSame($expectedTableName, $unQuoted);
77
78
        $unQuoted = $quoter->unquoteSimpleTableName($quoter->quoteTableName($tableName));
79
80
        $this->assertSame($expectedTableName, $unQuoted);
81
    }
82
83
    private function generateQuoterEscapingValues()
84
    {
85
        $result = [];
86
        $stringLength = 16;
87
88
        for ($i = 32; $i < 128 - $stringLength; $i += $stringLength) {
89
            $str = '';
90
            for ($symbol = $i; $symbol < $i + $stringLength; $symbol++) {
91
                $str .= mb_chr($symbol, 'UTF-8');
92
            }
93
            $result[] = $str;
94
95
            $str = '';
96
            for ($symbol = $i; $symbol < $i + $stringLength; $symbol++) {
97
                $str .= mb_chr($symbol, 'UTF-8') . mb_chr($symbol, 'UTF-8');
98
            }
99
            $result[] = $str;
100
        }
101
102
        return $result;
103
    }
104
}
105