|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of FlexPHP. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Freddie Gar <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
namespace FlexPHP\Database\Tests\Unit; |
|
11
|
|
|
|
|
12
|
|
|
use FlexPHP\Database\Column; |
|
13
|
|
|
use FlexPHP\Database\Tests\TestCase; |
|
14
|
|
|
use FlexPHP\Schema\SchemaAttribute; |
|
15
|
|
|
|
|
16
|
|
|
class ColumnTest extends TestCase |
|
17
|
|
|
{ |
|
18
|
|
|
public function testItDefinition(): void |
|
19
|
|
|
{ |
|
20
|
|
|
$schemaAttribute = new SchemaAttribute('foo', 'integer', [ |
|
21
|
|
|
'min' => 10, |
|
22
|
|
|
'max' => 100, |
|
23
|
|
|
]); |
|
24
|
|
|
|
|
25
|
|
|
$column = new Column($schemaAttribute); |
|
26
|
|
|
|
|
27
|
|
|
$this->assertEquals($schemaAttribute->name(), $column->getName()); |
|
28
|
|
|
$this->assertEquals($schemaAttribute->dataType(), $column->getType()); |
|
29
|
|
|
$this->assertFalse($column->isPrimaryKey()); |
|
30
|
|
|
$this->assertFalse($column->isForeingKey()); |
|
31
|
|
|
$this->assertEquals([ |
|
32
|
|
|
'length' => $schemaAttribute->maxLength(), |
|
33
|
|
|
'notnull' => $schemaAttribute->isRequired(), |
|
34
|
|
|
'autoincrement' => $schemaAttribute->isAi(), |
|
35
|
|
|
'comment' => $schemaAttribute->name(), |
|
36
|
|
|
'default' => $schemaAttribute->default(), |
|
37
|
|
|
], $column->getOptions()); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function testItDefinitionAi(): void |
|
41
|
|
|
{ |
|
42
|
|
|
$schemaAttribute = new SchemaAttribute('foo', 'integer', [ |
|
43
|
|
|
'pk' => true, |
|
44
|
|
|
'ai' => true, |
|
45
|
|
|
'required' => true, |
|
46
|
|
|
]); |
|
47
|
|
|
|
|
48
|
|
|
$column = new Column($schemaAttribute); |
|
49
|
|
|
|
|
50
|
|
|
$this->assertEquals($schemaAttribute->name(), $column->getName()); |
|
51
|
|
|
$this->assertEquals($schemaAttribute->dataType(), $column->getType()); |
|
52
|
|
|
$this->assertTrue($column->isPrimaryKey()); |
|
53
|
|
|
$this->assertFalse($column->isForeingKey()); |
|
54
|
|
|
$this->assertEquals([ |
|
55
|
|
|
'length' => $schemaAttribute->maxLength(), |
|
56
|
|
|
'notnull' => $schemaAttribute->isRequired(), |
|
57
|
|
|
'autoincrement' => $schemaAttribute->isAi(), |
|
58
|
|
|
'comment' => $schemaAttribute->name(), |
|
59
|
|
|
'default' => $schemaAttribute->default(), |
|
60
|
|
|
], $column->getOptions()); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function testItDefinitionPk(): void |
|
64
|
|
|
{ |
|
65
|
|
|
$schemaAttribute = new SchemaAttribute('bar', 'integer', [ |
|
66
|
|
|
'fk' => 'baz', |
|
67
|
|
|
]); |
|
68
|
|
|
|
|
69
|
|
|
$column = new Column($schemaAttribute); |
|
70
|
|
|
|
|
71
|
|
|
$this->assertEquals($schemaAttribute->name(), $column->getName()); |
|
72
|
|
|
$this->assertEquals($schemaAttribute->dataType(), $column->getType()); |
|
73
|
|
|
$this->assertFalse($column->isPrimaryKey()); |
|
74
|
|
|
$this->assertTrue($column->isForeingKey()); |
|
75
|
|
|
$this->assertEquals([ |
|
76
|
|
|
'length' => $schemaAttribute->maxLength(), |
|
77
|
|
|
'notnull' => $schemaAttribute->isRequired(), |
|
78
|
|
|
'autoincrement' => $schemaAttribute->isAi(), |
|
79
|
|
|
'comment' => $schemaAttribute->name(), |
|
80
|
|
|
'default' => $schemaAttribute->default(), |
|
81
|
|
|
], $column->getOptions()); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @dataProvider getDefault |
|
86
|
|
|
* |
|
87
|
|
|
* @param mixed $default |
|
88
|
|
|
*/ |
|
89
|
|
|
public function testItDefinitionDefault(string $dataType, $default): void |
|
90
|
|
|
{ |
|
91
|
|
|
$schemaAttribute = new SchemaAttribute('bar', $dataType, [ |
|
92
|
|
|
'default' => $default, |
|
93
|
|
|
]); |
|
94
|
|
|
|
|
95
|
|
|
$column = new Column($schemaAttribute); |
|
96
|
|
|
|
|
97
|
|
|
$this->assertEquals($schemaAttribute->name(), $column->getName()); |
|
98
|
|
|
$this->assertEquals($dataType, $column->getType()); |
|
99
|
|
|
$this->assertEquals([ |
|
100
|
|
|
'length' => $schemaAttribute->maxLength(), |
|
101
|
|
|
'notnull' => $schemaAttribute->isRequired(), |
|
102
|
|
|
'autoincrement' => $schemaAttribute->isAi(), |
|
103
|
|
|
'comment' => $schemaAttribute->name(), |
|
104
|
|
|
'default' => $schemaAttribute->default(), |
|
105
|
|
|
], $column->getOptions()); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
public function getDefault(): array |
|
109
|
|
|
{ |
|
110
|
|
|
return [ |
|
111
|
|
|
['string', null], |
|
112
|
|
|
['string', ''], |
|
113
|
|
|
['string', 'A'], |
|
114
|
|
|
['string', '1'], |
|
115
|
|
|
['integer', '1'], |
|
116
|
|
|
['float', '1.9'], |
|
117
|
|
|
['datetime', 'NOW'], |
|
118
|
|
|
]; |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|