feat: add Site model, migration, and factory
Implements Task 2: creates the Site model with fillable name, url, and description fields; the corresponding database migration; and a factory for generating test data. Includes TDD tests verifying model creation with and without optional description. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 5
parent
9b8dba5cf5
commit
889ac9b7f9
@@ -0,0 +1,17 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
|
||||||
|
class Site extends Model
|
||||||
|
{
|
||||||
|
use HasFactory;
|
||||||
|
|
||||||
|
protected $fillable = [
|
||||||
|
'name',
|
||||||
|
'url',
|
||||||
|
'description',
|
||||||
|
];
|
||||||
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Database\Factories;
|
||||||
|
|
||||||
|
use App\Models\Site;
|
||||||
|
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||||
|
|
||||||
|
class SiteFactory extends Factory
|
||||||
|
{
|
||||||
|
protected $model = Site::class;
|
||||||
|
|
||||||
|
public function definition(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'name' => fake()->company(),
|
||||||
|
'url' => fake()->url(),
|
||||||
|
'description' => fake()->optional()->sentence(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
return new class extends Migration
|
||||||
|
{
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
Schema::create('sites', function (Blueprint $table) {
|
||||||
|
$table->id();
|
||||||
|
$table->string('name');
|
||||||
|
$table->string('url');
|
||||||
|
$table->text('description')->nullable();
|
||||||
|
$table->timestamps();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::dropIfExists('sites');
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use App\Models\Site;
|
||||||
|
|
||||||
|
test('a site can be created with name, url, and optional description', function () {
|
||||||
|
$site = Site::create([
|
||||||
|
'name' => 'My Blog',
|
||||||
|
'url' => 'https://example.com',
|
||||||
|
'description' => 'A blog about things',
|
||||||
|
]);
|
||||||
|
|
||||||
|
expect($site->fresh())
|
||||||
|
->name->toBe('My Blog')
|
||||||
|
->url->toBe('https://example.com')
|
||||||
|
->description->toBe('A blog about things');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('a site can be created without a description', function () {
|
||||||
|
$site = Site::create([
|
||||||
|
'name' => 'My Blog',
|
||||||
|
'url' => 'https://example.com',
|
||||||
|
]);
|
||||||
|
|
||||||
|
expect($site->fresh()->description)->toBeNull();
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user