|
6 | 6 |
|
7 | 7 | use ArgumentCountError; |
8 | 8 | use DivisionByZeroError; |
| 9 | +use PHPUnit\Framework\Attributes\DataProvider; |
9 | 10 | use Yiisoft\ActiveRecord\ActiveQuery; |
10 | 11 | use Yiisoft\ActiveRecord\ArArrayHelper; |
11 | 12 | use Yiisoft\ActiveRecord\ConnectionProvider; |
@@ -1170,4 +1171,159 @@ public function testIsChanged(): void |
1170 | 1171 |
|
1171 | 1172 | $this->assertTrue((new Customer())->isChanged()); |
1172 | 1173 | } |
| 1174 | + |
| 1175 | + public static function dataUpsert(): array |
| 1176 | + { |
| 1177 | + return [ |
| 1178 | + 'insert values' => [ |
| 1179 | + 'values' => [ |
| 1180 | + |
| 1181 | + 'name' => 'user4', |
| 1182 | + 'address' => 'address4', |
| 1183 | + ], |
| 1184 | + 'insertProperties' => null, |
| 1185 | + 'updateValues' => false, |
| 1186 | + 'expected' => [ |
| 1187 | + 'id' => 4, |
| 1188 | + |
| 1189 | + 'name' => 'user4', |
| 1190 | + 'address' => 'address4', |
| 1191 | + ], |
| 1192 | + ], |
| 1193 | + 'insert part values' => [ |
| 1194 | + 'values' => [ |
| 1195 | + |
| 1196 | + 'name' => 'user4', |
| 1197 | + 'address' => 'address4', |
| 1198 | + ], |
| 1199 | + 'insertProperties' => [ |
| 1200 | + 'email', |
| 1201 | + 'name', |
| 1202 | + ], |
| 1203 | + 'updateValues' => false, |
| 1204 | + 'expected' => [ |
| 1205 | + 'id' => 4, |
| 1206 | + |
| 1207 | + 'name' => 'user4', |
| 1208 | + 'address' => 'address4', |
| 1209 | + ], |
| 1210 | + 'expectedAfterRefresh' => [ |
| 1211 | + 'id' => 4, |
| 1212 | + |
| 1213 | + 'name' => 'user4', |
| 1214 | + 'address' => null, |
| 1215 | + ], |
| 1216 | + ], |
| 1217 | + 'insert from insertProperties' => [ |
| 1218 | + 'values' => [ |
| 1219 | + |
| 1220 | + 'address' => 'address4', |
| 1221 | + ], |
| 1222 | + 'insertProperties' => [ |
| 1223 | + 'email', |
| 1224 | + 'name' => 'user4', |
| 1225 | + ], |
| 1226 | + 'updateValues' => false, |
| 1227 | + 'expected' => [ |
| 1228 | + 'id' => 4, |
| 1229 | + |
| 1230 | + 'name' => 'user4', |
| 1231 | + 'address' => 'address4', |
| 1232 | + ], |
| 1233 | + 'expectedAfterRefresh' => [ |
| 1234 | + 'id' => 4, |
| 1235 | + |
| 1236 | + 'name' => 'user4', |
| 1237 | + 'address' => null, |
| 1238 | + ], |
| 1239 | + ], |
| 1240 | + 'without changes' => [ |
| 1241 | + 'values' => [ |
| 1242 | + |
| 1243 | + 'name' => 'new name', |
| 1244 | + ], |
| 1245 | + 'insertProperties' => null, |
| 1246 | + 'updateValues' => false, |
| 1247 | + 'expected' => [ |
| 1248 | + 'id' => 3, |
| 1249 | + |
| 1250 | + 'name' => 'user3', |
| 1251 | + 'address' => 'address3', |
| 1252 | + ], |
| 1253 | + ], |
| 1254 | + 'update from values' => [ |
| 1255 | + 'values' => [ |
| 1256 | + |
| 1257 | + 'name' => 'new name', |
| 1258 | + 'address' => 'new address', |
| 1259 | + ], |
| 1260 | + 'insertProperties' => null, |
| 1261 | + 'updateValues' => true, |
| 1262 | + 'expected' => [ |
| 1263 | + 'id' => 3, |
| 1264 | + |
| 1265 | + 'name' => 'new name', |
| 1266 | + 'address' => 'new address', |
| 1267 | + ], |
| 1268 | + ], |
| 1269 | + 'update from updateValues' => [ |
| 1270 | + 'values' => [ |
| 1271 | + |
| 1272 | + 'address' => 'new address', |
| 1273 | + ], |
| 1274 | + 'insertProperties' => null, |
| 1275 | + 'updateValues' => [ |
| 1276 | + 'name' => 'another name', |
| 1277 | + 'address' => 'another address' |
| 1278 | + ], |
| 1279 | + 'expected' => [ |
| 1280 | + 'id' => 3, |
| 1281 | + |
| 1282 | + 'name' => 'another name', |
| 1283 | + 'address' => 'another address', |
| 1284 | + ], |
| 1285 | + ], |
| 1286 | + ]; |
| 1287 | + } |
| 1288 | + |
| 1289 | + #[DataProvider('dataUpsert')] |
| 1290 | + public function testUpsert( |
| 1291 | + array $values, |
| 1292 | + array|null $insertProperties, |
| 1293 | + array|bool $updateValues, |
| 1294 | + array $expected, |
| 1295 | + array|null $expectedAfterRefresh = null, |
| 1296 | + ): void { |
| 1297 | + $this->reloadFixtureAfterTest(); |
| 1298 | + |
| 1299 | + $customer = new Customer(); |
| 1300 | + |
| 1301 | + foreach ($values as $property => $value) { |
| 1302 | + $customer->set($property, $value); |
| 1303 | + } |
| 1304 | + |
| 1305 | + $this->assertTrue($customer->upsert($insertProperties, $updateValues)); |
| 1306 | + |
| 1307 | + $this->assertFalse($customer->isNewRecord()); |
| 1308 | + |
| 1309 | + foreach ($expected as $property => $value) { |
| 1310 | + $this->assertSame($value, $customer->get($property)); |
| 1311 | + } |
| 1312 | + |
| 1313 | + $customer->refresh(); |
| 1314 | + |
| 1315 | + foreach ($expectedAfterRefresh ?? $expected as $property => $value) { |
| 1316 | + $this->assertSame($value, $customer->get($property)); |
| 1317 | + } |
| 1318 | + } |
| 1319 | + |
| 1320 | + public function testUpsertWithException(): void |
| 1321 | + { |
| 1322 | + $customer = Customer::findByPk(1); |
| 1323 | + |
| 1324 | + $this->expectException(InvalidCallException::class); |
| 1325 | + $this->expectExceptionMessage('The record is not new and cannot be inserted.'); |
| 1326 | + |
| 1327 | + $customer->upsert(); |
| 1328 | + } |
1173 | 1329 | } |
0 commit comments