Skip to content

Commit e8cf47b

Browse files
terabytesoftwdevanych
authored andcommitted
Add tests addforeingkey validation (#23)
* Add testForengKeyException in CommandTest::class.
1 parent 732d97d commit e8cf47b

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

tests/CommandTest.php

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@
66

77
use Yiisoft\Db\Exception\Exception;
88
use Yiisoft\Db\Exception\InvalidConfigException;
9+
use Yiisoft\Db\Exception\IntegrityException;
910
use Yiisoft\Db\Exception\NotSupportedException;
1011
use Yiisoft\Db\Tests\CommandTest as AbstractCommandTest;
1112

13+
use function str_replace;
14+
use function version_compare;
15+
1216
class CommandTest extends AbstractCommandTest
1317
{
1418
protected ?string $driverName = 'sqlite';
@@ -106,8 +110,75 @@ public function testMultiStatementSupport(): void
106110
public function batchInsertSqlProvider(): array
107111
{
108112
$parent = parent::batchInsertSqlProvider();
113+
109114
unset($parent['wrongBehavior']); /** Produces SQL syntax error: General error: 1 near ".": syntax error */
110115

111116
return $parent;
112117
}
118+
119+
public function testForeingKeyException(): void
120+
{
121+
$db = $this->getConnection(false);
122+
123+
$db->createCommand('PRAGMA foreign_keys = ON')->execute();
124+
125+
$tableMaster = 'departments';
126+
$tableRelation = 'students';
127+
$name = 'test_fk_constraint';
128+
129+
/** @var \Yiisoft\Db\Sqlite\Schema $schema */
130+
$schema = $db->getSchema();
131+
132+
if ($schema->getTableSchema($tableRelation) !== null) {
133+
$db->createCommand()->dropTable($tableRelation)->execute();
134+
}
135+
136+
if ($schema->getTableSchema($tableMaster) !== null) {
137+
$db->createCommand()->dropTable($tableMaster)->execute();
138+
}
139+
140+
$db->createCommand()->createTable($tableMaster, [
141+
'department_id' => 'integer not null primary key autoincrement',
142+
'department_name' => 'nvarchar(50) null',
143+
])->execute();
144+
145+
$db->createCommand()->createTable($tableRelation, [
146+
'student_id' => 'integer primary key autoincrement not null',
147+
'student_name' => 'nvarchar(50) null',
148+
'department_id' => 'integer not null',
149+
'dateOfBirth' => 'date null'
150+
])->execute();
151+
152+
$db->createCommand()->addForeignKey(
153+
$name,
154+
$tableRelation,
155+
['Department_id'],
156+
$tableMaster,
157+
['Department_id']
158+
)->execute();
159+
160+
$db->createCommand(
161+
"INSERT INTO departments VALUES (1, 'IT')"
162+
)->execute();
163+
164+
$db->createCommand(
165+
'INSERT INTO students(student_name, department_id) VALUES ("John", 1);'
166+
)->execute();
167+
168+
$expectedMessageError = str_replace(
169+
"\r\n",
170+
"\n",
171+
<<<EOD
172+
SQLSTATE[23000]: Integrity constraint violation: 19 FOREIGN KEY constraint failed
173+
The SQL being executed was: INSERT INTO students(student_name, department_id) VALUES ("Samdark", 5)
174+
EOD
175+
);
176+
177+
$this->expectException(IntegrityException::class);
178+
$this->expectExceptionMessage($expectedMessageError);
179+
180+
$db->createCommand(
181+
'INSERT INTO students(student_name, department_id) VALUES ("Samdark", 5);'
182+
)->execute();
183+
}
113184
}

0 commit comments

Comments
 (0)