DatabaseEloquentIrregularPluralTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. namespace Illuminate\Tests\Database;
  3. use Illuminate\Database\Capsule\Manager as DB;
  4. use Illuminate\Database\Eloquent\Model;
  5. use Illuminate\Support\Carbon;
  6. use PHPUnit\Framework\TestCase;
  7. class DatabaseEloquentIrregularPluralTest extends TestCase
  8. {
  9. protected function setUp(): void
  10. {
  11. $db = new DB;
  12. $db->addConnection([
  13. 'driver' => 'sqlite',
  14. 'database' => ':memory:',
  15. ]);
  16. $db->bootEloquent();
  17. $db->setAsGlobal();
  18. $this->createSchema();
  19. }
  20. public function createSchema()
  21. {
  22. $this->schema()->create('irregular_plural_humans', function ($table) {
  23. $table->increments('id');
  24. $table->string('email')->unique();
  25. $table->timestamps();
  26. });
  27. $this->schema()->create('irregular_plural_tokens', function ($table) {
  28. $table->increments('id');
  29. $table->string('title');
  30. });
  31. $this->schema()->create('irregular_plural_human_irregular_plural_token', function ($table) {
  32. $table->integer('irregular_plural_human_id')->unsigned();
  33. $table->integer('irregular_plural_token_id')->unsigned();
  34. });
  35. $this->schema()->create('irregular_plural_mottoes', function ($table) {
  36. $table->increments('id');
  37. $table->string('name');
  38. });
  39. $this->schema()->create('cool_mottoes', function ($table) {
  40. $table->integer('irregular_plural_motto_id');
  41. $table->integer('cool_motto_id');
  42. $table->string('cool_motto_type');
  43. });
  44. }
  45. protected function tearDown(): void
  46. {
  47. $this->schema()->drop('irregular_plural_tokens');
  48. $this->schema()->drop('irregular_plural_humans');
  49. $this->schema()->drop('irregular_plural_human_irregular_plural_token');
  50. }
  51. protected function schema()
  52. {
  53. $connection = Model::getConnectionResolver()->connection();
  54. return $connection->getSchemaBuilder();
  55. }
  56. /** @test */
  57. public function itPluralizesTheTableName()
  58. {
  59. $model = new IrregularPluralHuman;
  60. $this->assertSame('irregular_plural_humans', $model->getTable());
  61. }
  62. /** @test */
  63. public function itTouchesTheParentWithAnIrregularPlural()
  64. {
  65. Carbon::setTestNow('2018-05-01 12:13:14');
  66. IrregularPluralHuman::create(['email' => 'taylorotwell@gmail.com']);
  67. IrregularPluralToken::insert([
  68. ['title' => 'The title'],
  69. ]);
  70. $human = IrregularPluralHuman::query()->first();
  71. $tokenIds = IrregularPluralToken::pluck('id');
  72. Carbon::setTestNow('2018-05-01 15:16:17');
  73. $human->irregularPluralTokens()->sync($tokenIds);
  74. $human->refresh();
  75. $this->assertSame('2018-05-01 12:13:14', (string) $human->created_at);
  76. $this->assertSame('2018-05-01 15:16:17', (string) $human->updated_at);
  77. }
  78. /** @test */
  79. public function itPluralizesMorphToManyRelationships()
  80. {
  81. $human = IrregularPluralHuman::create(['email' => 'bobby@example.com']);
  82. $human->mottoes()->create(['name' => 'Real eyes realize real lies']);
  83. $motto = IrregularPluralMotto::query()->first();
  84. $this->assertSame('Real eyes realize real lies', $motto->name);
  85. }
  86. }
  87. class IrregularPluralHuman extends Model
  88. {
  89. protected $guarded = [];
  90. public function irregularPluralTokens()
  91. {
  92. return $this->belongsToMany(
  93. IrregularPluralToken::class,
  94. 'irregular_plural_human_irregular_plural_token',
  95. 'irregular_plural_token_id',
  96. 'irregular_plural_human_id'
  97. );
  98. }
  99. public function mottoes()
  100. {
  101. return $this->morphToMany(IrregularPluralMotto::class, 'cool_motto');
  102. }
  103. }
  104. class IrregularPluralToken extends Model
  105. {
  106. protected $guarded = [];
  107. public $timestamps = false;
  108. protected $touches = [
  109. 'irregularPluralHumans',
  110. ];
  111. }
  112. class IrregularPluralMotto extends Model
  113. {
  114. protected $guarded = [];
  115. public $timestamps = false;
  116. public function irregularPluralHumans()
  117. {
  118. return $this->morphedByMany(IrregularPluralHuman::class, 'cool_motto');
  119. }
  120. }