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