EloquentModelConnectionsTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. namespace Illuminate\Tests\Integration\Database\Sqlite;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Illuminate\Database\Schema\Blueprint;
  5. use Illuminate\Support\Facades\Schema;
  6. use Illuminate\Support\Str;
  7. use Orchestra\Testbench\TestCase;
  8. class EloquentModelConnectionsTest extends TestCase
  9. {
  10. protected function getEnvironmentSetUp($app)
  11. {
  12. if (getenv('DB_CONNECTION') !== 'testing') {
  13. $this->markTestSkipped('Test requires a Sqlite connection.');
  14. }
  15. $app['config']->set('database.default', 'conn1');
  16. $app['config']->set('database.connections.conn1', [
  17. 'driver' => 'sqlite',
  18. 'database' => ':memory:',
  19. 'prefix' => '',
  20. ]);
  21. $app['config']->set('database.connections.conn2', [
  22. 'driver' => 'sqlite',
  23. 'database' => ':memory:',
  24. 'prefix' => '',
  25. ]);
  26. }
  27. protected function defineDatabaseMigrations()
  28. {
  29. Schema::create('parent', function (Blueprint $table) {
  30. $table->increments('id');
  31. $table->string('name');
  32. });
  33. Schema::create('child', function (Blueprint $table) {
  34. $table->increments('id');
  35. $table->string('name');
  36. $table->integer('parent_id');
  37. });
  38. Schema::connection('conn2')->create('parent', function (Blueprint $table) {
  39. $table->increments('id');
  40. $table->string('name');
  41. });
  42. Schema::connection('conn2')->create('child', function (Blueprint $table) {
  43. $table->increments('id');
  44. $table->string('name');
  45. $table->integer('parent_id');
  46. });
  47. }
  48. public function testChildObeysParentConnection()
  49. {
  50. $parent1 = ParentModel::create(['name' => Str::random()]);
  51. $parent1->children()->create(['name' => 'childOnConn1']);
  52. $parents1 = ParentModel::with('children')->get();
  53. $this->assertSame('childOnConn1', ChildModel::on('conn1')->first()->name);
  54. $this->assertSame('childOnConn1', $parent1->children()->first()->name);
  55. $this->assertSame('childOnConn1', $parents1[0]->children[0]->name);
  56. $parent2 = ParentModel::on('conn2')->create(['name' => Str::random()]);
  57. $parent2->children()->create(['name' => 'childOnConn2']);
  58. $parents2 = ParentModel::on('conn2')->with('children')->get();
  59. $this->assertSame('childOnConn2', ChildModel::on('conn2')->first()->name);
  60. $this->assertSame('childOnConn2', $parent2->children()->first()->name);
  61. $this->assertSame('childOnConn2', $parents2[0]->children[0]->name);
  62. }
  63. public function testChildUsesItsOwnConnectionIfSet()
  64. {
  65. $parent1 = ParentModel::create(['name' => Str::random()]);
  66. $parent1->childrenDefaultConn2()->create(['name' => 'childAlwaysOnConn2']);
  67. $parents1 = ParentModel::with('childrenDefaultConn2')->get();
  68. $this->assertSame('childAlwaysOnConn2', ChildModelDefaultConn2::first()->name);
  69. $this->assertSame('childAlwaysOnConn2', $parent1->childrenDefaultConn2()->first()->name);
  70. $this->assertSame('childAlwaysOnConn2', $parents1[0]->childrenDefaultConn2[0]->name);
  71. $this->assertSame('childAlwaysOnConn2', $parents1[0]->childrenDefaultConn2[0]->name);
  72. }
  73. public function testChildUsesItsOwnConnectionIfSetEvenIfParentExplicitConnection()
  74. {
  75. $parent1 = ParentModel::on('conn1')->create(['name' => Str::random()]);
  76. $parent1->childrenDefaultConn2()->create(['name' => 'childAlwaysOnConn2']);
  77. $parents1 = ParentModel::on('conn1')->with('childrenDefaultConn2')->get();
  78. $this->assertSame('childAlwaysOnConn2', ChildModelDefaultConn2::first()->name);
  79. $this->assertSame('childAlwaysOnConn2', $parent1->childrenDefaultConn2()->first()->name);
  80. $this->assertSame('childAlwaysOnConn2', $parents1[0]->childrenDefaultConn2[0]->name);
  81. }
  82. }
  83. class ParentModel extends Model
  84. {
  85. public $table = 'parent';
  86. public $timestamps = false;
  87. protected $guarded = [];
  88. public function children()
  89. {
  90. return $this->hasMany(ChildModel::class, 'parent_id');
  91. }
  92. public function childrenDefaultConn2()
  93. {
  94. return $this->hasMany(ChildModelDefaultConn2::class, 'parent_id');
  95. }
  96. }
  97. class ChildModel extends Model
  98. {
  99. public $table = 'child';
  100. public $timestamps = false;
  101. protected $guarded = [];
  102. public function parent()
  103. {
  104. return $this->belongsTo(ParentModel::class, 'parent_id');
  105. }
  106. }
  107. class ChildModelDefaultConn2 extends Model
  108. {
  109. public $connection = 'conn2';
  110. public $table = 'child';
  111. public $timestamps = false;
  112. protected $guarded = [];
  113. public function parent()
  114. {
  115. return $this->belongsTo(ParentModel::class, 'parent_id');
  116. }
  117. }