|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace TheCodingMachine\FluidSchema; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Schema\Schema; |
|
6
|
|
|
use Doctrine\DBAL\Types\Type; |
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
|
8
|
|
|
|
|
9
|
|
|
class TdbmFluidColumnTest extends TestCase |
|
10
|
|
|
{ |
|
11
|
|
|
public function testTypes() |
|
12
|
|
|
{ |
|
13
|
|
|
$schema = new Schema(); |
|
14
|
|
|
$fluid = new TdbmFluidSchema($schema); |
|
15
|
|
|
|
|
16
|
|
|
$posts = $fluid->table('posts'); |
|
17
|
|
|
|
|
18
|
|
|
$column = $posts->column('foo'); |
|
19
|
|
|
|
|
20
|
|
|
$dbalColumn = $schema->getTable('posts')->getColumn('foo'); |
|
21
|
|
|
|
|
22
|
|
|
$column->integer(); |
|
23
|
|
|
$this->assertSame(Type::getType(Type::INTEGER), $dbalColumn->getType()); |
|
24
|
|
|
|
|
25
|
|
|
$column->smallInt(); |
|
26
|
|
|
$this->assertSame(Type::getType(Type::SMALLINT), $dbalColumn->getType()); |
|
27
|
|
|
|
|
28
|
|
|
$column->bigInt(); |
|
29
|
|
|
$this->assertSame(Type::getType(Type::BIGINT), $dbalColumn->getType()); |
|
30
|
|
|
|
|
31
|
|
|
$column->decimal(12, 32); |
|
32
|
|
|
$this->assertSame(Type::getType(Type::DECIMAL), $dbalColumn->getType()); |
|
33
|
|
|
$this->assertSame(12, $dbalColumn->getPrecision()); |
|
34
|
|
|
$this->assertSame(32, $dbalColumn->getScale()); |
|
35
|
|
|
|
|
36
|
|
|
$column->float(32, 12); |
|
37
|
|
|
$this->assertSame(Type::getType(Type::FLOAT), $dbalColumn->getType()); |
|
38
|
|
|
$this->assertSame(32, $dbalColumn->getPrecision()); |
|
39
|
|
|
$this->assertSame(12, $dbalColumn->getScale()); |
|
40
|
|
|
|
|
41
|
|
|
$column->string(42, true); |
|
42
|
|
|
$this->assertSame(Type::getType(Type::STRING), $dbalColumn->getType()); |
|
43
|
|
|
$this->assertSame(42, $dbalColumn->getLength()); |
|
44
|
|
|
$this->assertSame(true, $dbalColumn->getFixed()); |
|
45
|
|
|
|
|
46
|
|
|
$column->text(); |
|
47
|
|
|
$this->assertSame(Type::getType(Type::TEXT), $dbalColumn->getType()); |
|
48
|
|
|
|
|
49
|
|
|
$column->guid(); |
|
50
|
|
|
$this->assertSame(Type::getType(Type::GUID), $dbalColumn->getType()); |
|
51
|
|
|
|
|
52
|
|
|
$column->blob(); |
|
53
|
|
|
$this->assertSame(Type::getType(Type::BLOB), $dbalColumn->getType()); |
|
54
|
|
|
|
|
55
|
|
|
$column->boolean(); |
|
56
|
|
|
$this->assertSame(Type::getType(Type::BOOLEAN), $dbalColumn->getType()); |
|
57
|
|
|
|
|
58
|
|
|
$column->date(); |
|
59
|
|
|
$this->assertSame(Type::getType(Type::DATE), $dbalColumn->getType()); |
|
60
|
|
|
|
|
61
|
|
|
$column->datetime(); |
|
62
|
|
|
$this->assertSame(Type::getType(Type::DATETIME), $dbalColumn->getType()); |
|
63
|
|
|
|
|
64
|
|
|
$column->datetimeTz(); |
|
65
|
|
|
$this->assertSame(Type::getType(Type::DATETIMETZ), $dbalColumn->getType()); |
|
66
|
|
|
|
|
67
|
|
|
$column->time(); |
|
68
|
|
|
$this->assertSame(Type::getType(Type::TIME), $dbalColumn->getType()); |
|
69
|
|
|
|
|
70
|
|
|
$column->array(); |
|
71
|
|
|
$this->assertSame(Type::getType(Type::TARRAY), $dbalColumn->getType()); |
|
72
|
|
|
|
|
73
|
|
|
$column->simpleArray(); |
|
74
|
|
|
$this->assertSame(Type::getType(Type::SIMPLE_ARRAY), $dbalColumn->getType()); |
|
75
|
|
|
|
|
76
|
|
|
$column->jsonArray(); |
|
77
|
|
|
$this->assertSame(Type::getType(Type::JSON_ARRAY), $dbalColumn->getType()); |
|
78
|
|
|
|
|
79
|
|
|
$column->object(); |
|
80
|
|
|
$this->assertSame(Type::getType(Type::OBJECT), $dbalColumn->getType()); |
|
81
|
|
|
|
|
82
|
|
|
if (defined('Doctrine\\DBAL\\Types\\Type::BINARY')) { |
|
83
|
|
|
$column->binary(43); |
|
84
|
|
|
$this->assertSame(Type::getType(Type::BINARY), $dbalColumn->getType()); |
|
85
|
|
|
$this->assertSame(43, $dbalColumn->getLength()); |
|
86
|
|
|
$this->assertSame(false, $dbalColumn->getFixed()); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
if (defined('Doctrine\\DBAL\\Types\\Type::DATE_IMMUTABLE')) { |
|
90
|
|
|
// Doctrine DBAL 2.6+ |
|
91
|
|
|
$column->dateImmutable(); |
|
92
|
|
|
$this->assertSame(Type::getType('date_immutable'), $dbalColumn->getType()); |
|
93
|
|
|
|
|
94
|
|
|
$column->datetimeImmutable(); |
|
95
|
|
|
$this->assertSame(Type::getType(Type::DATETIME_IMMUTABLE), $dbalColumn->getType()); |
|
96
|
|
|
|
|
97
|
|
|
$column->datetimeTzImmutable(); |
|
98
|
|
|
$this->assertSame(Type::getType(Type::DATETIMETZ_IMMUTABLE), $dbalColumn->getType()); |
|
99
|
|
|
|
|
100
|
|
|
$column->time(); |
|
101
|
|
|
$this->assertSame(Type::getType(Type::TIME), $dbalColumn->getType()); |
|
102
|
|
|
|
|
103
|
|
|
$column->timeImmutable(); |
|
104
|
|
|
$this->assertSame(Type::getType(Type::TIME_IMMUTABLE), $dbalColumn->getType()); |
|
105
|
|
|
|
|
106
|
|
|
$column->dateInterval(); |
|
107
|
|
|
$this->assertSame(Type::getType(Type::DATEINTERVAL), $dbalColumn->getType()); |
|
108
|
|
|
|
|
109
|
|
|
$column->json(); |
|
110
|
|
|
$this->assertSame(Type::getType(Type::JSON), $dbalColumn->getType()); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
$this->assertSame('foo', $column->getDbalColumn()->getName()); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
View Code Duplication |
public function testReference() |
|
|
|
|
|
|
117
|
|
|
{ |
|
118
|
|
|
$schema = new Schema(); |
|
119
|
|
|
$fluid = new TdbmFluidSchema($schema); |
|
120
|
|
|
|
|
121
|
|
|
$countries = $fluid->table('countries'); |
|
122
|
|
|
$countries->id(); |
|
123
|
|
|
|
|
124
|
|
|
$users = $fluid->table('users'); |
|
125
|
|
|
$users->column('country_id')->references('countries', 'myfk'); |
|
126
|
|
|
|
|
127
|
|
|
$dbalColumn = $schema->getTable('users')->getColumn('country_id'); |
|
128
|
|
|
|
|
129
|
|
|
$this->assertSame(Type::getType(Type::INTEGER), $dbalColumn->getType()); |
|
130
|
|
|
$fk = $schema->getTable('users')->getForeignKey('myfk'); |
|
131
|
|
|
$this->assertSame('users', $fk->getLocalTableName()); |
|
132
|
|
|
$this->assertSame('countries', $fk->getForeignTableName()); |
|
133
|
|
|
$this->assertSame(['country_id'], $fk->getLocalColumns()); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
public function testReferenceException() |
|
137
|
|
|
{ |
|
138
|
|
|
$schema = new Schema(); |
|
139
|
|
|
$fluid = new TdbmFluidSchema($schema); |
|
140
|
|
|
|
|
141
|
|
|
$countries = $fluid->table('countries'); |
|
142
|
|
|
$countries->column('id1')->integer(); |
|
143
|
|
|
$countries->column('id2')->integer(); |
|
144
|
|
|
$countries->primaryKey(['id1','id2']); |
|
145
|
|
|
|
|
146
|
|
|
$users = $fluid->table('users'); |
|
147
|
|
|
$this->expectException(FluidSchemaException::class); |
|
148
|
|
|
$users->column('country_id')->references('countries', 'myfk'); |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.