DatabaseEloquentPivotTest.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. namespace Illuminate\Tests\Database;
  3. use Illuminate\Database\Connection;
  4. use Illuminate\Database\ConnectionResolverInterface;
  5. use Illuminate\Database\Eloquent\Model;
  6. use Illuminate\Database\Eloquent\Relations\Pivot;
  7. use Illuminate\Database\Query\Grammars\Grammar;
  8. use Illuminate\Database\Query\Processors\Processor;
  9. use Mockery as m;
  10. use PHPUnit\Framework\TestCase;
  11. use stdClass;
  12. class DatabaseEloquentPivotTest extends TestCase
  13. {
  14. protected function tearDown(): void
  15. {
  16. m::close();
  17. }
  18. public function testPropertiesAreSetCorrectly()
  19. {
  20. $parent = m::mock(Model::class.'[getConnectionName]');
  21. $parent->shouldReceive('getConnectionName')->twice()->andReturn('connection');
  22. $parent->setConnectionResolver($resolver = m::mock(ConnectionResolverInterface::class));
  23. $resolver->shouldReceive('connection')->andReturn($connection = m::mock(Connection::class));
  24. $connection->shouldReceive('getQueryGrammar')->andReturn($grammar = m::mock(Grammar::class));
  25. $connection->shouldReceive('getPostProcessor')->andReturn($processor = m::mock(Processor::class));
  26. $parent->getConnection()->getQueryGrammar()->shouldReceive('getDateFormat')->andReturn('Y-m-d H:i:s');
  27. $parent->setDateFormat('Y-m-d H:i:s');
  28. $pivot = Pivot::fromAttributes($parent, ['foo' => 'bar', 'created_at' => '2015-09-12'], 'table', true);
  29. $this->assertEquals(['foo' => 'bar', 'created_at' => '2015-09-12 00:00:00'], $pivot->getAttributes());
  30. $this->assertSame('connection', $pivot->getConnectionName());
  31. $this->assertSame('table', $pivot->getTable());
  32. $this->assertTrue($pivot->exists);
  33. }
  34. public function testMutatorsAreCalledFromConstructor()
  35. {
  36. $parent = m::mock(Model::class.'[getConnectionName]');
  37. $parent->shouldReceive('getConnectionName')->once()->andReturn('connection');
  38. $pivot = DatabaseEloquentPivotTestMutatorStub::fromAttributes($parent, ['foo' => 'bar'], 'table', true);
  39. $this->assertTrue($pivot->getMutatorCalled());
  40. }
  41. public function testFromRawAttributesDoesNotDoubleMutate()
  42. {
  43. $parent = m::mock(Model::class.'[getConnectionName]');
  44. $parent->shouldReceive('getConnectionName')->once()->andReturn('connection');
  45. $pivot = DatabaseEloquentPivotTestJsonCastStub::fromRawAttributes($parent, ['foo' => json_encode(['name' => 'Taylor'])], 'table', true);
  46. $this->assertEquals(['name' => 'Taylor'], $pivot->foo);
  47. }
  48. public function testFromRawAttributesDoesNotMutate()
  49. {
  50. $parent = m::mock(Model::class.'[getConnectionName]');
  51. $parent->shouldReceive('getConnectionName')->once()->andReturn('connection');
  52. $pivot = DatabaseEloquentPivotTestMutatorStub::fromRawAttributes($parent, ['foo' => 'bar'], 'table', true);
  53. $this->assertFalse($pivot->getMutatorCalled());
  54. }
  55. public function testPropertiesUnchangedAreNotDirty()
  56. {
  57. $parent = m::mock(Model::class.'[getConnectionName]');
  58. $parent->shouldReceive('getConnectionName')->once()->andReturn('connection');
  59. $pivot = Pivot::fromAttributes($parent, ['foo' => 'bar', 'shimy' => 'shake'], 'table', true);
  60. $this->assertEquals([], $pivot->getDirty());
  61. }
  62. public function testPropertiesChangedAreDirty()
  63. {
  64. $parent = m::mock(Model::class.'[getConnectionName]');
  65. $parent->shouldReceive('getConnectionName')->once()->andReturn('connection');
  66. $pivot = Pivot::fromAttributes($parent, ['foo' => 'bar', 'shimy' => 'shake'], 'table', true);
  67. $pivot->shimy = 'changed';
  68. $this->assertEquals(['shimy' => 'changed'], $pivot->getDirty());
  69. }
  70. public function testTimestampPropertyIsSetIfCreatedAtInAttributes()
  71. {
  72. $parent = m::mock(Model::class.'[getConnectionName,getDates]');
  73. $parent->shouldReceive('getConnectionName')->andReturn('connection');
  74. $parent->shouldReceive('getDates')->andReturn([]);
  75. $pivot = DatabaseEloquentPivotTestDateStub::fromAttributes($parent, ['foo' => 'bar', 'created_at' => 'foo'], 'table');
  76. $this->assertTrue($pivot->timestamps);
  77. $pivot = DatabaseEloquentPivotTestDateStub::fromAttributes($parent, ['foo' => 'bar'], 'table');
  78. $this->assertFalse($pivot->timestamps);
  79. }
  80. public function testTimestampPropertyIsTrueWhenCreatingFromRawAttributes()
  81. {
  82. $parent = m::mock(Model::class.'[getConnectionName,getDates]');
  83. $parent->shouldReceive('getConnectionName')->andReturn('connection');
  84. $pivot = Pivot::fromRawAttributes($parent, ['foo' => 'bar', 'created_at' => 'foo'], 'table');
  85. $this->assertTrue($pivot->timestamps);
  86. }
  87. public function testKeysCanBeSetProperly()
  88. {
  89. $parent = m::mock(Model::class.'[getConnectionName]');
  90. $parent->shouldReceive('getConnectionName')->once()->andReturn('connection');
  91. $pivot = Pivot::fromAttributes($parent, ['foo' => 'bar'], 'table');
  92. $pivot->setPivotKeys('foreign', 'other');
  93. $this->assertSame('foreign', $pivot->getForeignKey());
  94. $this->assertSame('other', $pivot->getOtherKey());
  95. }
  96. public function testDeleteMethodDeletesModelByKeys()
  97. {
  98. $pivot = $this->getMockBuilder(Pivot::class)->onlyMethods(['newQueryWithoutRelationships'])->getMock();
  99. $pivot->setPivotKeys('foreign', 'other');
  100. $pivot->foreign = 'foreign.value';
  101. $pivot->other = 'other.value';
  102. $query = m::mock(stdClass::class);
  103. $query->shouldReceive('where')->once()->with(['foreign' => 'foreign.value', 'other' => 'other.value'])->andReturn($query);
  104. $query->shouldReceive('delete')->once()->andReturn(true);
  105. $pivot->expects($this->once())->method('newQueryWithoutRelationships')->willReturn($query);
  106. $rowsAffected = $pivot->delete();
  107. $this->assertEquals(1, $rowsAffected);
  108. }
  109. public function testPivotModelTableNameIsSingular()
  110. {
  111. $pivot = new Pivot;
  112. $this->assertSame('pivot', $pivot->getTable());
  113. }
  114. public function testPivotModelWithParentReturnsParentsTimestampColumns()
  115. {
  116. $parent = m::mock(Model::class);
  117. $parent->shouldReceive('getCreatedAtColumn')->andReturn('parent_created_at');
  118. $parent->shouldReceive('getUpdatedAtColumn')->andReturn('parent_updated_at');
  119. $pivotWithParent = new Pivot;
  120. $pivotWithParent->pivotParent = $parent;
  121. $this->assertSame('parent_created_at', $pivotWithParent->getCreatedAtColumn());
  122. $this->assertSame('parent_updated_at', $pivotWithParent->getUpdatedAtColumn());
  123. }
  124. public function testPivotModelWithoutParentReturnsModelTimestampColumns()
  125. {
  126. $model = new DummyModel;
  127. $pivotWithoutParent = new Pivot;
  128. $this->assertEquals($model->getCreatedAtColumn(), $pivotWithoutParent->getCreatedAtColumn());
  129. $this->assertEquals($model->getUpdatedAtColumn(), $pivotWithoutParent->getUpdatedAtColumn());
  130. }
  131. public function testWithoutRelations()
  132. {
  133. $original = new Pivot;
  134. $original->pivotParent = 'foo';
  135. $original->setRelation('bar', 'baz');
  136. $this->assertSame('baz', $original->getRelation('bar'));
  137. $pivot = $original->withoutRelations();
  138. $this->assertInstanceOf(Pivot::class, $pivot);
  139. $this->assertNotSame($pivot, $original);
  140. $this->assertSame('foo', $original->pivotParent);
  141. $this->assertNull($pivot->pivotParent);
  142. $this->assertTrue($original->relationLoaded('bar'));
  143. $this->assertFalse($pivot->relationLoaded('bar'));
  144. $pivot = $original->unsetRelations();
  145. $this->assertSame($pivot, $original);
  146. $this->assertNull($pivot->pivotParent);
  147. $this->assertFalse($pivot->relationLoaded('bar'));
  148. }
  149. }
  150. class DatabaseEloquentPivotTestDateStub extends Pivot
  151. {
  152. public function getDates()
  153. {
  154. return [];
  155. }
  156. }
  157. class DatabaseEloquentPivotTestMutatorStub extends Pivot
  158. {
  159. private $mutatorCalled = false;
  160. public function setFooAttribute($value)
  161. {
  162. $this->mutatorCalled = true;
  163. return $value;
  164. }
  165. public function getMutatorCalled()
  166. {
  167. return $this->mutatorCalled;
  168. }
  169. }
  170. class DatabaseEloquentPivotTestJsonCastStub extends Pivot
  171. {
  172. protected $casts = [
  173. 'foo' => 'json',
  174. ];
  175. }
  176. class DummyModel extends Model
  177. {
  178. //
  179. }