|
6 | 6 |
|
7 | 7 | use Yiisoft\Db\Exception\Exception;
|
8 | 8 | use Yiisoft\Db\Exception\InvalidConfigException;
|
| 9 | +use Yiisoft\Db\Exception\IntegrityException; |
9 | 10 | use Yiisoft\Db\Exception\NotSupportedException;
|
10 | 11 | use Yiisoft\Db\Tests\CommandTest as AbstractCommandTest;
|
11 | 12 |
|
| 13 | +use function str_replace; |
| 14 | +use function version_compare; |
| 15 | + |
12 | 16 | class CommandTest extends AbstractCommandTest
|
13 | 17 | {
|
14 | 18 | protected ?string $driverName = 'sqlite';
|
@@ -106,8 +110,75 @@ public function testMultiStatementSupport(): void
|
106 | 110 | public function batchInsertSqlProvider(): array
|
107 | 111 | {
|
108 | 112 | $parent = parent::batchInsertSqlProvider();
|
| 113 | + |
109 | 114 | unset($parent['wrongBehavior']); /** Produces SQL syntax error: General error: 1 near ".": syntax error */
|
110 | 115 |
|
111 | 116 | return $parent;
|
112 | 117 | }
|
| 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 | + } |
113 | 184 | }
|
0 commit comments