DatabaseEloquentMorphToTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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\MorphTo;
  6. use Illuminate\Tests\Database\stubs\TestEnum;
  7. use Mockery as m;
  8. use PHPUnit\Framework\TestCase;
  9. class DatabaseEloquentMorphToTest extends TestCase
  10. {
  11. protected $builder;
  12. protected $related;
  13. protected function tearDown(): void
  14. {
  15. m::close();
  16. }
  17. public function testLookupDictionaryIsProperlyConstructedForEnums()
  18. {
  19. if (version_compare(PHP_VERSION, '8.1') < 0) {
  20. $this->markTestSkipped('PHP 8.1 is required');
  21. } else {
  22. $relation = $this->getRelation();
  23. $relation->addEagerConstraints([
  24. $one = (object) ['morph_type' => 'morph_type_2', 'foreign_key' => TestEnum::test],
  25. ]);
  26. $dictionary = $relation->getDictionary();
  27. $relation->getDictionary();
  28. $enumKey = TestEnum::test;
  29. if (isset($enumKey->value)) {
  30. $value = $dictionary['morph_type_2'][$enumKey->value][0]->foreign_key;
  31. $this->assertEquals(TestEnum::test, $value);
  32. } else {
  33. $this->fail('An enum should contain value property');
  34. }
  35. }
  36. }
  37. public function testLookupDictionaryIsProperlyConstructed()
  38. {
  39. $stringish = new class
  40. {
  41. public function __toString()
  42. {
  43. return 'foreign_key_2';
  44. }
  45. };
  46. $relation = $this->getRelation();
  47. $relation->addEagerConstraints([
  48. $one = (object) ['morph_type' => 'morph_type_1', 'foreign_key' => 'foreign_key_1'],
  49. $two = (object) ['morph_type' => 'morph_type_1', 'foreign_key' => 'foreign_key_1'],
  50. $three = (object) ['morph_type' => 'morph_type_2', 'foreign_key' => 'foreign_key_2'],
  51. $four = (object) ['morph_type' => 'morph_type_2', 'foreign_key' => $stringish],
  52. ]);
  53. $dictionary = $relation->getDictionary();
  54. $this->assertEquals([
  55. 'morph_type_1' => [
  56. 'foreign_key_1' => [
  57. $one,
  58. $two,
  59. ],
  60. ],
  61. 'morph_type_2' => [
  62. 'foreign_key_2' => [
  63. $three,
  64. $four,
  65. ],
  66. ],
  67. ], $dictionary);
  68. }
  69. public function testMorphToWithDefault()
  70. {
  71. $relation = $this->getRelation()->withDefault();
  72. $this->builder->shouldReceive('first')->once()->andReturnNull();
  73. $newModel = new EloquentMorphToModelStub;
  74. $this->assertEquals($newModel, $relation->getResults());
  75. }
  76. public function testMorphToWithDynamicDefault()
  77. {
  78. $relation = $this->getRelation()->withDefault(function ($newModel) {
  79. $newModel->username = 'taylor';
  80. });
  81. $this->builder->shouldReceive('first')->once()->andReturnNull();
  82. $newModel = new EloquentMorphToModelStub;
  83. $newModel->username = 'taylor';
  84. $result = $relation->getResults();
  85. $this->assertEquals($newModel, $result);
  86. $this->assertSame('taylor', $result->username);
  87. }
  88. public function testMorphToWithArrayDefault()
  89. {
  90. $relation = $this->getRelation()->withDefault(['username' => 'taylor']);
  91. $this->builder->shouldReceive('first')->once()->andReturnNull();
  92. $newModel = new EloquentMorphToModelStub;
  93. $newModel->username = 'taylor';
  94. $result = $relation->getResults();
  95. $this->assertEquals($newModel, $result);
  96. $this->assertSame('taylor', $result->username);
  97. }
  98. public function testMorphToWithZeroMorphType()
  99. {
  100. $parent = $this->getMockBuilder(EloquentMorphToModelStub::class)->onlyMethods(['getAttributeFromArray', 'morphEagerTo', 'morphInstanceTo'])->getMock();
  101. $parent->method('getAttributeFromArray')->with('relation_type')->willReturn(0);
  102. $parent->expects($this->once())->method('morphInstanceTo');
  103. $parent->expects($this->never())->method('morphEagerTo');
  104. $parent->relation();
  105. }
  106. public function testMorphToWithEmptyStringMorphType()
  107. {
  108. $parent = $this->getMockBuilder(EloquentMorphToModelStub::class)->onlyMethods(['getAttributeFromArray', 'morphEagerTo', 'morphInstanceTo'])->getMock();
  109. $parent->method('getAttributeFromArray')->with('relation_type')->willReturn('');
  110. $parent->expects($this->once())->method('morphEagerTo');
  111. $parent->expects($this->never())->method('morphInstanceTo');
  112. $parent->relation();
  113. }
  114. public function testMorphToWithSpecifiedClassDefault()
  115. {
  116. $parent = new EloquentMorphToModelStub;
  117. $parent->relation_type = EloquentMorphToRelatedStub::class;
  118. $relation = $parent->relation()->withDefault();
  119. $newModel = new EloquentMorphToRelatedStub;
  120. $result = $relation->getResults();
  121. $this->assertEquals($newModel, $result);
  122. }
  123. public function testAssociateMethodSetsForeignKeyAndTypeOnModel()
  124. {
  125. $parent = m::mock(Model::class);
  126. $parent->shouldReceive('getAttribute')->with('foreign_key')->andReturn('foreign.value');
  127. $relation = $this->getRelationAssociate($parent);
  128. $associate = m::mock(Model::class);
  129. $associate->shouldReceive('getAttribute')->andReturn(1);
  130. $associate->shouldReceive('getMorphClass')->andReturn('Model');
  131. $parent->shouldReceive('setAttribute')->once()->with('foreign_key', 1);
  132. $parent->shouldReceive('setAttribute')->once()->with('morph_type', 'Model');
  133. $parent->shouldReceive('setRelation')->once()->with('relation', $associate);
  134. $relation->associate($associate);
  135. }
  136. public function testAssociateMethodIgnoresNullValue()
  137. {
  138. $parent = m::mock(Model::class);
  139. $parent->shouldReceive('getAttribute')->once()->with('foreign_key')->andReturn('foreign.value');
  140. $relation = $this->getRelationAssociate($parent);
  141. $parent->shouldReceive('setAttribute')->once()->with('foreign_key', null);
  142. $parent->shouldReceive('setAttribute')->once()->with('morph_type', null);
  143. $parent->shouldReceive('setRelation')->once()->with('relation', null);
  144. $relation->associate(null);
  145. }
  146. public function testDissociateMethodDeletesUnsetsKeyAndTypeOnModel()
  147. {
  148. $parent = m::mock(Model::class);
  149. $parent->shouldReceive('getAttribute')->once()->with('foreign_key')->andReturn('foreign.value');
  150. $relation = $this->getRelation($parent);
  151. $parent->shouldReceive('setAttribute')->once()->with('foreign_key', null);
  152. $parent->shouldReceive('setAttribute')->once()->with('morph_type', null);
  153. $parent->shouldReceive('setRelation')->once()->with('relation', null);
  154. $relation->dissociate();
  155. }
  156. public function testIsNotNull()
  157. {
  158. $relation = $this->getRelation();
  159. $relation->getRelated()->shouldReceive('getTable')->never();
  160. $relation->getRelated()->shouldReceive('getConnectionName')->never();
  161. $this->assertFalse($relation->is(null));
  162. }
  163. public function testIsModel()
  164. {
  165. $relation = $this->getRelation();
  166. $this->related->shouldReceive('getConnectionName')->once()->andReturn('relation');
  167. $model = m::mock(Model::class);
  168. $model->shouldReceive('getAttribute')->once()->with('id')->andReturn('foreign.value');
  169. $model->shouldReceive('getTable')->once()->andReturn('relation');
  170. $model->shouldReceive('getConnectionName')->once()->andReturn('relation');
  171. $this->assertTrue($relation->is($model));
  172. }
  173. public function testIsModelWithIntegerParentKey()
  174. {
  175. $parent = m::mock(Model::class);
  176. // when addConstraints is called we need to return the foreign value
  177. $parent->shouldReceive('getAttribute')->once()->with('foreign_key')->andReturn('foreign.value');
  178. // when getParentKey is called we want to return an integer
  179. $parent->shouldReceive('getAttribute')->once()->with('foreign_key')->andReturn(1);
  180. $relation = $this->getRelation($parent);
  181. $this->related->shouldReceive('getConnectionName')->once()->andReturn('relation');
  182. $model = m::mock(Model::class);
  183. $model->shouldReceive('getAttribute')->once()->with('id')->andReturn('1');
  184. $model->shouldReceive('getTable')->once()->andReturn('relation');
  185. $model->shouldReceive('getConnectionName')->once()->andReturn('relation');
  186. $this->assertTrue($relation->is($model));
  187. }
  188. public function testIsModelWithIntegerRelatedKey()
  189. {
  190. $parent = m::mock(Model::class);
  191. // when addConstraints is called we need to return the foreign value
  192. $parent->shouldReceive('getAttribute')->once()->with('foreign_key')->andReturn('foreign.value');
  193. // when getParentKey is called we want to return a string
  194. $parent->shouldReceive('getAttribute')->once()->with('foreign_key')->andReturn('1');
  195. $relation = $this->getRelation($parent);
  196. $this->related->shouldReceive('getConnectionName')->once()->andReturn('relation');
  197. $model = m::mock(Model::class);
  198. $model->shouldReceive('getAttribute')->once()->with('id')->andReturn(1);
  199. $model->shouldReceive('getTable')->once()->andReturn('relation');
  200. $model->shouldReceive('getConnectionName')->once()->andReturn('relation');
  201. $this->assertTrue($relation->is($model));
  202. }
  203. public function testIsModelWithIntegerKeys()
  204. {
  205. $parent = m::mock(Model::class);
  206. // when addConstraints is called we need to return the foreign value
  207. $parent->shouldReceive('getAttribute')->once()->with('foreign_key')->andReturn('foreign.value');
  208. // when getParentKey is called we want to return an integer
  209. $parent->shouldReceive('getAttribute')->once()->with('foreign_key')->andReturn(1);
  210. $relation = $this->getRelation($parent);
  211. $this->related->shouldReceive('getConnectionName')->once()->andReturn('relation');
  212. $model = m::mock(Model::class);
  213. $model->shouldReceive('getAttribute')->once()->with('id')->andReturn(1);
  214. $model->shouldReceive('getTable')->once()->andReturn('relation');
  215. $model->shouldReceive('getConnectionName')->once()->andReturn('relation');
  216. $this->assertTrue($relation->is($model));
  217. }
  218. public function testIsNotModelWithNullParentKey()
  219. {
  220. $parent = m::mock(Model::class);
  221. // when addConstraints is called we need to return the foreign value
  222. $parent->shouldReceive('getAttribute')->once()->with('foreign_key')->andReturn('foreign.value');
  223. // when getParentKey is called we want to return null
  224. $parent->shouldReceive('getAttribute')->once()->with('foreign_key')->andReturn(null);
  225. $relation = $this->getRelation($parent);
  226. $this->related->shouldReceive('getConnectionName')->never();
  227. $model = m::mock(Model::class);
  228. $model->shouldReceive('getAttribute')->once()->with('id')->andReturn('foreign.value');
  229. $model->shouldReceive('getTable')->never();
  230. $model->shouldReceive('getConnectionName')->never();
  231. $this->assertFalse($relation->is($model));
  232. }
  233. public function testIsNotModelWithNullRelatedKey()
  234. {
  235. $relation = $this->getRelation();
  236. $this->related->shouldReceive('getConnectionName')->never();
  237. $model = m::mock(Model::class);
  238. $model->shouldReceive('getAttribute')->once()->with('id')->andReturn(null);
  239. $model->shouldReceive('getTable')->never();
  240. $model->shouldReceive('getConnectionName')->never();
  241. $this->assertFalse($relation->is($model));
  242. }
  243. public function testIsNotModelWithAnotherKey()
  244. {
  245. $relation = $this->getRelation();
  246. $this->related->shouldReceive('getConnectionName')->never();
  247. $model = m::mock(Model::class);
  248. $model->shouldReceive('getAttribute')->once()->with('id')->andReturn('foreign.value.two');
  249. $model->shouldReceive('getTable')->never();
  250. $model->shouldReceive('getConnectionName')->never();
  251. $this->assertFalse($relation->is($model));
  252. }
  253. public function testIsNotModelWithAnotherTable()
  254. {
  255. $relation = $this->getRelation();
  256. $this->related->shouldReceive('getConnectionName')->never();
  257. $model = m::mock(Model::class);
  258. $model->shouldReceive('getAttribute')->once()->with('id')->andReturn('foreign.value');
  259. $model->shouldReceive('getTable')->once()->andReturn('table.two');
  260. $model->shouldReceive('getConnectionName')->never();
  261. $this->assertFalse($relation->is($model));
  262. }
  263. public function testIsNotModelWithAnotherConnection()
  264. {
  265. $relation = $this->getRelation();
  266. $this->related->shouldReceive('getConnectionName')->once()->andReturn('relation');
  267. $model = m::mock(Model::class);
  268. $model->shouldReceive('getAttribute')->once()->with('id')->andReturn('foreign.value');
  269. $model->shouldReceive('getTable')->once()->andReturn('relation');
  270. $model->shouldReceive('getConnectionName')->once()->andReturn('relation.two');
  271. $this->assertFalse($relation->is($model));
  272. }
  273. protected function getRelationAssociate($parent)
  274. {
  275. $builder = m::mock(Builder::class);
  276. $builder->shouldReceive('where')->with('relation.id', '=', 'foreign.value');
  277. $related = m::mock(Model::class);
  278. $related->shouldReceive('getKey')->andReturn(1);
  279. $related->shouldReceive('getTable')->andReturn('relation');
  280. $builder->shouldReceive('getModel')->andReturn($related);
  281. return new MorphTo($builder, $parent, 'foreign_key', 'id', 'morph_type', 'relation');
  282. }
  283. public function getRelation($parent = null, $builder = null)
  284. {
  285. $this->builder = $builder ?: m::mock(Builder::class);
  286. $this->builder->shouldReceive('where')->with('relation.id', '=', 'foreign.value');
  287. $this->related = m::mock(Model::class);
  288. $this->related->shouldReceive('getKeyName')->andReturn('id');
  289. $this->related->shouldReceive('getTable')->andReturn('relation');
  290. $this->builder->shouldReceive('getModel')->andReturn($this->related);
  291. $parent = $parent ?: new EloquentMorphToModelStub;
  292. return m::mock(MorphTo::class.'[createModelByType]', [$this->builder, $parent, 'foreign_key', 'id', 'morph_type', 'relation']);
  293. }
  294. }
  295. class EloquentMorphToModelStub extends Model
  296. {
  297. public $foreign_key = 'foreign.value';
  298. public $table = 'eloquent_morph_to_model_stubs';
  299. public function relation()
  300. {
  301. return $this->morphTo();
  302. }
  303. }
  304. class EloquentMorphToRelatedStub extends Model
  305. {
  306. public $table = 'eloquent_morph_to_related_stubs';
  307. }