DatabaseEloquentHasManyTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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\HasMany;
  7. use Mockery as m;
  8. use PHPUnit\Framework\TestCase;
  9. use stdClass;
  10. class DatabaseEloquentHasManyTest extends TestCase
  11. {
  12. protected function tearDown(): void
  13. {
  14. m::close();
  15. }
  16. public function testMakeMethodDoesNotSaveNewModel()
  17. {
  18. $relation = $this->getRelation();
  19. $instance = $this->expectNewModel($relation, ['name' => 'taylor']);
  20. $instance->expects($this->never())->method('save');
  21. $this->assertEquals($instance, $relation->make(['name' => 'taylor']));
  22. }
  23. public function testMakeManyCreatesARelatedModelForEachRecord()
  24. {
  25. $records = [
  26. 'taylor' => ['name' => 'taylor'],
  27. 'colin' => ['name' => 'colin'],
  28. ];
  29. $relation = $this->getRelation();
  30. $relation->getRelated()->shouldReceive('newCollection')->once()->andReturn(new Collection);
  31. $taylor = $this->expectNewModel($relation, ['name' => 'taylor']);
  32. $taylor->expects($this->never())->method('save');
  33. $colin = $this->expectNewModel($relation, ['name' => 'colin']);
  34. $colin->expects($this->never())->method('save');
  35. $instances = $relation->makeMany($records);
  36. $this->assertInstanceOf(Collection::class, $instances);
  37. $this->assertEquals($taylor, $instances[0]);
  38. $this->assertEquals($colin, $instances[1]);
  39. }
  40. public function testCreateMethodProperlyCreatesNewModel()
  41. {
  42. $relation = $this->getRelation();
  43. $created = $this->expectCreatedModel($relation, ['name' => 'taylor']);
  44. $this->assertEquals($created, $relation->create(['name' => 'taylor']));
  45. }
  46. public function testForceCreateMethodProperlyCreatesNewModel()
  47. {
  48. $relation = $this->getRelation();
  49. $created = $this->expectForceCreatedModel($relation, ['name' => 'taylor']);
  50. $this->assertEquals($created, $relation->forceCreate(['name' => 'taylor']));
  51. $this->assertEquals(1, $created->getAttribute('foreign_key'));
  52. }
  53. public function testFindOrNewMethodFindsModel()
  54. {
  55. $relation = $this->getRelation();
  56. $relation->getQuery()->shouldReceive('find')->once()->with('foo', ['*'])->andReturn($model = m::mock(stdClass::class));
  57. $model->shouldReceive('setAttribute')->never();
  58. $this->assertInstanceOf(stdClass::class, $relation->findOrNew('foo'));
  59. }
  60. public function testFindOrNewMethodReturnsNewModelWithForeignKeySet()
  61. {
  62. $relation = $this->getRelation();
  63. $relation->getQuery()->shouldReceive('find')->once()->with('foo', ['*'])->andReturn(null);
  64. $relation->getRelated()->shouldReceive('newInstance')->once()->with()->andReturn($model = m::mock(Model::class));
  65. $model->shouldReceive('setAttribute')->once()->with('foreign_key', 1);
  66. $this->assertInstanceOf(Model::class, $relation->findOrNew('foo'));
  67. }
  68. public function testFirstOrNewMethodFindsFirstModel()
  69. {
  70. $relation = $this->getRelation();
  71. $relation->getQuery()->shouldReceive('where')->once()->with(['foo'])->andReturn($relation->getQuery());
  72. $relation->getQuery()->shouldReceive('first')->once()->with()->andReturn($model = m::mock(stdClass::class));
  73. $model->shouldReceive('setAttribute')->never();
  74. $this->assertInstanceOf(stdClass::class, $relation->firstOrNew(['foo']));
  75. }
  76. public function testFirstOrNewMethodWithValuesFindsFirstModel()
  77. {
  78. $relation = $this->getRelation();
  79. $relation->getQuery()->shouldReceive('where')->once()->with(['foo' => 'bar'])->andReturn($relation->getQuery());
  80. $relation->getQuery()->shouldReceive('first')->once()->with()->andReturn($model = m::mock(stdClass::class));
  81. $relation->getRelated()->shouldReceive('newInstance')->never();
  82. $model->shouldReceive('setAttribute')->never();
  83. $this->assertInstanceOf(stdClass::class, $relation->firstOrNew(['foo' => 'bar'], ['baz' => 'qux']));
  84. }
  85. public function testFirstOrNewMethodReturnsNewModelWithForeignKeySet()
  86. {
  87. $relation = $this->getRelation();
  88. $relation->getQuery()->shouldReceive('where')->once()->with(['foo'])->andReturn($relation->getQuery());
  89. $relation->getQuery()->shouldReceive('first')->once()->with()->andReturn(null);
  90. $model = $this->expectNewModel($relation, ['foo']);
  91. $this->assertEquals($model, $relation->firstOrNew(['foo']));
  92. }
  93. public function testFirstOrNewMethodWithValuesCreatesNewModelWithForeignKeySet()
  94. {
  95. $relation = $this->getRelation();
  96. $relation->getQuery()->shouldReceive('where')->once()->with(['foo' => 'bar'])->andReturn($relation->getQuery());
  97. $relation->getQuery()->shouldReceive('first')->once()->with()->andReturn(null);
  98. $model = $this->expectNewModel($relation, ['foo' => 'bar', 'baz' => 'qux']);
  99. $this->assertEquals($model, $relation->firstOrNew(['foo' => 'bar'], ['baz' => 'qux']));
  100. }
  101. public function testFirstOrCreateMethodFindsFirstModel()
  102. {
  103. $relation = $this->getRelation();
  104. $relation->getQuery()->shouldReceive('where')->once()->with(['foo'])->andReturn($relation->getQuery());
  105. $relation->getQuery()->shouldReceive('first')->once()->with()->andReturn($model = m::mock(stdClass::class));
  106. $relation->getRelated()->shouldReceive('newInstance')->never();
  107. $model->shouldReceive('setAttribute')->never();
  108. $model->shouldReceive('save')->never();
  109. $this->assertInstanceOf(stdClass::class, $relation->firstOrCreate(['foo']));
  110. }
  111. public function testFirstOrCreateMethodWithValuesFindsFirstModel()
  112. {
  113. $relation = $this->getRelation();
  114. $relation->getQuery()->shouldReceive('where')->once()->with(['foo' => 'bar'])->andReturn($relation->getQuery());
  115. $relation->getQuery()->shouldReceive('first')->once()->with()->andReturn($model = m::mock(stdClass::class));
  116. $relation->getRelated()->shouldReceive('newInstance')->never();
  117. $model->shouldReceive('setAttribute')->never();
  118. $model->shouldReceive('save')->never();
  119. $this->assertInstanceOf(stdClass::class, $relation->firstOrCreate(['foo' => 'bar'], ['baz' => 'qux']));
  120. }
  121. public function testFirstOrCreateMethodCreatesNewModelWithForeignKeySet()
  122. {
  123. $relation = $this->getRelation();
  124. $relation->getQuery()->shouldReceive('where')->once()->with(['foo'])->andReturn($relation->getQuery());
  125. $relation->getQuery()->shouldReceive('first')->once()->with()->andReturn(null);
  126. $model = $this->expectCreatedModel($relation, ['foo']);
  127. $this->assertEquals($model, $relation->firstOrCreate(['foo']));
  128. }
  129. public function testFirstOrCreateMethodWithValuesCreatesNewModelWithForeignKeySet()
  130. {
  131. $relation = $this->getRelation();
  132. $relation->getQuery()->shouldReceive('where')->once()->with(['foo' => 'bar'])->andReturn($relation->getQuery());
  133. $relation->getQuery()->shouldReceive('first')->once()->with()->andReturn(null);
  134. $model = $this->expectCreatedModel($relation, ['foo' => 'bar', 'baz' => 'qux']);
  135. $this->assertEquals($model, $relation->firstOrCreate(['foo' => 'bar'], ['baz' => 'qux']));
  136. }
  137. public function testUpdateOrCreateMethodFindsFirstModelAndUpdates()
  138. {
  139. $relation = $this->getRelation();
  140. $relation->getQuery()->shouldReceive('where')->once()->with(['foo'])->andReturn($relation->getQuery());
  141. $relation->getQuery()->shouldReceive('first')->once()->with()->andReturn($model = m::mock(stdClass::class));
  142. $relation->getRelated()->shouldReceive('newInstance')->never();
  143. $model->shouldReceive('fill')->once()->with(['bar']);
  144. $model->shouldReceive('save')->once();
  145. $this->assertInstanceOf(stdClass::class, $relation->updateOrCreate(['foo'], ['bar']));
  146. }
  147. public function testUpdateOrCreateMethodCreatesNewModelWithForeignKeySet()
  148. {
  149. $relation = $this->getRelation();
  150. $relation->getQuery()->shouldReceive('where')->once()->with(['foo'])->andReturn($relation->getQuery());
  151. $relation->getQuery()->shouldReceive('first')->once()->with()->andReturn(null);
  152. $relation->getRelated()->shouldReceive('newInstance')->once()->with(['foo'])->andReturn($model = m::mock(Model::class));
  153. $model->shouldReceive('save')->once()->andReturn(true);
  154. $model->shouldReceive('fill')->once()->with(['bar']);
  155. $model->shouldReceive('setAttribute')->once()->with('foreign_key', 1);
  156. $this->assertInstanceOf(Model::class, $relation->updateOrCreate(['foo'], ['bar']));
  157. }
  158. public function testRelationIsProperlyInitialized()
  159. {
  160. $relation = $this->getRelation();
  161. $model = m::mock(Model::class);
  162. $relation->getRelated()->shouldReceive('newCollection')->andReturnUsing(function ($array = []) {
  163. return new Collection($array);
  164. });
  165. $model->shouldReceive('setRelation')->once()->with('foo', m::type(Collection::class));
  166. $models = $relation->initRelation([$model], 'foo');
  167. $this->assertEquals([$model], $models);
  168. }
  169. public function testEagerConstraintsAreProperlyAdded()
  170. {
  171. $relation = $this->getRelation();
  172. $relation->getParent()->shouldReceive('getKeyName')->once()->andReturn('id');
  173. $relation->getParent()->shouldReceive('getKeyType')->once()->andReturn('int');
  174. $relation->getQuery()->shouldReceive('whereIntegerInRaw')->once()->with('table.foreign_key', [1, 2]);
  175. $model1 = new EloquentHasManyModelStub;
  176. $model1->id = 1;
  177. $model2 = new EloquentHasManyModelStub;
  178. $model2->id = 2;
  179. $relation->addEagerConstraints([$model1, $model2]);
  180. }
  181. public function testEagerConstraintsAreProperlyAddedWithStringKey()
  182. {
  183. $relation = $this->getRelation();
  184. $relation->getParent()->shouldReceive('getKeyName')->once()->andReturn('id');
  185. $relation->getParent()->shouldReceive('getKeyType')->once()->andReturn('string');
  186. $relation->getQuery()->shouldReceive('whereIn')->once()->with('table.foreign_key', [1, 2]);
  187. $model1 = new EloquentHasManyModelStub;
  188. $model1->id = 1;
  189. $model2 = new EloquentHasManyModelStub;
  190. $model2->id = 2;
  191. $relation->addEagerConstraints([$model1, $model2]);
  192. }
  193. public function testModelsAreProperlyMatchedToParents()
  194. {
  195. $relation = $this->getRelation();
  196. $result1 = new EloquentHasManyModelStub;
  197. $result1->foreign_key = 1;
  198. $result2 = new EloquentHasManyModelStub;
  199. $result2->foreign_key = 2;
  200. $result3 = new EloquentHasManyModelStub;
  201. $result3->foreign_key = 2;
  202. $model1 = new EloquentHasManyModelStub;
  203. $model1->id = 1;
  204. $model2 = new EloquentHasManyModelStub;
  205. $model2->id = 2;
  206. $model3 = new EloquentHasManyModelStub;
  207. $model3->id = 3;
  208. $relation->getRelated()->shouldReceive('newCollection')->andReturnUsing(function ($array) {
  209. return new Collection($array);
  210. });
  211. $models = $relation->match([$model1, $model2, $model3], new Collection([$result1, $result2, $result3]), 'foo');
  212. $this->assertEquals(1, $models[0]->foo[0]->foreign_key);
  213. $this->assertCount(1, $models[0]->foo);
  214. $this->assertEquals(2, $models[1]->foo[0]->foreign_key);
  215. $this->assertEquals(2, $models[1]->foo[1]->foreign_key);
  216. $this->assertCount(2, $models[1]->foo);
  217. $this->assertNull($models[2]->foo);
  218. }
  219. public function testCreateManyCreatesARelatedModelForEachRecord()
  220. {
  221. $records = [
  222. 'taylor' => ['name' => 'taylor'],
  223. 'colin' => ['name' => 'colin'],
  224. ];
  225. $relation = $this->getRelation();
  226. $relation->getRelated()->shouldReceive('newCollection')->once()->andReturn(new Collection);
  227. $taylor = $this->expectCreatedModel($relation, ['name' => 'taylor']);
  228. $colin = $this->expectCreatedModel($relation, ['name' => 'colin']);
  229. $instances = $relation->createMany($records);
  230. $this->assertInstanceOf(Collection::class, $instances);
  231. $this->assertEquals($taylor, $instances[0]);
  232. $this->assertEquals($colin, $instances[1]);
  233. }
  234. protected function getRelation()
  235. {
  236. $builder = m::mock(Builder::class);
  237. $builder->shouldReceive('whereNotNull')->with('table.foreign_key');
  238. $builder->shouldReceive('where')->with('table.foreign_key', '=', 1);
  239. $related = m::mock(Model::class);
  240. $builder->shouldReceive('getModel')->andReturn($related);
  241. $parent = m::mock(Model::class);
  242. $parent->shouldReceive('getAttribute')->with('id')->andReturn(1);
  243. $parent->shouldReceive('getCreatedAtColumn')->andReturn('created_at');
  244. $parent->shouldReceive('getUpdatedAtColumn')->andReturn('updated_at');
  245. return new HasMany($builder, $parent, 'table.foreign_key', 'id');
  246. }
  247. protected function expectNewModel($relation, $attributes = null)
  248. {
  249. $model = $this->getMockBuilder(Model::class)->onlyMethods(['setAttribute', 'save'])->getMock();
  250. $relation->getRelated()->shouldReceive('newInstance')->with($attributes)->andReturn($model);
  251. $model->expects($this->once())->method('setAttribute')->with('foreign_key', 1);
  252. return $model;
  253. }
  254. protected function expectCreatedModel($relation, $attributes)
  255. {
  256. $model = $this->expectNewModel($relation, $attributes);
  257. $model->expects($this->once())->method('save');
  258. return $model;
  259. }
  260. protected function expectForceCreatedModel($relation, $attributes)
  261. {
  262. $attributes[$relation->getForeignKeyName()] = $relation->getParentKey();
  263. $model = m::mock(Model::class);
  264. $model->shouldReceive('getAttribute')->with($relation->getForeignKeyName())->andReturn($relation->getParentKey());
  265. $relation->getRelated()->shouldReceive('forceCreate')->once()->with($attributes)->andReturn($model);
  266. return $model;
  267. }
  268. }
  269. class EloquentHasManyModelStub extends Model
  270. {
  271. public $foreign_key = 'foreign.value';
  272. }