Passed
Pull Request — master (#412)
by Wilmer
36:35 queued 23:53
created

AbstractSchemaTest::testGetPDOType()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 27
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 18
nc 2
nop 0
dl 0
loc 27
rs 9.6666
c 1
b 0
f 0
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\ColumnSchemaBuilder;
11
use Yiisoft\Db\Schema\Schema;
12
use Yiisoft\Db\Tests\Support\Assert;
13
use Yiisoft\Db\Tests\Support\TestTrait;
14
15
use function fclose;
16
use function fopen;
17
use function print_r;
18
19
abstract class AbstractSchemaTest extends TestCase
20
{
21
    use TestTrait;
22
23
    public function testCreateColumnSchemaBuilder(): void
24
    {
25
        $db = $this->getConnection();
26
27
        $schema = $db->getSchema();
0 ignored issues
show
Unused Code introduced by
The assignment to $schema is dead and can be removed.
Loading history...
28
        $columnSchemaBuilder = $db->getSchema()->createColumnSchemaBuilder('string');
29
30
        $this->assertInstanceOf(ColumnSchemaBuilder::class, $columnSchemaBuilder);
31
        $this->assertSame('string', $columnSchemaBuilder->getType());
32
    }
33
34
    public function testColumnSchemaDbTypecastWithEmptyCharType(): void
35
    {
36
        $columnSchema = new ColumnSchema();
37
        $columnSchema->setType(Schema::TYPE_CHAR);
38
39
        $this->assertSame('', $columnSchema->dbTypecast(''));
40
    }
41
42
    public function testGetDefaultSchema(): void
43
    {
44
        $db = $this->getConnection();
45
46
        $schema = $db->getSchema();
47
48
        $this->assertNull($schema->getDefaultSchema());
49
    }
50
51
    public function testGetPDOType(): void
52
    {
53
        $values = [
54
            [null, PDO::PARAM_NULL],
55
            ['', PDO::PARAM_STR],
56
            ['hello', PDO::PARAM_STR],
57
            [0, PDO::PARAM_INT],
58
            [1, PDO::PARAM_INT],
59
            [1337, PDO::PARAM_INT],
60
            [true, PDO::PARAM_BOOL],
61
            [false, PDO::PARAM_BOOL],
62
            [$fp = fopen(__FILE__, 'rb'), PDO::PARAM_LOB],
63
        ];
64
65
        $db = $this->getConnection();
66
67
        $schema = $db->getSchema();
68
69
        foreach ($values as $value) {
70
            $this->assertSame(
71
                $value[1],
72
                $schema->getPdoType($value[0]),
73
                '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

73
                'type for value ' . /** @scrutinizer ignore-type */ print_r($value[0], true) . ' does not match.',
Loading history...
74
            );
75
        }
76
77
        fclose($fp);
78
    }
79
80
    public function testIsReadQuery(): void
81
    {
82
        $db = $this->getConnection();
83
84
        $schema = $db->getSchema();
85
86
        $this->assertTrue($schema->isReadQuery('SELECT * FROM tbl'));
87
        $this->assertTrue($schema->isReadQuery('SELECT * FROM tbl WHERE id=1'));
88
        $this->assertTrue($schema->isReadQuery('SELECT * FROM tbl WHERE id=1 LIMIT 1'));
89
        $this->assertTrue($schema->isReadQuery('SELECT * FROM tbl WHERE id=1 LIMIT 1 OFFSET 1'));
90
    }
91
92
    public function testRefresh(): void
93
    {
94
        $db = $this->getConnection();
95
96
        $schema = $db->getSchema();
97
        $schema->refresh();
98
99
        $this->assertSame([], Assert::getInaccessibleProperty($schema, 'tableMetadata'));
100
        $this->assertSame([], Assert::getInaccessibleProperty($schema, 'tableNames'));
101
    }
102
}
103