DatabaseEloquentIntegrationWithTablePrefixTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace Illuminate\Tests\Database;
  3. use Illuminate\Database\Capsule\Manager as DB;
  4. use Illuminate\Database\Eloquent\Collection;
  5. use Illuminate\Database\Eloquent\Model as Eloquent;
  6. use Illuminate\Database\Eloquent\Relations\Relation;
  7. use PHPUnit\Framework\TestCase;
  8. class DatabaseEloquentIntegrationWithTablePrefixTest extends TestCase
  9. {
  10. /**
  11. * Bootstrap Eloquent.
  12. *
  13. * @return void
  14. */
  15. protected function setUp(): void
  16. {
  17. $db = new DB;
  18. $db->addConnection([
  19. 'driver' => 'sqlite',
  20. 'database' => ':memory:',
  21. ]);
  22. $db->bootEloquent();
  23. $db->setAsGlobal();
  24. Eloquent::getConnectionResolver()->connection()->setTablePrefix('prefix_');
  25. $this->createSchema();
  26. }
  27. protected function createSchema()
  28. {
  29. $this->schema('default')->create('users', function ($table) {
  30. $table->increments('id');
  31. $table->string('email');
  32. $table->timestamps();
  33. });
  34. $this->schema('default')->create('friends', function ($table) {
  35. $table->integer('user_id');
  36. $table->integer('friend_id');
  37. });
  38. $this->schema('default')->create('posts', function ($table) {
  39. $table->increments('id');
  40. $table->integer('user_id');
  41. $table->integer('parent_id')->nullable();
  42. $table->string('name');
  43. $table->timestamps();
  44. });
  45. $this->schema('default')->create('photos', function ($table) {
  46. $table->increments('id');
  47. $table->morphs('imageable');
  48. $table->string('name');
  49. $table->timestamps();
  50. });
  51. }
  52. /**
  53. * Tear down the database schema.
  54. *
  55. * @return void
  56. */
  57. protected function tearDown(): void
  58. {
  59. foreach (['default'] as $connection) {
  60. $this->schema($connection)->drop('users');
  61. $this->schema($connection)->drop('friends');
  62. $this->schema($connection)->drop('posts');
  63. $this->schema($connection)->drop('photos');
  64. }
  65. Relation::morphMap([], false);
  66. }
  67. public function testBasicModelHydration()
  68. {
  69. EloquentTestUser::create(['email' => 'taylorotwell@gmail.com']);
  70. EloquentTestUser::create(['email' => 'abigailotwell@gmail.com']);
  71. $models = EloquentTestUser::fromQuery('SELECT * FROM prefix_users WHERE email = ?', ['abigailotwell@gmail.com']);
  72. $this->assertInstanceOf(Collection::class, $models);
  73. $this->assertInstanceOf(EloquentTestUser::class, $models[0]);
  74. $this->assertSame('abigailotwell@gmail.com', $models[0]->email);
  75. $this->assertCount(1, $models);
  76. }
  77. /**
  78. * Helpers...
  79. */
  80. /**
  81. * Get a database connection instance.
  82. *
  83. * @return \Illuminate\Database\Connection
  84. */
  85. protected function connection($connection = 'default')
  86. {
  87. return Eloquent::getConnectionResolver()->connection($connection);
  88. }
  89. /**
  90. * Get a schema builder instance.
  91. *
  92. * @return \Illuminate\Database\Schema\Builder
  93. */
  94. protected function schema($connection = 'default')
  95. {
  96. return $this->connection($connection)->getSchemaBuilder();
  97. }
  98. }