DatabaseEloquentMorphTest.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. <?php
  2. namespace Illuminate\Tests\Database;
  3. use Foo\Bar\EloquentModelNamespacedStub;
  4. use Illuminate\Database\Eloquent\Builder;
  5. use Illuminate\Database\Eloquent\Model;
  6. use Illuminate\Database\Eloquent\Relations\MorphMany;
  7. use Illuminate\Database\Eloquent\Relations\MorphOne;
  8. use Illuminate\Database\Eloquent\Relations\Relation;
  9. use Mockery as m;
  10. use PHPUnit\Framework\TestCase;
  11. class DatabaseEloquentMorphTest extends TestCase
  12. {
  13. protected function tearDown(): void
  14. {
  15. Relation::morphMap([], false);
  16. m::close();
  17. }
  18. public function testMorphOneSetsProperConstraints()
  19. {
  20. $this->getOneRelation();
  21. }
  22. public function testMorphOneEagerConstraintsAreProperlyAdded()
  23. {
  24. $relation = $this->getOneRelation();
  25. $relation->getParent()->shouldReceive('getKeyName')->once()->andReturn('id');
  26. $relation->getParent()->shouldReceive('getKeyType')->once()->andReturn('string');
  27. $relation->getQuery()->shouldReceive('whereIn')->once()->with('table.morph_id', [1, 2]);
  28. $relation->getQuery()->shouldReceive('where')->once()->with('table.morph_type', get_class($relation->getParent()));
  29. $model1 = new EloquentMorphResetModelStub;
  30. $model1->id = 1;
  31. $model2 = new EloquentMorphResetModelStub;
  32. $model2->id = 2;
  33. $relation->addEagerConstraints([$model1, $model2]);
  34. }
  35. /**
  36. * Note that the tests are the exact same for morph many because the classes share this code...
  37. * Will still test to be safe.
  38. */
  39. public function testMorphManySetsProperConstraints()
  40. {
  41. $this->getManyRelation();
  42. }
  43. public function testMorphManyEagerConstraintsAreProperlyAdded()
  44. {
  45. $relation = $this->getManyRelation();
  46. $relation->getParent()->shouldReceive('getKeyName')->once()->andReturn('id');
  47. $relation->getParent()->shouldReceive('getKeyType')->once()->andReturn('int');
  48. $relation->getQuery()->shouldReceive('whereIntegerInRaw')->once()->with('table.morph_id', [1, 2]);
  49. $relation->getQuery()->shouldReceive('where')->once()->with('table.morph_type', get_class($relation->getParent()));
  50. $model1 = new EloquentMorphResetModelStub;
  51. $model1->id = 1;
  52. $model2 = new EloquentMorphResetModelStub;
  53. $model2->id = 2;
  54. $relation->addEagerConstraints([$model1, $model2]);
  55. }
  56. public function testMakeFunctionOnMorph()
  57. {
  58. $_SERVER['__eloquent.saved'] = false;
  59. // Doesn't matter which relation type we use since they share the code...
  60. $relation = $this->getOneRelation();
  61. $instance = m::mock(Model::class);
  62. $instance->shouldReceive('setAttribute')->once()->with('morph_id', 1);
  63. $instance->shouldReceive('setAttribute')->once()->with('morph_type', get_class($relation->getParent()));
  64. $instance->shouldReceive('save')->never();
  65. $relation->getRelated()->shouldReceive('newInstance')->once()->with(['name' => 'taylor'])->andReturn($instance);
  66. $this->assertEquals($instance, $relation->make(['name' => 'taylor']));
  67. }
  68. public function testCreateFunctionOnMorph()
  69. {
  70. // Doesn't matter which relation type we use since they share the code...
  71. $relation = $this->getOneRelation();
  72. $created = m::mock(Model::class);
  73. $created->shouldReceive('setAttribute')->once()->with('morph_id', 1);
  74. $created->shouldReceive('setAttribute')->once()->with('morph_type', get_class($relation->getParent()));
  75. $relation->getRelated()->shouldReceive('newInstance')->once()->with(['name' => 'taylor'])->andReturn($created);
  76. $created->shouldReceive('save')->once()->andReturn(true);
  77. $this->assertEquals($created, $relation->create(['name' => 'taylor']));
  78. }
  79. public function testFindOrNewMethodFindsModel()
  80. {
  81. $relation = $this->getOneRelation();
  82. $relation->getQuery()->shouldReceive('find')->once()->with('foo', ['*'])->andReturn($model = m::mock(Model::class));
  83. $relation->getRelated()->shouldReceive('newInstance')->never();
  84. $model->shouldReceive('setAttribute')->never();
  85. $model->shouldReceive('save')->never();
  86. $this->assertInstanceOf(Model::class, $relation->findOrNew('foo'));
  87. }
  88. public function testFindOrNewMethodReturnsNewModelWithMorphKeysSet()
  89. {
  90. $relation = $this->getOneRelation();
  91. $relation->getQuery()->shouldReceive('find')->once()->with('foo', ['*'])->andReturn(null);
  92. $relation->getRelated()->shouldReceive('newInstance')->once()->with()->andReturn($model = m::mock(Model::class));
  93. $model->shouldReceive('setAttribute')->once()->with('morph_id', 1);
  94. $model->shouldReceive('setAttribute')->once()->with('morph_type', get_class($relation->getParent()));
  95. $model->shouldReceive('save')->never();
  96. $this->assertInstanceOf(Model::class, $relation->findOrNew('foo'));
  97. }
  98. public function testFirstOrNewMethodFindsFirstModel()
  99. {
  100. $relation = $this->getOneRelation();
  101. $relation->getQuery()->shouldReceive('where')->once()->with(['foo'])->andReturn($relation->getQuery());
  102. $relation->getQuery()->shouldReceive('first')->once()->with()->andReturn($model = m::mock(Model::class));
  103. $relation->getRelated()->shouldReceive('newInstance')->never();
  104. $model->shouldReceive('setAttribute')->never();
  105. $model->shouldReceive('save')->never();
  106. $this->assertInstanceOf(Model::class, $relation->firstOrNew(['foo']));
  107. }
  108. public function testFirstOrNewMethodWithValueFindsFirstModel()
  109. {
  110. $relation = $this->getOneRelation();
  111. $relation->getQuery()->shouldReceive('where')->once()->with(['foo' => 'bar'])->andReturn($relation->getQuery());
  112. $relation->getQuery()->shouldReceive('first')->once()->with()->andReturn($model = m::mock(Model::class));
  113. $relation->getRelated()->shouldReceive('newInstance')->never();
  114. $model->shouldReceive('setAttribute')->never();
  115. $model->shouldReceive('save')->never();
  116. $this->assertInstanceOf(Model::class, $relation->firstOrNew(['foo' => 'bar'], ['baz' => 'qux']));
  117. }
  118. public function testFirstOrNewMethodReturnsNewModelWithMorphKeysSet()
  119. {
  120. $relation = $this->getOneRelation();
  121. $relation->getQuery()->shouldReceive('where')->once()->with(['foo'])->andReturn($relation->getQuery());
  122. $relation->getQuery()->shouldReceive('first')->once()->with()->andReturn(null);
  123. $relation->getRelated()->shouldReceive('newInstance')->once()->with(['foo'])->andReturn($model = m::mock(Model::class));
  124. $model->shouldReceive('setAttribute')->once()->with('morph_id', 1);
  125. $model->shouldReceive('setAttribute')->once()->with('morph_type', get_class($relation->getParent()));
  126. $model->shouldReceive('save')->never();
  127. $this->assertInstanceOf(Model::class, $relation->firstOrNew(['foo']));
  128. }
  129. public function testFirstOrNewMethodWithValuesReturnsNewModelWithMorphKeysSet()
  130. {
  131. $relation = $this->getOneRelation();
  132. $relation->getQuery()->shouldReceive('where')->once()->with(['foo' => 'bar'])->andReturn($relation->getQuery());
  133. $relation->getQuery()->shouldReceive('first')->once()->with()->andReturn(null);
  134. $relation->getRelated()->shouldReceive('newInstance')->once()->with(['foo' => 'bar', 'baz' => 'qux'])->andReturn($model = m::mock(Model::class));
  135. $model->shouldReceive('setAttribute')->once()->with('morph_id', 1);
  136. $model->shouldReceive('setAttribute')->once()->with('morph_type', get_class($relation->getParent()));
  137. $model->shouldReceive('save')->never();
  138. $this->assertInstanceOf(Model::class, $relation->firstOrNew(['foo' => 'bar'], ['baz' => 'qux']));
  139. }
  140. public function testFirstOrCreateMethodFindsFirstModel()
  141. {
  142. $relation = $this->getOneRelation();
  143. $relation->getQuery()->shouldReceive('where')->once()->with(['foo'])->andReturn($relation->getQuery());
  144. $relation->getQuery()->shouldReceive('first')->once()->with()->andReturn($model = m::mock(Model::class));
  145. $relation->getRelated()->shouldReceive('newInstance')->never();
  146. $model->shouldReceive('setAttribute')->never();
  147. $model->shouldReceive('save')->never();
  148. $this->assertInstanceOf(Model::class, $relation->firstOrCreate(['foo']));
  149. }
  150. public function testFirstOrCreateMethodWithValuesFindsFirstModel()
  151. {
  152. $relation = $this->getOneRelation();
  153. $relation->getQuery()->shouldReceive('where')->once()->with(['foo' => 'bar'])->andReturn($relation->getQuery());
  154. $relation->getQuery()->shouldReceive('first')->once()->with()->andReturn($model = m::mock(Model::class));
  155. $relation->getRelated()->shouldReceive('newInstance')->never();
  156. $model->shouldReceive('setAttribute')->never();
  157. $model->shouldReceive('save')->never();
  158. $this->assertInstanceOf(Model::class, $relation->firstOrCreate(['foo' => 'bar'], ['baz' => 'qux']));
  159. }
  160. public function testFirstOrCreateMethodCreatesNewMorphModel()
  161. {
  162. $relation = $this->getOneRelation();
  163. $relation->getQuery()->shouldReceive('where')->once()->with(['foo'])->andReturn($relation->getQuery());
  164. $relation->getQuery()->shouldReceive('first')->once()->with()->andReturn(null);
  165. $relation->getRelated()->shouldReceive('newInstance')->once()->with(['foo'])->andReturn($model = m::mock(Model::class));
  166. $model->shouldReceive('setAttribute')->once()->with('morph_id', 1);
  167. $model->shouldReceive('setAttribute')->once()->with('morph_type', get_class($relation->getParent()));
  168. $model->shouldReceive('save')->once()->andReturn(true);
  169. $this->assertInstanceOf(Model::class, $relation->firstOrCreate(['foo']));
  170. }
  171. public function testFirstOrCreateMethodWithValuesCreatesNewMorphModel()
  172. {
  173. $relation = $this->getOneRelation();
  174. $relation->getQuery()->shouldReceive('where')->once()->with(['foo' => 'bar'])->andReturn($relation->getQuery());
  175. $relation->getQuery()->shouldReceive('first')->once()->with()->andReturn(null);
  176. $relation->getRelated()->shouldReceive('newInstance')->once()->with(['foo' => 'bar', 'baz' => 'qux'])->andReturn($model = m::mock(Model::class));
  177. $model->shouldReceive('setAttribute')->once()->with('morph_id', 1);
  178. $model->shouldReceive('setAttribute')->once()->with('morph_type', get_class($relation->getParent()));
  179. $model->shouldReceive('save')->once()->andReturn(true);
  180. $this->assertInstanceOf(Model::class, $relation->firstOrCreate(['foo' => 'bar'], ['baz' => 'qux']));
  181. }
  182. public function testUpdateOrCreateMethodFindsFirstModelAndUpdates()
  183. {
  184. $relation = $this->getOneRelation();
  185. $relation->getQuery()->shouldReceive('where')->once()->with(['foo'])->andReturn($relation->getQuery());
  186. $relation->getQuery()->shouldReceive('first')->once()->with()->andReturn($model = m::mock(Model::class));
  187. $relation->getRelated()->shouldReceive('newInstance')->never();
  188. $model->shouldReceive('setAttribute')->never();
  189. $model->shouldReceive('fill')->once()->with(['bar']);
  190. $model->shouldReceive('save')->once();
  191. $this->assertInstanceOf(Model::class, $relation->updateOrCreate(['foo'], ['bar']));
  192. }
  193. public function testUpdateOrCreateMethodCreatesNewMorphModel()
  194. {
  195. $relation = $this->getOneRelation();
  196. $relation->getQuery()->shouldReceive('where')->once()->with(['foo'])->andReturn($relation->getQuery());
  197. $relation->getQuery()->shouldReceive('first')->once()->with()->andReturn(null);
  198. $relation->getRelated()->shouldReceive('newInstance')->once()->with(['foo'])->andReturn($model = m::mock(Model::class));
  199. $model->shouldReceive('setAttribute')->once()->with('morph_id', 1);
  200. $model->shouldReceive('setAttribute')->once()->with('morph_type', get_class($relation->getParent()));
  201. $model->shouldReceive('save')->once()->andReturn(true);
  202. $model->shouldReceive('fill')->once()->with(['bar']);
  203. $this->assertInstanceOf(Model::class, $relation->updateOrCreate(['foo'], ['bar']));
  204. }
  205. public function testCreateFunctionOnNamespacedMorph()
  206. {
  207. $relation = $this->getNamespacedRelation('namespace');
  208. $created = m::mock(Model::class);
  209. $created->shouldReceive('setAttribute')->once()->with('morph_id', 1);
  210. $created->shouldReceive('setAttribute')->once()->with('morph_type', 'namespace');
  211. $relation->getRelated()->shouldReceive('newInstance')->once()->with(['name' => 'taylor'])->andReturn($created);
  212. $created->shouldReceive('save')->once()->andReturn(true);
  213. $this->assertEquals($created, $relation->create(['name' => 'taylor']));
  214. }
  215. public function testIsNotNull()
  216. {
  217. $relation = $this->getOneRelation();
  218. $relation->getRelated()->shouldReceive('getTable')->never();
  219. $relation->getRelated()->shouldReceive('getConnectionName')->never();
  220. $this->assertFalse($relation->is(null));
  221. }
  222. public function testIsModel()
  223. {
  224. $relation = $this->getOneRelation();
  225. $relation->getRelated()->shouldReceive('getTable')->once()->andReturn('table');
  226. $relation->getRelated()->shouldReceive('getConnectionName')->once()->andReturn('connection');
  227. $model = m::mock(Model::class);
  228. $model->shouldReceive('getAttribute')->once()->with('morph_id')->andReturn(1);
  229. $model->shouldReceive('getTable')->once()->andReturn('table');
  230. $model->shouldReceive('getConnectionName')->once()->andReturn('connection');
  231. $this->assertTrue($relation->is($model));
  232. }
  233. public function testIsModelWithStringRelatedKey()
  234. {
  235. $relation = $this->getOneRelation();
  236. $relation->getRelated()->shouldReceive('getTable')->once()->andReturn('table');
  237. $relation->getRelated()->shouldReceive('getConnectionName')->once()->andReturn('connection');
  238. $model = m::mock(Model::class);
  239. $model->shouldReceive('getAttribute')->once()->with('morph_id')->andReturn('1');
  240. $model->shouldReceive('getTable')->once()->andReturn('table');
  241. $model->shouldReceive('getConnectionName')->once()->andReturn('connection');
  242. $this->assertTrue($relation->is($model));
  243. }
  244. public function testIsNotModelWithNullRelatedKey()
  245. {
  246. $relation = $this->getOneRelation();
  247. $relation->getRelated()->shouldReceive('getTable')->never();
  248. $relation->getRelated()->shouldReceive('getConnectionName')->never();
  249. $model = m::mock(Model::class);
  250. $model->shouldReceive('getAttribute')->once()->with('morph_id')->andReturn(null);
  251. $model->shouldReceive('getTable')->never();
  252. $model->shouldReceive('getConnectionName')->never();
  253. $this->assertFalse($relation->is($model));
  254. }
  255. public function testIsNotModelWithAnotherRelatedKey()
  256. {
  257. $relation = $this->getOneRelation();
  258. $relation->getRelated()->shouldReceive('getTable')->never();
  259. $relation->getRelated()->shouldReceive('getConnectionName')->never();
  260. $model = m::mock(Model::class);
  261. $model->shouldReceive('getAttribute')->once()->with('morph_id')->andReturn(2);
  262. $model->shouldReceive('getTable')->never();
  263. $model->shouldReceive('getConnectionName')->never();
  264. $this->assertFalse($relation->is($model));
  265. }
  266. public function testIsNotModelWithAnotherTable()
  267. {
  268. $relation = $this->getOneRelation();
  269. $relation->getRelated()->shouldReceive('getTable')->once()->andReturn('table');
  270. $relation->getRelated()->shouldReceive('getConnectionName')->never();
  271. $model = m::mock(Model::class);
  272. $model->shouldReceive('getAttribute')->once()->with('morph_id')->andReturn(1);
  273. $model->shouldReceive('getTable')->once()->andReturn('table.two');
  274. $model->shouldReceive('getConnectionName')->never();
  275. $this->assertFalse($relation->is($model));
  276. }
  277. public function testIsNotModelWithAnotherConnection()
  278. {
  279. $relation = $this->getOneRelation();
  280. $relation->getRelated()->shouldReceive('getTable')->once()->andReturn('table');
  281. $relation->getRelated()->shouldReceive('getConnectionName')->once()->andReturn('connection');
  282. $model = m::mock(Model::class);
  283. $model->shouldReceive('getAttribute')->once()->with('morph_id')->andReturn(1);
  284. $model->shouldReceive('getTable')->once()->andReturn('table');
  285. $model->shouldReceive('getConnectionName')->once()->andReturn('connection.two');
  286. $this->assertFalse($relation->is($model));
  287. }
  288. protected function getOneRelation()
  289. {
  290. $builder = m::mock(Builder::class);
  291. $builder->shouldReceive('whereNotNull')->once()->with('table.morph_id');
  292. $builder->shouldReceive('where')->once()->with('table.morph_id', '=', 1);
  293. $related = m::mock(Model::class);
  294. $builder->shouldReceive('getModel')->andReturn($related);
  295. $parent = m::mock(Model::class);
  296. $parent->shouldReceive('getAttribute')->with('id')->andReturn(1);
  297. $parent->shouldReceive('getMorphClass')->andReturn(get_class($parent));
  298. $builder->shouldReceive('where')->once()->with('table.morph_type', get_class($parent));
  299. return new MorphOne($builder, $parent, 'table.morph_type', 'table.morph_id', 'id');
  300. }
  301. protected function getManyRelation()
  302. {
  303. $builder = m::mock(Builder::class);
  304. $builder->shouldReceive('whereNotNull')->once()->with('table.morph_id');
  305. $builder->shouldReceive('where')->once()->with('table.morph_id', '=', 1);
  306. $related = m::mock(Model::class);
  307. $builder->shouldReceive('getModel')->andReturn($related);
  308. $parent = m::mock(Model::class);
  309. $parent->shouldReceive('getAttribute')->with('id')->andReturn(1);
  310. $parent->shouldReceive('getMorphClass')->andReturn(get_class($parent));
  311. $builder->shouldReceive('where')->once()->with('table.morph_type', get_class($parent));
  312. return new MorphMany($builder, $parent, 'table.morph_type', 'table.morph_id', 'id');
  313. }
  314. protected function getNamespacedRelation($alias)
  315. {
  316. require_once __DIR__.'/stubs/EloquentModelNamespacedStub.php';
  317. Relation::morphMap([
  318. $alias => EloquentModelNamespacedStub::class,
  319. ]);
  320. $builder = m::mock(Builder::class);
  321. $builder->shouldReceive('whereNotNull')->once()->with('table.morph_id');
  322. $builder->shouldReceive('where')->once()->with('table.morph_id', '=', 1);
  323. $related = m::mock(Model::class);
  324. $builder->shouldReceive('getModel')->andReturn($related);
  325. $parent = m::mock(EloquentModelNamespacedStub::class);
  326. $parent->shouldReceive('getAttribute')->with('id')->andReturn(1);
  327. $parent->shouldReceive('getMorphClass')->andReturn($alias);
  328. $builder->shouldReceive('where')->once()->with('table.morph_type', $alias);
  329. return new MorphOne($builder, $parent, 'table.morph_type', 'table.morph_id', 'id');
  330. }
  331. }
  332. class EloquentMorphResetModelStub extends Model
  333. {
  334. //
  335. }