DatabaseEloquentBelongsToTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. <?php
  2. namespace Illuminate\Tests\Database;
  3. use Illuminate\Database\Eloquent\Builder;
  4. use Illuminate\Database\Eloquent\Collection;
  5. use Illuminate\Database\Eloquent\Model;
  6. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  7. use Mockery as m;
  8. use PHPUnit\Framework\TestCase;
  9. use stdClass;
  10. class DatabaseEloquentBelongsToTest extends TestCase
  11. {
  12. protected $builder;
  13. protected $related;
  14. protected function tearDown(): void
  15. {
  16. m::close();
  17. }
  18. public function testBelongsToWithDefault()
  19. {
  20. $relation = $this->getRelation()->withDefault();
  21. $this->builder->shouldReceive('first')->once()->andReturnNull();
  22. $newModel = new EloquentBelongsToModelStub;
  23. $this->related->shouldReceive('newInstance')->once()->andReturn($newModel);
  24. $this->assertSame($newModel, $relation->getResults());
  25. }
  26. public function testBelongsToWithDynamicDefault()
  27. {
  28. $relation = $this->getRelation()->withDefault(function ($newModel) {
  29. $newModel->username = 'taylor';
  30. });
  31. $this->builder->shouldReceive('first')->once()->andReturnNull();
  32. $newModel = new EloquentBelongsToModelStub;
  33. $this->related->shouldReceive('newInstance')->once()->andReturn($newModel);
  34. $this->assertSame($newModel, $relation->getResults());
  35. $this->assertSame('taylor', $newModel->username);
  36. }
  37. public function testBelongsToWithArrayDefault()
  38. {
  39. $relation = $this->getRelation()->withDefault(['username' => 'taylor']);
  40. $this->builder->shouldReceive('first')->once()->andReturnNull();
  41. $newModel = new EloquentBelongsToModelStub;
  42. $this->related->shouldReceive('newInstance')->once()->andReturn($newModel);
  43. $this->assertSame($newModel, $relation->getResults());
  44. $this->assertSame('taylor', $newModel->username);
  45. }
  46. public function testEagerConstraintsAreProperlyAdded()
  47. {
  48. $relation = $this->getRelation();
  49. $relation->getRelated()->shouldReceive('getKeyName')->andReturn('id');
  50. $relation->getRelated()->shouldReceive('getKeyType')->andReturn('int');
  51. $relation->getQuery()->shouldReceive('whereIntegerInRaw')->once()->with('relation.id', ['foreign.value', 'foreign.value.two']);
  52. $models = [new EloquentBelongsToModelStub, new EloquentBelongsToModelStub, new AnotherEloquentBelongsToModelStub];
  53. $relation->addEagerConstraints($models);
  54. }
  55. public function testIdsInEagerConstraintsCanBeZero()
  56. {
  57. $keys = ['foreign.value', 0];
  58. if (version_compare(PHP_VERSION, '8.0.0-dev', '>=')) {
  59. sort($keys);
  60. }
  61. $relation = $this->getRelation();
  62. $relation->getRelated()->shouldReceive('getKeyName')->andReturn('id');
  63. $relation->getRelated()->shouldReceive('getKeyType')->andReturn('int');
  64. $relation->getQuery()->shouldReceive('whereIntegerInRaw')->once()->with('relation.id', $keys);
  65. $models = [new EloquentBelongsToModelStub, new EloquentBelongsToModelStubWithZeroId];
  66. $relation->addEagerConstraints($models);
  67. }
  68. public function testRelationIsProperlyInitialized()
  69. {
  70. $relation = $this->getRelation();
  71. $model = m::mock(Model::class);
  72. $model->shouldReceive('setRelation')->once()->with('foo', null);
  73. $models = $relation->initRelation([$model], 'foo');
  74. $this->assertEquals([$model], $models);
  75. }
  76. public function testModelsAreProperlyMatchedToParents()
  77. {
  78. $relation = $this->getRelation();
  79. $result1 = m::mock(stdClass::class);
  80. $result1->shouldReceive('getAttribute')->with('id')->andReturn(1);
  81. $result2 = m::mock(stdClass::class);
  82. $result2->shouldReceive('getAttribute')->with('id')->andReturn(2);
  83. $result3 = m::mock(stdClass::class);
  84. $result3->shouldReceive('getAttribute')->with('id')->andReturn(new class
  85. {
  86. public function __toString()
  87. {
  88. return '3';
  89. }
  90. });
  91. $model1 = new EloquentBelongsToModelStub;
  92. $model1->foreign_key = 1;
  93. $model2 = new EloquentBelongsToModelStub;
  94. $model2->foreign_key = 2;
  95. $model3 = new EloquentBelongsToModelStub;
  96. $model3->foreign_key = new class
  97. {
  98. public function __toString()
  99. {
  100. return '3';
  101. }
  102. };
  103. $models = $relation->match([$model1, $model2, $model3], new Collection([$result1, $result2, $result3]), 'foo');
  104. $this->assertEquals(1, $models[0]->foo->getAttribute('id'));
  105. $this->assertEquals(2, $models[1]->foo->getAttribute('id'));
  106. $this->assertEquals('3', $models[2]->foo->getAttribute('id'));
  107. }
  108. public function testAssociateMethodSetsForeignKeyOnModel()
  109. {
  110. $parent = m::mock(Model::class);
  111. $parent->shouldReceive('getAttribute')->once()->with('foreign_key')->andReturn('foreign.value');
  112. $relation = $this->getRelation($parent);
  113. $associate = m::mock(Model::class);
  114. $associate->shouldReceive('getAttribute')->once()->with('id')->andReturn(1);
  115. $parent->shouldReceive('setAttribute')->once()->with('foreign_key', 1);
  116. $parent->shouldReceive('setRelation')->once()->with('relation', $associate);
  117. $relation->associate($associate);
  118. }
  119. public function testDissociateMethodUnsetsForeignKeyOnModel()
  120. {
  121. $parent = m::mock(Model::class);
  122. $parent->shouldReceive('getAttribute')->once()->with('foreign_key')->andReturn('foreign.value');
  123. $relation = $this->getRelation($parent);
  124. $parent->shouldReceive('setAttribute')->once()->with('foreign_key', null);
  125. // Always set relation when we received Model
  126. $parent->shouldReceive('setRelation')->once()->with('relation', null);
  127. $relation->dissociate();
  128. }
  129. public function testAssociateMethodSetsForeignKeyOnModelById()
  130. {
  131. $parent = m::mock(Model::class);
  132. $parent->shouldReceive('getAttribute')->once()->with('foreign_key')->andReturn('foreign.value');
  133. $relation = $this->getRelation($parent);
  134. $parent->shouldReceive('setAttribute')->once()->with('foreign_key', 1);
  135. // Always unset relation when we received id, regardless of dirtiness
  136. $parent->shouldReceive('isDirty')->never();
  137. $parent->shouldReceive('unsetRelation')->once()->with($relation->getRelationName());
  138. $relation->associate(1);
  139. }
  140. public function testDefaultEagerConstraintsWhenIncrementing()
  141. {
  142. $relation = $this->getRelation();
  143. $relation->getRelated()->shouldReceive('getKeyName')->andReturn('id');
  144. $relation->getRelated()->shouldReceive('getKeyType')->andReturn('int');
  145. $relation->getQuery()->shouldReceive('whereIntegerInRaw')->once()->with('relation.id', m::mustBe([]));
  146. $models = [new MissingEloquentBelongsToModelStub, new MissingEloquentBelongsToModelStub];
  147. $relation->addEagerConstraints($models);
  148. }
  149. public function testDefaultEagerConstraintsWhenIncrementingAndNonIntKeyType()
  150. {
  151. $relation = $this->getRelation(null, 'string');
  152. $relation->getQuery()->shouldReceive('whereIn')->once()->with('relation.id', m::mustBe([]));
  153. $models = [new MissingEloquentBelongsToModelStub, new MissingEloquentBelongsToModelStub];
  154. $relation->addEagerConstraints($models);
  155. }
  156. public function testDefaultEagerConstraintsWhenNotIncrementing()
  157. {
  158. $relation = $this->getRelation();
  159. $relation->getRelated()->shouldReceive('getKeyName')->andReturn('id');
  160. $relation->getRelated()->shouldReceive('getKeyType')->andReturn('int');
  161. $relation->getQuery()->shouldReceive('whereIntegerInRaw')->once()->with('relation.id', m::mustBe([]));
  162. $models = [new MissingEloquentBelongsToModelStub, new MissingEloquentBelongsToModelStub];
  163. $relation->addEagerConstraints($models);
  164. }
  165. public function testIsNotNull()
  166. {
  167. $relation = $this->getRelation();
  168. $this->related->shouldReceive('getConnectionName')->never();
  169. $this->assertFalse($relation->is(null));
  170. }
  171. public function testIsModel()
  172. {
  173. $relation = $this->getRelation();
  174. $this->related->shouldReceive('getConnectionName')->once()->andReturn('relation');
  175. $model = m::mock(Model::class);
  176. $model->shouldReceive('getAttribute')->once()->with('id')->andReturn('foreign.value');
  177. $model->shouldReceive('getTable')->once()->andReturn('relation');
  178. $model->shouldReceive('getConnectionName')->once()->andReturn('relation');
  179. $this->assertTrue($relation->is($model));
  180. }
  181. public function testIsModelWithIntegerParentKey()
  182. {
  183. $parent = m::mock(Model::class);
  184. // when addConstraints is called we need to return the foreign value
  185. $parent->shouldReceive('getAttribute')->once()->with('foreign_key')->andReturn('foreign.value');
  186. // when getParentKey is called we want to return an integer
  187. $parent->shouldReceive('getAttribute')->once()->with('foreign_key')->andReturn(1);
  188. $relation = $this->getRelation($parent);
  189. $this->related->shouldReceive('getConnectionName')->once()->andReturn('relation');
  190. $model = m::mock(Model::class);
  191. $model->shouldReceive('getAttribute')->once()->with('id')->andReturn('1');
  192. $model->shouldReceive('getTable')->once()->andReturn('relation');
  193. $model->shouldReceive('getConnectionName')->once()->andReturn('relation');
  194. $this->assertTrue($relation->is($model));
  195. }
  196. public function testIsModelWithIntegerRelatedKey()
  197. {
  198. $parent = m::mock(Model::class);
  199. // when addConstraints is called we need to return the foreign value
  200. $parent->shouldReceive('getAttribute')->once()->with('foreign_key')->andReturn('foreign.value');
  201. // when getParentKey is called we want to return a string
  202. $parent->shouldReceive('getAttribute')->once()->with('foreign_key')->andReturn('1');
  203. $relation = $this->getRelation($parent);
  204. $this->related->shouldReceive('getConnectionName')->once()->andReturn('relation');
  205. $model = m::mock(Model::class);
  206. $model->shouldReceive('getAttribute')->once()->with('id')->andReturn(1);
  207. $model->shouldReceive('getTable')->once()->andReturn('relation');
  208. $model->shouldReceive('getConnectionName')->once()->andReturn('relation');
  209. $this->assertTrue($relation->is($model));
  210. }
  211. public function testIsModelWithIntegerKeys()
  212. {
  213. $parent = m::mock(Model::class);
  214. // when addConstraints is called we need to return the foreign value
  215. $parent->shouldReceive('getAttribute')->once()->with('foreign_key')->andReturn('foreign.value');
  216. // when getParentKey is called we want to return an integer
  217. $parent->shouldReceive('getAttribute')->once()->with('foreign_key')->andReturn(1);
  218. $relation = $this->getRelation($parent);
  219. $this->related->shouldReceive('getConnectionName')->once()->andReturn('relation');
  220. $model = m::mock(Model::class);
  221. $model->shouldReceive('getAttribute')->once()->with('id')->andReturn(1);
  222. $model->shouldReceive('getTable')->once()->andReturn('relation');
  223. $model->shouldReceive('getConnectionName')->once()->andReturn('relation');
  224. $this->assertTrue($relation->is($model));
  225. }
  226. public function testIsNotModelWithNullParentKey()
  227. {
  228. $parent = m::mock(Model::class);
  229. // when addConstraints is called we need to return the foreign value
  230. $parent->shouldReceive('getAttribute')->once()->with('foreign_key')->andReturn('foreign.value');
  231. // when getParentKey is called we want to return null
  232. $parent->shouldReceive('getAttribute')->once()->with('foreign_key')->andReturn(null);
  233. $relation = $this->getRelation($parent);
  234. $this->related->shouldReceive('getConnectionName')->never();
  235. $model = m::mock(Model::class);
  236. $model->shouldReceive('getAttribute')->once()->with('id')->andReturn('foreign.value');
  237. $model->shouldReceive('getTable')->never();
  238. $model->shouldReceive('getConnectionName')->never();
  239. $this->assertFalse($relation->is($model));
  240. }
  241. public function testIsNotModelWithNullRelatedKey()
  242. {
  243. $relation = $this->getRelation();
  244. $this->related->shouldReceive('getConnectionName')->never();
  245. $model = m::mock(Model::class);
  246. $model->shouldReceive('getAttribute')->once()->with('id')->andReturn(null);
  247. $model->shouldReceive('getTable')->never();
  248. $model->shouldReceive('getConnectionName')->never();
  249. $this->assertFalse($relation->is($model));
  250. }
  251. public function testIsNotModelWithAnotherKey()
  252. {
  253. $relation = $this->getRelation();
  254. $this->related->shouldReceive('getConnectionName')->never();
  255. $model = m::mock(Model::class);
  256. $model->shouldReceive('getAttribute')->once()->with('id')->andReturn('foreign.value.two');
  257. $model->shouldReceive('getTable')->never();
  258. $model->shouldReceive('getConnectionName')->never();
  259. $this->assertFalse($relation->is($model));
  260. }
  261. public function testIsNotModelWithAnotherTable()
  262. {
  263. $relation = $this->getRelation();
  264. $this->related->shouldReceive('getConnectionName')->never();
  265. $model = m::mock(Model::class);
  266. $model->shouldReceive('getAttribute')->once()->with('id')->andReturn('foreign.value');
  267. $model->shouldReceive('getTable')->once()->andReturn('table.two');
  268. $model->shouldReceive('getConnectionName')->never();
  269. $this->assertFalse($relation->is($model));
  270. }
  271. public function testIsNotModelWithAnotherConnection()
  272. {
  273. $relation = $this->getRelation();
  274. $this->related->shouldReceive('getConnectionName')->once()->andReturn('relation');
  275. $model = m::mock(Model::class);
  276. $model->shouldReceive('getAttribute')->once()->with('id')->andReturn('foreign.value');
  277. $model->shouldReceive('getTable')->once()->andReturn('relation');
  278. $model->shouldReceive('getConnectionName')->once()->andReturn('relation.two');
  279. $this->assertFalse($relation->is($model));
  280. }
  281. protected function getRelation($parent = null, $keyType = 'int')
  282. {
  283. $this->builder = m::mock(Builder::class);
  284. $this->builder->shouldReceive('where')->with('relation.id', '=', 'foreign.value');
  285. $this->related = m::mock(Model::class);
  286. $this->related->shouldReceive('getKeyType')->andReturn($keyType);
  287. $this->related->shouldReceive('getKeyName')->andReturn('id');
  288. $this->related->shouldReceive('getTable')->andReturn('relation');
  289. $this->builder->shouldReceive('getModel')->andReturn($this->related);
  290. $parent = $parent ?: new EloquentBelongsToModelStub;
  291. return new BelongsTo($this->builder, $parent, 'foreign_key', 'id', 'relation');
  292. }
  293. }
  294. class EloquentBelongsToModelStub extends Model
  295. {
  296. public $foreign_key = 'foreign.value';
  297. }
  298. class AnotherEloquentBelongsToModelStub extends Model
  299. {
  300. public $foreign_key = 'foreign.value.two';
  301. }
  302. class EloquentBelongsToModelStubWithZeroId extends Model
  303. {
  304. public $foreign_key = 0;
  305. }
  306. class MissingEloquentBelongsToModelStub extends Model
  307. {
  308. public $foreign_key;
  309. }