|
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
|
|
|
], $column->getOptions()); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function testItDefinitionAi(): void |
|
40
|
|
|
{ |
|
41
|
|
|
$schemaAttribute = new SchemaAttribute('foo', 'integer', [ |
|
42
|
|
|
'pk' => true, |
|
43
|
|
|
'ai' => true, |
|
44
|
|
|
'required' => true, |
|
45
|
|
|
]); |
|
46
|
|
|
|
|
47
|
|
|
$column = new Column($schemaAttribute); |
|
48
|
|
|
|
|
49
|
|
|
$this->assertEquals($schemaAttribute->name(), $column->getName()); |
|
50
|
|
|
$this->assertEquals($schemaAttribute->dataType(), $column->getType()); |
|
51
|
|
|
$this->assertTrue($column->isPrimaryKey()); |
|
52
|
|
|
$this->assertFalse($column->isForeingKey()); |
|
53
|
|
|
$this->assertEquals([ |
|
54
|
|
|
'length' => $schemaAttribute->maxLength(), |
|
55
|
|
|
'notnull' => $schemaAttribute->isRequired(), |
|
56
|
|
|
'autoincrement' => $schemaAttribute->isAi(), |
|
57
|
|
|
'comment' => $schemaAttribute->name(), |
|
58
|
|
|
], $column->getOptions()); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function testItDefinitionPk(): void |
|
62
|
|
|
{ |
|
63
|
|
|
$schemaAttribute = new SchemaAttribute('bar', 'integer', [ |
|
64
|
|
|
'fk' => 'baz', |
|
65
|
|
|
]); |
|
66
|
|
|
|
|
67
|
|
|
$column = new Column($schemaAttribute); |
|
68
|
|
|
|
|
69
|
|
|
$this->assertEquals($schemaAttribute->name(), $column->getName()); |
|
70
|
|
|
$this->assertEquals($schemaAttribute->dataType(), $column->getType()); |
|
71
|
|
|
$this->assertFalse($column->isPrimaryKey()); |
|
72
|
|
|
$this->assertTrue($column->isForeingKey()); |
|
73
|
|
|
$this->assertEquals([ |
|
74
|
|
|
'length' => $schemaAttribute->maxLength(), |
|
75
|
|
|
'notnull' => $schemaAttribute->isRequired(), |
|
76
|
|
|
'autoincrement' => $schemaAttribute->isAi(), |
|
77
|
|
|
'comment' => $schemaAttribute->name(), |
|
78
|
|
|
], $column->getOptions()); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|