DatabaseEloquentHasOneThroughIntegrationTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. <?php
  2. namespace Illuminate\Tests\Database;
  3. use Illuminate\Database\Capsule\Manager as DB;
  4. use Illuminate\Database\Eloquent\Model as Eloquent;
  5. use Illuminate\Database\Eloquent\ModelNotFoundException;
  6. use Illuminate\Database\Eloquent\SoftDeletes;
  7. use PHPUnit\Framework\TestCase;
  8. class DatabaseEloquentHasOneThroughIntegrationTest extends TestCase
  9. {
  10. protected function setUp(): void
  11. {
  12. $db = new DB;
  13. $db->addConnection([
  14. 'driver' => 'sqlite',
  15. 'database' => ':memory:',
  16. ]);
  17. $db->bootEloquent();
  18. $db->setAsGlobal();
  19. $this->createSchema();
  20. }
  21. /**
  22. * Setup the database schema.
  23. *
  24. * @return void
  25. */
  26. public function createSchema()
  27. {
  28. $this->schema()->create('users', function ($table) {
  29. $table->increments('id');
  30. $table->string('email')->unique();
  31. $table->unsignedInteger('position_id')->unique()->nullable();
  32. $table->string('position_short');
  33. $table->timestamps();
  34. $table->softDeletes();
  35. });
  36. $this->schema()->create('contracts', function ($table) {
  37. $table->increments('id');
  38. $table->integer('user_id')->unique();
  39. $table->string('title');
  40. $table->text('body');
  41. $table->string('email');
  42. $table->timestamps();
  43. });
  44. $this->schema()->create('positions', function ($table) {
  45. $table->increments('id');
  46. $table->string('name');
  47. $table->string('shortname');
  48. $table->timestamps();
  49. });
  50. }
  51. /**
  52. * Tear down the database schema.
  53. *
  54. * @return void
  55. */
  56. protected function tearDown(): void
  57. {
  58. $this->schema()->drop('users');
  59. $this->schema()->drop('contracts');
  60. $this->schema()->drop('positions');
  61. }
  62. public function testItLoadsAHasOneThroughRelationWithCustomKeys()
  63. {
  64. $this->seedData();
  65. $contract = HasOneThroughTestPosition::first()->contract;
  66. $this->assertSame('A title', $contract->title);
  67. }
  68. public function testItLoadsADefaultHasOneThroughRelation()
  69. {
  70. $this->migrateDefault();
  71. $this->seedDefaultData();
  72. $contract = HasOneThroughDefaultTestPosition::first()->contract;
  73. $this->assertSame('A title', $contract->title);
  74. $this->assertArrayNotHasKey('email', $contract->getAttributes());
  75. $this->resetDefault();
  76. }
  77. public function testItLoadsARelationWithCustomIntermediateAndLocalKey()
  78. {
  79. $this->seedData();
  80. $contract = HasOneThroughIntermediateTestPosition::first()->contract;
  81. $this->assertSame('A title', $contract->title);
  82. }
  83. public function testEagerLoadingARelationWithCustomIntermediateAndLocalKey()
  84. {
  85. $this->seedData();
  86. $contract = HasOneThroughIntermediateTestPosition::with('contract')->first()->contract;
  87. $this->assertSame('A title', $contract->title);
  88. }
  89. public function testWhereHasOnARelationWithCustomIntermediateAndLocalKey()
  90. {
  91. $this->seedData();
  92. $position = HasOneThroughIntermediateTestPosition::whereHas('contract', function ($query) {
  93. $query->where('title', 'A title');
  94. })->get();
  95. $this->assertCount(1, $position);
  96. }
  97. public function testFirstOrFailThrowsAnException()
  98. {
  99. $this->expectException(ModelNotFoundException::class);
  100. $this->expectExceptionMessage('No query results for model [Illuminate\Tests\Database\HasOneThroughTestContract].');
  101. HasOneThroughTestPosition::create(['id' => 1, 'name' => 'President', 'shortname' => 'ps'])
  102. ->user()->create(['id' => 1, 'email' => 'taylorotwell@gmail.com', 'position_short' => 'ps']);
  103. HasOneThroughTestPosition::first()->contract()->firstOrFail();
  104. }
  105. public function testFindOrFailThrowsAnException()
  106. {
  107. $this->expectException(ModelNotFoundException::class);
  108. HasOneThroughTestPosition::create(['id' => 1, 'name' => 'President', 'shortname' => 'ps'])
  109. ->user()->create(['id' => 1, 'email' => 'taylorotwell@gmail.com', 'position_short' => 'ps']);
  110. HasOneThroughTestPosition::first()->contract()->findOrFail(1);
  111. }
  112. public function testFirstRetrievesFirstRecord()
  113. {
  114. $this->seedData();
  115. $contract = HasOneThroughTestPosition::first()->contract()->first();
  116. $this->assertNotNull($contract);
  117. $this->assertSame('A title', $contract->title);
  118. }
  119. public function testAllColumnsAreRetrievedByDefault()
  120. {
  121. $this->seedData();
  122. $contract = HasOneThroughTestPosition::first()->contract()->first();
  123. $this->assertEquals([
  124. 'id',
  125. 'user_id',
  126. 'title',
  127. 'body',
  128. 'email',
  129. 'created_at',
  130. 'updated_at',
  131. 'laravel_through_key',
  132. ], array_keys($contract->getAttributes()));
  133. }
  134. public function testOnlyProperColumnsAreSelectedIfProvided()
  135. {
  136. $this->seedData();
  137. $contract = HasOneThroughTestPosition::first()->contract()->first(['title', 'body']);
  138. $this->assertEquals([
  139. 'title',
  140. 'body',
  141. 'laravel_through_key',
  142. ], array_keys($contract->getAttributes()));
  143. }
  144. public function testChunkReturnsCorrectModels()
  145. {
  146. $this->seedData();
  147. $this->seedDataExtended();
  148. $position = HasOneThroughTestPosition::find(1);
  149. $position->contract()->chunk(10, function ($contractsChunk) {
  150. $contract = $contractsChunk->first();
  151. $this->assertEquals([
  152. 'id',
  153. 'user_id',
  154. 'title',
  155. 'body',
  156. 'email',
  157. 'created_at',
  158. 'updated_at',
  159. 'laravel_through_key', ], array_keys($contract->getAttributes()));
  160. });
  161. }
  162. public function testCursorReturnsCorrectModels()
  163. {
  164. $this->seedData();
  165. $this->seedDataExtended();
  166. $position = HasOneThroughTestPosition::find(1);
  167. $contracts = $position->contract()->cursor();
  168. foreach ($contracts as $contract) {
  169. $this->assertEquals([
  170. 'id',
  171. 'user_id',
  172. 'title',
  173. 'body',
  174. 'email',
  175. 'created_at',
  176. 'updated_at',
  177. 'laravel_through_key', ], array_keys($contract->getAttributes()));
  178. }
  179. }
  180. public function testEachReturnsCorrectModels()
  181. {
  182. $this->seedData();
  183. $this->seedDataExtended();
  184. $position = HasOneThroughTestPosition::find(1);
  185. $position->contract()->each(function ($contract) {
  186. $this->assertEquals([
  187. 'id',
  188. 'user_id',
  189. 'title',
  190. 'body',
  191. 'email',
  192. 'created_at',
  193. 'updated_at',
  194. 'laravel_through_key', ], array_keys($contract->getAttributes()));
  195. });
  196. }
  197. public function testLazyReturnsCorrectModels()
  198. {
  199. $this->seedData();
  200. $this->seedDataExtended();
  201. $position = HasOneThroughTestPosition::find(1);
  202. $position->contract()->lazy()->each(function ($contract) {
  203. $this->assertEquals([
  204. 'id',
  205. 'user_id',
  206. 'title',
  207. 'body',
  208. 'email',
  209. 'created_at',
  210. 'updated_at',
  211. 'laravel_through_key', ], array_keys($contract->getAttributes()));
  212. });
  213. }
  214. public function testIntermediateSoftDeletesAreIgnored()
  215. {
  216. $this->seedData();
  217. HasOneThroughSoftDeletesTestUser::first()->delete();
  218. $contract = HasOneThroughSoftDeletesTestPosition::first()->contract;
  219. $this->assertSame('A title', $contract->title);
  220. }
  221. public function testEagerLoadingLoadsRelatedModelsCorrectly()
  222. {
  223. $this->seedData();
  224. $position = HasOneThroughSoftDeletesTestPosition::with('contract')->first();
  225. $this->assertSame('ps', $position->shortname);
  226. $this->assertSame('A title', $position->contract->title);
  227. }
  228. /**
  229. * Helpers...
  230. */
  231. protected function seedData()
  232. {
  233. HasOneThroughTestPosition::create(['id' => 1, 'name' => 'President', 'shortname' => 'ps'])
  234. ->user()->create(['id' => 1, 'email' => 'taylorotwell@gmail.com', 'position_short' => 'ps'])
  235. ->contract()->create(['title' => 'A title', 'body' => 'A body', 'email' => 'taylorotwell@gmail.com']);
  236. }
  237. protected function seedDataExtended()
  238. {
  239. $position = HasOneThroughTestPosition::create(['id' => 2, 'name' => 'Vice President', 'shortname' => 'vp']);
  240. $position->user()->create(['id' => 2, 'email' => 'example1@gmail.com', 'position_short' => 'vp'])
  241. ->contract()->create(
  242. ['title' => 'Example1 title1', 'body' => 'Example1 body1', 'email' => 'example1contract1@gmail.com']
  243. );
  244. }
  245. /**
  246. * Seed data for a default HasOneThrough setup.
  247. */
  248. protected function seedDefaultData()
  249. {
  250. HasOneThroughDefaultTestPosition::create(['id' => 1, 'name' => 'President'])
  251. ->user()->create(['id' => 1, 'email' => 'taylorotwell@gmail.com'])
  252. ->contract()->create(['title' => 'A title', 'body' => 'A body']);
  253. }
  254. /**
  255. * Drop the default tables.
  256. */
  257. protected function resetDefault()
  258. {
  259. $this->schema()->drop('users_default');
  260. $this->schema()->drop('contracts_default');
  261. $this->schema()->drop('positions_default');
  262. }
  263. /**
  264. * Migrate tables for classes with a Laravel "default" HasOneThrough setup.
  265. */
  266. protected function migrateDefault()
  267. {
  268. $this->schema()->create('users_default', function ($table) {
  269. $table->increments('id');
  270. $table->string('email')->unique();
  271. $table->unsignedInteger('has_one_through_default_test_position_id')->unique()->nullable();
  272. $table->timestamps();
  273. });
  274. $this->schema()->create('contracts_default', function ($table) {
  275. $table->increments('id');
  276. $table->integer('has_one_through_default_test_user_id')->unique();
  277. $table->string('title');
  278. $table->text('body');
  279. $table->timestamps();
  280. });
  281. $this->schema()->create('positions_default', function ($table) {
  282. $table->increments('id');
  283. $table->string('name');
  284. $table->timestamps();
  285. });
  286. }
  287. /**
  288. * Get a database connection instance.
  289. *
  290. * @return \Illuminate\Database\Connection
  291. */
  292. protected function connection()
  293. {
  294. return Eloquent::getConnectionResolver()->connection();
  295. }
  296. /**
  297. * Get a schema builder instance.
  298. *
  299. * @return \Illuminate\Database\Schema\Builder
  300. */
  301. protected function schema()
  302. {
  303. return $this->connection()->getSchemaBuilder();
  304. }
  305. }
  306. /**
  307. * Eloquent Models...
  308. */
  309. class HasOneThroughTestUser extends Eloquent
  310. {
  311. protected $table = 'users';
  312. protected $guarded = [];
  313. public function contract()
  314. {
  315. return $this->hasOne(HasOneThroughTestContract::class, 'user_id');
  316. }
  317. }
  318. /**
  319. * Eloquent Models...
  320. */
  321. class HasOneThroughTestContract extends Eloquent
  322. {
  323. protected $table = 'contracts';
  324. protected $guarded = [];
  325. public function owner()
  326. {
  327. return $this->belongsTo(HasOneThroughTestUser::class, 'user_id');
  328. }
  329. }
  330. class HasOneThroughTestPosition extends Eloquent
  331. {
  332. protected $table = 'positions';
  333. protected $guarded = [];
  334. public function contract()
  335. {
  336. return $this->hasOneThrough(HasOneThroughTestContract::class, HasOneThroughTestUser::class, 'position_id', 'user_id');
  337. }
  338. public function user()
  339. {
  340. return $this->hasOne(HasOneThroughTestUser::class, 'position_id');
  341. }
  342. }
  343. /**
  344. * Eloquent Models...
  345. */
  346. class HasOneThroughDefaultTestUser extends Eloquent
  347. {
  348. protected $table = 'users_default';
  349. protected $guarded = [];
  350. public function contract()
  351. {
  352. return $this->hasOne(HasOneThroughDefaultTestContract::class);
  353. }
  354. }
  355. /**
  356. * Eloquent Models...
  357. */
  358. class HasOneThroughDefaultTestContract extends Eloquent
  359. {
  360. protected $table = 'contracts_default';
  361. protected $guarded = [];
  362. public function owner()
  363. {
  364. return $this->belongsTo(HasOneThroughDefaultTestUser::class);
  365. }
  366. }
  367. class HasOneThroughDefaultTestPosition extends Eloquent
  368. {
  369. protected $table = 'positions_default';
  370. protected $guarded = [];
  371. public function contract()
  372. {
  373. return $this->hasOneThrough(HasOneThroughDefaultTestContract::class, HasOneThroughDefaultTestUser::class);
  374. }
  375. public function user()
  376. {
  377. return $this->hasOne(HasOneThroughDefaultTestUser::class);
  378. }
  379. }
  380. class HasOneThroughIntermediateTestPosition extends Eloquent
  381. {
  382. protected $table = 'positions';
  383. protected $guarded = [];
  384. public function contract()
  385. {
  386. return $this->hasOneThrough(HasOneThroughTestContract::class, HasOneThroughTestUser::class, 'position_short', 'email', 'shortname', 'email');
  387. }
  388. public function user()
  389. {
  390. return $this->hasOne(HasOneThroughTestUser::class, 'position_id');
  391. }
  392. }
  393. class HasOneThroughSoftDeletesTestUser extends Eloquent
  394. {
  395. use SoftDeletes;
  396. protected $table = 'users';
  397. protected $guarded = [];
  398. public function contract()
  399. {
  400. return $this->hasOne(HasOneThroughSoftDeletesTestContract::class, 'user_id');
  401. }
  402. }
  403. /**
  404. * Eloquent Models...
  405. */
  406. class HasOneThroughSoftDeletesTestContract extends Eloquent
  407. {
  408. protected $table = 'contracts';
  409. protected $guarded = [];
  410. public function owner()
  411. {
  412. return $this->belongsTo(HasOneThroughSoftDeletesTestUser::class, 'user_id');
  413. }
  414. }
  415. class HasOneThroughSoftDeletesTestPosition extends Eloquent
  416. {
  417. protected $table = 'positions';
  418. protected $guarded = [];
  419. public function contract()
  420. {
  421. return $this->hasOneThrough(HasOneThroughSoftDeletesTestContract::class, HasOneThroughTestUser::class, 'position_id', 'user_id');
  422. }
  423. public function user()
  424. {
  425. return $this->hasOne(HasOneThroughSoftDeletesTestUser::class, 'position_id');
  426. }
  427. }