From cabccb8f63a783e5b98e077ca13c76634bf05cd6 Mon Sep 17 00:00:00 2001 From: Michal Skogemann Date: Fri, 13 Jun 2025 09:23:02 +0200 Subject: [PATCH] Add basic tests using Testbench --- phpunit.xml | 10 ++++++++++ tests/HasTasksTest.php | 23 +++++++++++++++++++++++ tests/TestCase.php | 42 ++++++++++++++++++++++++++++++++++++++++++ tests/User.php | 15 +++++++++++++++ 4 files changed, 90 insertions(+) create mode 100644 phpunit.xml create mode 100644 tests/HasTasksTest.php create mode 100644 tests/TestCase.php create mode 100644 tests/User.php diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..a48abe7 --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,10 @@ + + + + + ./tests + + + diff --git a/tests/HasTasksTest.php b/tests/HasTasksTest.php new file mode 100644 index 0000000..e4f1901 --- /dev/null +++ b/tests/HasTasksTest.php @@ -0,0 +1,23 @@ + 'Test User']); + + $task = $user->addTask(['name' => 'Test Task']); + + $this->assertInstanceOf(Task::class, $task); + $this->assertEquals($user->id, $task->owner_id); + $this->assertEquals(User::class, $task->owner_class); + + $this->assertCount(1, $user->tasks); + $this->assertTrue($user->tasks->first()->is($task)); + $this->assertCount(1, $user->pendingTasks()); + } +} diff --git a/tests/TestCase.php b/tests/TestCase.php new file mode 100644 index 0000000..b8d0ed3 --- /dev/null +++ b/tests/TestCase.php @@ -0,0 +1,42 @@ +id(); + $table->string('name')->nullable(); + $table->timestamps(); + }); + + // run package migrations + $this->loadMigrationsFrom(__DIR__.'/../database/migrations'); + } + + protected function getPackageProviders($app) + { + return [ + TasksServiceProvider::class, + ]; + } + + protected function getEnvironmentSetUp($app) + { + $app['config']->set('database.default', 'testing'); + $app['config']->set('database.connections.testing', [ + 'driver' => 'sqlite', + 'database' => ':memory:', + 'prefix' => '', + ]); + } +} diff --git a/tests/User.php b/tests/User.php new file mode 100644 index 0000000..362a03d --- /dev/null +++ b/tests/User.php @@ -0,0 +1,15 @@ +