Skip to content

Commit 7710af1

Browse files
committed
Tests: improved
1 parent 081c47c commit 7710af1

3 files changed

Lines changed: 198 additions & 1 deletion

File tree

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
<?php
2+
3+
use CzProject\SqlGenerator\Drivers;
4+
use CzProject\SqlGenerator\SqlDocument;
5+
use CzProject\SqlGenerator\Statements\IndexDefinition;
6+
use Tester\Assert;
7+
8+
require __DIR__ . '/../bootstrap.php';
9+
10+
11+
test(function () {
12+
13+
$sql = new SqlDocument;
14+
$driver = new Drivers\MysqlDriver;
15+
16+
$contactTable = $sql->alterTable('contact');
17+
$contactTable->setOption('ENGINE', 'InnoDB');
18+
19+
// columns
20+
$contactTable->addColumn('active', 'TINYINT', array(1), array('UNSIGNED' => NULL))
21+
->setDefaultValue(TRUE)
22+
->setNullable()
23+
->setComment('Contact status')
24+
->moveAfterColumn('name');
25+
26+
$contactTable->addColumn('id', 'INT')
27+
->setAutoIncrement()
28+
->moveToFirstPosition();
29+
30+
$contactTable->modifyColumn('name', 'VARCHAR(200)')
31+
->setDefaultValue('XYZ')
32+
->setComment('Name of contact')
33+
->setNullable()
34+
->moveAfterColumn('id');
35+
36+
$contactTable->modifyColumn('id', 'INT')
37+
->setAutoIncrement()
38+
->moveToFirstPosition();
39+
40+
$contactTable->dropColumn('removed');
41+
42+
// indexes
43+
$contactTable->addIndex(NULL, IndexDefinition::TYPE_PRIMARY)
44+
->addColumn('id');
45+
46+
$contactTable->dropIndex('name');
47+
48+
// foreign keys
49+
$contactTable->dropForeignKey('fk_creator');
50+
$contactTable->addForeignKey('fk_creator', 'creator_id', 'user', 'id')
51+
->setOnUpdateAction('NO ACTION')
52+
->setOnDeleteAction('NO ACTION');
53+
54+
// comment
55+
$contactTable->setComment('Table of contacts.');
56+
57+
Assert::same(implode("\n", array(
58+
'',
59+
'ALTER TABLE `contact`',
60+
"ADD COLUMN `active` TINYINT(1) UNSIGNED NULL DEFAULT 1 COMMENT 'Contact status' AFTER `name`,",
61+
"ADD COLUMN `id` INT NOT NULL AUTO_INCREMENT FIRST,",
62+
"MODIFY COLUMN `name` VARCHAR(200) NULL DEFAULT 'XYZ' COMMENT 'Name of contact' AFTER `id`,",
63+
"MODIFY COLUMN `id` INT NOT NULL AUTO_INCREMENT FIRST,",
64+
"DROP COLUMN `removed`,",
65+
"ADD PRIMARY KEY (`id`),",
66+
"DROP INDEX `name`,",
67+
"DROP FOREIGN KEY `fk_creator`,",
68+
"ADD CONSTRAINT `fk_creator` FOREIGN KEY (`creator_id`) REFERENCES `user` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,",
69+
'COMMENT \'Table of contacts.\',',
70+
'ENGINE=InnoDB;',
71+
'',
72+
)), $sql->toSql($driver));
73+
74+
});
75+
76+
77+
test(function () {
78+
79+
$sql = new SqlDocument;
80+
$driver = new Drivers\MysqlDriver;
81+
82+
$contactTable = $sql->createTable('contact');
83+
$contactTable->addColumn('name', 'VARCHAR(100)');
84+
85+
Assert::exception(function () use ($contactTable) {
86+
87+
$contactTable->addColumn('name', 'VARCHAR(50)');
88+
89+
}, 'CzProject\SqlGenerator\DuplicateException', "Column 'name' already exists.");
90+
91+
});
92+
93+
94+
test(function () {
95+
96+
$sql = new SqlDocument;
97+
98+
$contactTable = $sql->createTable('contact');
99+
$contactTable->addIndex('name', 'INDEX');
100+
101+
Assert::exception(function () use ($contactTable) {
102+
103+
$contactTable->addIndex('name', 'INDEX');
104+
105+
}, 'CzProject\SqlGenerator\DuplicateException', "Index 'name' already exists.");
106+
107+
});
108+
109+
110+
test(function () {
111+
112+
$sql = new SqlDocument;
113+
114+
$contactTable = $sql->createTable('contact');
115+
$contactTable->addForeignKey('fk_person', 'person_id', 'person', 'id');
116+
117+
Assert::exception(function () use ($contactTable) {
118+
119+
$contactTable->addForeignKey('fk_person', 'person_id', 'person', 'id');
120+
121+
}, 'CzProject\SqlGenerator\DuplicateException', "Foreign key 'fk_person' already exists.");
122+
123+
});

tests/SqlGenerator/SqlDocument.createTable.phpt

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ test(function () {
3636
->addColumn('name', 'ASC', 100)
3737
->addColumn('surname', 'DESC', 100);
3838

39+
$contactTable->addForeignKey('fk_creator', 'creator_id', 'user', 'id');
40+
3941
Assert::same(implode("\n", array(
4042
'',
4143
'CREATE TABLE `contact` (',
@@ -47,7 +49,8 @@ test(function () {
4749
"\t`created` DATETIME NOT NULL,",
4850
"\t`removed` DATETIME NULL,",
4951
"\tPRIMARY KEY (`id`),",
50-
"\tUNIQUE KEY `name_surname` (`name` (100), `surname` (100) DESC)",
52+
"\tUNIQUE KEY `name_surname` (`name` (100), `surname` (100) DESC),",
53+
"\tCONSTRAINT `fk_creator` FOREIGN KEY (`creator_id`) REFERENCES `user` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT",
5154
')',
5255
'COMMENT \'Clients table.\'',
5356
'ENGINE=InnoDB;',
@@ -72,3 +75,35 @@ test(function () {
7275
}, 'CzProject\SqlGenerator\DuplicateException', "Column 'name' already exists.");
7376

7477
});
78+
79+
80+
test(function () {
81+
82+
$sql = new SqlDocument;
83+
84+
$contactTable = $sql->createTable('contact');
85+
$contactTable->addIndex('name', 'INDEX');
86+
87+
Assert::exception(function () use ($contactTable) {
88+
89+
$contactTable->addIndex('name', 'INDEX');
90+
91+
}, 'CzProject\SqlGenerator\DuplicateException', "Index 'name' already exists.");
92+
93+
});
94+
95+
96+
test(function () {
97+
98+
$sql = new SqlDocument;
99+
100+
$contactTable = $sql->createTable('contact');
101+
$contactTable->addForeignKey('fk_person', 'person_id', 'person', 'id');
102+
103+
Assert::exception(function () use ($contactTable) {
104+
105+
$contactTable->addForeignKey('fk_person', 'person_id', 'person', 'id');
106+
107+
}, 'CzProject\SqlGenerator\DuplicateException', "Foreign key 'fk_person' already exists.");
108+
109+
});
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
use CzProject\SqlGenerator\Drivers;
4+
use CzProject\SqlGenerator\SqlDocument;
5+
use CzProject\SqlGenerator\Statements\IndexDefinition;
6+
use Tester\Assert;
7+
8+
require __DIR__ . '/../bootstrap.php';
9+
10+
11+
test(function () {
12+
13+
$sql = new SqlDocument;
14+
$driver = new Drivers\MysqlDriver;
15+
16+
$contactTable = $sql->createTable('contact')
17+
->setComment('Clients table.');
18+
$contactTable->addColumn('id', 'INT', NULL, array('UNSIGNED' => NULL))
19+
->setAutoIncrement();
20+
21+
$sql->alterTable('book')
22+
->dropColumn('name');
23+
24+
$expected = array();
25+
$expected[] = implode("\n", array(
26+
'CREATE TABLE `contact` (',
27+
"\t`id` INT UNSIGNED NOT NULL AUTO_INCREMENT",
28+
')',
29+
'COMMENT \'Clients table.\';',
30+
));
31+
32+
$expected[] = implode("\n", array(
33+
'ALTER TABLE `book`',
34+
'DROP COLUMN `name`;',
35+
));
36+
37+
Assert::same($expected, $sql->getSqlQueries($driver));
38+
39+
});

0 commit comments

Comments
 (0)