Add Document and DocumentTranslation factories
This commit is contained in:
@@ -3,6 +3,7 @@
|
|||||||
namespace App\Models;
|
namespace App\Models;
|
||||||
|
|
||||||
use App\Helpers\SlugHelper;
|
use App\Helpers\SlugHelper;
|
||||||
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||||
@@ -17,7 +18,7 @@
|
|||||||
|
|
||||||
class Document extends Model
|
class Document extends Model
|
||||||
{
|
{
|
||||||
use SoftDeletes;
|
use HasFactory, SoftDeletes;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the route key for the model.
|
* Get the route key for the model.
|
||||||
|
|||||||
@@ -0,0 +1,62 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Database\Factories;
|
||||||
|
|
||||||
|
use App\Helpers\SlugHelper;
|
||||||
|
use App\Models\Document;
|
||||||
|
use App\Models\DocumentTranslation;
|
||||||
|
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @extends Factory<Document>
|
||||||
|
*/
|
||||||
|
class DocumentFactory extends Factory
|
||||||
|
{
|
||||||
|
protected $model = Document::class;
|
||||||
|
|
||||||
|
public function definition(): array
|
||||||
|
{
|
||||||
|
$title = fake()->unique()->sentence(3);
|
||||||
|
|
||||||
|
return [
|
||||||
|
'path' => $title . '.md',
|
||||||
|
'slug' => SlugHelper::generate($title),
|
||||||
|
'default_locale' => 'en',
|
||||||
|
'file_size' => 0,
|
||||||
|
'file_hash' => str_repeat('0', 64),
|
||||||
|
'file_modified_at' => now(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* After creating, attach a translation in the document's default_locale.
|
||||||
|
* Pass an explicit ['title' => ..., 'content' => ...] via withTranslation() to override.
|
||||||
|
*/
|
||||||
|
public function configure(): static
|
||||||
|
{
|
||||||
|
return $this->afterCreating(function (Document $document) {
|
||||||
|
if ($document->translations()->count() === 0) {
|
||||||
|
DocumentTranslation::factory()->create([
|
||||||
|
'document_id' => $document->id,
|
||||||
|
'locale' => $document->default_locale,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Override the default_locale and create the corresponding translation.
|
||||||
|
*/
|
||||||
|
public function defaultLocale(string $locale): static
|
||||||
|
{
|
||||||
|
return $this->state(['default_locale' => $locale]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Suppress automatic translation creation (caller will create manually).
|
||||||
|
*/
|
||||||
|
public function withoutTranslations(): static
|
||||||
|
{
|
||||||
|
return $this->afterCreating(fn () => null);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Database\Factories;
|
||||||
|
|
||||||
|
use App\Models\Document;
|
||||||
|
use App\Models\DocumentTranslation;
|
||||||
|
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @extends Factory<DocumentTranslation>
|
||||||
|
*/
|
||||||
|
class DocumentTranslationFactory extends Factory
|
||||||
|
{
|
||||||
|
protected $model = DocumentTranslation::class;
|
||||||
|
|
||||||
|
public function definition(): array
|
||||||
|
{
|
||||||
|
$title = fake()->sentence(3);
|
||||||
|
$content = fake()->paragraphs(3, true);
|
||||||
|
|
||||||
|
return [
|
||||||
|
'document_id' => Document::factory()->withoutTranslations(),
|
||||||
|
'locale' => 'en',
|
||||||
|
'title' => $title,
|
||||||
|
'content' => $content,
|
||||||
|
'rendered_html' => '<p>' . e($content) . '</p>',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user