|
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(); |
|
|
|
|
|
|
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.', |
|
|
|
|
|
|
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
|
|
|
|