@@ -286,16 +286,24 @@ protected function newBelongsToMany(Builder $query, Model $parent, $table, $fore
286
286
{
287
287
$ relation = Mockery::mock (BelongsToMany::class)->makePartial ();
288
288
$ relation ->__construct (...func_get_args ());
289
+ $ instance = new BelongsToManyCreateOrFirstTestRelatedModel ([
290
+ 'id ' => 456 ,
291
+ 'attr ' => 'foo ' ,
292
+ 'val ' => 'bar ' ,
293
+ 'created_at ' => '2023-01-01T00:00:00.000000Z ' ,
294
+ 'updated_at ' => '2023-01-01T00:00:00.000000Z ' ,
295
+ 'pivot ' => [
296
+ 'source_id ' => 123 ,
297
+ 'related_id ' => 456 ,
298
+ ],
299
+ ]);
300
+ $ instance ->exists = true ;
301
+ $ instance ->wasRecentlyCreated = false ;
302
+ $ instance ->syncOriginal ();
289
303
$ relation
290
304
->expects ('createOrFirst ' )
291
305
->with (['attr ' => 'foo ' ], ['val ' => 'bar ' ], [], true )
292
- ->andReturn (new BelongsToManyCreateOrFirstTestRelatedModel ([
293
- 'id ' => 456 ,
294
- 'attr ' => 'foo ' ,
295
- 'val ' => 'bar ' ,
296
- 'created_at ' => '2023-01-01T00:00:00.000000Z ' ,
297
- 'updated_at ' => '2023-01-01T00:00:00.000000Z ' ,
298
- ]));
306
+ ->andReturn ($ instance );
299
307
300
308
return $ relation ;
301
309
}
@@ -333,6 +341,104 @@ protected function newBelongsToMany(Builder $query, Model $parent, $table, $fore
333
341
'val ' => 'bar ' ,
334
342
'created_at ' => '2023-01-01T00:00:00.000000Z ' ,
335
343
'updated_at ' => '2023-01-01T00:00:00.000000Z ' ,
344
+ 'pivot ' => [
345
+ 'source_id ' => 123 ,
346
+ 'related_id ' => 456 ,
347
+ ],
348
+ ], $ result ->toArray ());
349
+ }
350
+
351
+ public function testUpdateOrCreateMethodCreatesNewRelated (): void
352
+ {
353
+ $ source = new class () extends BelongsToManyCreateOrFirstTestSourceModel
354
+ {
355
+ protected function newBelongsToMany (Builder $ query , Model $ parent , $ table , $ foreignPivotKey , $ relatedPivotKey , $ parentKey , $ relatedKey , $ relationName = null ): BelongsToMany
356
+ {
357
+ $ relation = Mockery::mock (BelongsToMany::class)->makePartial ();
358
+ $ relation ->__construct (...func_get_args ());
359
+ $ instance = new BelongsToManyCreateOrFirstTestRelatedModel ([
360
+ 'id ' => 456 ,
361
+ 'attr ' => 'foo ' ,
362
+ 'val ' => 'bar ' ,
363
+ 'created_at ' => '2023-01-01T00:00:00.000000Z ' ,
364
+ 'updated_at ' => '2023-01-01T00:00:00.000000Z ' ,
365
+ ]);
366
+ $ instance ->exists = true ;
367
+ $ instance ->wasRecentlyCreated = true ;
368
+ $ instance ->syncOriginal ();
369
+ $ relation
370
+ ->expects ('firstOrCreate ' )
371
+ ->with (['attr ' => 'foo ' ], ['val ' => 'baz ' ], [], true )
372
+ ->andReturn ($ instance );
373
+
374
+ return $ relation ;
375
+ }
376
+ };
377
+ $ source ->id = 123 ;
378
+ $ this ->mockConnectionForModels (
379
+ [$ source , new BelongsToManyCreateOrFirstTestRelatedModel ()],
380
+ 'SQLite ' ,
381
+ );
382
+
383
+ $ result = $ source ->related ()->updateOrCreate (['attr ' => 'foo ' ], ['val ' => 'baz ' ]);
384
+ $ this ->assertEquals ([
385
+ 'id ' => 456 ,
386
+ 'attr ' => 'foo ' ,
387
+ 'val ' => 'bar ' ,
388
+ 'created_at ' => '2023-01-01T00:00:00.000000Z ' ,
389
+ 'updated_at ' => '2023-01-01T00:00:00.000000Z ' ,
390
+ ], $ result ->toArray ());
391
+ }
392
+
393
+ public function testUpdateOrCreateMethodUpdatesExistingRelated (): void
394
+ {
395
+ $ source = new class () extends BelongsToManyCreateOrFirstTestSourceModel
396
+ {
397
+ protected function newBelongsToMany (Builder $ query , Model $ parent , $ table , $ foreignPivotKey , $ relatedPivotKey , $ parentKey , $ relatedKey , $ relationName = null ): BelongsToMany
398
+ {
399
+ $ relation = Mockery::mock (BelongsToMany::class)->makePartial ();
400
+ $ relation ->__construct (...func_get_args ());
401
+ $ instance = new BelongsToManyCreateOrFirstTestRelatedModel ([
402
+ 'id ' => 456 ,
403
+ 'attr ' => 'foo ' ,
404
+ 'val ' => 'bar ' ,
405
+ 'created_at ' => '2023-01-01T00:00:00.000000Z ' ,
406
+ 'updated_at ' => '2023-01-01T00:00:00.000000Z ' ,
407
+ ]);
408
+ $ instance ->exists = true ;
409
+ $ instance ->wasRecentlyCreated = false ;
410
+ $ instance ->syncOriginal ();
411
+ $ relation
412
+ ->expects ('firstOrCreate ' )
413
+ ->with (['attr ' => 'foo ' ], ['val ' => 'baz ' ], [], true )
414
+ ->andReturn ($ instance );
415
+
416
+ return $ relation ;
417
+ }
418
+ };
419
+ $ source ->id = 123 ;
420
+ $ this ->mockConnectionForModels (
421
+ [$ source , new BelongsToManyCreateOrFirstTestRelatedModel ()],
422
+ 'SQLite ' ,
423
+ );
424
+ $ source ->getConnection ()->shouldReceive ('transactionLevel ' )->andReturn (0 );
425
+ $ source ->getConnection ()->shouldReceive ('getName ' )->andReturn ('sqlite ' );
426
+
427
+ $ source ->getConnection ()
428
+ ->expects ('update ' )
429
+ ->with (
430
+ 'update "related_table" set "val" = ?, "updated_at" = ? where "id" = ? ' ,
431
+ ['baz ' , '2023-01-01 00:00:00 ' , 456 ],
432
+ )
433
+ ->andReturn (1 );
434
+
435
+ $ result = $ source ->related ()->updateOrCreate (['attr ' => 'foo ' ], ['val ' => 'baz ' ]);
436
+ $ this ->assertEquals ([
437
+ 'id ' => 456 ,
438
+ 'attr ' => 'foo ' ,
439
+ 'val ' => 'baz ' ,
440
+ 'created_at ' => '2023-01-01T00:00:00.000000Z ' ,
441
+ 'updated_at ' => '2023-01-01T00:00:00.000000Z ' ,
336
442
], $ result ->toArray ());
337
443
}
338
444
0 commit comments