DatabaseEloquentMorphToManyTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace Illuminate\Tests\Database;
  3. use Illuminate\Database\Eloquent\Builder;
  4. use Illuminate\Database\Eloquent\Model;
  5. use Illuminate\Database\Eloquent\Relations\MorphToMany;
  6. use Mockery as m;
  7. use PHPUnit\Framework\TestCase;
  8. use stdClass;
  9. class DatabaseEloquentMorphToManyTest extends TestCase
  10. {
  11. protected function tearDown(): void
  12. {
  13. m::close();
  14. }
  15. public function testEagerConstraintsAreProperlyAdded()
  16. {
  17. $relation = $this->getRelation();
  18. $relation->getParent()->shouldReceive('getKeyName')->andReturn('id');
  19. $relation->getParent()->shouldReceive('getKeyType')->once()->andReturn('int');
  20. $relation->getQuery()->shouldReceive('whereIntegerInRaw')->once()->with('taggables.taggable_id', [1, 2]);
  21. $relation->getQuery()->shouldReceive('where')->once()->with('taggables.taggable_type', get_class($relation->getParent()));
  22. $model1 = new EloquentMorphToManyModelStub;
  23. $model1->id = 1;
  24. $model2 = new EloquentMorphToManyModelStub;
  25. $model2->id = 2;
  26. $relation->addEagerConstraints([$model1, $model2]);
  27. }
  28. public function testAttachInsertsPivotTableRecord()
  29. {
  30. $relation = $this->getMockBuilder(MorphToMany::class)->onlyMethods(['touchIfTouching'])->setConstructorArgs($this->getRelationArguments())->getMock();
  31. $query = m::mock(stdClass::class);
  32. $query->shouldReceive('from')->once()->with('taggables')->andReturn($query);
  33. $query->shouldReceive('insert')->once()->with([['taggable_id' => 1, 'taggable_type' => get_class($relation->getParent()), 'tag_id' => 2, 'foo' => 'bar']])->andReturn(true);
  34. $relation->getQuery()->shouldReceive('getQuery')->andReturn($mockQueryBuilder = m::mock(stdClass::class));
  35. $mockQueryBuilder->shouldReceive('newQuery')->once()->andReturn($query);
  36. $relation->expects($this->once())->method('touchIfTouching');
  37. $relation->attach(2, ['foo' => 'bar']);
  38. }
  39. public function testDetachRemovesPivotTableRecord()
  40. {
  41. $relation = $this->getMockBuilder(MorphToMany::class)->onlyMethods(['touchIfTouching'])->setConstructorArgs($this->getRelationArguments())->getMock();
  42. $query = m::mock(stdClass::class);
  43. $query->shouldReceive('from')->once()->with('taggables')->andReturn($query);
  44. $query->shouldReceive('where')->once()->with('taggables.taggable_id', 1)->andReturn($query);
  45. $query->shouldReceive('where')->once()->with('taggable_type', get_class($relation->getParent()))->andReturn($query);
  46. $query->shouldReceive('whereIn')->once()->with('taggables.tag_id', [1, 2, 3]);
  47. $query->shouldReceive('delete')->once()->andReturn(true);
  48. $relation->getQuery()->shouldReceive('getQuery')->andReturn($mockQueryBuilder = m::mock(stdClass::class));
  49. $mockQueryBuilder->shouldReceive('newQuery')->once()->andReturn($query);
  50. $relation->expects($this->once())->method('touchIfTouching');
  51. $this->assertTrue($relation->detach([1, 2, 3]));
  52. }
  53. public function testDetachMethodClearsAllPivotRecordsWhenNoIDsAreGiven()
  54. {
  55. $relation = $this->getMockBuilder(MorphToMany::class)->onlyMethods(['touchIfTouching'])->setConstructorArgs($this->getRelationArguments())->getMock();
  56. $query = m::mock(stdClass::class);
  57. $query->shouldReceive('from')->once()->with('taggables')->andReturn($query);
  58. $query->shouldReceive('where')->once()->with('taggables.taggable_id', 1)->andReturn($query);
  59. $query->shouldReceive('where')->once()->with('taggable_type', get_class($relation->getParent()))->andReturn($query);
  60. $query->shouldReceive('whereIn')->never();
  61. $query->shouldReceive('delete')->once()->andReturn(true);
  62. $relation->getQuery()->shouldReceive('getQuery')->andReturn($mockQueryBuilder = m::mock(stdClass::class));
  63. $mockQueryBuilder->shouldReceive('newQuery')->once()->andReturn($query);
  64. $relation->expects($this->once())->method('touchIfTouching');
  65. $this->assertTrue($relation->detach());
  66. }
  67. public function getRelation()
  68. {
  69. [$builder, $parent] = $this->getRelationArguments();
  70. return new MorphToMany($builder, $parent, 'taggable', 'taggables', 'taggable_id', 'tag_id', 'id', 'id');
  71. }
  72. public function getRelationArguments()
  73. {
  74. $parent = m::mock(Model::class);
  75. $parent->shouldReceive('getMorphClass')->andReturn(get_class($parent));
  76. $parent->shouldReceive('getKey')->andReturn(1);
  77. $parent->shouldReceive('getCreatedAtColumn')->andReturn('created_at');
  78. $parent->shouldReceive('getUpdatedAtColumn')->andReturn('updated_at');
  79. $parent->shouldReceive('getMorphClass')->andReturn(get_class($parent));
  80. $parent->shouldReceive('getAttribute')->with('id')->andReturn(1);
  81. $builder = m::mock(Builder::class);
  82. $related = m::mock(Model::class);
  83. $builder->shouldReceive('getModel')->andReturn($related);
  84. $related->shouldReceive('getTable')->andReturn('tags');
  85. $related->shouldReceive('getKeyName')->andReturn('id');
  86. $related->shouldReceive('qualifyColumn')->with('id')->andReturn('tags.id');
  87. $related->shouldReceive('getMorphClass')->andReturn(get_class($related));
  88. $builder->shouldReceive('join')->once()->with('taggables', 'tags.id', '=', 'taggables.tag_id');
  89. $builder->shouldReceive('where')->once()->with('taggables.taggable_id', '=', 1);
  90. $builder->shouldReceive('where')->once()->with('taggables.taggable_type', get_class($parent));
  91. return [$builder, $parent, 'taggable', 'taggables', 'taggable_id', 'tag_id', 'id', 'id', 'relation_name', false];
  92. }
  93. }
  94. class EloquentMorphToManyModelStub extends Model
  95. {
  96. protected $guarded = [];
  97. }