Passed
Pull Request — master (#380)
by Wilmer
05:19 queued 02:05
created

AbstractSchemaTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 36
dl 0
loc 71
rs 10
c 1
b 0
f 0
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A testGetDefaultSchema() 0 7 1
A testColumnSchemaDbTypecastWithEmptyCharType() 0 6 1
A testGetPDOType() 0 27 2
A testIsReadQuery() 0 10 1
A testRefresh() 0 9 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Tests;
6
7
use PDO;
8
use PHPUnit\Framework\TestCase;
9
use Yiisoft\Db\Schema\ColumnSchema;
10
use Yiisoft\Db\Schema\Schema;
11
use Yiisoft\Db\Tests\Support\Assert;
12
use Yiisoft\Db\Tests\Support\TestTrait;
13
14
use function fclose;
15
use function fopen;
16
use function print_r;
17
18
abstract class AbstractSchemaTest extends TestCase
19
{
20
    use TestTrait;
21
22
    public function testColumnSchemaDbTypecastWithEmptyCharType(): void
23
    {
24
        $columnSchema = new ColumnSchema();
25
        $columnSchema->setType(Schema::TYPE_CHAR);
26
27
        $this->assertSame('', $columnSchema->dbTypecast(''));
28
    }
29
30
    public function testGetDefaultSchema(): void
31
    {
32
        $db = $this->getConnection();
33
34
        $schema = $db->getSchema();
35
36
        $this->assertNull($schema->getDefaultSchema());
37
    }
38
39
    public function testGetPDOType(): void
40
    {
41
        $values = [
42
            [null, PDO::PARAM_NULL],
43
            ['', PDO::PARAM_STR],
44
            ['hello', PDO::PARAM_STR],
45
            [0, PDO::PARAM_INT],
46
            [1, PDO::PARAM_INT],
47
            [1337, PDO::PARAM_INT],
48
            [true, PDO::PARAM_BOOL],
49
            [false, PDO::PARAM_BOOL],
50
            [$fp = fopen(__FILE__, 'rb'), PDO::PARAM_LOB],
51
        ];
52
53
        $db = $this->getConnection();
54
55
        $schema = $db->getSchema();
56
57
        foreach ($values as $value) {
58
            $this->assertSame(
59
                $value[1],
60
                $schema->getPdoType($value[0]),
61
                'type for value ' . print_r($value[0], true) . ' does not match.',
0 ignored issues
show
Bug introduced by
Are you sure print_r($value[0], true) of type string|true can be used in concatenation? ( Ignorable by Annotation )

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

61
                'type for value ' . /** @scrutinizer ignore-type */ print_r($value[0], true) . ' does not match.',
Loading history...
62
            );
63
        }
64
65
        fclose($fp);
66
    }
67
68
    public function testIsReadQuery(): void
69
    {
70
        $db = $this->getConnection();
71
72
        $schema = $db->getSchema();
73
74
        $this->assertTrue($schema->isReadQuery('SELECT * FROM tbl'));
75
        $this->assertTrue($schema->isReadQuery('SELECT * FROM tbl WHERE id=1'));
76
        $this->assertTrue($schema->isReadQuery('SELECT * FROM tbl WHERE id=1 LIMIT 1'));
77
        $this->assertTrue($schema->isReadQuery('SELECT * FROM tbl WHERE id=1 LIMIT 1 OFFSET 1'));
78
    }
79
80
    public function testRefresh(): void
81
    {
82
        $db = $this->getConnection();
83
84
        $schema = $db->getSchema();
85
        $schema->refresh();
86
87
        $this->assertSame([], Assert::getInaccessibleProperty($schema, 'tableMetadata'));
88
        $this->assertSame([], Assert::getInaccessibleProperty($schema, 'tableNames'));
89
    }
90
}
91