Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ class User extends Model
use HasTasks;
}

class Project extends Model
{
use HasTasks;
}

// Create a task
$user->addTask(
[
Expand All @@ -36,6 +41,9 @@ $user->addTask(
]
);

// You can also attach tasks to other models
$project->addTask(['name' => 'Deploy new version']);

// Get all tasks
$user->tasks;

Expand Down
12 changes: 12 additions & 0 deletions tests/HasTasksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,16 @@ public function test_model_can_create_and_retrieve_tasks()
$this->assertTrue($user->tasks->first()->is($task));
$this->assertCount(1, $user->pendingTasks());
}

public function test_task_owner_is_polymorphic()
{
$project = Project::create(['name' => 'Test Project']);

$task = $project->addTask(['name' => 'Project Task']);

$this->assertInstanceOf(Task::class, $task);
$this->assertEquals($project->id, $task->owner_id);
$this->assertEquals(Project::class, $task->owner_class);
$this->assertTrue($task->owner->is($project));
}
}
15 changes: 15 additions & 0 deletions tests/Project.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Michal78\Tasks\Tests;

use Illuminate\Database\Eloquent\Model;
use Michal78\Tasks\Traits\HasTasks;

class Project extends Model
{
use HasTasks;

protected $table = 'projects';

protected $guarded = [];
}
6 changes: 6 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ protected function setUp(): void
$table->timestamps();
});

Schema::create('projects', function (Blueprint $table) {
$table->id();
$table->string('name')->nullable();
$table->timestamps();
});

// run package migrations
$this->loadMigrationsFrom(__DIR__.'/../database/migrations');
}
Expand Down
Loading